mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-09 07:45:00 -04:00
Merge bc093bce2c
into 7c66dacc40
This commit is contained in:
commit
59da375446
3 changed files with 291 additions and 228 deletions
|
@ -336,16 +336,23 @@ export function ipv4ToStr(ipInt) {
|
|||
|
||||
|
||||
/**
|
||||
* Converts an IPv6 address from string format to numerical array format.
|
||||
* Converts an IPv6 address from string format to numerical format.
|
||||
*
|
||||
* @param {string} ipStr
|
||||
* @returns {number[]}
|
||||
* @param {boolean} retArr If true, an array is returned. If false, a BigInt is returned.
|
||||
* @returns {(number[]|number)}
|
||||
*
|
||||
* @example
|
||||
* // returns [65280, 0, 0, 0, 0, 0, 4369, 8738]
|
||||
* strToIpv6("ff00::1111:2222");
|
||||
*
|
||||
* // returns 3.3895313892515355e+38
|
||||
* strToIpv6("ff00::1111:2222", false);
|
||||
*/
|
||||
export function strToIpv6(ipStr) {
|
||||
export function strToIpv6(ipStr, retArr=true) {
|
||||
if (retArr === false) {
|
||||
return Number("0x" + expandIpv6(ipStr).replace(/:/g, ""));
|
||||
}
|
||||
let j = 0;
|
||||
const blocks = ipStr.split(":"),
|
||||
numBlocks = parseBlocks(blocks),
|
||||
|
@ -378,6 +385,49 @@ export function strToIpv6(ipStr) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Expands an IPv6 address in string format to its 'longhand' equivalent.
|
||||
*
|
||||
* @param {string} ipStr
|
||||
* @returns {string}
|
||||
*
|
||||
* @example
|
||||
* // returns "5555:126f:0000:0000:0000:0000:0000:0001"
|
||||
* expandIpv6("5555:126f::1");
|
||||
*/
|
||||
export function expandIpv6(ipStr) {
|
||||
const padHex = function (ipStr) {
|
||||
if (ipStr.length === 39) {
|
||||
return ipStr;
|
||||
}
|
||||
const blockArray = ipStr.split(":");
|
||||
let reconstructed = "";
|
||||
blockArray.forEach((a) => {
|
||||
for (let i = a.length; i < 4; i++) {
|
||||
reconstructed += "0";
|
||||
}
|
||||
reconstructed += a + ":";
|
||||
});
|
||||
return reconstructed.substring(0, reconstructed.length - 1);
|
||||
};
|
||||
ipStr = padHex(ipStr);
|
||||
const doubleColonOffset = ipStr.search("::");
|
||||
if (doubleColonOffset === -1) {
|
||||
return ipStr;
|
||||
}
|
||||
const totalBlocks = ipStr.match(/:/g).length - (ipStr.startsWith(":") || ipStr.endsWith(":")),
|
||||
reqBlocks = 8 - totalBlocks;
|
||||
let expandedBlocks = (ipStr.startsWith(":") ? "" : ":") + "0000";
|
||||
for (let i = 1; i < reqBlocks; i++) {
|
||||
expandedBlocks += ":0000";
|
||||
}
|
||||
ipStr = ipStr.substring(0, doubleColonOffset) + expandedBlocks + ":" + ipStr.substring(doubleColonOffset + 2);
|
||||
if (ipStr.endsWith(":")) {
|
||||
ipStr = ipStr.substring(0, ipStr.length - 1);
|
||||
}
|
||||
return padHex(ipStr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an IPv6 address from numerical array format to string format.
|
||||
*
|
||||
|
|
|
@ -38,8 +38,9 @@ class ParseIPv6Address extends Operation {
|
|||
run(input, args) {
|
||||
let match,
|
||||
output = "";
|
||||
|
||||
if ((match = IPV6_REGEX.exec(input))) {
|
||||
if (!(match = IPV6_REGEX.exec(input))) {
|
||||
throw new OperationError("Invalid IPv6 address.");
|
||||
}
|
||||
const ipv6 = strToIpv6(match[1]),
|
||||
longhand = ipv6ToStr(ipv6),
|
||||
shorthand = ipv6ToStr(ipv6, true);
|
||||
|
@ -265,9 +266,6 @@ class ParseIPv6Address extends Operation {
|
|||
output += "\nInterface identifier: " + intIdent +
|
||||
"\nMAC address: " + mac;
|
||||
}
|
||||
} else {
|
||||
throw new OperationError("Invalid IPv6 address");
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
||||
import {strToIpv6} from "../lib/IP.mjs";
|
||||
|
||||
/**
|
||||
* Sort operation
|
||||
|
@ -38,7 +39,7 @@ class Sort extends Operation {
|
|||
{
|
||||
"name": "Order",
|
||||
"type": "option",
|
||||
"value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric", "Numeric (hexadecimal)"]
|
||||
"value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IPv4 address", "IPv6 address", "Numeric", "Numeric (hexadecimal)"]
|
||||
}
|
||||
];
|
||||
}
|
||||
|
@ -58,8 +59,10 @@ class Sort extends Operation {
|
|||
sorted = sorted.sort();
|
||||
} else if (order === "Alphabetical (case insensitive)") {
|
||||
sorted = sorted.sort(Sort._caseInsensitiveSort);
|
||||
} else if (order === "IP address") {
|
||||
sorted = sorted.sort(Sort._ipSort);
|
||||
} else if (order === "IPv4 address") {
|
||||
sorted = sorted.sort(Sort._ipv4Sort);
|
||||
} else if (order === "IPv6 address") {
|
||||
sorted = sorted.sort(Sort._ipv6Sort);
|
||||
} else if (order === "Numeric") {
|
||||
sorted = sorted.sort(Sort._numericSort);
|
||||
} else if (order === "Numeric (hexadecimal)") {
|
||||
|
@ -91,7 +94,7 @@ class Sort extends Operation {
|
|||
* @param {string} b
|
||||
* @returns {number}
|
||||
*/
|
||||
static _ipSort(a, b) {
|
||||
static _ipv4Sort(a, b) {
|
||||
let a_ = a.split("."),
|
||||
b_ = b.split(".");
|
||||
|
||||
|
@ -105,6 +108,18 @@ class Sort extends Operation {
|
|||
return a_ - b_;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comparison operator for sorting of IPv6 addresses.
|
||||
*
|
||||
* @param {string} a
|
||||
* @param {string} b
|
||||
* @returns {number}
|
||||
*/
|
||||
static _ipv6Sort(a, b) {
|
||||
const numericalA = strToIpv6(a, false), numericalB = strToIpv6(b, false);
|
||||
return numericalA - numericalB;
|
||||
}
|
||||
|
||||
/**
|
||||
* Comparison operation for sorting of numeric values.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue