Skip to content
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

performance improvements using multiple child processes #20

Merged
merged 1 commit into from
Jan 10, 2023
Merged
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
85 changes: 49 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

"use strict";

const crypto = require('crypto');
const variationsStream = require('variations-stream');
const pkg = require('./package');
const variationsStream = require("variations-stream");
const pkg = require("./package");
const { fork } = require("child_process");

const defaultAlphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const defaultAlphabet =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const defaultMaxLength = 12;
const token = process.argv[2];
const alphabet = process.argv[3] || defaultAlphabet;
const maxLength = Number(process.argv[4]) || defaultMaxLength;

if (typeof(token) === 'undefined' || token === '--help') {
if (typeof token === "undefined" || token === "--help") {
console.log(
`jwt-cracker version ${pkg.version}
`jwt-cracker version ${pkg.version}

Usage:
jwt-cracker <token> [<alphabet>] [<maxLength>]
Expand All @@ -23,50 +24,62 @@ if (typeof(token) === 'undefined' || token === '--help') {
alphabet the alphabet to use for the brute force (default: ${defaultAlphabet})
maxLength the max length of the string generated during the brute force (default: ${defaultMaxLength})
`
);
);
process.exit(0);
}

const generateSignature = function(content, secret) {
return (
crypto.createHmac('sha256', secret)
.update(content)
.digest('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_')
);
};

const printResult = function(startTime, attempts, result) {
const printResult = function (startTime, attempts, result) {
if (result) {
console.log('SECRET FOUND:', result);
console.log("SECRET FOUND:", result);
} else {
console.log('SECRET NOT FOUND');
console.log("SECRET NOT FOUND");
}
console.log('Time taken (sec):', ((new Date).getTime() - startTime)/1000);
console.log('Attempts:', attempts);
console.log("Time taken (sec):", (new Date().getTime() - startTime) / 1000);
console.log("Attempts:", attempts);
};

const [header, payload, signature] = token.split('.');
const [header, payload, signature] = token.split(".");
const content = `${header}.${payload}`;

const startTime = new Date().getTime();
let attempts = 0;
const chunkSize = 20000;
let chunk = [];

variationsStream(alphabet, maxLength)
.on('data', function(comb) {
attempts++;
const currentSignature = generateSignature(content, comb);
if (attempts%100000 === 0) {
console.log('Attempts:', attempts);
}
if (currentSignature == signature) {
printResult(startTime, attempts, comb);
process.exit(0);
.on("data", function (comb) {
chunk.push(comb);
if (chunk.length >= chunkSize) {
// save chunk and reset it
forkChunk(chunk);
chunk = [];
}
})
.on('end', function(){
.on("end", function () {
printResult(startTime, attempts);
process.exit(1);
})
;
});

function forkChunk(chunk) {
const child = fork("process-chunk.js");
child.send({ chunk, content, signature });
child.on("message", function (result) {
attempts += chunkSize;
if (result === null && attempts % 100000 === 0) {
console.log("Attempts:", attempts);
}
if (result) {
// secret found, print result and exit
printResult(startTime, attempts, result);
process.exit(0);
}
});
child.on("exit", function () {
// check if all child processes have finished, and if so, exit
checkFinished();
});
}

function checkFinished() {
// check if all child processes have finished, and if so, exit
}
23 changes: 23 additions & 0 deletions process-chunk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const crypto = require("crypto");

const generateSignature = function (content, secret) {
return crypto
.createHmac("sha256", secret)
.update(content)
.digest("base64")
.replace(/=/g, "")
.replace(/\+/g, "-")
.replace(/\//g, "_");
};
process.on("message", function ({ chunk, content, signature }) {
console.log("process spawned", chunk.length);
for (let i = 0; i < chunk.length; i++) {
const currentSignature = generateSignature(content, chunk[i]);
if (currentSignature == signature) {
process.send(chunk[i]);
process.exit(0);
}
}
process.send(null);
process.exit(1);
});