Skip to content

add Protobuf Stream Decode operation #1384

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"URL Decode",
"Protobuf Decode",
"Protobuf Encode",
"Protobuf Stream Decode",
"VarInt Encode",
"VarInt Decode",
"JA3 Fingerprint",
Expand Down
24 changes: 24 additions & 0 deletions src/core/lib/Protobuf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ class Protobuf {
return this.mergeDecodes(input);
}

/**
* Parse Protobuf stream data
* @param {byteArray} input
* @param {any[]} args
* @returns {any[]}
*/
static decodeStream(input, args) {
this.updateProtoRoot(args[0]);
this.showUnknownFields = args[1];
this.showTypes = args[2];

const streams = new Protobuf(input);
const output = [];
let objLength = streams._varInt();
while (!isNaN(objLength) && objLength > 0) {
const subData = streams.data.slice(streams.offset, streams.offset + objLength);
output.push(this.mergeDecodes(subData));
streams.offset += objLength;
objLength = streams._varInt();
}

return output;
}

/**
* Update the parsedProto, throw parsing errors
*
Expand Down
54 changes: 54 additions & 0 deletions src/core/operations/ProtobufStreamDecode.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @author GCHQ Contributor [3]
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";
import Protobuf from "../lib/Protobuf.mjs";

/**
* Protobuf Stream Decode operation
*/
class ProtobufStreamDecode extends Operation {

/**
* ProtobufStreamDecode constructor
*/
constructor() {
super();

this.name = "Protobuf Stream Decode";
this.module = "Protobuf";
this.description = "Decodes Protobuf encoded data from streams to a JSON array representation of the data using the field number as the field key.<br><br>If a .proto schema is defined, the encoded data will be decoded with reference to the schema. Only one message instance will be decoded. <br><br><u>Show Unknown Fields</u><br>When a schema is used, this option shows fields that are present in the input data but not defined in the schema.<br><br><u>Show Types</u><br>Show the type of a field next to its name. For undefined fields, the wiretype and example types are shown instead.";
this.infoURL = "https://developers.google.com/protocol-buffers/docs/techniques#streaming";
this.inputType = "ArrayBuffer";
this.outputType = "JSON";
this.args = [
{
name: "Show Types",
type: "boolean",
value: false
}
];
}

/**
* @param {ArrayBuffer} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
input = new Uint8Array(input);
try {
// provide standard values for currently removed arguments
return Protobuf.decodeStream(input, ["", false, ...args]);
} catch (err) {
throw new OperationError(err);
}
}

}

export default ProtobufStreamDecode;
29 changes: 29 additions & 0 deletions tests/operations/tests/Protobuf.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,33 @@ TestRegister.addTests([
}
]
},
{
name: "Protobuf Stream Decode: no schema",
input: "0d081c1203596f751a024d65202b0c0a0a0a066162633132331200",
expectedOutput: JSON.stringify([
{
"1": 28,
"2": "You",
"3": "Me",
"4": 43
},
{
"1": {
"1": "abc123",
"2": {}
}
}
], null, 4),
recipeConfig: [
{
"op": "From Hex",
"args": ["Auto"]
},
{
"op": "Protobuf Stream Decode",
"args": [false]
}
]
},

]);