forked from lmammino/jwt-cracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (68 loc) · 2.07 KB
/
Copy pathindex.js
File metadata and controls
81 lines (68 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env node
import { fileURLToPath } from 'node:url'
import { join } from 'node:path'
import { fork } from 'node:child_process'
import variationsStream from 'variations-stream'
import ArgsParser from './argsParser.js'
import JWTValidator from './jwtValidator.js'
import Constants from './constants.js'
const __dirname = fileURLToPath(new URL('.',
import.meta.url))
const args = new ArgsParser()
const token = args.token
const alphabet = args.alphabet
const maxLength = args.maxLength
const validToken = JWTValidator.validateToken(token)
if (!validToken) {
process.exit(Constants.EXIT_CODE_FAILURE)
}
const printResult = function (startTime, attempts, result) {
if (result) {
console.log('SECRET FOUND:', result)
} else {
console.log('SECRET NOT FOUND')
}
console.log('Time taken (sec):', (new Date().getTime() - startTime) / 1000)
console.log('Attempts:', attempts)
}
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) {
chunk.push(comb)
if (chunk.length >= chunkSize) {
// save chunk and reset it
forkChunk(chunk)
chunk = []
}
})
.on('end', function () {
printResult(startTime, attempts)
process.exit(Constants.EXIT_CODE_FAILURE)
})
function forkChunk (chunk) {
const child = fork(join(__dirname, '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(Constants.EXIT_CODE_SUCCESS)
}
})
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
}