4
4
#include <openssl/evp.h>
5
5
#include <openssl/hmac.h>
6
6
#include <stdbool.h>
7
+ #include <time.h>
7
8
#include "base64.h"
8
9
9
10
@@ -22,61 +23,63 @@ size_t g_to_encrypt_len = 0;
22
23
char * g_alphabet = NULL ;
23
24
size_t g_alphabet_len = 0 ;
24
25
25
- bool check (const char * secret , size_t secret_len ) {
26
- // printf("Trying %s (%lu)\n", secret, secret_len);
26
+ unsigned char * g_result = NULL ;
27
+ unsigned int g_result_len = 0 ;
28
+
29
+ char * g_buffer = NULL ;
27
30
31
+ EVP_MD * g_evp_md = NULL ;
32
+
33
+ bool check (const char * secret , size_t secret_len ) {
28
34
// Hash to_encrypt using HMAC-SHA256 into result
29
- unsigned int result_len = 0 ;
30
- unsigned char * result = malloc (EVP_MAX_MD_SIZE );
31
35
HMAC (
32
- EVP_sha256 () ,
36
+ g_evp_md ,
33
37
(const unsigned char * ) secret , secret_len ,
34
38
(const unsigned char * ) g_to_encrypt , g_to_encrypt_len ,
35
- result , & result_len
39
+ g_result , & g_result_len
36
40
);
37
41
38
42
// Compare the computed hash to the given decoded base64 signature.
39
43
// If there is a match, we just found the key.
40
- int res = memcmp (result , g_signature , g_signature_len );
41
- free (result );
42
- return res == 0 ;
44
+ return memcmp (g_result , g_signature , g_signature_len ) == 0 ;
43
45
}
44
46
45
- void bruteImpl (char * str , int index , int maxDepth )
47
+ bool bruteImpl (char * str , int index , int maxDepth )
46
48
{
47
49
for (int i = 0 ; i < g_alphabet_len ; ++ i )
48
50
{
49
51
str [index ] = g_alphabet [i ];
50
52
51
53
if (index == maxDepth - 1 ) {
52
- bool stop = check ((const char * ) str , strlen (str ));
53
- if (stop ) {
54
- printf ("FOUND \"%s\"" , str );
55
- exit (0 );
56
- }
54
+ if (check ((const char * ) str , maxDepth )) return true;
55
+ }
56
+ else {
57
+ if (bruteImpl (str , index + 1 , maxDepth )) return true;
57
58
}
58
- else bruteImpl (str , index + 1 , maxDepth );
59
59
}
60
+
61
+ return false;
60
62
}
61
63
62
- void bruteSequential (int maxLen )
64
+ char * bruteSequential (int maxLen )
63
65
{
64
- char * buf = malloc ( maxLen + 1 ) ;
66
+ char * ret = NULL ;
65
67
66
68
for (int i = 1 ; i <= maxLen ; ++ i )
67
69
{
68
- memset (buf , 0 , maxLen + 1 );
69
- bruteImpl (buf , 0 , i );
70
+ if (bruteImpl (g_buffer , 0 , i )) {
71
+ ret = strdup (g_buffer );
72
+ break ;
73
+ }
70
74
}
71
75
72
- printf ("No solution found :-(\n" );
73
- free (buf );
76
+ return ret ;
74
77
}
75
78
76
79
void usage (const char * cmd ) {
77
80
printf ("%s <token> [alphabet] [max_len]\n"
78
- "Defaults: max_len=6, "
79
- "alphabet=eariotnslcudpmhgbfywkvxzjqEARIOTNSLCUDPMHGBFYWKVXZJQ0123456789" , cmd );
81
+ "Defaults: max_len=6, "
82
+ "alphabet=eariotnslcudpmhgbfywkvxzjqEARIOTNSLCUDPMHGBFYWKVXZJQ0123456789" , cmd );
80
83
}
81
84
82
85
int main (int argc , char * * argv ) {
@@ -91,11 +94,10 @@ int main(int argc, char **argv) {
91
94
// Get the token
92
95
char * jwt = argv [1 ];
93
96
94
- if (argc > 2 ) {
95
- g_alphabet = argv [2 ];
96
- }
97
+ if (argc > 2 )
98
+ g_alphabet = argv [2 ];
97
99
if (argc > 3 )
98
- max_len = (size_t ) atoi (argv [3 ]);
100
+ max_len = (size_t ) atoi (argv [3 ]);
99
101
100
102
g_alphabet_len = strlen (g_alphabet );
101
103
@@ -122,7 +124,26 @@ int main(int argc, char **argv) {
122
124
// is returned by this function
123
125
g_signature_len = Base64decode ((char * ) g_signature , (const char * ) g_signature_b64 );
124
126
125
- bruteSequential (max_len );
127
+ g_result = malloc (EVP_MAX_MD_SIZE );
128
+ g_buffer = malloc (max_len + 1 );
129
+ g_evp_md = EVP_sha256 ();
130
+
131
+ clock_t start = clock (), diff ;
132
+ char * secret = bruteSequential (max_len );
133
+ diff = clock () - start ;
134
+
135
+
136
+ if (secret == NULL )
137
+ printf ("No solution found :-(\n" );
138
+ else
139
+ printf ("Secret is \"%s\"\n" , secret );
140
+
141
+ free (g_result );
142
+ free (g_buffer );
143
+ free (secret );
144
+
145
+ int msec = diff * 1000 / CLOCKS_PER_SEC ;
146
+ printf ("Time taken %d seconds %d milliseconds" , msec /1000 , msec %1000 );
126
147
127
148
return 0 ;
128
149
}
0 commit comments