Debian Codegraph @

src/core/lmgerman.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 <stdint.h>
33 #include <string.h>
34
35 #include "lmgerman.h"
36 #include "lmtable.h"
37 #include "hash.h"
38 #include "ophel.h"
39 #include "original.h"
40 #include "misc.h"
41
42 static uint64_t lmgerman_sizes[10] = { 4, 170911176, 599772172, 1199544344, 599747104, 1199494208, 599734104, 1199468208, 599782562, 1199565124};
43 /*-------------------------------------------------------------------------*/
44 int lmgerman_setup(void *tbl_) {
45 table_t *tbl = tbl_;
46
47 tbl->ncols = 14000;
48 tbl->offset = 14000;
49 tbl->sizes = lmgerman_sizes;
50
51 tbl->find = lmgerman_find;
52 tbl->check = lmgerman_check;
53 tbl->isvalid = lmgerman_isvalid;
54
55 tbl->lookup_idx = lmtable_lookup_idx;
56 tbl->lookup_end = lmtable_lookup_end;
57 tbl->lookup_srt = lmgerman_lookup_srt;
58
59 return 1;
60 }
61 /*-------------------------------------------------------------------------*/
62 void lmgerman_find(void *hsh_, void *tbl_, void *el_) {
63 hash_t *hsh = hsh_;
64 table_t *tbl = tbl_;
65 ophel_t *el = el_;
66 uchar_t *hash = hsh->hash;;
67 uchar_t *pwd = (uchar_t*)el->pwd;
68 ophstat_t *stat = el->stat;
69
70 int c;
71 int col = el->col;
72 int ncols = tbl->ncols;
73 int n_redux = col + tbl->idx * tbl->offset;
74
75 uchar_t tmp[8];
76
77 /* Hash and redux until the last column. */
78
79 lmgerman_mkredux(tbl, hash, pwd, n_redux);
80
81 for (c=1; c<ncols-col; ++c) {
82 ++n_redux;
83
84 lmtable_mkhash(pwd, tmp);
85 lmgerman_mkredux(tbl, tmp, pwd, n_redux);
86 ++stat->hredux;
87 }
88
89 /* Compute the prefix and postfix. */
90
91 uint64_t pw_bin = lmgerman_bin73(pwd);
92 uint32_t prefix = (uint32_t)(pw_bin >> 18 & 0x00000000FFFFFFFF);
93 uint16_t postfix = (uint16_t)(pw_bin & 0x000000000000FFFF);
94
95 el->prefix = prefix;
96 el->postfix = postfix;
97 }
98 /*-------------------------------------------------------------------------*/
99 int lmgerman_lookup_srt(void *hsh_, void *tbl_, void *el_) {
100 table_t *tbl = tbl_;
101 ophel_t *el = el_;
102
103 uint64_t offset = el->offset;
104 uint32_t start;
105
106 ophstat_t *stat = el->stat;
107 ++stat->start;
108
109 /* If the .start file has been preloaded, then we look into the memory. */
110
111 if (tbl->srtmem) {
112 uint32_t *mem = (uint32_t*)(tbl->srtmem + offset);
113 start = mem[0];
114 }
115
116 /* Otherwise, we access the disk. */
117
118 else {
119 FILE *srtfile = tbl->srtfile;
120
121 fseeko(srtfile, offset, SEEK_SET);
122 fread(&start, sizeof(start), 1, srtfile);
123 ++stat->fseek_srt;
124 }
125
126 /* Make sure that start is in the correct byte order. */
127
128 start = ftohl(start);
129
130 /* Convert it to a valid password. */
131
132 uchar_t *pwd = (uchar_t*)el->pwd;
133
134 pwd[5] = 0;
135 pwd[6] = 0;
136 pwd[7] = 0;
137
138 lmgerman_unbin36(start, pwd);
139
140 return 1;
141 }
142 /*-------------------------------------------------------------------------*/
143 int lmgerman_check(void *hsh_, void *tbl_, void *el_) {
144 hash_t *hsh = hsh_;
145 table_t *tbl = tbl_;
146 ophel_t *el = el_;
147 uchar_t *hash = hsh->hash;
148 uchar_t *pwd = (uchar_t*)el->pwd;
149 ophstat_t *stat = el->stat;
150
151 int c;
152 int col = el->col;
153 int ncols = tbl->ncols;
154 int n_redux = tbl->idx * tbl->offset;
155
156 uchar_t tmp[8];
157
158 /* Hash and redux until the starting column. */
159
160 stat->falarm_hredux = 0;
161
162 for (c=ncols-col; c<ncols; ++c) {
163 lmtable_mkhash(pwd, tmp);
164 lmgerman_mkredux(tbl, tmp, pwd, n_redux);
165
166 ++n_redux;
167 ++stat->hredux;
168 ++stat->falarm_hredux;
169 }
170
171 /* Check if we got the same hash. */
172
173 lmtable_mkhash(pwd, tmp);
174 wincp1252_to_ascii(pwd);
175
176 if (memcmp(tmp, hash, 8) == 0) {
177 ++stat->match_table;
178 stat->falarm_hredux = 0;
179 return 1;
180 }
181
182 else {
183 ++stat->falarm;
184 return 0;
185 }
186 }
187 /*-------------------------------------------------------------------------*/
188 int lmgerman_isvalid(void *hsh_, void *tbl_) {
189 hash_t *hsh = hsh_;
190
191 return (hsh->kind == lm1 || hsh->kind == lm2) ? 1 : 0;
192 }
193 /*-------------------------------------------------------------------------*/
194 void lmgerman_mkredux(table_t *tbl, uchar_t *hash, uchar_t *pwd,
195 int n_redux) {
196
197 const uchar_t *charset = extended_chars;
198 int chars_size = sizeof(extended_chars);
199 int lmgerman_chars_size = 4;
200
201 /* Convert the hash to two unsigned 32-bit integers. */
202
203 uint32_t idx[2];
204 uint32_t int_hash[2];
205
206 int_hash[0] = ftohl(*(uint32_t*)hash);
207 int_hash[1] = ftohl(*(uint32_t*)(hash+4));
208
209 /* XOR the 1st integer with n_redux */
210
211 idx[0] = int_hash[0] ^ n_redux;
212 idx[1] = int_hash[1];
213
214 /* Generate the password. */
215
216 int n = idx[1];
217 uchar_t len, k, x=0, rank, i, j=0;
218 uchar_t chars1[8], chars2[8];
219
220 if (n <= lmgerman_spec_max[0].max)
221 x=0;
222 else
223 if (n <= lmgerman_spec_max[1].max)
224 x=1;
225 else
226 if (n <= lmgerman_spec_max[2].max)
227 x=2;
228 else
229 for (i=3; i<28; i++)
230 if (n <= lmgerman_spec_max[i].max) {
231 x=i;
232 break;
233 }
234
235 len = lmgerman_spec_max[x].n;
236 k = lmgerman_spec_max[x].k;
237
238 for (i=0; i<4 || i<(len-k); i++) {
239 chars1[i] = charset[idx[0] % chars_size];
240 idx[0] /= chars_size;
241 }
242
243 for (i=4; i<(len-k); i++) {
244 chars1[i] = charset[idx[1] % chars_size];
245 idx[1] /= chars_size;
246 }
247
248 rank = idx[1] % lmgerman_choose[len][k];
249
250 if (tbl->kind == lmgermanv2)
251 idx[1] /= lmgerman_choose[len][k];
252
253 for (i=0; i<k; i++) {
254 chars2[i] = lmgerman_chars[idx[1] % lmgerman_chars_size];
255 idx[1] /= lmgerman_chars_size;
256 };
257
258 for (i=0,j=0,k=0; i<len; i++)
259 if (my_getbit(lmgerman_ranks[x][rank],i)) {
260 pwd[i] = chars2[k];
261 k++;
262 } else {
263 pwd[i] = chars1[j];
264 j++;
265 }
266
267 for (i=len; i<7; i++) pwd[i]=0;
268 }
269 /*-------------------------------------------------------------------------*/
270 uint64_t lmgerman_bin73(uchar_t *input) {
271 int i = 0;
272 uint64_t sum = 0;
273
274 for (i=0; i<7&&input[i]; i++) {
275 char *x1 = (char*)memchr(lmgerman_chars, input[i], 73);
276 char *x2 = (char*)lmgerman_chars;
277
278 sum = sum*73 + (uint64_t)(x1-x2);
279 }
280
281 return sum;
282 }
283 /*-------------------------------------------------------------------------*/
284 void lmgerman_unbin36(uint32_t input, uchar_t *output) {
285 int i = 0;
286
287 for (i=5; i>=0; i--) {
288 output[i] = lmgerman_chars[input % 36 + 4];
289 input /= 36;
290 }
291 }