mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-22 07:46:16 -04:00
Added Bcrypt, Scrypt, BSON and string operations along with many new tests.
This commit is contained in:
parent
2f5b0533d8
commit
715ca1c292
28 changed files with 1290 additions and 84 deletions
59
src/core/operations/BSON.js
Normal file
59
src/core/operations/BSON.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
import bsonjs from "bson";
|
||||
import {Buffer} from "buffer";
|
||||
|
||||
|
||||
/**
|
||||
* BSON operations.
|
||||
*
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*
|
||||
* @namespace
|
||||
*/
|
||||
const BSON = {
|
||||
|
||||
/**
|
||||
* BSON serialise operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {ArrayBuffer}
|
||||
*/
|
||||
runBSONSerialise(input, args) {
|
||||
if (!input) return new ArrayBuffer();
|
||||
|
||||
const bson = new bsonjs();
|
||||
|
||||
try {
|
||||
const data = JSON.parse(input);
|
||||
return bson.serialize(data).buffer;
|
||||
} catch (err) {
|
||||
return err.toString();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* BSON deserialise operation.
|
||||
*
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
runBSONDeserialise(input, args) {
|
||||
if (!input.byteLength) return "";
|
||||
|
||||
const bson = new bsonjs();
|
||||
|
||||
try {
|
||||
const data = bson.deserialize(new Buffer(input));
|
||||
return JSON.stringify(data, null, 2);
|
||||
} catch (err) {
|
||||
return err.toString();
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default BSON;
|
|
@ -148,6 +148,10 @@ const ByteRepr = {
|
|||
throw "Error: Base argument must be between 2 and 36";
|
||||
}
|
||||
|
||||
if (input.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (base !== 16 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
|
||||
|
||||
// Split into groups of 2 if the whole string is concatenated and
|
||||
|
|
|
@ -483,12 +483,11 @@ const Code = {
|
|||
|
||||
|
||||
/**
|
||||
* Converts to snake_case.
|
||||
* To Snake Case operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
runToSnakeCase(input, args) {
|
||||
const smart = args[0];
|
||||
|
@ -502,12 +501,11 @@ const Code = {
|
|||
|
||||
|
||||
/**
|
||||
* Converts to camelCase.
|
||||
* To Camel Case operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
runToCamelCase(input, args) {
|
||||
const smart = args[0];
|
||||
|
@ -521,12 +519,11 @@ const Code = {
|
|||
|
||||
|
||||
/**
|
||||
* Converts to kebab-case.
|
||||
* To Kebab Case operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*
|
||||
*/
|
||||
runToKebabCase(input, args) {
|
||||
const smart = args[0];
|
||||
|
@ -537,6 +534,7 @@ const Code = {
|
|||
return kebabCase(input);
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
export default Code;
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
import moment from "moment-timezone";
|
||||
|
||||
|
||||
/**
|
||||
* Date and time operations.
|
||||
*
|
||||
|
@ -57,24 +60,29 @@ const DateTime = {
|
|||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {number}
|
||||
* @returns {string}
|
||||
*/
|
||||
runToUnixTimestamp: function(input, args) {
|
||||
let units = args[0],
|
||||
const units = args[0],
|
||||
treatAsUTC = args[1],
|
||||
showDateTime = args[2],
|
||||
d = treatAsUTC ? moment.utc(input) : moment(input);
|
||||
|
||||
let result = "";
|
||||
|
||||
if (units === "Seconds (s)") {
|
||||
return d.unix();
|
||||
result = d.unix();
|
||||
} else if (units === "Milliseconds (ms)") {
|
||||
return d.valueOf();
|
||||
result = d.valueOf();
|
||||
} else if (units === "Microseconds (μs)") {
|
||||
return d.valueOf() * 1000;
|
||||
result = d.valueOf() * 1000;
|
||||
} else if (units === "Nanoseconds (ns)") {
|
||||
return d.valueOf() * 1000000;
|
||||
result = d.valueOf() * 1000000;
|
||||
} else {
|
||||
throw "Unrecognised unit";
|
||||
}
|
||||
|
||||
return showDateTime ? `${result} (${d.tz("UTC").format("ddd D MMMM YYYY HH:mm:ss")} UTC)` : result.toString();
|
||||
},
|
||||
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ import * as SHA3 from "js-sha3";
|
|||
import Checksum from "./Checksum.js";
|
||||
import ctph from "ctph.js";
|
||||
import ssdeep from "ssdeep.js";
|
||||
import bcrypt from "bcryptjs";
|
||||
import scrypt from "scryptsy";
|
||||
|
||||
|
||||
/**
|
||||
|
@ -449,6 +451,127 @@ const Hash = {
|
|||
},
|
||||
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
BCRYPT_ROUNDS: 10,
|
||||
|
||||
/**
|
||||
* Bcrypt operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
runBcrypt: async function (input, args) {
|
||||
const rounds = args[0];
|
||||
const salt = await bcrypt.genSalt(rounds);
|
||||
|
||||
return await bcrypt.hash(input, salt, null, p => {
|
||||
// Progress callback
|
||||
if (ENVIRONMENT_IS_WORKER())
|
||||
self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Bcrypt compare operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
runBcryptCompare: async function (input, args) {
|
||||
const hash = args[0];
|
||||
|
||||
const match = await bcrypt.compare(input, hash, null, p => {
|
||||
// Progress callback
|
||||
if (ENVIRONMENT_IS_WORKER())
|
||||
self.sendStatusMessage(`Progress: ${(p * 100).toFixed(0)}%`);
|
||||
});
|
||||
|
||||
return match ? "Match: " + input : "No match";
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Bcrypt parse operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
runBcryptParse: async function (input, args) {
|
||||
try {
|
||||
return `Rounds: ${bcrypt.getRounds(input)}
|
||||
Salt: ${bcrypt.getSalt(input)}
|
||||
Password hash: ${input.split(bcrypt.getSalt(input))[1]}
|
||||
Full hash: ${input}`;
|
||||
} catch (err) {
|
||||
return "Error: " + err.toString();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
KEY_FORMAT: ["Hex", "Base64", "UTF8", "Latin1"],
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
SCRYPT_ITERATIONS: 16384,
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
SCRYPT_MEM_FACTOR: 8,
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
SCRYPT_PARALLEL_FACTOR: 1,
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
SCRYPT_KEY_LENGTH: 64,
|
||||
|
||||
/**
|
||||
* Scrypt operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
runScrypt: function (input, args) {
|
||||
const salt = Utils.convertToByteString(args[0].string || "", args[0].option),
|
||||
iterations = args[1],
|
||||
memFactor = args[2],
|
||||
parallelFactor = args[3],
|
||||
keyLength = args[4];
|
||||
|
||||
try {
|
||||
const data = scrypt(
|
||||
input, salt, iterations, memFactor, parallelFactor, keyLength,
|
||||
p => {
|
||||
// Progress callback
|
||||
if (ENVIRONMENT_IS_WORKER())
|
||||
self.sendStatusMessage(`Progress: ${p.percent.toFixed(0)}%`);
|
||||
}
|
||||
);
|
||||
|
||||
return data.toString("hex");
|
||||
} catch (err) {
|
||||
return "Error: " + err.toString();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Generate all hashes operation.
|
||||
*
|
||||
|
|
|
@ -78,7 +78,7 @@ const Hexdump = {
|
|||
*/
|
||||
runFrom: function(input, args) {
|
||||
let output = [],
|
||||
regex = /^\s*(?:[\dA-F]{4,16}:?)?\s*((?:[\dA-F]{2}\s){1,8}(?:\s|[\dA-F]{2}-)(?:[\dA-F]{2}\s){1,8}|(?:[\dA-F]{2}\s|[\dA-F]{4}\s)+)/igm,
|
||||
regex = /^\s*(?:[\dA-F]{4,16}h?:?)?\s*((?:[\dA-F]{2}\s){1,8}(?:\s|[\dA-F]{2}-)(?:[\dA-F]{2}\s){1,8}|(?:[\dA-F]{2}\s|[\dA-F]{4}\s)+)/igm,
|
||||
block, line;
|
||||
|
||||
while ((block = regex.exec(input))) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import Utils from "../Utils.js";
|
||||
import jsesc from "jsesc";
|
||||
|
||||
|
||||
/**
|
||||
|
@ -219,35 +220,45 @@ const StrUtils = {
|
|||
* @constant
|
||||
* @default
|
||||
*/
|
||||
ESCAPE_REPLACEMENTS: [
|
||||
{"escaped": "\\\\", "unescaped": "\\"}, // Must be first
|
||||
{"escaped": "\\'", "unescaped": "'"},
|
||||
{"escaped": "\\\"", "unescaped": "\""},
|
||||
{"escaped": "\\n", "unescaped": "\n"},
|
||||
{"escaped": "\\r", "unescaped": "\r"},
|
||||
{"escaped": "\\t", "unescaped": "\t"},
|
||||
{"escaped": "\\b", "unescaped": "\b"},
|
||||
{"escaped": "\\f", "unescaped": "\f"},
|
||||
],
|
||||
QUOTE_TYPES: ["Single", "Double", "Backtick"],
|
||||
/**
|
||||
* @constant
|
||||
* @default
|
||||
*/
|
||||
ESCAPE_LEVEL: ["Special chars", "Everything", "Minimal"],
|
||||
|
||||
/**
|
||||
* Escape string operation.
|
||||
*
|
||||
* @author Vel0x [dalemy@microsoft.com]
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*
|
||||
* @example
|
||||
* StrUtils.runUnescape("Don't do that", [])
|
||||
* StrUtils.runEscape("Don't do that", [])
|
||||
* > "Don\'t do that"
|
||||
* StrUtils.runUnescape(`Hello
|
||||
* StrUtils.runEscape(`Hello
|
||||
* World`, [])
|
||||
* > "Hello\nWorld"
|
||||
*/
|
||||
runEscape: function(input, args) {
|
||||
return StrUtils._replaceByKeys(input, "unescaped", "escaped");
|
||||
const level = args[0],
|
||||
quotes = args[1],
|
||||
jsonCompat = args[2],
|
||||
es6Compat = args[3],
|
||||
lowercaseHex = !args[4];
|
||||
|
||||
return jsesc(input, {
|
||||
quotes: quotes.toLowerCase(),
|
||||
es6: es6Compat,
|
||||
escapeEverything: level === "Everything",
|
||||
minimal: level === "Minimal",
|
||||
json: jsonCompat,
|
||||
lowercaseHex: lowercaseHex,
|
||||
});
|
||||
},
|
||||
|
||||
|
||||
|
@ -255,6 +266,7 @@ const StrUtils = {
|
|||
* Unescape string operation.
|
||||
*
|
||||
* @author Vel0x [dalemy@microsoft.com]
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
|
@ -268,32 +280,7 @@ const StrUtils = {
|
|||
* World`
|
||||
*/
|
||||
runUnescape: function(input, args) {
|
||||
return StrUtils._replaceByKeys(input, "escaped", "unescaped");
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Replaces all matching tokens in ESCAPE_REPLACEMENTS with the correction. The
|
||||
* ordering is determined by the patternKey and the replacementKey.
|
||||
*
|
||||
* @author Vel0x [dalemy@microsoft.com]
|
||||
* @author Matt C [matt@artemisbot.uk]
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {string} pattern_key
|
||||
* @param {string} replacement_key
|
||||
* @returns {string}
|
||||
*/
|
||||
_replaceByKeys: function(input, patternKey, replacementKey) {
|
||||
let output = input;
|
||||
|
||||
// Catch the \\x encoded characters
|
||||
if (patternKey === "escaped") output = Utils.parseEscapedChars(input);
|
||||
|
||||
StrUtils.ESCAPE_REPLACEMENTS.forEach(replacement => {
|
||||
output = output.split(replacement[patternKey]).join(replacement[replacementKey]);
|
||||
});
|
||||
return output;
|
||||
return Utils.parseEscapedChars(input);
|
||||
},
|
||||
|
||||
|
||||
|
|
|
@ -50,6 +50,40 @@ const Unicode = {
|
|||
},
|
||||
|
||||
|
||||
/**
|
||||
* Escape Unicode Characters operation.
|
||||
*
|
||||
* @param {string} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
runEscape: function(input, args) {
|
||||
const regexWhitelist = /[ -~]/i,
|
||||
prefix = args[0],
|
||||
encodeAll = args[1],
|
||||
padding = args[2],
|
||||
uppercaseHex = args[3];
|
||||
|
||||
let output = "",
|
||||
character = "";
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
character = input[i];
|
||||
if (!encodeAll && regexWhitelist.test(character)) {
|
||||
// It’s a printable ASCII character so don’t escape it.
|
||||
output += character;
|
||||
continue;
|
||||
}
|
||||
|
||||
let cp = character.codePointAt(0).toString(16);
|
||||
if (uppercaseHex) cp = cp.toUpperCase();
|
||||
output += prefix + cp.padStart(padding, "0");
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Lookup table to add prefixes to unicode delimiters so that they can be used in a regex.
|
||||
*
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue