-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Cyberchef cli #1043
Closed
Closed
Cyberchef cli #1043
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,180 @@ | ||
#!/usr/bin/env node | ||
/** | ||
* @author Boolean263 [[email protected]] | ||
* @copyright Crown Copyright 2020 | ||
* @license Apache-2.0 | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require("fs"); | ||
|
||
///////// Helper Functions ///////// | ||
|
||
let slurpStream = (istream) => { // {{{1 | ||
// Slurp the contents of a stream up into a Buffer to pass to CyberChef | ||
var ret = []; | ||
var len = 0; | ||
return new Promise(resolve => { | ||
istream.on('readable', () => { | ||
var chunk; | ||
while ((chunk = istream.read()) !== null) { | ||
ret.push(chunk); | ||
len += chunk.length; | ||
} | ||
resolve(Buffer.concat(ret, len)); | ||
}); | ||
}); | ||
}; // }}}1 | ||
|
||
let slurp = (fname) => { // {{{1 | ||
// Slurp the contents of a file (or stdin) into a Buffer | ||
let istream; | ||
|
||
if (fname === undefined || fname == '-') { | ||
istream = process.stdin; | ||
if (istream.isTTY) { | ||
return Promise.reject(new Error("TTY input not supported")); | ||
} | ||
} | ||
else { | ||
istream = fs.createReadStream(fname, { flags: 'r' }); | ||
} | ||
return slurpStream(istream); | ||
}; // }}}1 | ||
|
||
let getPort = (value, dummyPrevious) => { // {{{1 | ||
// Get a valid port number from the command line | ||
let ret = parseInt(value, 10); | ||
if (ret < 1 || ret > 65535) { | ||
throw new Error("invalid port number"); | ||
} | ||
return ret; | ||
}; | ||
// }}}1 | ||
|
||
///////// MAIN ///////// {{{1 | ||
|
||
const chef = require("cyberchef"); | ||
const program = require("commander"); | ||
|
||
program | ||
.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)'); | ||
|
||
try { | ||
program.exitOverride().parse(process.argv); | ||
} | ||
catch (e) { | ||
if (e.code != 'commander.helpDisplayed') { | ||
console.error("Run with '--help' for usage"); | ||
} | ||
process.exit(1); | ||
} | ||
|
||
// If we get no inputs and we aren't running a server, | ||
// make stdin our single input | ||
let inputs = program.args; | ||
if (inputs.length == 0 && !program.listen) { | ||
inputs = [ '-' ]; | ||
} | ||
|
||
// Likewise stdout for our output | ||
let ostream; | ||
let path; | ||
let outputIsDir = false; | ||
if (program.output === undefined && !program.listen) { | ||
ostream = process.stdout; | ||
} | ||
else if (inputs.length > 0) { | ||
// See if our output is a directory | ||
let st; | ||
try { | ||
st = fs.statSync(program.output); | ||
outputIsDir = st.isDirectory(); | ||
} | ||
catch(err) { | ||
// We're fine if the output doesn't exist yet | ||
if (err.code != 'ENOENT') throw err; | ||
} | ||
if (!outputIsDir) { | ||
ostream = fs.createWriteStream(program.output); | ||
} | ||
} | ||
if (outputIsDir) path = require("path"); | ||
|
||
let recipe; | ||
slurp(program.recipeFile).then((data) => { | ||
recipe = JSON.parse(data); | ||
}) | ||
.catch((err) => { | ||
console.error(`Error parsing recipe: ${err.message}`); | ||
process.exit(2); | ||
}) | ||
.then(() => { | ||
// First, deal with any files we want to read | ||
for(let i of inputs) { | ||
slurp(i).then((data) => { | ||
let 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; | ||
}) | ||
.catch((err) => { | ||
console.error(err.message); | ||
process.exitCode = 2; | ||
}); | ||
} | ||
|
||
// 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) => { | ||
let output = chef.bake(data, recipe); | ||
socket.write(output.presentAs("string", true)); | ||
socket.end(); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
}); | ||
|
||
// 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); | ||
}) |
This file contains 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 |
---|---|---|
|
@@ -35,6 +35,9 @@ | |
"Firefox >= 38", | ||
"node >= 10" | ||
], | ||
"bin": { | ||
"cyberchef": "./cli.js" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.8.7", | ||
"@babel/plugin-transform-runtime": "^7.8.3", | ||
|
@@ -98,6 +101,7 @@ | |
"bson": "^4.0.3", | ||
"chi-squared": "^1.1.0", | ||
"codepage": "^1.14.0", | ||
"commander": "^2.14.1", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose this version of commander because it's also the version pulled by codepage, in the hopes of minimizing impact of adding a dependency. |
||
"core-js": "^3.6.4", | ||
"crypto-api": "^0.8.5", | ||
"crypto-js": "^4.0.0", | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One quirk of Node I haven't yet been able to work out is why I need the failure handler on this
then()
call in addition to thecatch()
call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need the failure handler on it, you can either use that or
.catch()
.