Browse Source

Merge branch 'h345983745-defang-ip'

n1474335 5 năm trước cách đây
mục cha
commit
1b4471a946

+ 2 - 1
src/core/config/Categories.json

@@ -179,7 +179,8 @@
             "Group IP addresses",
             "Encode NetBIOS Name",
             "Decode NetBIOS Name",
-            "Defang URL"
+            "Defang URL",
+            "Defang IP Addresses"
         ]
     },
     {

+ 61 - 0
src/core/operations/DefangIPAddresses.mjs

@@ -0,0 +1,61 @@
+/**
+ * @author h345983745
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+
+
+/**
+ * Defang IP Addresses operation
+ */
+class DefangIPAddresses extends Operation {
+
+    /**
+     * DefangIPAddresses constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Defang IP Addresses";
+        this.module = "Default";
+        this.description = "Takes a IPv4 or IPv6 address and 'Defangs' it, meaning the IP becomes invalid, removing the risk of accidentally utilising it as an IP address.";
+        this.infoURL = "https://isc.sans.edu/forums/diary/Defang+all+the+things/22744/";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [];
+
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        input = input.replace(IPV4_REGEX, x => {
+            return x.replace(/\./g, "[.]");
+        });
+
+        input = input.replace(IPV6_REGEX, x => {
+            return x.replace(/:/g, "[:]");
+        });
+
+        return input;
+    }
+}
+
+export default DefangIPAddresses;
+
+
+/**
+ * IPV4 regular expression
+ */
+const IPV4_REGEX = new RegExp("(?:(?:\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d\\d|\\d)(?:\\/\\d{1,2})?", "g");
+
+
+/**
+ * IPV6 regular expression
+ */
+const IPV6_REGEX = new RegExp("((?=.*::)(?!.*::.+::)(::)?([\\dA-Fa-f]{1,4}:(:|\\b)|){5}|([\\dA-Fa-f]{1,4}:){6})((([\\dA-Fa-f]{1,4}((?!\\3)::|:\\b|(?![\\dA-Fa-f])))|(?!\\2\\3)){2}|(((2[0-4]|1\\d|[1-9])?\\d|25[0-5])\\.?\\b){4})", "g");

+ 1 - 0
tests/operations/index.mjs

@@ -87,6 +87,7 @@ import "./tests/BLAKE2b";
 import "./tests/BLAKE2s";
 import "./tests/Protobuf";
 import "./tests/ParseSSHHostKey";
+import "./tests/DefangIP";
 
 // Cannot test operations that use the File type yet
 //import "./tests/SplitColourChannels";

+ 43 - 0
tests/operations/tests/DefangIP.mjs

@@ -0,0 +1,43 @@
+/**
+ * DefangIP tests.
+ *
+ * @author h345983745
+ *
+ * @copyright Crown Copyright 2019
+ * @license Apache-2.0
+ */
+import TestRegister from "../TestRegister";
+
+TestRegister.addTests([
+    {
+        name: "Defang IP: Valid IPV4",
+        input: "192.168.1.1",
+        expectedOutput: "192[.]168[.]1[.]1",
+        recipeConfig: [
+            {
+                op: "Defang IP",
+                args: [],
+            },
+        ],
+    }, {
+        name: "Defang IP: Valid IPV6",
+        input: "2001:0db8:85a3:0000:0000:8a2e:0370:7343",
+        expectedOutput: "2001[:]0db8[:]85a3[:]0000[:]0000[:]8a2e[:]0370[:]7343",
+        recipeConfig: [
+            {
+                op: "Defang IP",
+                args: [],
+            },
+        ],
+    }, {
+        name: "Defang IP: Valid IPV6 Shorthand",
+        input: "2001:db8:3c4d:15::1a2f:1a2b",
+        expectedOutput: "2001[:]db8[:]3c4d[:]15[:][:]1a2f[:]1a2b",
+        recipeConfig: [
+            {
+                op: "Defang IP",
+                args: [],
+            },
+        ],
+    },
+]);