-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
170 lines (153 loc) · 3.92 KB
/
main.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
"math/big"
"strings"
"sync"
)
type jwtHeader struct {
Algorithm string `json:"alg"`
Type string `json:"typ"`
}
type jwt struct {
header *jwtHeader
payload string
message, signature []byte
}
func parseJWT(input string) (*jwt, error) {
parts := strings.Split(input, ".")
decodedParts := make([][]byte, len(parts))
if len(parts) != 3 {
return nil, errors.New("invalid jwt: does not contain 3 parts (header, payload, signature)")
}
for i := range parts {
decodedParts[i] = make([]byte, base64.RawURLEncoding.DecodedLen(len(parts[i])))
if _, err := base64.RawURLEncoding.Decode(decodedParts[i], []byte(parts[i])); err != nil {
return nil, err
}
}
var parsedHeader jwtHeader
if err := json.Unmarshal(decodedParts[0], &parsedHeader); err != nil {
return nil, err
}
return &jwt{
header: &parsedHeader,
payload: string(decodedParts[1]),
message: []byte(parts[0] + "." + parts[1]),
signature: decodedParts[2],
}, nil
}
func generateSignature(message, secret []byte) []byte {
hasher := hmac.New(sha256.New, secret)
hasher.Write(message)
return hasher.Sum(nil)
}
func generateSecrets(alphabet string, n int, wg *sync.WaitGroup, done chan struct{}) <-chan string {
if n <= 0 {
return nil
}
c := make(chan string)
wg.Add(1)
go func() {
defer close(c)
var helper func(string)
helper = func(input string) {
if len(input) == n {
return
}
select {
case <-done:
return
default:
}
for _, char := range alphabet {
s := input + string(char)
c <- s
helper(s)
}
}
helper("")
wg.Done()
}()
return c
}
func main() {
token := flag.String("token", "", "The full HS256 jwt token to crack")
alphabet := flag.String("alphabet", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", "The alphabet to use for the brute force")
prefix := flag.String("prefix", "", "A string that is always prefixed to the secret")
suffix := flag.String("suffix", "", "A string that is always suffixed to the secret")
maxLength := flag.Int("maxlen", 12, "The max length of the string generated during the brute force")
flag.Parse()
if *token == "" {
fmt.Println("Parameter token is empty\n")
flag.Usage()
return
}
if *alphabet == "" {
fmt.Println("Parameter alphabet is empty\n")
flag.Usage()
return
}
if *maxLength == 0 {
fmt.Println("Parameter maxlen is 0\n")
flag.Usage()
return
}
parsed, err := parseJWT(*token)
if err != nil {
fmt.Printf("Could not parse JWT: %v\n", err)
return
}
fmt.Printf("Parsed JWT:\n- Algorithm: %s\n- Type: %s\n- Payload: %s\n- Signature (hex): %s\n\n",
parsed.header.Algorithm,
parsed.header.Type,
parsed.payload,
hex.EncodeToString(parsed.signature))
if strings.ToUpper(parsed.header.Algorithm) != "HS256" {
fmt.Println("Unsupported algorithm")
return
}
combinations := big.NewInt(0)
for i := 1; i <= *maxLength; i++ {
alen, mlen := big.NewInt(int64(len(*alphabet))), big.NewInt(int64(i))
combinations.Add(combinations, alen.Exp(alen, mlen, nil))
}
fmt.Printf("There are %s combinations to attempt\nStart cracking JWT secret...\n", combinations.String())
done := make(chan struct{})
wg := &sync.WaitGroup{}
var found bool
var attempts uint64
for secret := range generateSecrets(*alphabet, *maxLength, wg, done) {
wg.Add(1)
go func(s string, i uint64) {
select {
case <-done:
wg.Done()
return
default:
}
if bytes.Equal(parsed.signature, generateSignature(parsed.message, []byte(*prefix+s+*suffix))) {
fmt.Printf("Found secret in %d attempts: %s\n", attempts, *prefix+s+*suffix)
found = true
close(done)
}
wg.Done()
}(secret, attempts)
attempts++
if attempts%100000 == 0 {
fmt.Printf("Attempts: %d\n", attempts)
}
}
wg.Wait()
if !found {
fmt.Printf("No secret found in %d attempts\n", attempts)
}
}