Skip to content

Commit

Permalink
refactor: add broadcast
Browse files Browse the repository at this point in the history
  • Loading branch information
imyelo committed May 22, 2019
1 parent bf19b44 commit 493d56e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
3 changes: 3 additions & 0 deletions bin/padoracle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

const { crack } = require('..')
94 changes: 51 additions & 43 deletions lib/index.js
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)
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"pkcs7": "^1.0.2"
},
"dependencies": {
"eventemitter3": "^3.1.2",
"left-pad": "^1.3.0",
"log-update": "^3.2.0"
}
Expand Down

0 comments on commit 493d56e

Please sign in to comment.