-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathripper.c
More file actions
474 lines (407 loc) · 14.8 KB
/
Copy pathripper.c
File metadata and controls
474 lines (407 loc) · 14.8 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <stdbool.h> // For using the 'bool' type
// --- Constants and Global Data ---
#define RATE 127
#define RANGE 1000000 // Number of results
// The global data arrays
static uint8_t remap_table[] = { 7, 2, 1, 4, 8, 3, 6, 5,
6, 5, 4, 1, 7, 8, 3, 2,
7, 5, 6, 1, 4, 3, 2, 8,
1, 3, 8, 5, 7, 6, 4, 2,
2, 3, 5, 7, 4, 6, 1, 8,
8, 2, 4, 7, 1, 3, 6, 5,
4, 5, 1, 8, 7, 3, 2, 6,
4, 8, 5, 7, 1, 2, 3, 6,
3, 7, 5, 2, 1, 8, 6, 4,
2, 5, 1, 4, 8, 7, 3, 6,
8, 7, 2, 6, 4, 1, 5, 3,
4, 7, 1, 3, 8, 2, 6, 5,
8, 6, 2, 4, 7, 3, 5, 1,
2, 5, 1, 3, 6, 4, 7, 8,
4, 3, 8, 6, 2, 7, 5, 1,
7, 4, 8, 5, 1, 2, 6, 3 };
static uint8_t hash_table[] = { 0, 32, 0, 4, 1, 0, 0, 2, 128, 0, 0, 8, 64, 0, 16, 0,
16, 4, 0, 0, 32, 64, 0, 128, 8, 1, 0, 0, 0, 2, 0, 0,
2, 0, 64, 32, 0, 0, 16, 0, 0, 1, 8, 128, 4, 0, 0, 0,
0, 128, 0, 16, 4, 0, 0, 32, 8, 0, 1, 0, 0, 0, 2, 64,
0, 0, 1, 0, 0, 64, 16, 0, 0, 2, 4, 0, 0, 8, 128, 32,
16, 0, 8, 0, 0, 2, 64, 0, 0, 0, 0, 4, 1, 32, 0, 128,
56, 104, 152, 200, 35, 75, 115, 155, 12, 10, 12, 10, 10, 12, 10, 12,
6, 1, 0, 3, 7, 2, 5, 4, 5, 4, 3, 0, 6, 7, 2, 1,
6, 4, 5, 0, 3, 2, 1, 7, 0, 2, 7, 4, 6, 5, 3, 1,
1, 2, 4, 6, 3, 5, 0, 7, 7, 1, 3, 6, 0, 2, 5, 4,
3, 4, 0, 7, 6, 2, 1, 5, 3, 7, 4, 6, 0, 1, 2, 5,
2, 6, 4, 1, 0, 7, 5, 3, 1, 4, 0, 3, 7, 6, 2, 5,
7, 6, 1, 5, 3, 0, 4, 2, 3, 6, 0, 2, 7, 1, 5, 4,
7, 5, 1, 3, 6, 2, 4, 0, 1, 4, 0, 2, 5, 3, 6, 7,
3, 2, 7, 5, 1, 6, 4, 0, 6, 3, 7, 4, 0, 1, 5, 2 };
#define array_size ((16 * (RANGE / RATE)) + 10000)
// Global output arrays
uint8_t output_array_1[array_size] = { 0 };
uint8_t output_array_2[array_size] = { 0 };
uint8_t output_array_3[array_size] = { 0 };
uint8_t output_array_4[array_size] = { 0 };
// --- Structure for Thread Arguments ---
typedef struct {
int lower;
int upper;
bool print;
int thread_number;
} ThreadArgs;
// --- Function Prototypes ---
int hash_subroutine(uint8_t* _1e8x, int index, int n);
void bruteforce(ThreadArgs *args);
void fast_test(uint8_t* password, bool print);
// --- Core Logic Functions ---
/**
* @brief Performs a fast test to check if a given password is valid.
*/
void fast_test(uint8_t* password, bool print) {
uint8_t password_copy[16];
memcpy(password_copy, password, 16 * sizeof(uint8_t));
uint8_t idx = 0;
uint8_t x, y, item = 0;
int _1e96 = 15;
int _1e94 = 0;
bool _1e94_flag = false;
uint8_t and_03 = 0;
int zf = 0;
// Remap Password
for (int i = 0; i < 16; i++) {
idx = 7;
while (password[i] != remap_table[idx + 8 * i]) {
idx -= 1;
}
password[i] = idx;
}
// Calculate Hash
uint8_t _1e8x[6] = { 0 };
x = 0; y = 0; item = 0;
_1e96 = 15;
_1e94 = 0;
_1e94_flag = false;
while (_1e96 >= 0) {
_1e94_flag = false;
y = 0;
item = password[_1e96];
item = item << 5;
_1e94 = item;
if (_1e94 > 0) {
_1e94_flag = true;
}
for (int k = 0; k < 3; k++) {
item = 0;
while (item == 0) {
item = y;
item = item << 4;
item = item + _1e96;
x = item;
item = hash_table[x];
if (item == 0) {
y += 1;
}
}
x = y;
_1e94 = _1e94 << 1;
if (_1e94 >= 256) {
_1e8x[x] = _1e8x[x] | item;
if (!_1e94_flag) {
_1e94 = 0;
}
else {
_1e94 = _1e94 & 0xFF;
}
}
y += 1;
}
_1e96 -= 1;
}
// Hash Normalization
and_03 = _1e8x[3] & 0x03;
_1e94 = and_03;
for (int i = 0; i < 4; i++) {
and_03 = and_03 << 1;
}
_1e94 = and_03 | _1e94;
_1e8x[0] = _1e8x[0] ^ _1e94;
_1e8x[1] = _1e8x[1] ^ _1e94;
_1e8x[2] = _1e8x[2] ^ _1e94;
_1e8x[5] = _1e8x[5] ^ _1e94;
_1e8x[4] = _1e8x[4] ^ _1e94;
// Hash Testing
zf = hash_subroutine(_1e8x, 0, 3);
int res = 0;
if (zf == 1) {
zf = hash_subroutine(_1e8x, 1, 4);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 2, 5);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 5, 6);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 4, 7);
if (zf == 1) {
if ((_1e8x[5] & 64) == 0) {
_1e94 = _1e8x[5] & 191;
res = ((_1e8x[0] | _1e8x[1]) | _1e94) | _1e8x[4];
if (res == 0) {
if (print) {
for (int i = 0; i < 16; i++) {
printf("%d ", (int)password_copy[i]);
}
printf("\n");
}
}
}
else {
int last_test = _1e8x[3] & 4;
if (last_test == 0) {
if (print) {
for (int i = 0; i < 16; i++) {
printf("%d ", (int)password_copy[i]);
}
printf("\n");
}
}
}
}
}
}
}
}
}
/**
* @brief Bruteforces randomly generated passwords and checks validity.
*
* This function is the entry point for the thread and accepts a structure pointer.
*/
void bruteforce(ThreadArgs *args) {
// Extract parameters from the struct pointer
int lower = args->lower;
int upper = args->upper;
bool print = args->print;
int thread_number = args->thread_number;
uint8_t password[16];
uint8_t password_copy[16];
uint8_t idx = 0;
uint8_t x, y, item = 0;
int _1e96 = 15;
int _1e94 = 0;
bool _1e94_flag = false;
uint8_t and_03 = 0;
int zf = 0;
int counter = 0;
for (int u = lower; u < upper; u++) {
// Initialize random seed using 'u'
srand(u);
// Generate password
for (int i = 0; i < 16; i++) {
uint8_t number = (rand() % 8) + 1;
password[i] = number;
password_copy[i] = number;
}
// Remap Password
for (int i = 0; i < 16; ++i) {
idx = 7;
while (password[i] != remap_table[idx + 8 * i]) {
idx -= 1;
}
password[i] = idx;
}
// Calculate Hash
uint8_t _1e8x[6] = { 0 };
x = 0; y = 0; item = 0;
_1e96 = 15;
_1e94 = 0;
_1e94_flag = false;
while (_1e96 >= 0) {
_1e94_flag = false;
y = 0;
item = password[_1e96];
item = item << 5;
_1e94 = item;
if (_1e94 > 0) {
_1e94_flag = true;
}
for (int k = 0; k < 3; k++) {
item = 0;
while (item == 0) {
item = y;
item = item << 4;
item = item + _1e96;
x = item;
item = hash_table[x];
if (item == 0) {
y += 1;
}
}
x = y;
_1e94 = _1e94 << 1;
if (_1e94 >= 256) {
_1e8x[x] = _1e8x[x] | item;
if (!_1e94_flag) {
_1e94 = 0;
}
else {
_1e94 = _1e94 & 0xFF;
}
}
y += 1;
}
_1e96 -= 1;
}
// Hash Normalization
and_03 = _1e8x[3] & 0x03;
_1e94 = and_03;
for (int i = 0; i < 4; i++) {
and_03 = and_03 << 1;
}
_1e94 = and_03 | _1e94;
_1e8x[0] = _1e8x[0] ^ _1e94;
_1e8x[1] = _1e8x[1] ^ _1e94;
_1e8x[2] = _1e8x[2] ^ _1e94;
_1e8x[5] = _1e8x[5] ^ _1e94;
_1e8x[4] = _1e8x[4] ^ _1e94;
// Hash Testing
zf = hash_subroutine(_1e8x, 0, 3);
int res = 0;
if (zf == 1) {
zf = hash_subroutine(_1e8x, 1, 4);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 2, 5);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 5, 6);
if (zf == 1) {
zf = hash_subroutine(_1e8x, 4, 7);
if (zf == 1) {
if ((_1e8x[5] & 64) == 0) {
_1e94 = _1e8x[5] & 191;
res = ((_1e8x[0] | _1e8x[1]) | _1e94) | _1e8x[4];
if (res == 0) {
// Use ternary operator for array selection
uint8_t* target_array = (thread_number == 1) ? output_array_1 :
(thread_number == 2) ? output_array_2 :
(thread_number == 3) ? output_array_3 :
output_array_4;
memcpy(target_array + (counter * 16), password_copy, 16 * sizeof(uint8_t));
counter += 1;
}
}
else {
int last_test = _1e8x[3] & 4;
if (last_test == 0) {
// Use ternary operator for array selection
uint8_t* target_array = (thread_number == 1) ? output_array_1 :
(thread_number == 2) ? output_array_2 :
(thread_number == 3) ? output_array_3 :
output_array_4;
memcpy(target_array + (counter * 16), password_copy, 16 * sizeof(uint8_t));
counter += 1;
}
}
}
}
}
}
}
}
// --- File Handling (Pure C) ---
char filename[64];
// snprintf is the safer C equivalent of sprintf
snprintf(filename, sizeof(filename), "Thread %d - Lower %d - Upper %d - Results %d.html",
thread_number, lower, upper, counter);
FILE *myfile = fopen(filename, "w");
if (myfile != NULL) {
// Start HTML
fprintf(myfile, "<html");
for (int i = 0; i < 16 * counter; i++) {
// Get the correct array pointer
uint8_t* current_array = (thread_number == 1) ? output_array_1 :
(thread_number == 2) ? output_array_2 :
(thread_number == 3) ? output_array_3 :
output_array_4;
// Print value and space
fprintf(myfile, "%d ", (int)current_array[i]);
if ((i + 1) % 16 == 0) {
fprintf(myfile, "<br>");
}
}
fprintf(myfile, "</html>");
fclose(myfile);
}
else {
printf("Unable to open file %s\n", filename);
// Note: myfile is already initialized, but good practice to check
if (myfile) fclose(myfile);
}
// Print final count to console
printf("%d\n", counter);
}
/**
* @brief Subroutine for hashing calculation.
*/
int hash_subroutine(uint8_t* _1e8x, int index, int n) {
int zf = 0;
int cf = 0;
int res1 = 0;
int res2 = 0;
int _1e94 = _1e8x[index];
int tmp = _1e8x[3];
tmp = tmp >> n;
int _1e95 = tmp & 1;
int count_x = 0;
tmp = _1e94;
cf = tmp & 1;
tmp = tmp >> 1;
if (cf == 1) {
count_x += 1;
}
while (tmp != 0) {
cf = tmp & 1;
tmp = tmp >> 1;
if (cf == 1) {
count_x += 1;
}
}
res1 = count_x ^ _1e95;
res2 = res1 & 1;
cf = res1 & 1;
if (res2 == 0) {
zf = 1;
}
return zf;
}
// --- Main Function ---
int main() {
// 1. Start time logging
time_t start_time = time(NULL);
// 2. Thread pointers declaration
pthread_t th1, th2, th3, th4;
// 3. Argument structures for each thread
ThreadArgs args1 = { .lower = 0, .upper = RANGE, .print = false, .thread_number = 1 };
ThreadArgs args2 = { .lower = RANGE, .upper = 2 * RANGE, .print = false, .thread_number = 2 };
ThreadArgs args3 = { .lower = 2 * RANGE, .upper = 3 * RANGE, .print = false, .thread_number = 3 };
ThreadArgs args4 = { .lower = 3 * RANGE, .upper = 4 * RANGE, .print = false, .thread_number = 4 };
// 4. Create threads, passing the address of the argument structure
// The function pointer is cast to void* (*)(void*) to match pthread_create signature
pthread_create(&th1, NULL, (void* (*)(void*))bruteforce, &args1);
pthread_create(&th2, NULL, (void* (*)(void*))bruteforce, &args2);
pthread_create(&th3, NULL, (void* (*)(void*))bruteforce, &args3);
pthread_create(&th4, NULL, (void* (*)(void*))bruteforce, &args4);
// 5. Wait for all threads to complete (Join)
pthread_join(th1, NULL);
pthread_join(th2, NULL);
pthread_join(th3, NULL);
pthread_join(th4, NULL);
// 6. End time logging
time_t end_time = time(NULL);
// Calculate elapsed seconds (using difference in time_t)
double elapsed_seconds = difftime(end_time, start_time);
// 7. Print result
printf("Elapsed time is %f seconds\n", elapsed_seconds);
return 0;
}