From da74d9b22d23d915f85dba54c2dcdc61cca68de8 Mon Sep 17 00:00:00 2001 From: c65722 <53181351+c65722@users.noreply.github.com> Date: Sun, 15 Sep 2024 18:14:21 +0100 Subject: [PATCH] Add Strip UDP header operation --- src/core/config/Categories.json | 1 + src/core/operations/StripUDPHeader.mjs | 51 +++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/StripUDPHeader.mjs | 69 +++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 src/core/operations/StripUDPHeader.mjs create mode 100644 tests/operations/tests/StripUDPHeader.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index bebdd6a5e2..e9599b7c33 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -236,6 +236,7 @@ "Parse IPv4 header", "Parse TCP", "Parse UDP", + "Strip UDP header", "Parse SSH Host Key", "Parse URI", "URL Encode", diff --git a/src/core/operations/StripUDPHeader.mjs b/src/core/operations/StripUDPHeader.mjs new file mode 100644 index 0000000000..0847c58fd5 --- /dev/null +++ b/src/core/operations/StripUDPHeader.mjs @@ -0,0 +1,51 @@ +/** + * @author c65722 [] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import Stream from "../lib/Stream.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Strip UDP header operation + */ +class StripUDPHeader extends Operation { + + /** + * StripUDPHeader constructor + */ + constructor() { + super(); + + this.name = "Strip UDP header"; + this.module = "Default"; + this.description = "Strips the UDP header from a UDP datagram, outputting the payload."; + this.infoURL = "https://wikipedia.org/wiki/User_Datagram_Protocol"; + this.inputType = "ArrayBuffer"; + this.outputType = "ArrayBuffer"; + this.args = []; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {ArrayBuffer} + */ + run(input, args) { + const HEADER_LEN = 8; + + const s = new Stream(new Uint8Array(input)); + if (s.length < HEADER_LEN) { + throw new OperationError("Need 8 bytes for a UDP Header"); + } + + s.moveTo(HEADER_LEN); + + return s.getBytes().buffer; + } + +} + +export default StripUDPHeader; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 40ce7a2ee6..468c14812a 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -142,6 +142,7 @@ import "./tests/SIGABA.mjs"; import "./tests/SM4.mjs"; // import "./tests/SplitColourChannels.mjs"; // Cannot test operations that use the File type yet import "./tests/StrUtils.mjs"; +import "./tests/StripUDPHeader.mjs"; import "./tests/Subsection.mjs"; import "./tests/SwapCase.mjs"; import "./tests/SymmetricDifference.mjs"; diff --git a/tests/operations/tests/StripUDPHeader.mjs b/tests/operations/tests/StripUDPHeader.mjs new file mode 100644 index 0000000000..d753ec5094 --- /dev/null +++ b/tests/operations/tests/StripUDPHeader.mjs @@ -0,0 +1,69 @@ +/** + * Strip UDP header tests. + * + * @author c65722 [] + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "Strip UDP header: No payload", + input: "8111003500000000", + expectedOutput: "", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Strip UDP header", + args: [], + }, + { + op: "To Hex", + args: ["None", 0] + } + ] + }, + { + name: "Strip UDP header: Payload", + input: "8111003500080000ffffffffffffffff", + expectedOutput: "ffffffffffffffff", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Strip UDP header", + args: [], + }, + { + op: "To Hex", + args: ["None", 0] + } + ] + }, + { + name: "Strip UDP header: Input length less than header length", + input: "81110035000000", + expectedOutput: "Need 8 bytes for a UDP Header", + recipeConfig: [ + { + op: "From Hex", + args: ["None"] + }, + { + op: "Strip UDP header", + args: [], + }, + { + op: "To Hex", + args: ["None", 0] + } + ] + } +]);