Skip to content

Commit 240b0ba

Browse files
committed
Try to remove as much work as possible from the loop
1 parent 608a841 commit 240b0ba

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ CC = gcc
22
RM = rm -f
33
CP = cp
44

5-
CFLAGS += -I/usr/local/opt/openssl/include
5+
CFLAGS += -I/usr/local/opt/openssl/include -O3
66
LDFLAGS += -lssl -lcrypto
77

88
NAME = jwtcrack

main.c

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <openssl/evp.h>
55
#include <openssl/hmac.h>
66
#include <stdbool.h>
7+
#include <time.h>
78
#include "base64.h"
89

910

@@ -22,61 +23,63 @@ size_t g_to_encrypt_len = 0;
2223
char *g_alphabet = NULL;
2324
size_t g_alphabet_len = 0;
2425

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;
2730

31+
EVP_MD *g_evp_md = NULL;
32+
33+
bool check(const char *secret, size_t secret_len) {
2834
// Hash to_encrypt using HMAC-SHA256 into result
29-
unsigned int result_len = 0;
30-
unsigned char* result = malloc(EVP_MAX_MD_SIZE);
3135
HMAC(
32-
EVP_sha256(),
36+
g_evp_md,
3337
(const unsigned char *) secret, secret_len,
3438
(const unsigned char *) g_to_encrypt, g_to_encrypt_len,
35-
result, &result_len
39+
g_result, &g_result_len
3640
);
3741

3842
// Compare the computed hash to the given decoded base64 signature.
3943
// 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;
4345
}
4446

45-
void bruteImpl(char* str, int index, int maxDepth)
47+
bool bruteImpl(char* str, int index, int maxDepth)
4648
{
4749
for (int i = 0; i < g_alphabet_len; ++i)
4850
{
4951
str[index] = g_alphabet[i];
5052

5153
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;
5758
}
58-
else bruteImpl(str, index + 1, maxDepth);
5959
}
60+
61+
return false;
6062
}
6163

62-
void bruteSequential(int maxLen)
64+
char *bruteSequential(int maxLen)
6365
{
64-
char* buf = malloc(maxLen + 1);
66+
char *ret = NULL;
6567

6668
for (int i = 1; i <= maxLen; ++i)
6769
{
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+
}
7074
}
7175

72-
printf("No solution found :-(\n");
73-
free(buf);
76+
return ret;
7477
}
7578

7679
void usage(const char *cmd) {
7780
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);
8083
}
8184

8285
int main(int argc, char **argv) {
@@ -91,11 +94,10 @@ int main(int argc, char **argv) {
9194
// Get the token
9295
char *jwt = argv[1];
9396

94-
if (argc > 2) {
95-
g_alphabet = argv[2];
96-
}
97+
if (argc > 2)
98+
g_alphabet = argv[2];
9799
if (argc > 3)
98-
max_len = (size_t) atoi(argv[3]);
100+
max_len = (size_t) atoi(argv[3]);
99101

100102
g_alphabet_len = strlen(g_alphabet);
101103

@@ -122,7 +124,26 @@ int main(int argc, char **argv) {
122124
// is returned by this function
123125
g_signature_len = Base64decode((char *) g_signature, (const char *) g_signature_b64);
124126

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);
126147

127148
return 0;
128149
}

0 commit comments

Comments
 (0)