-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
43 deletions.
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,3 @@ | ||
#!/usr/bin/env node | ||
|
||
const { crack } = require('..') |
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 |
---|---|---|
@@ -1,66 +1,74 @@ | ||
const EventEmitter = require('eventemitter3') | ||
const log = require('log-update') | ||
const pad = require('left-pad') | ||
const { replace, xor } = require('./utils') | ||
|
||
const ALL_HEX = Array.from(Array(256)).map((v, i) => pad(i.toString(16), 2, 0)) | ||
// <- ['00', '01', ... 'fe', 'ff'] | ||
|
||
async function crack (iv, cipher, challenge) { | ||
let original = Buffer.concat([iv, cipher]) | ||
let size = iv.length | ||
let intermediary = Buffer.alloc(cipher.length) | ||
let plain | ||
class Cracker { | ||
constructor () { | ||
this.broadcast = new EventEmitter() | ||
} | ||
|
||
console.log('--- Crack start ---') | ||
async crack (iv, cipher, challenge) { | ||
let original = Buffer.concat([iv, cipher]) | ||
let size = iv.length | ||
let intermediary = Buffer.alloc(cipher.length) | ||
let plain | ||
|
||
for (let block = 0; block * size < cipher.length; block++) { | ||
console.log('Current block:', block) | ||
console.log('--- Crack start ---') | ||
|
||
for (let padding = 1; padding <= size; padding++) { | ||
console.log('Intermediary value:', intermediary.toString('hex')) | ||
console.log('Current block: %s, padding: %s', block, padding) | ||
for (let block = 0; block * size < cipher.length; block++) { | ||
console.log('Current block:', block) | ||
|
||
let input = Buffer.concat([iv, cipher.slice(0, size * (block + 1))]) | ||
let found | ||
for (let padding = 1; padding <= size; padding++) { | ||
console.log('Intermediary value:', intermediary.toString('hex')) | ||
console.log('Current block: %s, padding: %s', block, padding) | ||
|
||
for (let i = 1; i < padding; i++) { | ||
input = replace(input, size * (block + 1) - i, Buffer.from([padding ^ intermediary[size * (block + 1) - i]])) | ||
} | ||
let input = Buffer.concat([iv, cipher.slice(0, size * (block + 1))]) | ||
let found | ||
|
||
for (let i = 0; i < ALL_HEX.length; i++) { | ||
let hex = ALL_HEX[i] | ||
let sample = replace(input, size * (block + 1) - padding, Buffer.from(hex, 'hex')) | ||
if (sample.equals(original)) { | ||
log('key found: (backup)', `0x${hex}`) | ||
found = hex | ||
continue | ||
for (let i = 1; i < padding; i++) { | ||
input = replace(input, size * (block + 1) - i, Buffer.from([padding ^ intermediary[size * (block + 1) - i]])) | ||
} | ||
if (await challenge(sample.slice(0, size), sample.slice(size))) { | ||
log('key found:', `0x${hex}`) | ||
found = hex | ||
break | ||
|
||
for (let i = 0; i < ALL_HEX.length; i++) { | ||
let hex = ALL_HEX[i] | ||
let sample = replace(input, size * (block + 1) - padding, Buffer.from(hex, 'hex')) | ||
if (sample.equals(original)) { | ||
log('key found: (backup)', `0x${hex}`) | ||
found = hex | ||
continue | ||
} | ||
if (await challenge(sample.slice(0, size), sample.slice(size))) { | ||
log('key found:', `0x${hex}`) | ||
found = hex | ||
break | ||
} | ||
log('invalid:', `0x${hex}`) | ||
} | ||
log('invalid:', `0x${hex}`) | ||
} | ||
|
||
if (!found) { | ||
throw new Error('All the challenges failed.') | ||
if (!found) { | ||
throw new Error('All the challenges failed.') | ||
} | ||
intermediary[size * (block + 1) - padding] = padding ^ parseInt(found, 16) | ||
log.done() | ||
} | ||
intermediary[size * (block + 1) - padding] = padding ^ parseInt(found, 16) | ||
log.done() | ||
console.log('Intermediary value:', intermediary.toString('hex')) | ||
} | ||
console.log('Intermediary value:', intermediary.toString('hex')) | ||
} | ||
|
||
plain = xor(original.slice(0, intermediary.length), intermediary) | ||
console.log('Plain text:', plain.toString()) | ||
console.log('Plain text (hex):', plain.toString('hex')) | ||
console.log('--- Crack end ---') | ||
plain = xor(original.slice(0, intermediary.length), intermediary) | ||
console.log('Plain text:', plain.toString()) | ||
console.log('Plain text (hex):', plain.toString('hex')) | ||
console.log('--- Crack end ---') | ||
|
||
return { | ||
intermediary, | ||
plain, | ||
return { | ||
intermediary, | ||
plain, | ||
} | ||
} | ||
} | ||
|
||
exports.crack = crack | ||
exports.Cracker = Cracker | ||
exports.crack = (...args) => (new Cracker()).crack(...args) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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