mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 08:16:17 -04:00
Merge branch 'h345983745-udp-header-parser'
This commit is contained in:
commit
1efccff730
5 changed files with 154 additions and 1 deletions
|
@ -3,7 +3,6 @@
|
||||||
[](https://travis-ci.org/gchq/CyberChef)
|
[](https://travis-ci.org/gchq/CyberChef)
|
||||||
[](https://david-dm.org/gchq/CyberChef)
|
[](https://david-dm.org/gchq/CyberChef)
|
||||||
[](https://www.npmjs.com/package/cyberchef)
|
[](https://www.npmjs.com/package/cyberchef)
|
||||||

|
|
||||||
[](https://github.com/gchq/CyberChef/blob/master/LICENSE)
|
[](https://github.com/gchq/CyberChef/blob/master/LICENSE)
|
||||||
[](https://gitter.im/gchq/CyberChef?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
[](https://gitter.im/gchq/CyberChef?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
|
||||||
|
|
||||||
|
|
|
@ -167,6 +167,7 @@
|
||||||
"Parse IP range",
|
"Parse IP range",
|
||||||
"Parse IPv6 address",
|
"Parse IPv6 address",
|
||||||
"Parse IPv4 header",
|
"Parse IPv4 header",
|
||||||
|
"Parse UDP",
|
||||||
"Parse SSH Host Key",
|
"Parse SSH Host Key",
|
||||||
"Parse URI",
|
"Parse URI",
|
||||||
"URL Encode",
|
"URL Encode",
|
||||||
|
|
84
src/core/operations/ParseUDP.mjs
Normal file
84
src/core/operations/ParseUDP.mjs
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
/**
|
||||||
|
* @author h345983745 []
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation.mjs";
|
||||||
|
import Stream from "../lib/Stream.mjs";
|
||||||
|
import {toHex} from "../lib/Hex.mjs";
|
||||||
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse UDP operation
|
||||||
|
*/
|
||||||
|
class ParseUDP extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ParseUDP constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.name = "Parse UDP";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "Parses a UDP header and payload (if present).";
|
||||||
|
this.infoURL = "https://wikipedia.org/wiki/User_Datagram_Protocol";
|
||||||
|
this.inputType = "ArrayBuffer";
|
||||||
|
this.outputType = "json";
|
||||||
|
this.presentType = "html";
|
||||||
|
this.args = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {ArrayBuffer} input
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
if (input.byteLength < 8) {
|
||||||
|
throw new OperationError("Need 8 bytes for a UDP Header");
|
||||||
|
}
|
||||||
|
|
||||||
|
const s = new Stream(new Uint8Array(input));
|
||||||
|
// Parse Header
|
||||||
|
const UDPPacket = {
|
||||||
|
"Source port": s.readInt(2),
|
||||||
|
"Destination port": s.readInt(2),
|
||||||
|
"Length": s.readInt(2),
|
||||||
|
"Checksum": toHex(s.getBytes(2), "")
|
||||||
|
};
|
||||||
|
// Parse data if present
|
||||||
|
if (s.hasMore()) {
|
||||||
|
UDPPacket.Data = toHex(s.getBytes(UDPPacket.Length - 8), "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return UDPPacket;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the UDP Packet in a table style
|
||||||
|
* @param {Object} data
|
||||||
|
* @returns {html}
|
||||||
|
*/
|
||||||
|
present(data) {
|
||||||
|
const html = [];
|
||||||
|
html.push("<table class='table table-hover table-sm table-bordered table-nonfluid' style='table-layout: fixed'>");
|
||||||
|
html.push("<tr>");
|
||||||
|
html.push("<th>Field</th>");
|
||||||
|
html.push("<th>Value</th>");
|
||||||
|
html.push("</tr>");
|
||||||
|
|
||||||
|
for (const key in data) {
|
||||||
|
html.push("<tr>");
|
||||||
|
html.push("<td style=\"word-wrap:break-word\">" + key + "</td>");
|
||||||
|
html.push("<td>" + data[key] + "</td>");
|
||||||
|
html.push("</tr>");
|
||||||
|
}
|
||||||
|
html.push("</table>");
|
||||||
|
return html.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default ParseUDP;
|
|
@ -88,6 +88,7 @@ import "./tests/BLAKE2s";
|
||||||
import "./tests/Protobuf";
|
import "./tests/Protobuf";
|
||||||
import "./tests/ParseSSHHostKey";
|
import "./tests/ParseSSHHostKey";
|
||||||
import "./tests/DefangIP";
|
import "./tests/DefangIP";
|
||||||
|
import "./tests/ParseUDP";
|
||||||
|
|
||||||
// Cannot test operations that use the File type yet
|
// Cannot test operations that use the File type yet
|
||||||
//import "./tests/SplitColourChannels";
|
//import "./tests/SplitColourChannels";
|
||||||
|
|
68
tests/operations/tests/ParseUDP.mjs
Normal file
68
tests/operations/tests/ParseUDP.mjs
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/**
|
||||||
|
* Parse UDP tests.
|
||||||
|
*
|
||||||
|
* @author h345983745
|
||||||
|
*
|
||||||
|
* @copyright Crown Copyright 2019
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Parse UDP: No Data - JSON",
|
||||||
|
input: "04 89 00 35 00 2c 01 01",
|
||||||
|
expectedOutput: "{\"Source port\":1161,\"Destination port\":53,\"Length\":44,\"Checksum\":\"0101\"}",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "From Hex",
|
||||||
|
args: ["Auto"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "Parse UDP",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "JSON Minify",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}, {
|
||||||
|
name: "Parse UDP: With Data - JSON",
|
||||||
|
input: "04 89 00 35 00 2c 01 01 02 02",
|
||||||
|
expectedOutput: "{\"Source port\":1161,\"Destination port\":53,\"Length\":44,\"Checksum\":\"0101\",\"Data\":\"0202\"}",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "From Hex",
|
||||||
|
args: ["Auto"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "Parse UDP",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "JSON Minify",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Parse UDP: Not Enough Bytes",
|
||||||
|
input: "04 89 00",
|
||||||
|
expectedOutput: "Need 8 bytes for a UDP Header",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
op: "From Hex",
|
||||||
|
args: ["Auto"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "Parse UDP",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
op: "JSON Minify",
|
||||||
|
args: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue