-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd4_fork_cracker.cpp
More file actions
232 lines (207 loc) · 8.21 KB
/
Copy pathmd4_fork_cracker.cpp
File metadata and controls
232 lines (207 loc) · 8.21 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
/*
* M25-514 Kirgizov Temurmalik Kutbiddin ugli
*
* MD4 Brute Force with fork impl.
*/
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdint>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include <openssl/md4.h>
using namespace std;
const int PASSWORD_LENGTH = 3;
const int ALPHABET_SIZE = 26;
const int A = 514;
const int B = 8;
const long long TOTAL_PASSWORDS = 208827064576LL;
int calculate_processes_count(int A, int B) {
int X = 2 * A + B;
return (X % 5) + 2;
}
int calculate_distribution_type(int A, int B) {
int X = 2 * A + B;
return X % 4;
}
// OpenSSL MD4 computation
string computeMD4(const char* data, size_t len) {
unsigned char digest[MD4_DIGEST_LENGTH]; // 16 bytes
MD4((unsigned char*)data, len, digest);
// Convert binary digest to hex string
stringstream ss;
for (int i = 0; i < MD4_DIGEST_LENGTH; i++) {
ss << hex << setw(2) << setfill('0') << (int)digest[i];
}
return ss.str();
}
// OpenSSL MD4 computation
void number_to_password(long long num, char* password) {
for (int i = 0; i < PASSWORD_LENGTH; i++) {
password[i] = 'a' + (num % ALPHABET_SIZE);
num /= ALPHABET_SIZE;
}
password[PASSWORD_LENGTH] = '\0';
}
// child process worker with his distribution type
void crack_password_process(int process_id, int num_processes,
const string& target_hash,
int distribution_type,
int pipe_write) {
char password[PASSWORD_LENGTH + 1];
long long checked = 0;
long long start, end, step;
if (distribution_type == 0) {
long long chunk_size = TOTAL_PASSWORDS / num_processes;
start = process_id * chunk_size;
end = (process_id == num_processes - 1) ? TOTAL_PASSWORDS : (process_id + 1) * chunk_size;
step = 1;
cout << "Process " << process_id << " (PID " << getpid()
<< "): Block L->R [" << start << ", " << end << ")" << endl;
} else if (distribution_type == 1) {
long long chunk_size = TOTAL_PASSWORDS / num_processes;
long long reverse_id = num_processes - 1 - process_id;
start = reverse_id * chunk_size;
end = (reverse_id == num_processes - 1) ? TOTAL_PASSWORDS : (reverse_id + 1) * chunk_size;
step = 1;
cout << "Process " << process_id << " (PID " << getpid()
<< "): Block R->L [" << start << ", " << end << ")" << endl;
} else if (distribution_type == 2) {
start = process_id;
end = TOTAL_PASSWORDS;
step = num_processes;
cout << "Process " << process_id << " (PID " << getpid()
<< "): Cyclic (every " << num_processes << "th from " << start << ")" << endl;
} else {
start = TOTAL_PASSWORDS - 1 - process_id;
end = -1;
step = -num_processes;
cout << "Process " << process_id << " (PID " << getpid()
<< "): Reverse Cyclic (from " << start << " backwards)" << endl;
}
// Brute force ...
if (step > 0) {
for (long long i = start; i < end; i += step) {
number_to_password(i, password);
if (computeMD4(password, PASSWORD_LENGTH) == target_hash) {
cout << "\nProcess " << process_id << " (PID " << getpid()
<< ") FOUND THE PASSWORD!" << endl;
cout << "Password: " << password << endl;
cout << "Checked: " << checked << " combinations" << endl;
// Send password to parent via pipe
write(pipe_write, password, PASSWORD_LENGTH);
return;
}
checked++;
if (checked % 10000000 == 0) {
cout << "Process " << process_id << ": checked "
<< checked << " passwords..." << endl;
}
}
} else {
// Back search
for (long long i = start; i > end; i += step) {
number_to_password(i, password);
if (computeMD4(password, PASSWORD_LENGTH) == target_hash) {
cout << "\nProcess " << process_id << " (PID " << getpid()
<< ") FOUND THE PASSWORD!" << endl;
cout << "Password: " << password << endl;
cout << "Checked: " << checked << " combinations" << endl;
write(pipe_write, password, PASSWORD_LENGTH);
return;
}
checked++;
if (checked % 10000000 == 0) {
cout << "Process " << process_id << ": checked "
<< checked << " passwords..." << endl;
}
}
}
cout << "Process " << process_id << " finished: checked "
<< checked << " passwords (not found)" << endl;
// not found -> notify parent
char empty[PASSWORD_LENGTH] = {0};
write(pipe_write, empty, PASSWORD_LENGTH);
}
int main(int argc, char* argv[]) {
// Default test hash for "aaa"
string target_hash = "918d7099b77c7a06634c62ccaf5ebac7";
if (argc > 1) {target_hash = argv[1];}
int num_processes = calculate_processes_count(A, B);
int distribution_type = calculate_distribution_type(A, B);
cout << "A = " << A << ", B = " << B << endl;
cout << "Number of processes: " << num_processes << endl;
cout << "Distribution type: " << distribution_type;
switch(distribution_type) {
case 0: cout << " (Block: Left→Right)"; break;
case 1: cout << " (Block: Right→Left)"; break;
case 2: cout << " (Cyclic)"; break;
case 3: cout << " (Reverse Cyclic)"; break;
}
cout << "Target hash: " << target_hash << endl;
cout << "Password length: " << PASSWORD_LENGTH << " characters" << endl;
cout << "Total combinations: " << TOTAL_PASSWORDS << endl;
cout << "Parent PID: " << getpid() << endl;
cout << "Testing MD4 implementation:" << endl;
cout << "MD4('aaa') = " << computeMD4("aaa", 3) << endl;
// Create pipes for inter-process communication
// Each child process will send result through its pipe
int pipes[num_processes][2];
pid_t pids[num_processes];
cout << "Creating " << num_processes << " child processes..." << endl;
// Fork child processes
for (int i = 0; i < num_processes; i++) {
if (pipe(pipes[i]) == -1) {
cerr << "Error creating pipe for process " << i << endl;
return 1;
}
pid_t pid = fork();
if (pid == -1) {
cerr << "Error forking process " << i << endl;
return 1;
} else if (pid == 0) {
// child only can write
close(pipes[i][0]);
crack_password_process(i, num_processes, target_hash,
distribution_type, pipes[i][1]);
close(pipes[i][1]);
exit(0);
} else {
pids[i] = pid;
// parent only can read
close(pipes[i][1]);
}
}
string found_password = "";
cout << "Waiting for results from child processes..." << endl;
// check for all pipes
for (int i = 0; i < num_processes; i++) {
char buffer[PASSWORD_LENGTH + 1] = {0};
int bytes_read = read(pipes[i][0], buffer, PASSWORD_LENGTH);
if (bytes_read > 0 && buffer[0] != '\0' && found_password.empty()) {
buffer[PASSWORD_LENGTH] = '\0';
found_password = string(buffer);
cout << "Received password from process " << i
<< " (PID " << pids[i] << ")" << endl;
for (int j = 0; j < num_processes; j++) {
if (j != i) {
kill(pids[j], SIGTERM);
}
}
}
close(pipes[i][0]);
}
for (int i = 0; i < num_processes; i++) {
int status;
waitpid(pids[i], &status, 0);
}
if (!found_password.empty()) {
cout << "Password found: " << found_password << endl;
cout << "Verification: " << computeMD4(found_password.c_str(), found_password.length()) << endl;
} else {
cout << "Password not found in search space" << endl;
}
return 0;
}