Debian Codegraph @

src/core/ntdict.c

raw symbols SCIP
1 /*
2 *
3 * Ophcrack is a Lanmanager/NTLM hash cracker based on the faster time-memory
4 * trade-off using rainbow tables.
5 *
6 * Created with the help of: Maxime Mueller, Luca Wullschleger, Claude
7 * Hochreutiner, Andreas Huber and Etienne Dysli.
8 *
9 * Copyright (c) 2008 Philippe Oechslin, Cedric Tissieres, Bertrand Mesot
10 *
11 * Ophcrack is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * Ophcrack is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with Ophcrack; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * This program is released under the GPL with the additional exemption
26 * that compiling, linking, and/or using OpenSSL is allowed.
27 *
28 *
29 *
30 *
31 */
32 #include <string.h>
33
34 #include "ntdict.h"
35 #include "table.h"
36 #include "hash.h"
37 #include "ophel.h"
38 #include "original.h"
39 #include "lmtable.h"
40
41 #define my_islower(x) (((unsigned)(x) >= 'a') && ((unsigned)(x) <= 'z'))
42
43 static uchar_t worddict[65536][16];
44 static uchar_t worddict_len[65536];
45 static uchar_t postdict[4096][16];
46 static uchar_t postdict_len[4096];
47 static int dicinit = 1;
48 static uint64_t ntdict_sizes[10] = { 4, 16777216, 34481438, 68962876, 34473102, 68946204, 34475286, 68950572, 34479228, 68958456};
49 /*-------------------------------------------------------------------------*/
50 int ntdict_setup(void *tbl_) {
51 table_t *tbl = tbl_;
52
53 tbl->ncols = 20000;
54 tbl->offset = 64000;
55 tbl->sizes = ntdict_sizes;
56
57 tbl->find = ntdict_find;
58 tbl->check = ntdict_check;
59 tbl->isvalid = ntdict_isvalid;
60
61 tbl->lookup_idx = lmtable_lookup_idx;
62 tbl->lookup_end = lmtable_lookup_end;
63 tbl->lookup_srt = ntdict_lookup_srt;
64
65 /* Load the dictionary files if they have not been loaded yet. */
66
67 if (dicinit) {
68 char fname[STR_BUFF_SIZE];
69 int i;
70
71 /* Load the woddict file. */
72
73 FILE *worddict_file;
74
75 snprintf(fname, sizeof(fname), "%s/worddict", tbl->path);
76 if ((worddict_file = fopen(fname, "rb")) == 0) return 0;
77
78 worddict[0][0]=0;
79 worddict_len[0]=0;
80
81 for (i=1; i<65536; i++) {
82 fread(worddict[i], 16, 1, worddict_file);
83 worddict_len[i]=(uchar_t)strlen((char*)worddict[i]);
84 }
85
86 fclose(worddict_file);
87
88 /* Load the postdict file. */
89
90 FILE *postdict_file;
91
92 snprintf(fname, sizeof(fname), "%s/postdict", tbl->path);
93 if ((postdict_file = fopen(fname, "r")) == 0) return 0;
94
95 postdict[0][0]=0;
96 postdict_len[0]=0;
97
98 for (i=1; i<4096; i++) {
99 fread(postdict[i], 16, 1, postdict_file);
100 postdict_len[i]=(uchar_t)strlen((char*)postdict[i]);
101 }
102
103 fclose(postdict_file);
104
105 dicinit = 0;
106 }
107
108 return 1;
109 }
110 /*-------------------------------------------------------------------------*/
111 void ntdict_find(void *hsh_, void *tbl_, void *el_) {
112 hash_t *hsh = hsh_;
113 table_t *tbl = tbl_;
114 ophel_t *el = el_;
115 uchar_t *hash = hsh->hash;
116 uchar_t *pwd = (uchar_t*)el->pwd;
117 ophstat_t *stat = el->stat;
118
119 int c;
120 int col = el->col;
121 int ncols = tbl->ncols;
122 int n_redux = col + tbl->idx * tbl->offset;
123
124 uchar_t tmp[16];
125
126 memcpy(tmp, hash, 16);
127
128 /* Hash and redux until the last column. */
129
130 for (c=1; c<ncols-col; c++) {
131 n_redux++;
132
133 ntdict_mkredux(tbl, tmp, pwd, n_redux);
134 ++stat->hredux;
135 ntdict_mkhash(pwd, tmp);
136 }
137
138 /* Compute the prefix and postfix. */
139
140 uint32_t prefix = tmp[2];
141 prefix += tmp[3]<<8;
142 prefix += (tmp[4]&0x3F)<<16;
143
144 uint16_t postfix = tmp[0];
145 postfix += tmp[1]<<8;
146
147 el->prefix = prefix;
148 el->postfix = postfix;
149 }
150 /*-------------------------------------------------------------------------*/
151 int ntdict_lookup_srt(void *hsh_, void *tbl_, void *el_) {
152 table_t *tbl = tbl_;
153 ophel_t *el = el_;
154
155 uint64_t offset = el->offset;
156 uint32_t start;
157
158 ophstat_t *stat = el->stat;
159 ++stat->start;
160
161 /* If the .start file has been preloaded, then we look into the memory. */
162
163 if (tbl->srtmem) {
164 uint32_t *mem = (uint32_t*)(tbl->srtmem + offset);
165 start = mem[0];
166 }
167
168 /* Otherwise, we access the disk. */
169
170 else {
171 FILE *srtfile = tbl->srtfile;
172
173 fseeko(srtfile, offset, SEEK_SET);
174 fread(&start, sizeof(start), 1, srtfile);
175 ++stat->fseek_srt;
176 }
177
178 /* Make sure that start is in the correct byte order. */
179
180 start = ftohl(start);
181
182 /* Convert it to a valid hash (passed through pwd anyway). */
183
184 uchar_t *pwd = (uchar_t*) el->pwd;
185
186 pwd[0] = start&0xFF;
187 pwd[1] = (start&0xFF00)>>8;
188 pwd[2] = (start&0xFF0000)>>16;
189 pwd[3] = (start&0xFF000000)>>24;
190
191 return 1;
192 }
193 /*-------------------------------------------------------------------------*/
194 int ntdict_check(void *hsh_, void *tbl_, void *el_) {
195 hash_t *hsh = hsh_;
196 table_t *tbl = tbl_;
197 ophel_t *el = el_;
198 uchar_t *hash = hsh->hash;
199 uchar_t *pwd = (uchar_t*)el->pwd;
200 ophstat_t *stat = el->stat;
201
202 int c;
203 int col = el->col;
204 int ncols = tbl->ncols;
205 int n_redux = tbl->idx * tbl->offset;
206
207 uchar_t tmp[16];
208
209 memset(tmp, 0, 16);
210 memcpy(tmp, el->pwd, 4);
211
212 /* Hash and redux until the starting column. */
213
214 stat->falarm_hredux = 0;
215
216 for (c=ncols-col; c<=ncols; c++) {
217 ntdict_mkredux(tbl, tmp, pwd, n_redux);
218 ntdict_mkhash(pwd, tmp);
219
220
221 ++n_redux;
222 ++stat->hredux;
223 ++stat->falarm_hredux;
224 }
225
226 /* Check if we got the same hash. */
227
228 if (memcmp(tmp, hash, 16) == 0) {
229 ++stat->match_table;
230 stat->falarm_hredux = 0;
231 return 1;
232 }
233
234 else {
235 ++stat->falarm;
236 return 0;
237 }
238 }
239 /*-------------------------------------------------------------------------*/
240 int ntdict_isvalid(void *hsh_, void *tbl_) {
241 hash_t *hsh = hsh_;
242
243 return hsh->kind == nt ? 1 : 0;
244 }
245 /*-------------------------------------------------------------------------*/
246 void ntdict_mkredux(table_t *tbl, uchar_t *hash, uchar_t *pwd,
247 int n_redux) {
248
249 /* Convert the hash to four unsigned 32-bit integers */
250 /* and xor the lower 4 bytes of hash with n_redux */
251
252 uchar_t idx[5];
253
254 int r = n_redux;
255
256 idx[0] = hash[0]^(r & 0xFF);
257 r>>=8;
258 idx[1] = hash[1]^(r & 0xFF);
259 r>>=8;
260 idx[2] = hash[2]^(r & 0xFF);
261 r>>=8;
262 idx[3] = hash[3]^(r & 0xFF);
263 idx[4] = hash[4];
264
265 int i;
266
267 /* Compute the password according to the lower 32 bits of the hash: */
268
269 /* combinations: */
270
271 /* prefix: one of 64 (6 bits) from in[0]
272 capitalize word: ( 1 bit) from in[0] 0x40
273 capitalize 1st letter: ( 1 bit) from in[0] 0x80
274 1337speak: ( 1 bit) from in[3] 0x01
275 one word of dict (16 bits) from in[1] and in[2]
276 postfix: select ( 1 bit) from in[3] 0x02
277 postdict (12 bits) from in[3] and in[4]
278 or
279 two chars (12 bits) from in[3] and in[4]
280 ---------
281 38 bits
282 */
283
284 int capitalized_1st=0;
285 int capitalized=0;
286 int leet = 0;
287 uint32_t pos = 0;
288 memset(pwd, 0, 32*sizeof(uchar_t));
289
290 /* Compute the prefix */
291
292 pwd[pos] = ntdict63[idx[0]&PREFIX];
293
294 if ((pwd[pos]) && my_islower(pwd[pos]) && (idx[0]&CAPITALIZE_1ST)) {
295 pwd[pos]-=32;
296 capitalized_1st=1;
297 }
298
299 if (pwd[pos]) pos++; /* increment only if not empty char */
300
301 /* Compute the word */
302
303 uint32_t word = idx[1]+(idx[2]<<8);
304
305 for (i=0; i<worddict_len[word]; i++) {
306 pwd[pos]=worddict[word][i];
307
308 /* Leetspeak */
309 if (idx[3]&L33TSP34K)
310 switch(pwd[pos]) {
311 case 'o': pwd[pos]='0'; leet=1; break;
312 case 'l': pwd[pos]='1'; leet=1; break;
313 case 'i': pwd[pos]='1'; leet=1; break;
314 case 'z': pwd[pos]='2'; leet=1; break;
315 case 'e': pwd[pos]='3'; leet=1; break;
316 case 'a': pwd[pos]='4'; leet=1; break;
317 case 's': pwd[pos]='5'; leet=1; break;
318 case 't': pwd[pos]='7'; leet=1; break;
319 case 'b': pwd[pos]='8'; leet=1; break;
320 }
321
322 /* capitalize first */
323 if ((pos==0) && my_islower(pwd[pos]) && (idx[0]&CAPITALIZE_1ST)) {
324 pwd[pos]-=32;
325 capitalized_1st=1;
326 }
327
328 /* capitalize */
329 if ((pos!=0) && my_islower(pwd[pos]) && (idx[0]&CAPITALIZE)) {
330 pwd[pos]-=32;
331 capitalized=1;
332 }
333 pos++;
334 }
335
336 /* Exceptions */
337
338 /* word was not capitalizable, do something else instead */
339 if ((idx[0]&CAPITALIZE) && !capitalized)
340 pwd[pos++]='.';
341
342 /* first letter was not capitalizable, do something else instead */
343 if ((idx[0]&CAPITALIZE_1ST) && !capitalized_1st)
344 pwd[pos++]=',';
345
346 /* word was not leetable, do something else instead */
347 if ((idx[3]&L33TSP34K) && !leet)
348 pwd[pos++]='-';
349
350 /* Compute the postifx */
351
352 if (idx[3]&CHOSE_POST) { /* postdict */
353 word = ((idx[3]&POST_1)>>2) + ((idx[4]&POST_2)<<6);
354 for (i=0; i<postdict_len[word]; i++)
355 pwd[pos++]=postdict[word][i];
356 }
357 else { /* two chars */
358 pwd[pos++]=ntdict64[(idx[3]&POST_1)>>2];
359 pwd[pos]=ntdict63[(idx[4]&POST_2)];
360 if (pwd[pos]) pos++; /* increment only if not empty char */
361 }
362 }
363 /*-------------------------------------------------------------------------*/
364 void ntdict_mkhash(uchar_t *pwd, uchar_t *hash) {
365 make_nthash((char*)pwd, (char*)hash);
366 }