From a61feb00d1a91e0a62ea23380feef6f75a03c8aa Mon Sep 17 00:00:00 2001 From: Didier Stevens Date: Sun, 25 Dec 2022 12:23:07 +0100 Subject: [PATCH] Operation Change IP Format: added Little Endian format --- src/core/operations/ChangeIPFormat.mjs | 18 ++++++++-- tests/operations/tests/ChangeIPFormat.mjs | 40 +++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/core/operations/ChangeIPFormat.mjs b/src/core/operations/ChangeIPFormat.mjs index c9adc5d8..a7e72708 100644 --- a/src/core/operations/ChangeIPFormat.mjs +++ b/src/core/operations/ChangeIPFormat.mjs @@ -29,12 +29,12 @@ class ChangeIPFormat extends Operation { { "name": "Input format", "type": "option", - "value": ["Dotted Decimal", "Decimal", "Octal", "Hex"] + "value": ["Dotted Decimal", "Decimal", "Decimal (Little Endian)", "Octal", "Octal (Little Endian)", "Hex"] }, { "name": "Output format", "type": "option", - "value": ["Dotted Decimal", "Decimal", "Octal", "Hex"] + "value": ["Dotted Decimal", "Decimal", "Decimal (Little Endian)", "Octal", "Octal (Little Endian)", "Hex"] } ]; } @@ -71,9 +71,15 @@ class ChangeIPFormat extends Operation { case "Decimal": baIp = this.fromNumber(lines[i].toString(), 10); break; + case "Decimal (Little Endian)": + baIp = Utils.intToByteArray(parseInt(lines[i].toString(), 10), 4, "little"); + break; case "Octal": baIp = this.fromNumber(lines[i].toString(), 8); break; + case "Octal (Little Endian)": + baIp = Utils.intToByteArray(parseInt(lines[i].toString(), 8), 4, "little"); + break; case "Hex": baIp = fromHex(lines[i]); break; @@ -98,10 +104,18 @@ class ChangeIPFormat extends Operation { decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0; output += decIp.toString() + "\n"; break; + case "Decimal (Little Endian)": + decIp = Utils.byteArrayToInt(baIp, "little"); + output += decIp.toString() + "\n"; + break; case "Octal": decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0; output += "0" + decIp.toString(8) + "\n"; break; + case "Octal (Little Endian)": + decIp = Utils.byteArrayToInt(baIp, "little"); + output += "0" + decIp.toString(8) + "\n"; + break; case "Hex": hexIp = ""; for (j = 0; j < baIp.length; j++) { diff --git a/tests/operations/tests/ChangeIPFormat.mjs b/tests/operations/tests/ChangeIPFormat.mjs index d92ffb79..26c92124 100644 --- a/tests/operations/tests/ChangeIPFormat.mjs +++ b/tests/operations/tests/ChangeIPFormat.mjs @@ -48,5 +48,45 @@ TestRegister.addTests([ args: ["Octal", "Decimal"], }, ], + }, { + name: "Change IP format: Decimal (Little Endian) to Dotted Decimal", + input: "16885952", + expectedOutput: "192.168.1.1", + recipeConfig: [ + { + op: "Change IP format", + args: ["Decimal (Little Endian)", "Dotted Decimal"], + }, + ], + }, { + name: "Change IP format: Dotted Decimal to Decimal (Little Endian)", + input: "192.168.1.1", + expectedOutput: "16885952", + recipeConfig: [ + { + op: "Change IP format", + args: ["Dotted Decimal", "Decimal (Little Endian)"], + }, + ], + }, { + name: "Change IP format: Octal (Little Endian) to Dotted Decimal", + input: "0100324300", + expectedOutput: "192.168.1.1", + recipeConfig: [ + { + op: "Change IP format", + args: ["Octal (Little Endian)", "Dotted Decimal"], + }, + ], + }, { + name: "Change IP format: Dotted Decimal to Octal (Little Endian)", + input: "192.168.1.1", + expectedOutput: "0100324300", + recipeConfig: [ + { + op: "Change IP format", + args: ["Dotted Decimal", "Octal (Little Endian)"], + }, + ], }, ]);