mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
XOR Brute Force operation now has a variable key length
This commit is contained in:
parent
d68523a54e
commit
cb6708c02e
3 changed files with 44 additions and 20 deletions
|
@ -112,7 +112,7 @@ function loadRequiredModules(recipeConfig) {
|
||||||
|
|
||||||
if (!OpModules.hasOwnProperty(module)) {
|
if (!OpModules.hasOwnProperty(module)) {
|
||||||
console.log("Loading module " + module);
|
console.log("Loading module " + module);
|
||||||
sendStatusMessage("Loading module " + module);
|
self.sendStatusMessage("Loading module " + module);
|
||||||
self.importScripts(self.docURL + "/" + module + ".js");
|
self.importScripts(self.docURL + "/" + module + ".js");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -124,9 +124,9 @@ function loadRequiredModules(recipeConfig) {
|
||||||
*
|
*
|
||||||
* @param {string} msg
|
* @param {string} msg
|
||||||
*/
|
*/
|
||||||
function sendStatusMessage(msg) {
|
self.sendStatusMessage = function(msg) {
|
||||||
self.postMessage({
|
self.postMessage({
|
||||||
action: "statusMessage",
|
action: "statusMessage",
|
||||||
data: msg
|
data: msg
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
|
@ -325,13 +325,13 @@ const OperationConfig = {
|
||||||
},
|
},
|
||||||
"XOR Brute Force": {
|
"XOR Brute Force": {
|
||||||
module: "Default",
|
module: "Default",
|
||||||
description: "Enumerate all possible XOR solutions. Current maximum key length is 2 due to browser performance.<br><br>Optionally enter a regex string that you expect to find in the plaintext to filter results (crib).",
|
description: "Enumerate all possible XOR solutions. Current maximum key length is 2 due to browser performance.<br><br>Optionally enter a string that you expect to find in the plaintext to filter results (crib).",
|
||||||
inputType: "byteArray",
|
inputType: "byteArray",
|
||||||
outputType: "string",
|
outputType: "string",
|
||||||
args: [
|
args: [
|
||||||
{
|
{
|
||||||
name: "Key length",
|
name: "Key length",
|
||||||
type: "option",
|
type: "number",
|
||||||
value: BitwiseOp.XOR_BRUTE_KEY_LENGTH
|
value: BitwiseOp.XOR_BRUTE_KEY_LENGTH
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -91,7 +91,7 @@ const BitwiseOp = {
|
||||||
* @constant
|
* @constant
|
||||||
* @default
|
* @default
|
||||||
*/
|
*/
|
||||||
XOR_BRUTE_KEY_LENGTH: ["1", "2"],
|
XOR_BRUTE_KEY_LENGTH: 1,
|
||||||
/**
|
/**
|
||||||
* @constant
|
* @constant
|
||||||
* @default
|
* @default
|
||||||
|
@ -121,37 +121,61 @@ const BitwiseOp = {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
runXorBrute: function (input, args) {
|
runXorBrute: function (input, args) {
|
||||||
const keyLength = parseInt(args[0], 10),
|
const keyLength = args[0],
|
||||||
sampleLength = args[1],
|
sampleLength = args[1],
|
||||||
sampleOffset = args[2],
|
sampleOffset = args[2],
|
||||||
scheme = args[3],
|
scheme = args[3],
|
||||||
nullPreserving = args[4],
|
nullPreserving = args[4],
|
||||||
printKey = args[5],
|
printKey = args[5],
|
||||||
outputHex = args[6],
|
outputHex = args[6],
|
||||||
crib = args[7];
|
crib = args[7].toLowerCase();
|
||||||
|
|
||||||
let output = "",
|
let output = [],
|
||||||
result,
|
result,
|
||||||
resultUtf8,
|
resultUtf8,
|
||||||
regex;
|
record = "";
|
||||||
|
|
||||||
input = input.slice(sampleOffset, sampleOffset + sampleLength);
|
input = input.slice(sampleOffset, sampleOffset + sampleLength);
|
||||||
|
|
||||||
if (crib !== "") {
|
if (self) self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
|
||||||
regex = new RegExp(crib, "im");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an integer to an array of bytes expressing that number.
|
||||||
|
*
|
||||||
|
* @param {number} int
|
||||||
|
* @param {number} len - Length of the resulting array
|
||||||
|
* @returns {array}
|
||||||
|
*/
|
||||||
|
const intToByteArray = (int, len) => {
|
||||||
|
let res = Array(len).fill(0);
|
||||||
|
for (let i = len - 1; i >= 0; i--) {
|
||||||
|
res[i] = int & 0xff;
|
||||||
|
int = int >>> 8;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
|
for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
|
||||||
result = BitwiseOp._bitOp(input, Utils.fromHex(key.toString(16)), BitwiseOp._xor, nullPreserving, scheme);
|
if (key % 10000 === 0) {
|
||||||
|
if (self) self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
|
||||||
|
}
|
||||||
|
|
||||||
|
result = BitwiseOp._bitOp(input, intToByteArray(key, keyLength), BitwiseOp._xor, nullPreserving, scheme);
|
||||||
resultUtf8 = Utils.byteArrayToUtf8(result);
|
resultUtf8 = Utils.byteArrayToUtf8(result);
|
||||||
if (crib !== "" && resultUtf8.search(regex) === -1) continue;
|
record = "";
|
||||||
if (printKey) output += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
|
|
||||||
if (outputHex) output += Utils.toHex(result) + "\n";
|
if (crib && resultUtf8.toLowerCase().indexOf(crib) < 0) continue;
|
||||||
else output += Utils.printable(resultUtf8, false) + "\n";
|
if (printKey) record += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
|
||||||
if (printKey) output += "\n";
|
if (outputHex) {
|
||||||
|
record += Utils.toHex(result);
|
||||||
|
} else {
|
||||||
|
record += Utils.printable(resultUtf8, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push(record);
|
||||||
}
|
}
|
||||||
return output;
|
|
||||||
|
return output.join("\n");
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue