-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add Parse Ethernet frame Operation, allow Parse IPv4 Header to cascade #1722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
62bd446
Add Parse Ethernet frame Operation
Kalkran faa2ab2
Merge branch 'master' into parse-ethernet-frame
Kalkran bfe526b
fix newlines in description
Kalkran bab366e
Merge branch 'master' into parse-ethernet-frame
Kalkran cdaefe9
Add output field to Parse IPv4 Header
Kalkran dcfe23a
Fix linting errors
Kalkran 5c0e62c
Merge branch 'master' into parse-ethernet-frame
a3957273 bd64256
Merge branch 'gchq:master' into parse-ethernet-frame
Kalkran 78b33f9
Fix UI browser test
Kalkran 2cd47f7
Merge branch 'master' into parse-ethernet-frame
Kalkran 68f69d6
Merge branch 'master' into parse-ethernet-frame
Kalkran a489bc5
Merge branch 'master' into parse-ethernet-frame
Kalkran 3bb99ca
Merge branch 'master' into parse-ethernet-frame
C85297 81982e2
Merge branch 'master' into parse-ethernet-frame
GCHQDeveloper581 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * @author tedk [tedk@ted.do] | ||
| * @copyright Crown Copyright 2024 | ||
| * @license Apache-2.0 | ||
| */ | ||
|
|
||
| import Operation from "../Operation.mjs"; | ||
| import OperationError from "../errors/OperationError.mjs"; | ||
| import Utils from "../Utils.mjs"; | ||
| import {fromHex, toHex} from "../lib/Hex.mjs"; | ||
|
|
||
| /** | ||
| * Parse Ethernet frame operation | ||
| */ | ||
| class ParseEthernetFrame extends Operation { | ||
|
|
||
| /** | ||
| * ParseEthernetFrame constructor | ||
| */ | ||
| constructor() { | ||
| super(); | ||
|
|
||
| this.name = "Parse Ethernet frame"; | ||
| this.module = "Default"; | ||
| this.description = "Parses an Ethernet frame and either shows the deduced values (Source and destination MAC, VLANs) or returns the packet data.\\n\\nGood for use in conjunction with the Parse IPv4, and Parse TCP/UDP recipes."; | ||
| this.infoURL = "https://en.wikipedia.org/wiki/Ethernet_frame#Frame_%E2%80%93_data_link_layer"; | ||
| this.inputType = "string"; | ||
| this.outputType = "html"; | ||
| this.args = [ | ||
| { | ||
| name: "Input type", | ||
| type: "option", | ||
| value: [ | ||
| "Raw", "Hex" | ||
|
Kalkran marked this conversation as resolved.
|
||
| ], | ||
| defaultIndex: 0, | ||
| }, | ||
| { | ||
| name: "Return type", | ||
| type: "option", | ||
| value: [ | ||
| "Text output", "Packet data", "Packet data (hex)", | ||
| ], | ||
| defaultIndex: 0, | ||
| } | ||
| ]; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @param {string} input | ||
| * @param {Object[]} args | ||
| * @returns {html} | ||
| */ | ||
| run(input, args) { | ||
| const format = args[0]; | ||
| const outputFormat = args[1]; | ||
|
|
||
| if (format === "Hex") { | ||
| input = fromHex(input); | ||
| } else if (format === "Raw") { | ||
| input = new Uint8Array(Utils.strToArrayBuffer(input)); | ||
| } else { | ||
| throw new OperationError("Invalid input format selected."); | ||
| } | ||
|
|
||
| const destinationMac = input.slice(0, 6); | ||
| const sourceMac = input.slice(6, 12); | ||
|
|
||
| let offset = 12; | ||
| const vlans = []; | ||
|
|
||
| while (offset < input.length) { | ||
| const ethType = Utils.byteArrayToChars(input.slice(offset, offset+2)); | ||
| offset += 2; | ||
|
|
||
|
|
||
| if (ethType === "\x08\x00") { | ||
| break; | ||
| } else if (ethType === "\x81\x00" || ethType === "\x88\xA8") { | ||
| // Parse the VLAN tag: | ||
| // [0000] 0000 0000 0000 | ||
| // ^^^ PRIO - Ignored | ||
| // ^ DEI - Ignored | ||
| // ^^^^ ^^^^ ^^^^ VLAN ID | ||
| const vlanTag = input.slice(offset+2, offset+4); | ||
| vlans.push((vlanTag[0] & 0b00001111) << 4 | vlanTag[1]); | ||
|
|
||
| offset += 2; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const packetData = input.slice(offset); | ||
|
|
||
| if (outputFormat === "Packet data") { | ||
| return Utils.byteArrayToChars(packetData); | ||
| } else if (outputFormat === "Packet data (hex)") { | ||
| return toHex(packetData); | ||
| } else if (outputFormat === "Text output") { | ||
| let retval = `Source MAC: ${toHex(sourceMac, ":")}\nDestination MAC: ${toHex(destinationMac, ":")}\n`; | ||
| if (vlans.length > 0) { | ||
| retval += `VLAN: ${vlans.join(", ")}\n`; | ||
| } | ||
| retval += `Data:\n${toHex(packetData)}`; | ||
| return retval; | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| export default ParseEthernetFrame; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /** | ||
| * Parse Ethernet frame tests. | ||
| * | ||
| * @author tedk [tedk@ted.do] | ||
| * @copyright Crown Copyright 2017 | ||
| * @license Apache-2.0 | ||
| */ | ||
| import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
|
||
| TestRegister.addTests([ | ||
| { | ||
| name: "Parse plain Ethernet frame", | ||
| input: "000000000000ffffffffffff08004500", | ||
| expectedOutput: "Source MAC: ff:ff:ff:ff:ff:ff\nDestination MAC: 00:00:00:00:00:00\nData:\n45 00", | ||
| recipeConfig: [ | ||
| { | ||
| "op": "Parse Ethernet frame", | ||
| "args": ["Hex", "Text output"] | ||
| } | ||
| ] | ||
| }, | ||
| // Example PCAP data from: https://packetlife.net/captures/protocol/vlan/ | ||
| { | ||
| name: "Parse Ethernet frame with one VLAN tag (802.1q)", | ||
| input: "01000ccdcdd00013c3dfae188100a0760165aaaa", | ||
| expectedOutput: "Source MAC: 00:13:c3:df:ae:18\nDestination MAC: 01:00:0c:cd:cd:d0\nVLAN: 117\nData:\naa aa", | ||
| recipeConfig: [ | ||
| { | ||
| "op": "Parse Ethernet frame", | ||
| "args": ["Hex", "Text output"] | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| name: "Parse Ethernet frame with two VLAN tags (802.1ad)", | ||
| input: "0019aa7de688002155c8f13c810000d18100001408004500", | ||
| expectedOutput: "Source MAC: 00:21:55:c8:f1:3c\nDestination MAC: 00:19:aa:7d:e6:88\nVLAN: 16, 128\nData:\n45 00", | ||
| recipeConfig: [ | ||
| { | ||
| "op": "Parse Ethernet frame", | ||
| "args": ["Hex", "Text output"] | ||
| } | ||
| ] | ||
| } | ||
| ]); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.