Debian Codegraph @

src/core/ntnum.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) 2009 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 #include <ctype.h>
34 #include <stdlib.h>
35
36 #include "ntnum.h"
37 #include "table.h"
38 #include "hash.h"
39 #include "ophel.h"
40 #include "original.h"
41 #include "lmtable.h"
42
43 static uint64_t ntnum_sizes[10] = { 4, 7086244, 259203456, 518406912, 259204606, 518409212, 259212464, 518424928, 259218600, 518437200};
44 /*-------------------------------------------------------------------------*/
45 int ntnum_setup(void *tbl_) {
46 table_t *tbl = tbl_;
47
48 tbl->ncols = 15000;
49 tbl->offset = 100000;
50 tbl->sizes = ntnum_sizes;
51
52 tbl->find = ntnum_find;
53 tbl->check = ntnum_check;
54 tbl->isvalid = ntnum_isvalid;
55
56 tbl->lookup_idx = lmtable_lookup_idx;
57 tbl->lookup_end = lmtable_lookup_end;
58 tbl->lookup_srt = ntnum_lookup_srt;
59
60 return 1;
61 }
62 /*-------------------------------------------------------------------------*/
63 void ntnum_find(void *hsh_, void *tbl_, void *el_) {
64 hash_t *hsh = hsh_;
65 table_t *tbl = tbl_;
66 ophel_t *el = el_;
67 uchar_t *hash = hsh->hash;
68 uchar_t *pwd = (uchar_t*)el->pwd;
69 ophstat_t *stat = el->stat;
70
71 int c;
72 int col = el->col;
73 int ncols = tbl->ncols;
74 int n_redux = col + tbl->idx * tbl->offset;
75
76 uchar_t tmp[16];
77
78 /* Hash and redux until the last column. */
79
80 ntnum_mkredux(tbl, hash, pwd, n_redux);
81
82 for (c=1; c<ncols-col; ++c) {
83 ++n_redux;
84
85 ntnum_mkhash(pwd, tmp);
86 ntnum_mkredux(tbl, tmp, pwd, n_redux);
87 ++stat->hredux;
88 }
89
90 /* Compute the prefix and postfix. */
91 uint32_t prefix = ntnum_bin11(pwd, 6);
92 uint16_t postfix = (uint16_t) ntnum_bin11(pwd+6,6);
93
94 el->prefix = prefix;
95 el->postfix = postfix;
96 }
97 /*-------------------------------------------------------------------------*/
98 int ntnum_lookup_srt(void *hsh_, void *tbl_, void *el_) {
99 table_t *tbl = tbl_;
100 ophel_t *el = el_;
101
102 uint64_t offset = el->offset;
103 uint32_t start;
104
105 ophstat_t *stat = el->stat;
106 ++stat->start;
107
108 /* If the .start file has been preloaded, then we look into the memory. */
109
110 if (tbl->srtmem) {
111 uint32_t *mem = (uint32_t*)(tbl->srtmem + offset);
112 start = mem[0];
113 }
114
115 /* Otherwise, we access the disk. */
116
117 else {
118 FILE *srtfile = tbl->srtfile;
119
120 fseeko(srtfile, offset, SEEK_SET);
121 fread(&start, sizeof(start), 1, srtfile);
122 ++stat->fseek_srt;
123 }
124
125 /* Make sure that start is in the correct byte order. */
126
127 start = ftohl(start);
128
129 /* Convert it to a valid password. */
130
131 uchar_t *pwd = (uchar_t*)el->pwd;
132
133 uint32_t len = (start >= 1000000000) ? 10 : (start >= 100000000) ? 9 :
134 (start >= 10000000) ? 8 : (start >= 1000000) ? 7 : (start >= 100000) ? 6 :
135 (start >= 10000) ? 5 : (start >= 1000) ? 4 : (start >= 100) ? 3 :
136 (start >= 10) ? 2 : 1;
137
138 memset(pwd, 0, 13);
139 memset(pwd, '0', 10-len);
140 sprintf((char*)pwd+10-len, "%u", start);
141
142 return 1;
143 }
144 /*-------------------------------------------------------------------------*/
145 int ntnum_check(void *hsh_, void *tbl_, void *el_) {
146 hash_t *hsh = hsh_;
147 table_t *tbl = tbl_;
148 ophel_t *el = el_;
149 uchar_t *hash = hsh->hash;
150 uchar_t *pwd = (uchar_t*)el->pwd;
151 ophstat_t *stat = el->stat;
152
153 int c;
154 int col = el->col;
155 int ncols = tbl->ncols;
156 int n_redux = tbl->idx * tbl->offset;
157
158 uchar_t tmp[16];
159
160 /* Hash and redux until the starting column. */
161
162 stat->falarm_hredux = 0;
163
164 for (c=ncols-col; c<ncols; ++c) {
165 ntnum_mkhash(pwd, tmp);
166 ntnum_mkredux(tbl, tmp, pwd, n_redux);
167
168 ++n_redux;
169 ++stat->hredux;
170 ++stat->falarm_hredux;
171 }
172
173 /* Check if we got the same hash. */
174
175 ntnum_mkhash(pwd, tmp);
176
177 if (memcmp(tmp, hash, sizeof(tmp)) == 0) {
178 ++stat->match_table;
179 stat->falarm_hredux = 0;
180 return 1;
181 }
182
183 else {
184 ++stat->falarm;
185 return 0;
186 }
187 }
188 /*-------------------------------------------------------------------------*/
189 int ntnum_isvalid(void *hsh_, void *tbl_) {
190 hash_t *hsh = hsh_;
191
192 return hsh->kind == nt ? 1 : 0;
193 }
194 /*-------------------------------------------------------------------------*/
195 void ntnum_mkredux(table_t *tbl, uchar_t *hash, uchar_t *pwd,
196 int n_redux) {
197
198 /* Convert the hash to four unsigned 32-bit integers. */
199
200 uint32_t idx[4];
201
202 idx[0] = ftohl(*(uint32_t*)hash) ^ (uint32_t) n_redux;
203 idx[1] = ftohl(*(uint32_t*)(hash+4));
204 idx[2] = ftohl(*(uint32_t*)(hash+8));
205 idx[3] = ftohl(*(uint32_t*)(hash+12));
206
207 /* Compute the password */
208 uint64_t q = (uint64_t) idx[1] << 32 | (uint64_t) idx[0];
209 uint64_t l = (uint64_t) idx[3] << 32 | (uint64_t) idx[2];
210 l = l % 1000000000000LLU;
211
212 uint32_t len = (l >= 100000000000LLU) ? 12 : (l >= 10000000000LLU) ? 11 :
213 (l >= 1000000000) ? 10 : (l >= 100000000) ? 9 : (l >= 10000000) ? 8 :
214 (l >= 1000000) ? 7 : (l >= 100000) ? 6 : (l >= 10000) ? 5 :
215 (l >= 1000) ? 4 : (l >= 100) ? 3 : (l >= 10) ? 2 : 1;
216
217 q = (len == 12) ? q % 1000000000000LLU : (len == 11) ? q % 100000000000LLU :
218 (len == 10) ? q % 10000000000LLU : (len == 9) ? q % 1000000000 :
219 (len == 8) ? q % 100000000 : (len == 7) ? q % 10000000 :
220 (len == 6) ? q % 1000000 : (len == 5) ? q % 100000 :
221 (len == 4) ? q % 10000 : (len == 3) ? q % 1000 :
222 (len == 2) ? q % 100 : q % 10;
223
224 uint32_t len_q = (q >= 100000000000LLU) ? 12 : (q >= 10000000000LLU) ? 11 :
225 (q >= 1000000000) ? 10 : (q >= 100000000) ? 9 : (q >= 10000000) ? 8 :
226 (q >= 1000000) ? 7 : (q >= 100000) ? 6 : (q >= 10000) ? 5 :
227 (q >= 1000) ? 4 : (q >= 100) ? 3 : (q >= 10) ? 2 : 1;
228
229 memset(pwd, 0, 13);
230 memset(pwd, '0', len-len_q);
231 snprintf((char*)pwd+len-len_q, len_q+1, "%llu", (long long unsigned int)q);
232 //ulltoa2(str+len-len_q, q);
233
234 }
235 /*-------------------------------------------------------------------------*/
236 void ntnum_mkhash(uchar_t *pwd, uchar_t *hash) {
237 make_nthash((char*)pwd, (char*)hash);
238 }
239 /*-------------------------------------------------------------------------*/
240 uint32_t ntnum_bin11(uchar_t *input, int length) {
241
242 uint32_t sum=0;
243 int i = 0;
244
245 for (i=0; i<length; i++)
246 sum = sum*11 + (input[i] ? input[i]-0x2f : 0);
247 return sum;
248 }