-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmd4_cracker_openssl.cpp
More file actions
198 lines (163 loc) · 6.06 KB
/
Copy pathmd4_cracker_openssl.cpp
File metadata and controls
198 lines (163 loc) · 6.06 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
/*
M25-514 Kirgizov Temurmalik Kutbiddin ugli.
BDZ-2. MD4 Brute Force (OpenSSL) impl.
*/
#include <iostream>
#include <thread>
#include <vector>
#include <atomic>
#include <iomanip>
#include <sstream>
#include <mutex>
#include <chrono>
#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;
atomic<bool> password_found(false);
mutex output_mutex;
string found_password = "";
int calculate_threads_count(int A, int B) {
int X = 2 * A + B;
return X % 5 + 2;
}
int calculate_distribution_type(int A, int B) {
return (2 * A + B) % 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 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();
}
// Convert number to password string
// Base-26 encoding: aaaaaaaa, baaaaaaa, caaaaaaa, etc.
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';
}
/**
* Block distribution worker
* Each thread gets a continuous chunk of passwords
*/
void crack_block(int thread_id, int num_threads, const string& target_hash,
long long start_offset = 0) {
long long chunk_size = TOTAL_PASSWORDS / num_threads;
long long start = thread_id * chunk_size + start_offset;
long long end = (thread_id == num_threads - 1) ?
TOTAL_PASSWORDS + start_offset :
(thread_id + 1) * chunk_size + start_offset;
char password[PASSWORD_LENGTH + 1];
long long checked = 0;
{
lock_guard<mutex> lock(output_mutex);
cout << "Thread " << thread_id << ": range [" << start << ", " << end << ")" << endl;
}
for (long long i = start; i < end && !password_found; i++) {
number_to_password(i, password);
if (computeMD4(password, PASSWORD_LENGTH) == target_hash) {
password_found = true;
found_password = password;
lock_guard<mutex> lock(output_mutex);
cout << "Thread " << thread_id << " FOUND IT! XDDDDDDDD lox-pwd" << endl;
cout << "Password: " << password << endl;
return;
}
if (++checked % 10000000 == 0) {
lock_guard<mutex> lock(output_mutex);
cout << "Thread " << thread_id << ": " << checked << " checked" << endl;
}
}
}
/**
* Cyclic distribution worker
* Thread i checks passwords i, i+N, i+2N, i+3N, ...
*/
void crack_cyclic(int thread_id, int num_threads, const string& target_hash,
bool reverse = false) {
char password[PASSWORD_LENGTH + 1];
long long checked = 0;
long long start = reverse ? (TOTAL_PASSWORDS - 1 - thread_id) : thread_id;
long long step = reverse ? -num_threads : num_threads;
long long limit = reverse ? -1 : TOTAL_PASSWORDS;
for (long long i = start;
(reverse ? i > limit : i < limit) && !password_found;
i += step) {
number_to_password(i, password);
if (computeMD4(password, PASSWORD_LENGTH) == target_hash) {
password_found = true;
found_password = password;
lock_guard<mutex> lock(output_mutex);
cout << "\nThread " << thread_id << " FOUND IT!" << endl;
cout << "Password: " << password << endl;
return;
}
if (++checked % 10000000 == 0) {
lock_guard<mutex> lock(output_mutex);
cout << "Thread " << thread_id << ": " << checked << " checked" << endl;
}
}
}
int main(int argc, char* argv[]) {
// Default: MD4 hash of "aaa"
string target_hash = "918d7099b77c7a06634c62ccaf5ebac7";
if (argc > 1) target_hash = argv[1];
int num_threads = calculate_threads_count(A, B);
int dist_type = calculate_distribution_type(A, B);
cout << "Threads: " << num_threads << endl;
cout << "Distribution: " << dist_type;
switch(dist_type) {
case 0: cout << " (Block L->R)"; break;
case 1: cout << " (Block R->L)"; break;
case 2: cout << " (Cyclic)"; break;
case 3: cout << " (Reverse Cyclic)"; break;
}
cout << endl;
cout << "Target: " << target_hash << endl;
// Test
cout << "\nTest: MD4('aaa') = " << computeMD4("aaa", 3) << endl << endl;
vector<thread> threads;
auto start_time = chrono::high_resolution_clock::now();
// Launch threads based on distribution type
switch(dist_type) {
case 0: // Block L->R
for (int i = 0; i < num_threads; i++)
threads.emplace_back(crack_block, i, num_threads, ref(target_hash), 0);
break;
case 1: // Block R->L
for (int i = 0; i < num_threads; i++)
threads.emplace_back(crack_block, i, num_threads, ref(target_hash),
TOTAL_PASSWORDS - (TOTAL_PASSWORDS / num_threads) * num_threads);
break;
case 2: // Cyclic
for (int i = 0; i < num_threads; i++)
threads.emplace_back(crack_cyclic, i, num_threads, ref(target_hash), false);
break;
case 3: // Reverse Cyclic
for (int i = 0; i < num_threads; i++)
threads.emplace_back(crack_cyclic, i, num_threads, ref(target_hash), true);
break;
}
for (auto& t : threads) t.join();
auto duration = chrono::duration_cast<chrono::seconds>(
chrono::high_resolution_clock::now() - start_time);
if (password_found) {
cout << "SUCCESS! Password: " << found_password << endl;
cout << "Verification: " << computeMD4(found_password.c_str(), PASSWORD_LENGTH) << endl;
} else {
cout << "Not found" << endl;
}
cout << "Time bruteforcing: " << duration.count() << "s" << endl;
return 0;
}