This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
md5_gpu.cu
258 lines (195 loc) · 7.47 KB
/
md5_gpu.cu
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
/**
* CUDA MD5 cracker
* Copyright (C) 2015 Konrad Kusnierz <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <sstream>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <curand_kernel.h>
#include <device_functions.h>
#define CONST_WORD_LIMIT 10
#define CONST_CHARSET_LIMIT 100
#define CONST_CHARSET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
#define CONST_CHARSET_LENGTH (sizeof(CONST_CHARSET) - 1)
#define CONST_WORD_LENGTH_MIN 1
#define CONST_WORD_LENGTH_MAX 8
#define TOTAL_BLOCKS 16384UL
#define TOTAL_THREADS 512UL
#define HASHES_PER_KERNEL 128UL
#include "assert.cu"
#include "md5.cu"
/* Global variables */
uint8_t g_wordLength;
char g_word[CONST_WORD_LIMIT];
char g_charset[CONST_CHARSET_LIMIT];
char g_cracked[CONST_WORD_LIMIT];
__device__ char g_deviceCharset[CONST_CHARSET_LIMIT];
__device__ char g_deviceCracked[CONST_WORD_LIMIT];
__device__ __host__ bool next(uint8_t* length, char* word, uint32_t increment){
uint32_t idx = 0;
uint32_t add = 0;
while(increment > 0 && idx < CONST_WORD_LIMIT){
if(idx >= *length && increment > 0){
increment--;
}
add = increment + word[idx];
word[idx] = add % CONST_CHARSET_LENGTH;
increment = add / CONST_CHARSET_LENGTH;
idx++;
}
if(idx > *length){
*length = idx;
}
if(idx > CONST_WORD_LENGTH_MAX){
return false;
}
return true;
}
__global__ void md5Crack(uint8_t wordLength, char* charsetWord, uint32_t hash01, uint32_t hash02, uint32_t hash03, uint32_t hash04){
uint32_t idx = (blockIdx.x * blockDim.x + threadIdx.x) * HASHES_PER_KERNEL;
/* Shared variables */
__shared__ char sharedCharset[CONST_CHARSET_LIMIT];
/* Thread variables */
char threadCharsetWord[CONST_WORD_LIMIT];
char threadTextWord[CONST_WORD_LIMIT];
uint8_t threadWordLength;
uint32_t threadHash01, threadHash02, threadHash03, threadHash04;
/* Copy everything to local memory */
memcpy(threadCharsetWord, charsetWord, CONST_WORD_LIMIT);
memcpy(&threadWordLength, &wordLength, sizeof(uint8_t));
memcpy(sharedCharset, g_deviceCharset, sizeof(uint8_t) * CONST_CHARSET_LIMIT);
/* Increment current word by thread index */
next(&threadWordLength, threadCharsetWord, idx);
for(uint32_t hash = 0; hash < HASHES_PER_KERNEL; hash++){
for(uint32_t i = 0; i < threadWordLength; i++){
threadTextWord[i] = sharedCharset[threadCharsetWord[i]];
}
md5Hash((unsigned char*)threadTextWord, threadWordLength, &threadHash01, &threadHash02, &threadHash03, &threadHash04);
if(threadHash01 == hash01 && threadHash02 == hash02 && threadHash03 == hash03 && threadHash04 == hash04){
memcpy(g_deviceCracked, threadTextWord, threadWordLength);
}
if(!next(&threadWordLength, threadCharsetWord, 1)){
break;
}
}
}
int main(int argc, char* argv[]){
/* Check arguments */
if(argc != 2 || strlen(argv[1]) != 32){
std::cout << argv[0] << " <md5_hash>" << std::endl;
return -1;
}
/* Amount of available devices */
int devices;
ERROR_CHECK(cudaGetDeviceCount(&devices));
/* Sync type */
ERROR_CHECK(cudaSetDeviceFlags(cudaDeviceScheduleSpin));
/* Display amount of devices */
std::cout << "Notice: " << devices << " device(s) found" << std::endl;
/* Hash stored as u32 integers */
uint32_t md5Hash[4];
/* Parse argument */
for(uint8_t i = 0; i < 4; i++){
char tmp[16];
strncpy(tmp, argv[1] + i * 8, 8);
sscanf(tmp, "%x", &md5Hash[i]);
md5Hash[i] = (md5Hash[i] & 0xFF000000) >> 24 | (md5Hash[i] & 0x00FF0000) >> 8 | (md5Hash[i] & 0x0000FF00) << 8 | (md5Hash[i] & 0x000000FF) << 24;
}
/* Fill memory */
memset(g_word, 0, CONST_WORD_LIMIT);
memset(g_cracked, 0, CONST_WORD_LIMIT);
memcpy(g_charset, CONST_CHARSET, CONST_CHARSET_LENGTH);
/* Current word length = minimum word length */
g_wordLength = CONST_WORD_LENGTH_MIN;
/* Main device */
cudaSetDevice(0);
/* Time */
cudaEvent_t clockBegin;
cudaEvent_t clockLast;
cudaEventCreate(&clockBegin);
cudaEventCreate(&clockLast);
cudaEventRecord(clockBegin, 0);
/* Current word is different on each device */
char** words = new char*[devices];
for(int device = 0; device < devices; device++){
cudaSetDevice(device);
/* Copy to each device */
ERROR_CHECK(cudaMemcpyToSymbol(g_deviceCharset, g_charset, sizeof(uint8_t) * CONST_CHARSET_LIMIT, 0, cudaMemcpyHostToDevice));
ERROR_CHECK(cudaMemcpyToSymbol(g_deviceCracked, g_cracked, sizeof(uint8_t) * CONST_WORD_LIMIT, 0, cudaMemcpyHostToDevice));
/* Allocate on each device */
ERROR_CHECK(cudaMalloc((void**)&words[device], sizeof(uint8_t) * CONST_WORD_LIMIT));
}
while(true){
bool result = false;
bool found = false;
for(int device = 0; device < devices; device++){
cudaSetDevice(device);
/* Copy current data */
ERROR_CHECK(cudaMemcpy(words[device], g_word, sizeof(uint8_t) * CONST_WORD_LIMIT, cudaMemcpyHostToDevice));
/* Start kernel */
md5Crack<<<TOTAL_BLOCKS, TOTAL_THREADS>>>(g_wordLength, words[device], md5Hash[0], md5Hash[1], md5Hash[2], md5Hash[3]);
/* Global increment */
result = next(&g_wordLength, g_word, TOTAL_THREADS * HASHES_PER_KERNEL * TOTAL_BLOCKS);
}
/* Display progress */
char word[CONST_WORD_LIMIT];
for(int i = 0; i < g_wordLength; i++){
word[i] = g_charset[g_word[i]];
}
std::cout << "Notice: currently at " << std::string(word, g_wordLength) << " (" << (uint32_t)g_wordLength << ")" << std::endl;
for(int device = 0; device < devices; device++){
cudaSetDevice(device);
/* Synchronize now */
cudaDeviceSynchronize();
/* Copy result */
ERROR_CHECK(cudaMemcpyFromSymbol(g_cracked, g_deviceCracked, sizeof(uint8_t) * CONST_WORD_LIMIT, 0, cudaMemcpyDeviceToHost));
/* Check result */
if(found = *g_cracked != 0){
std::cout << "Notice: cracked " << g_cracked << std::endl;
break;
}
}
if(!result || found){
if(!result && !found){
std::cout << "Notice: found nothing (host)" << std::endl;
}
break;
}
}
for(int device = 0; device < devices; device++){
cudaSetDevice(device);
/* Free on each device */
cudaFree((void**)words[device]);
}
/* Free array */
delete[] words;
/* Main device */
cudaSetDevice(0);
float milliseconds = 0;
cudaEventRecord(clockLast, 0);
cudaEventSynchronize(clockLast);
cudaEventElapsedTime(&milliseconds, clockBegin, clockLast);
std::cout << "Notice: computation time " << milliseconds << " ms" << std::endl;
cudaEventDestroy(clockBegin);
cudaEventDestroy(clockLast);
}