2020-05-28 11:44:37 -04:00
|
|
|
#!/usr/bin/env node
|
|
|
|
/**
|
|
|
|
* @author Boolean263 [boolean263@protonmail.com]
|
|
|
|
* @copyright Crown Copyright 2020
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
2020-05-28 16:16:55 -04:00
|
|
|
"use strict";
|
2020-05-28 11:44:37 -04:00
|
|
|
|
|
|
|
const fs = require("fs");
|
2020-05-28 15:48:24 -04:00
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
/* * * * * Helper Functions * * * * */
|
2020-05-28 11:44:37 -04:00
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
/**
|
|
|
|
* Slurp the contents of a stream up into a Buffer to pass to CyberChef
|
|
|
|
*
|
|
|
|
* @param {Stream} istream
|
|
|
|
*/
|
|
|
|
const slurpStream = (istream) => { // {{{1
|
|
|
|
const ret = [];
|
|
|
|
let len = 0;
|
2020-05-28 11:44:37 -04:00
|
|
|
return new Promise(resolve => {
|
2020-05-28 16:16:55 -04:00
|
|
|
istream.on("readable", () => {
|
|
|
|
let chunk;
|
2020-05-28 11:44:37 -04:00
|
|
|
while ((chunk = istream.read()) !== null) {
|
|
|
|
ret.push(chunk);
|
|
|
|
len += chunk.length;
|
|
|
|
}
|
|
|
|
resolve(Buffer.concat(ret, len));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}; // }}}1
|
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
/**
|
|
|
|
* Slurp the contents of a file (or stdin) into a Buffer
|
|
|
|
*
|
|
|
|
* @param {String} fname
|
|
|
|
*/
|
|
|
|
const slurp = (fname) => { // {{{1
|
2020-05-28 11:44:37 -04:00
|
|
|
let istream;
|
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
if (fname === undefined || fname === "-") {
|
2020-05-28 11:44:37 -04:00
|
|
|
istream = process.stdin;
|
|
|
|
if (istream.isTTY) {
|
2020-05-28 15:48:24 -04:00
|
|
|
return Promise.reject(new Error("TTY input not supported"));
|
2020-05-28 11:44:37 -04:00
|
|
|
}
|
2020-05-28 16:16:55 -04:00
|
|
|
} else {
|
|
|
|
istream = fs.createReadStream(fname, { flags: "r" });
|
2020-05-28 11:44:37 -04:00
|
|
|
}
|
|
|
|
return slurpStream(istream);
|
|
|
|
}; // }}}1
|
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
/**
|
|
|
|
* Get a valid port number from the command line
|
|
|
|
*
|
|
|
|
* Used by commander
|
|
|
|
*
|
|
|
|
* @param {String} value
|
|
|
|
* @param {String} dummyPrevious
|
|
|
|
*/
|
|
|
|
const getPort = (value, dummyPrevious) => { // {{{1
|
|
|
|
const ret = parseInt(value, 10);
|
2020-05-28 11:44:37 -04:00
|
|
|
if (ret < 1 || ret > 65535) {
|
|
|
|
throw new Error("invalid port number");
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
// }}}1
|
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
/* * * * * MAIN * * * * */ // {{{1
|
2020-05-28 11:44:37 -04:00
|
|
|
|
2020-05-28 15:48:24 -04:00
|
|
|
const chef = require("cyberchef");
|
|
|
|
const program = require("commander");
|
|
|
|
|
2020-05-28 11:44:37 -04:00
|
|
|
program
|
2020-05-28 16:16:55 -04:00
|
|
|
.version(require("./package.json").version)
|
|
|
|
.description("Bake data from files and/or TCP clients " +
|
|
|
|
"using a CyberChef recipe.")
|
|
|
|
.usage("[options] [file [file ...]]")
|
|
|
|
.requiredOption("-r, --recipe-file <file>",
|
|
|
|
"recipe JSON file")
|
|
|
|
.option("-l, --listen [port]",
|
|
|
|
"listen on TCP port for data (random if not given)", getPort, false)
|
|
|
|
.option("-o, --output <file-or-dir>",
|
|
|
|
"where to write result (file input only; default:stdout)");
|
2020-05-28 15:48:24 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
program.exitOverride().parse(process.argv);
|
2020-05-28 16:16:55 -04:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== "commander.helpDisplayed") {
|
|
|
|
console.error("Run with \"--help\" for usage");
|
2020-05-28 15:48:24 -04:00
|
|
|
}
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2020-05-28 11:44:37 -04:00
|
|
|
|
|
|
|
// If we get no inputs and we aren't running a server,
|
|
|
|
// make stdin our single input
|
|
|
|
let inputs = program.args;
|
2020-05-28 16:16:55 -04:00
|
|
|
if (inputs.length === 0 && !program.listen) {
|
|
|
|
inputs = ["-"];
|
2020-05-28 11:44:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Likewise stdout for our output
|
|
|
|
let ostream;
|
2020-05-28 15:48:24 -04:00
|
|
|
let path;
|
2020-05-28 11:44:37 -04:00
|
|
|
let outputIsDir = false;
|
|
|
|
if (program.output === undefined && !program.listen) {
|
|
|
|
ostream = process.stdout;
|
2020-05-28 16:16:55 -04:00
|
|
|
} else if (inputs.length > 0) {
|
2020-05-28 11:44:37 -04:00
|
|
|
// See if our output is a directory
|
|
|
|
let st;
|
|
|
|
try {
|
|
|
|
st = fs.statSync(program.output);
|
|
|
|
outputIsDir = st.isDirectory();
|
2020-05-28 16:16:55 -04:00
|
|
|
} catch (err) {
|
|
|
|
// We"re fine if the output doesn"t exist yet
|
|
|
|
if (err.code !== "ENOENT") throw err;
|
2020-05-28 11:44:37 -04:00
|
|
|
}
|
|
|
|
if (!outputIsDir) {
|
|
|
|
ostream = fs.createWriteStream(program.output);
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 15:48:24 -04:00
|
|
|
if (outputIsDir) path = require("path");
|
2020-05-28 11:44:37 -04:00
|
|
|
|
|
|
|
let recipe;
|
|
|
|
slurp(program.recipeFile).then((data) => {
|
|
|
|
recipe = JSON.parse(data);
|
|
|
|
})
|
2020-05-28 16:16:55 -04:00
|
|
|
.catch((err) => {
|
|
|
|
console.error(`Error parsing recipe: ${err.message}`);
|
|
|
|
process.exit(2);
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
// First, deal with any files we want to read
|
|
|
|
for (const i of inputs) {
|
|
|
|
slurp(i).then((data) => {
|
|
|
|
const output = chef.bake(data, recipe);
|
|
|
|
if (outputIsDir) {
|
|
|
|
let outFileName = path.basename(i);
|
|
|
|
if (outFileName === "-") outFileName = "from-stdin";
|
|
|
|
ostream = fs.createWriteStream(
|
|
|
|
path.join(program.output, outFileName));
|
|
|
|
}
|
|
|
|
ostream.write(output.presentAs("string", true));
|
|
|
|
if (outputIsDir) ostream.end();
|
|
|
|
},
|
|
|
|
(err) => {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exitCode = 2;
|
2020-05-28 11:44:37 -04:00
|
|
|
})
|
2020-05-28 16:16:55 -04:00
|
|
|
.catch((err) => {
|
|
|
|
console.error(err.message);
|
|
|
|
process.exitCode = 2;
|
|
|
|
});
|
|
|
|
}
|
2020-05-28 11:44:37 -04:00
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
// Next, listen for TCP requests.
|
|
|
|
// This is intentionally hardcoded to localhost to discourage
|
|
|
|
// the use of this script as a production system.
|
|
|
|
if (program.listen) {
|
|
|
|
const net = require("net");
|
|
|
|
const server = net.createServer((socket) => {
|
|
|
|
slurpStream(socket).then((data) => {
|
|
|
|
const output = chef.bake(data, recipe);
|
|
|
|
socket.write(output.presentAs("string", true));
|
|
|
|
socket.end();
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
2020-05-28 11:44:37 -04:00
|
|
|
});
|
|
|
|
|
2020-05-28 16:16:55 -04:00
|
|
|
// If no port given by user, let the OS choose one
|
|
|
|
if (program.listen === true) program.listen = 0;
|
|
|
|
server.listen(program.listen, "127.0.0.1")
|
|
|
|
.on("listening", () => {
|
|
|
|
console.log("Now listening on " +
|
|
|
|
server.address().address +
|
|
|
|
":" + server.address().port);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Exit gracefully
|
|
|
|
process.on("SIGINT", () => {
|
|
|
|
console.log("Exiting");
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(3);
|
|
|
|
});
|