-
Notifications
You must be signed in to change notification settings - Fork 0
/
hack.c
152 lines (110 loc) · 2.99 KB
/
hack.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "secret.h"
unsigned long cpu_clock();
int sort_cmp(const void *a, const void *b);
int check_password(char* password);
char* crack_password(int rounds);
int main(int argc, char **argv) {
srand(time(NULL));
int rounds = 1024;
if (argc > 1) {
rounds = atoi(argv[1]);
} else {
printf("Defaulting to %d rounds.\n", rounds);
printf("To change the number of rounds, use: %s [rounds]\n", argv[0]);
}
char *password = crack_password(rounds);
if (password == NULL) {
printf("Unable to crack.\n");
} else {
printf("Password: \"%s\"\n", password);
free(password);
}
return 0;
}
int sort_cmp(const void *a, const void *b) {
return (*(unsigned long*)a - *(unsigned long*)b);
}
char* crack_password(int rounds) {
const int length = 64;
const char lower_bound = 32;
const char upper_bound = 127;
const int range = upper_bound - lower_bound;
char *password = malloc(length);
if (password == NULL) {
exit(1);
}
memset(password, 0, length);
char pass_rnd_align[length + 64];
int *attempt_cycles[range];
for (int i = 0; i < range; i++) {
attempt_cycles[i] = malloc(rounds * sizeof(int));
if (attempt_cycles[i] == NULL) {
for (int j = 0; j < i; j++) {
free(attempt_cycles[j]);
}
free(password);
exit(1);
}
}
int median_cycles[range];
char charset[range];
for (char c = lower_bound; c < upper_bound; c++) {
charset[c - lower_bound] = c;
}
printf("Attempting to crack password with %d rounds...\n", rounds);
for (int i = 0; i < length - 1; i++) {
for (int j = 0; j < rounds; j++) {
for (int k = 0; k < range; k++) {
int swap = rand() % range;
char temp = charset[k];
charset[k] = charset[swap];
charset[swap] = temp;
}
int offset = rand() & 63;
char *pass_rnd_align_ptr = pass_rnd_align + offset;
strcpy(pass_rnd_align_ptr, password);
pass_rnd_align_ptr[i + 1] = '\0';
for (int k = 0; k < range; k++) {
char c = charset[k];
int char_index = c - lower_bound;
pass_rnd_align_ptr[i] = c;
unsigned long start = cpu_clock();
if (check_password(pass_rnd_align_ptr)) {
for (int i = 0; i < range; i++) {
free(attempt_cycles[i]);
}
password[i] = c;
return password;
}
unsigned long end = cpu_clock();
int duration = end - start;
attempt_cycles[char_index][j] = duration;
}
}
for (int j = 0; j < range; j++) {
qsort(attempt_cycles[j], rounds, sizeof(int), sort_cmp);
median_cycles[j] = attempt_cycles[j][rounds >> 1];
}
int max_cycles = 0;
char candidate;
for (char k = lower_bound; k < upper_bound; k++) {
int char_index = k - lower_bound;
password[i] = k;
if (median_cycles[char_index] > max_cycles) {
candidate = k;
max_cycles = median_cycles[char_index];
}
}
password[i] = candidate;
printf("Still no hax. Best guess yet: \"%s\"\n", password);
}
free(password);
for (int i = 0; i < range; i++) {
free(attempt_cycles[i]);
}
return NULL;
}