| 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 |
#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 |
|
| 66 |
|
| 67 |
if (dicinit) { |
| 68 |
char fname[STR_BUFF_SIZE]; |
| 69 |
int i; |
| 70 |
|
| 71 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 162 |
|
| 163 |
if (tbl->srtmem) { |
| 164 |
uint32_t *mem = (uint32_t*)(tbl->srtmem + offset); |
| 165 |
start = mem[0]; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 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 |
|
| 179 |
|
| 180 |
start = ftohl(start); |
| 181 |
|
| 182 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 250 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 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++; |
| 300 |
|
| 301 |
|
| 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 |
|
| 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 |
|
| 323 |
if ((pos==0) && my_islower(pwd[pos]) && (idx[0]&CAPITALIZE_1ST)) { |
| 324 |
pwd[pos]-=32; |
| 325 |
capitalized_1st=1; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
if ((pos!=0) && my_islower(pwd[pos]) && (idx[0]&CAPITALIZE)) { |
| 330 |
pwd[pos]-=32; |
| 331 |
capitalized=1; |
| 332 |
} |
| 333 |
pos++; |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if ((idx[0]&CAPITALIZE) && !capitalized) |
| 340 |
pwd[pos++]='.'; |
| 341 |
|
| 342 |
|
| 343 |
if ((idx[0]&CAPITALIZE_1ST) && !capitalized_1st) |
| 344 |
pwd[pos++]=','; |
| 345 |
|
| 346 |
|
| 347 |
if ((idx[3]&L33TSP34K) && !leet) |
| 348 |
pwd[pos++]='-'; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
if (idx[3]&CHOSE_POST) { |
| 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 { |
| 358 |
pwd[pos++]=ntdict64[(idx[3]&POST_1)>>2]; |
| 359 |
pwd[pos]=ntdict63[(idx[4]&POST_2)]; |
| 360 |
if (pwd[pos]) pos++; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
void ntdict_mkhash(uchar_t *pwd, uchar_t *hash) { |
| 365 |
make_nthash((char*)pwd, (char*)hash); |
| 366 |
} |