From fa20939dd4d79bea4ad6b56ba8811fe84c518bd7 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Sat, 18 Feb 2017 16:13:19 +0000 Subject: [PATCH] Added initial functionality for 'Parse IPv4 header' operation. --- src/js/config/Categories.js | 1 + src/js/config/OperationConfig.js | 17 +++++- src/js/operations/IP.js | 89 +++++++++++++++++++++++++++++++- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/src/js/config/Categories.js b/src/js/config/Categories.js index 2611f30..3a84b0e 100755 --- a/src/js/config/Categories.js +++ b/src/js/config/Categories.js @@ -127,6 +127,7 @@ var Categories = [ "Parse User Agent", "Parse IP range", "Parse IPv6 address", + "Parse IPv4 header", "Parse URI", "URL Encode", "URL Decode", diff --git a/src/js/config/OperationConfig.js b/src/js/config/OperationConfig.js index b0dfc73..5e82efa 100755 --- a/src/js/config/OperationConfig.js +++ b/src/js/config/OperationConfig.js @@ -791,11 +791,24 @@ var OperationConfig = { }, "Parse IPv6 address": { description: "Displays the longhand and shorthand versions of a valid IPv6 address.

Recognises all reserved ranges and parses encapsulated or tunnelled addresses including Teredo and 6to4.", - run: IP.runParseIpv6, + run: IP.runParseIPv6, inputType: "string", outputType: "string", args: [] }, + "Parse IPv4 header": { + description: "Given an IPv4 header as raw bytes, this operations parses and displays each field in an easily readable format.", + run: IP.runParseIPv4Header, + inputType: "string", + outputType: "string", + args: [ + { + name: "Input format", + type: "option", + value: IP.IP_HEADER_FORMAT + } + ] + }, "Text encoding": { description: "Translates the data between different character encodings.

Supported charsets are:", run: CharEnc.run, @@ -2034,7 +2047,7 @@ var OperationConfig = { ] }, "Regular expression": { - description: "Define your own regular expression to search the input data with, optionally choosing from a list of pre-defined patterns.", + description: "Define your own regular expression (regex) to search the input data with, optionally choosing from a list of pre-defined patterns.", run: StrUtils.runRegex, manualBake: true, inputType: "string", diff --git a/src/js/operations/IP.js b/src/js/operations/IP.js index a8df40d..25adf53 100755 --- a/src/js/operations/IP.js +++ b/src/js/operations/IP.js @@ -1,4 +1,4 @@ -/* globals BigInteger */ +/* globals BigInteger, Checksum */ /** * Internet Protocol address operations. @@ -78,7 +78,7 @@ var IP = { * @param {Object[]} args * @returns {string} */ - runParseIpv6: function (input, args) { + runParseIPv6: function (input, args) { var match, output = ""; @@ -401,6 +401,91 @@ var IP = { }, + /** + * @constant + * @default + */ + IP_HEADER_FORMAT: ["Hex", "Raw"], + + /** + * Parse IPv4 header operation. + * + * @param {byteArray} input + * @param {Object[]} args + * @returns {string} + */ + runParseIPv4Header: function(input, args) { + var format = args[0], + output; + + if (format === "Hex") { + input = Utils.fromHex(input); + } else if (format === "Raw") { + input = Utils.strToByteArray(input); + } else { + return "Unrecognised input format."; + } + + var version = (input[0] >>> 4) & 0x0f, + ihl = input[0] & 0x0f, + dscp = (input[1] >>> 2) & 0x3f, + ecn = input[1] & 0x03, + length = input[2] << 8 | input[3], + identification = input[4] << 8 | input[5], + flags = (input[6] >>> 5) & 0x07, + fragOffset = (input[6] & 0x1f) << 8 | input[7], + ttl = input[8], + protocol = input[9], + checksum = input[10] << 8 | input[11], + srcIP = input[12] << 24 | input[13] << 16 | input[14] << 8 | input[15], + dstIP = input[16] << 24 | input[17] << 16 | input[18] << 8 | input[19], + checksumHeader = input.slice(0, 10).concat([0, 0]).concat(input.slice(12, 20)); + + // Version + if (version !== 4) { + version = version + " (Error: for IPv4 headers, this should always be set to 4)"; + } + + // IHL + if (ihl < 5) { + ihl = ihl + " (Error: this should always be at least 5)"; + } else if (ihl > 5) { + // sort out options... + } + + // + + + // Check checksum + var correctChecksum = Checksum.runTCPIP(checksumHeader, []), + givenChecksum = Utils.hex(checksum), + checksumResult; + if (correctChecksum === givenChecksum) { + checksumResult = givenChecksum + " (correct)"; + } else { + checksumResult = givenChecksum + " (incorrect, should be " + correctChecksum + ")"; + } + + output = "Version: " + version + + "\nInternet Header Length (IHL): " + ihl + + "\nDifferentiated Services Code Point (DSCP): " + dscp + + "\nECN: " + ecn + + "\nTotal length: " + length + + "\nIdentification: " + identification + + "\nFlags: " + flags + + "\nFragment offset: " + fragOffset + + "\nTime-To-Live: " + ttl + + "\nProtocol: " + protocol + + "\nHeader checksum: " + checksumResult + + "\nSource IP address: " + IP._ipv4ToStr(srcIP) + + "\nDestination IP address: " + IP._ipv4ToStr(dstIP) + + "\nCorrect checksum: " + Checksum.runTCPIP(checksumHeader, []); + + + return output; + }, + + /** * @constant * @default