| 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 <config.h> |
| 33 | #ifndef WIN32 |
| 34 | #include <stdlib.h> |
| 35 | #endif |
| 36 | #include <stdio.h> |
| 37 | #include <stdint.h> |
| 38 | #include <unistd.h> |
| 39 | #include <string.h> |
| 40 | #include <assert.h> |
| 41 | #include <math.h> |
| 42 | #ifdef WIN32 |
| 43 | #include <windows.h> |
| 44 | #endif |
| 45 | #ifdef HAVE_GUI |
| 46 | #include <QApplication> |
| 47 | #include <QtCharts> |
| 48 | using namespace QtCharts; |
| 49 | #endif |
| 50 | |
| 51 | #include "ophcrack.h" |
| 52 | #include "message.h" |
| 53 | #ifdef HAVE_GUI |
| 54 | #include "ophcrackgui.h" |
| 55 | #endif |
| 56 | #include "fsm.h" |
| 57 | #include "arg.h" |
| 58 | #include "misc.h" |
| 59 | /*-------------------------------------------------------------------------*/ |
| 60 | struct timeval tm_main_start; |
| 61 | struct timeval tm_main_total; |
| 62 | fsm_t *fsm = 0; |
| 63 | /*-------------------------------------------------------------------------*/ |
| 64 | void usage() { |
| 65 | printf("ophcrack %s by Objectif Securite (http://www.objectif-securite.ch)\n\n", PACKAGE_VERSION); |
| 66 | printf("Usage: ophcrack [OPTIONS]\n"); |
| 67 | printf("Cracks Windows passwords with Rainbow tables\n\n"); |
| 68 | printf(" -a disable audit mode (default)\n"); |
| 69 | printf(" -A enable audit mode\n"); |
| 70 | printf(" -b disable bruteforce\n"); |
| 71 | printf(" -B enable bruteforce (default)\n"); |
| 72 | printf(" -c config_file specify the config file to use\n"); |
| 73 | printf(" -D display (lots of!) debugging information\n"); |
| 74 | printf(" -d dir specify tables base directory\n"); |
| 75 | printf(" -e do not display empty passwords\n"); |
| 76 | printf(" -f file load hashes from the specified file (pwdump or session)\n"); |
| 77 | printf(" -g disable GUI\n"); |
| 78 | printf(" -h display this information\n"); |
| 79 | printf(" -i hide usernames\n"); |
| 80 | printf(" -I show usernames (default)\n"); |
| 81 | printf(" -l file log all output to the specified file\n"); |
| 82 | printf(" -n num specify the number of threads to use\n"); |
| 83 | printf(" -o file write cracking output to file in pwdump format\n"); |
| 84 | printf(" -p num preload (0 none, 1 index, 2 index+end, 3 all default)\n"); |
| 85 | printf(" -q quiet mode\n"); |
| 86 | printf(" -r launch the cracking when ophcrack starts (GUI only)\n"); |
| 87 | printf(" -s disable session auto-saving\n"); |
| 88 | printf(" -S session_file specify the file to use to automatically save the progress of the search\n"); |
| 89 | printf(" -u display statistics when cracking ends\n"); |
| 90 | printf(" -t table1[,a[,b,...]][:table2[,a[,b,...]]]\n"); |
| 91 | printf(" specify which table to use in the directory given by -d\n"); |
| 92 | printf(" -v verbose\n"); |
| 93 | printf(" -w dir load hashes from encrypted SAM file in directory dir\n"); |
| 94 | printf(" -x file export data in CSV format to file\n"); |
| 95 | printf("\n\nExample:\tophcrack -g -d /path/to/tables -t xp_free_fast,0,3:vista_free -f in.txt\n\n"); |
| 96 | printf("\t\tLaunch ophcrack in command line using tables 0 and 3 in\n"); |
| 97 | printf("\t\t/path/to/tables/xp_free_fast and all tables in /path/to/tables/vista_free\n"); |
| 98 | printf("\t\tand cracks hashes from pwdump file in.txt\n\n"); |
| 99 | exit(0); |
| 100 | } |
| 101 | /*-------------------------------------------------------------------------*/ |
| 102 | void load_config(arg_t *arg) { |
| 103 | char buff[STR_BUFF_SIZE]; |
| 104 | |
| 105 | /* If no config file is given on the command line, then we try the |
| 106 | default one. */ |
| 107 | |
| 108 | if (arg->cfname == 0) { |
| 109 | #ifndef WIN32 |
| 110 | char *home = getenv("HOME"); |
| 111 | snprintf(buff, sizeof(buff), "%s/.ophcrackrc", home); |
| 112 | #else |
| 113 | snprintf(buff, sizeof(buff), "./.ophcrackrc"); |
| 114 | #endif |
| 115 | |
| 116 | arg->cfname = strdup(buff); |
| 117 | arg_read_conf(arg); |
| 118 | } |
| 119 | |
| 120 | /* Otherwise, we read the one provided. */ |
| 121 | |
| 122 | else |
| 123 | if (arg_read_conf(arg) == -1) { |
| 124 | fprintf(stderr, "Cannot open file %s for reading.\n", arg->cfname); |
| 125 | exit(1); |
| 126 | } |
| 127 | } |
| 128 | /*-------------------------------------------------------------------------*/ |
| 129 | int load_tables(ophcrack_t *crack, arg_t *arg, list_t *table_str) { |
| 130 | list_nd_t *tnd; |
| 131 | list_nd_t *dnd; |
| 132 | |
| 133 | list_t *table_path = arg->table_path; |
| 134 | list_t *tables = list_alloc(); |
| 135 | |
| 136 | for (tnd = table_str->head; tnd != 0; tnd = tnd->next) { |
| 137 | char *tblstr = (char*)tnd->data; |
| 138 | |
| 139 | for (dnd = table_path->head; dnd != 0; dnd = dnd->next) { |
| 140 | char *dir = (char*)dnd->data; |
| 141 | int ret = table_open(tables, dir, tblstr); |
| 142 | |
| 143 | /* The tables have been successfully opened. */ |
| 144 | |
| 145 | if (ret >= 0) { |
| 146 | if (!arg->use_gui && ret > 0) { |
| 147 | if (!arg->quiet || arg->lfile != stdout) { |
| 148 | if (dir[0] != 0) |
| 149 | fprintf(arg->lfile, |
| 150 | "Opened %d table(s) from %s/%s.\n", |
| 151 | ret, dir, tblstr); |
| 152 | else |
| 153 | fprintf(arg->lfile, |
| 154 | "Opened %d table(s) from %s.\n", |
| 155 | ret, tblstr); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | /* The opening of the tables failed. */ |
| 163 | |
| 164 | else if (!arg->use_gui && ret < -1) { |
| 165 | fprintf(stderr, "Cannot open the table(s) %s.\n", tblstr); |
| 166 | exit(1); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // If the table has not been found and we are in command line |
| 171 | // mode, then we complain. |
| 172 | |
| 173 | if (!arg->use_gui && dnd == 0) { |
| 174 | fprintf(stderr, "Did not find the requested table(s) %s.\n", tblstr); |
| 175 | exit(1); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* If we are in command line mode, then we check that something has |
| 180 | actually been loaded. */ |
| 181 | |
| 182 | int ntables = tables->size; |
| 183 | |
| 184 | if (!arg->use_gui && ntables == 0) { |
| 185 | fprintf(stderr, "No tables were loaded. Use the -t option to specify some.\n"); |
| 186 | exit(1); |
| 187 | } |
| 188 | |
| 189 | /* Add the tables to the list of tables used by ophcrack. */ |
| 190 | |
| 191 | list_nd_t *nd; |
| 192 | |
| 193 | for (nd = tables->head; nd != 0; nd = nd->next) { |
| 194 | table_t *tbl = (table_t*)nd->data; |
| 195 | |
| 196 | ophcrack_setup_table(tbl); |
| 197 | |
| 198 | int ret = table_verify(tbl); |
| 199 | if (ret > 0) |
| 200 | ophcrack_add_table(crack, tbl); |
| 201 | else if (!arg->use_gui && ret < 0) |
| 202 | fprintf(stderr, "Size of table(s) %s,%d is invalid. Download it again.\n", tbl->name, tbl->idx); |
| 203 | |
| 204 | |
| 205 | } |
| 206 | |
| 207 | list_free(tables); |
| 208 | |
| 209 | return ntables; |
| 210 | } |
| 211 | /*-------------------------------------------------------------------------*/ |
| 212 | int load_hashes_pwdump(ophcrack_t *crack, int id) { |
| 213 | arg_t *arg = crack->arg; |
| 214 | |
| 215 | const char *hfname = arg->hfname; |
| 216 | FILE *file = fopen(hfname, "r"); |
| 217 | |
| 218 | if (!file) { |
| 219 | fprintf(stderr, "Cannot open file %s.\n", hfname); |
| 220 | exit(1); |
| 221 | } |
| 222 | |
| 223 | list_t *hashes = list_alloc(); |
| 224 | int npwds = hash_load_pwdump(hashes, file, id); |
| 225 | int nhashes = hashes->size; |
| 226 | |
| 227 | fclose(file); |
| 228 | |
| 229 | /* Check that something has actually been loaded. */ |
| 230 | |
| 231 | if (npwds == 0) { |
| 232 | fprintf(stderr, "No proper hashes have been found in %s.\n", hfname); |
| 233 | exit(1); |
| 234 | } else if (!arg->use_gui) |
| 235 | if (!arg->quiet || arg->lfile != stdout) |
| 236 | fprintf(arg->lfile, "%d hashes have been found in %s.\n", |
| 237 | nhashes, hfname); |
| 238 | |
| 239 | /* Add the hashes to the list of hash we must potentially crack. */ |
| 240 | |
| 241 | list_nd_t *nd; |
| 242 | |
| 243 | for (nd = hashes->head; nd != 0; nd = nd->next) { |
| 244 | hash_t *hsh = (hash_t*)nd->data; |
| 245 | ophcrack_add_hash(crack, hsh); |
| 246 | } |
| 247 | |
| 248 | list_free(hashes); |
| 249 | |
| 250 | return npwds; |
| 251 | } |
| 252 | /*-------------------------------------------------------------------------*/ |
| 253 | int load_hashes_sam(ophcrack_t *crack, int id) { |
| 254 | arg_t *arg = crack->arg; |
| 255 | |
| 256 | const char *dir = arg->samdir; |
| 257 | |
| 258 | list_t *hashes = list_alloc(); |
| 259 | int npwds = hash_load_sam(hashes, dir, id); |
| 260 | int nhashes = hashes->size; |
| 261 | |
| 262 | // Check that something has actually been loaded. |
| 263 | |
| 264 | if (npwds == 0) { |
| 265 | fprintf(stderr, |
| 266 | "No proper hashes have been found in the encrypted SAM file in %s.\n", |
| 267 | arg->samdir); |
| 268 | exit(1); |
| 269 | } |
| 270 | |
| 271 | else if (npwds > 0 && !arg->use_gui) |
| 272 | if (!arg->quiet || arg->lfile != stdout) |
| 273 | fprintf(arg->lfile, |
| 274 | "%d hashes have been found in the encrypted SAM found in %s.\n\n", |
| 275 | nhashes, dir); |
| 276 | |
| 277 | // Check if the SYSTEM or SAM file could not be found. |
| 278 | |
| 279 | if (npwds == -1) { |
| 280 | fprintf(stderr, "No SYSTEM file has been found in %s.\n", arg->samdir); |
| 281 | exit(1); |
| 282 | } |
| 283 | |
| 284 | else if (npwds == -2) { |
| 285 | fprintf(stderr, "No SAM file has been found in %s.\n", arg->samdir); |
| 286 | exit(1); |
| 287 | } |
| 288 | |
| 289 | // Check if there was a problem while reading the SYSTEM or SAM file. |
| 290 | |
| 291 | else if (npwds == -3) { |
| 292 | fprintf(stderr, |
| 293 | "A problem occurred while reading the SYSTEM file found in %s.\n", |
| 294 | arg->samdir); |
| 295 | exit(1); |
| 296 | } |
| 297 | |
| 298 | else if (npwds == -4) { |
| 299 | fprintf(stderr, |
| 300 | "A problem occurred while reading the SAM file found in %s.\n", |
| 301 | arg->samdir); |
| 302 | exit(1); |
| 303 | } |
| 304 | |
| 305 | // Add the hashes to the list of hash we must potentially crack. |
| 306 | |
| 307 | list_nd_t *nd; |
| 308 | |
| 309 | for (nd = hashes->head; nd != 0; nd = nd->next) { |
| 310 | hash_t *hsh = (hash_t*)nd->data; |
| 311 | ophcrack_add_hash(crack, hsh); |
| 312 | } |
| 313 | |
| 314 | list_free(hashes); |
| 315 | |
| 316 | return npwds; |
| 317 | } |
| 318 | /*-------------------------------------------------------------------------*/ |
| 319 | void display_stats(ophcrack_t *crack) { |
| 320 | ophstat_t *stat = crack->stat; |
| 321 | arg_t *arg = crack->arg; |
| 322 | |
| 323 | if (arg->quiet && arg->lfile == stdout) return; |
| 324 | |
| 325 | fprintf(arg->lfile, "\n\nStatistics:\n"); |
| 326 | fprintf(arg->lfile, "%-32s: %llu\n", "# of hash/redux", (long long unsigned int) stat->hredux); |
| 327 | fprintf(arg->lfile, "%-32s: %llu\n", "# of fseek", (long long unsigned int) stat->fseek_idx+stat->fseek_end+stat->fseek_srt); |
| 328 | fprintf(arg->lfile, "%-32s: %llu\n", "# of false alarms", (long long unsigned int) stat->falarm); |
| 329 | fprintf(arg->lfile, "%-32s: %llu\n", "# of hash/redux per false alarm", |
| 330 | (stat->falarm >0) ? (long long unsigned int) (stat->falarm_hredux / stat->falarm) : (long long unsigned int) 0); |
| 331 | fprintf(arg->lfile, "%-32s: %llu\n", "# of matches in tables", (long long unsigned int) stat->match_table); |
| 332 | fprintf(arg->lfile, "%-32s: %llu\n", "# of matches in brute force", (long long unsigned int) stat->match_bforce); |
| 333 | fprintf(arg->lfile, "%-32s: %llu\n", "# of prefixs found", (long long unsigned int) stat->prefix); |
| 334 | fprintf(arg->lfile, "%-32s: %llu\n", "# of postfixs found", (long long unsigned int) stat->postfix); |
| 335 | fprintf(arg->lfile, "%-32s: %llu\n", "# of start", (long long unsigned int) stat->start); |
| 336 | fprintf(arg->lfile, "%-32s: %llu\n", "# of fseek in .index", (long long unsigned int) stat->fseek_idx); |
| 337 | fprintf(arg->lfile, "%-32s: %llu\n", "# of fseek in .bin", (long long unsigned int) stat->fseek_end); |
| 338 | fprintf(arg->lfile, "%-32s: %llu", "# of fseek in .start", (long long unsigned int) stat->fseek_srt); |
| 339 | } |
| 340 | /*-------------------------------------------------------------------------*/ |
| 341 | void display_work(ophcrack_t *crack, msg_work_t *msg, char *info) { |
| 342 | arg_t *arg = crack->arg; |
| 343 | |
| 344 | if (arg->quiet && arg->lfile == stdout) return; |
| 345 | |
| 346 | hash_t *hsh = msg->hsh; |
| 347 | table_t *tbl = msg->tbl; |
| 348 | ophkind_t kind = msg->kind; |
| 349 | int cmin = msg->cmin; |
| 350 | int cmax = msg->cmax; |
| 351 | |
| 352 | char *name = tbl->name; |
| 353 | int tidx = tbl->idx; |
| 354 | int hid = hsh->id; |
| 355 | |
| 356 | fprintf(arg->lfile, "%s", info); |
| 357 | |
| 358 | switch (kind) { |
| 359 | case find: |
| 360 | fprintf(arg->lfile, "Find chain end for "); |
| 361 | break; |
| 362 | |
| 363 | case lookup_idx: |
| 364 | fprintf(arg->lfile, "Lookup prefixes for "); |
| 365 | break; |
| 366 | |
| 367 | case lookup_end: |
| 368 | fprintf(arg->lfile, "Lookup postfixes for "); |
| 369 | break; |
| 370 | |
| 371 | case lookup_srt: |
| 372 | fprintf(arg->lfile, "Lookup chain starts for "); |
| 373 | break; |
| 374 | |
| 375 | case check: |
| 376 | fprintf(arg->lfile, "Checking chain starts for "); |
| 377 | break; |
| 378 | |
| 379 | default: |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | switch (hsh->kind) { |
| 384 | case lm1: |
| 385 | fprintf(arg->lfile, "1st LM hash #%d in table %s,%d from column %d to %d", |
| 386 | hid, name, tidx, cmax, cmin); |
| 387 | break; |
| 388 | |
| 389 | case lm2: |
| 390 | fprintf(arg->lfile, "2nd LM hash #%d in table %s,%d from column %d to %d", |
| 391 | hid, name, tidx, cmax, cmin); |
| 392 | break; |
| 393 | |
| 394 | case nt: |
| 395 | fprintf(arg->lfile, "NT hash #%d in table %s,%d from column %d to %d", |
| 396 | hid, name, tidx, cmax, cmin); |
| 397 | break; |
| 398 | |
| 399 | default: |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | fprintf(arg->lfile, "\n"); |
| 404 | } |
| 405 | /*-------------------------------------------------------------------------*/ |
| 406 | void display_load(ophcrack_t *crack, msg_load_t *msg, char *info) { |
| 407 | arg_t *arg = crack->arg; |
| 408 | |
| 409 | if (arg->quiet && arg->lfile == stdout) return; |
| 410 | |
| 411 | table_t *tbl = msg->tbl; |
| 412 | table_preload_t preload = msg->preload; |
| 413 | int done = msg->done; |
| 414 | uint64_t size = msg->size >> 20; |
| 415 | |
| 416 | char *name = tbl->name; |
| 417 | int tidx = tbl->idx; |
| 418 | |
| 419 | fprintf(arg->lfile, "%s", info); |
| 420 | |
| 421 | if (preload >= preload_init && preload <= preload_srt) { |
| 422 | if (done) |
| 423 | fprintf(arg->lfile, "Done preloading "); |
| 424 | else |
| 425 | fprintf(arg->lfile, "Preloading "); |
| 426 | } |
| 427 | |
| 428 | switch (preload) { |
| 429 | case preload_init: |
| 430 | if (tbl->shared_init) |
| 431 | fprintf(arg->lfile, "shared init data "); |
| 432 | else |
| 433 | fprintf(arg->lfile, "init data "); |
| 434 | break; |
| 435 | |
| 436 | case preload_idx: |
| 437 | fprintf(arg->lfile, ".index file "); |
| 438 | break; |
| 439 | |
| 440 | case preload_end: |
| 441 | fprintf(arg->lfile, ".bin file "); |
| 442 | break; |
| 443 | |
| 444 | case preload_srt: |
| 445 | fprintf(arg->lfile, ".start file "); |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | if (preload >= preload_init && preload <= preload_srt) { |
| 453 | fprintf(arg->lfile, "(%llu MB) ", (long long unsigned)size); |
| 454 | fprintf(arg->lfile, "of table %s,%d\n", name, tidx); |
| 455 | } |
| 456 | } |
| 457 | /*-------------------------------------------------------------------------*/ |
| 458 | void display_found(ophcrack_t *crack, msg_found_t *found, char *info) { |
| 459 | arg_t *arg = crack->arg; |
| 460 | |
| 461 | if (arg->quiet && arg->lfile == stdout) return; |
| 462 | |
| 463 | // Show that we have found something. |
| 464 | |
| 465 | hash_t *hsh = found->hsh; |
| 466 | table_t *tbl = found->tbl; |
| 467 | int col = found->col; |
| 468 | |
| 469 | char *pwd = hsh->pwd; |
| 470 | char *hinfo = hsh->info; |
| 471 | |
| 472 | int hid = hsh->id; |
| 473 | int showempty = arg->showempty; |
| 474 | |
| 475 | /* If the table pointer is null, then it is either a brute forced or |
| 476 | an empty pwd. */ |
| 477 | |
| 478 | fprintf(arg->lfile, "%s", info); |
| 479 | |
| 480 | if (!arg->auditmode) { |
| 481 | if (strcmp(pwd, "") == 0 && showempty) |
| 482 | fprintf(arg->lfile, "Found empty password for "); |
| 483 | else |
| 484 | fprintf(arg->lfile, "Found password %s for ", pwd); |
| 485 | |
| 486 | switch (hsh->kind) { |
| 487 | case lm1: |
| 488 | fprintf(arg->lfile, "1st LM hash #%d ", hid); |
| 489 | break; |
| 490 | |
| 491 | case lm2: |
| 492 | fprintf(arg->lfile, "2nd LM hash #%d ", hid); |
| 493 | break; |
| 494 | |
| 495 | case nt: |
| 496 | if (strcmp(hinfo, "") == 0) |
| 497 | fprintf(arg->lfile, "NT hash #%d ", hid); |
| 498 | else |
| 499 | if (arg->hideuname) |
| 500 | fprintf(arg->lfile, "user ***** (NT hash #%d) ", hid); |
| 501 | else |
| 502 | fprintf(arg->lfile, "user %s (NT hash #%d) ", hinfo, hid); |
| 503 | break; |
| 504 | |
| 505 | default: |
| 506 | break; |
| 507 | } |
| 508 | } else { |
| 509 | if (strcmp(pwd, "") == 0 && showempty) |
| 510 | fprintf(arg->lfile, "Found empty password for "); |
| 511 | else |
| 512 | fprintf(arg->lfile, "Found password of length %d for ", hsh->length); |
| 513 | |
| 514 | switch (hsh->kind) { |
| 515 | case lm1: |
| 516 | fprintf(arg->lfile, "1st LM hash #%d ", hid); |
| 517 | break; |
| 518 | |
| 519 | case lm2: |
| 520 | fprintf(arg->lfile, "2nd LM hash #%d ", hid); |
| 521 | break; |
| 522 | |
| 523 | case nt: |
| 524 | fprintf(arg->lfile, "NT hash #%d ", hid); |
| 525 | break; |
| 526 | |
| 527 | default: |
| 528 | break; |
| 529 | } |
| 530 | fprintf(arg->lfile, "category: %s ", category_string(hsh->category)); |
| 531 | } |
| 532 | if (tbl != 0) |
| 533 | fprintf(arg->lfile, "in table %s #%d at column %d.", tbl->name, tbl->idx, col); |
| 534 | |
| 535 | fprintf(arg->lfile, "\n"); |
| 536 | } |
| 537 | /*-------------------------------------------------------------------------*/ |
| 538 | void display_status(ophcrack_t *crack, char *info) { |
| 539 | scheduler_t *sched = crack->sched; |
| 540 | pthread_mutex_t *mutex = sched->mutex; |
| 541 | |
| 542 | pthread_mutex_lock(mutex); |
| 543 | int nactive = crack->active->size; |
| 544 | int nenabled = crack->enabled->size; |
| 545 | int nremain = crack->remaining->size; |
| 546 | |
| 547 | int npwds_total = crack->npwds_total; |
| 548 | int npwds_found = crack->npwds_found; |
| 549 | pthread_mutex_unlock(mutex); |
| 550 | |
| 551 | printf("%s", info); |
| 552 | |
| 553 | // Brute force. |
| 554 | |
| 555 | if (crack->arg->bforce) { |
| 556 | uint64_t curr = fsm->bforce_curr; |
| 557 | uint64_t total = fsm->bforce_total; |
| 558 | int ratio = 0; |
| 559 | |
| 560 | if (total > 0) |
| 561 | ratio = (100*curr) / total; |
| 562 | |
| 563 | if (ratio < 100) |
| 564 | printf("brute force (%d%%); ", ratio); |
| 565 | } |
| 566 | |
| 567 | // Preload. |
| 568 | |
| 569 | if (fsm->state == st_preload) { |
| 570 | uint64_t curr = fsm->psize_curr; |
| 571 | uint64_t total = fsm->psize_total; |
| 572 | int ratio = 0; |
| 573 | |
| 574 | if (total > 0) |
| 575 | ratio = (100*curr) / total; |
| 576 | |
| 577 | if (ratio < 100) |
| 578 | printf("preload (%d%%); ", ratio); |
| 579 | else { |
| 580 | assert(curr == total); |
| 581 | printf("preload done; "); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // Work. |
| 586 | |
| 587 | else { |
| 588 | list_t *tables = crack->tables; |
| 589 | list_nd_t *nd; |
| 590 | |
| 591 | int mean_cmax = 0; |
| 592 | int count = 0; |
| 593 | |
| 594 | for (nd = tables->head; nd != 0; nd = nd->next) { |
| 595 | table_t *tbl = (table_t*)nd->data; |
| 596 | |
| 597 | if (!tbl->enabled) continue; |
| 598 | |
| 599 | uint64_t ncols = tbl->ncols; |
| 600 | uint64_t cmax = tbl->cmax; |
| 601 | |
| 602 | uint64_t n = ncols * ncols; |
| 603 | uint64_t dcmax = ncols-cmax; |
| 604 | int ratio_cmax = (100*dcmax*dcmax) / n; |
| 605 | |
| 606 | mean_cmax += ratio_cmax; |
| 607 | ++count; |
| 608 | } |
| 609 | |
| 610 | if (count > 0) |
| 611 | mean_cmax /= count; |
| 612 | |
| 613 | int ndone = nenabled - nactive - nremain; |
| 614 | |
| 615 | printf("search (%d%%); ", mean_cmax); |
| 616 | printf("tables: total %d, done %d, using %d; ", nenabled, ndone, nactive); |
| 617 | } |
| 618 | |
| 619 | printf("pwd found %d/%d.", npwds_found, npwds_total); |
| 620 | } |
| 621 | /*-------------------------------------------------------------------------*/ |
| 622 | void handle_messages(ophcrack_t *crack) { |
| 623 | scheduler_t *sched = crack->sched; |
| 624 | pthread_mutex_t *mutex = sched->mutex; |
| 625 | |
| 626 | char info[STR_BUFF_SIZE] = {0}; |
| 627 | message_t *msg = 0; |
| 628 | int count = 1000; |
| 629 | int pwdfound = 0; |
| 630 | |
| 631 | arg_t *arg = crack->arg; |
| 632 | |
| 633 | /* Handle at most 'count' messages. */ |
| 634 | |
| 635 | struct timeval now; |
| 636 | gettimeofday(&now, 0); |
| 637 | |
| 638 | long tv_sec = now.tv_sec - tm_main_start.tv_sec; |
| 639 | long hour = tv_sec / 3600; |
| 640 | long sec = tv_sec - 3600 * hour; |
| 641 | long min = sec / 60; |
| 642 | |
| 643 | sec %= 60; |
| 644 | |
| 645 | snprintf(info, sizeof(info), "%ldh %2ldm %2lds; ", hour, min, sec); |
| 646 | |
| 647 | while (count-- && (msg = message_tryget())) { |
| 648 | if (arg->debug == 0 && !arg->quiet) printf("\x1B[2K\r"); |
| 649 | |
| 650 | /* A password has been found. */ |
| 651 | |
| 652 | if (msg->kind == msg_found) { |
| 653 | msg_found_t *found = (msg_found_t*)msg->data; |
| 654 | display_found(crack, found, info); |
| 655 | pwdfound = 1; |
| 656 | } |
| 657 | |
| 658 | /* Work message. */ |
| 659 | |
| 660 | else if (arg->verbose && msg->kind == msg_work) { |
| 661 | msg_work_t *work = (msg_work_t*)msg->data; |
| 662 | display_work(crack, work, info); |
| 663 | } |
| 664 | |
| 665 | /* Preload message. */ |
| 666 | |
| 667 | else if (arg->verbose && msg->kind == msg_preload) { |
| 668 | msg_load_t *load = (msg_load_t*)msg->data; |
| 669 | if (load->tbl) display_load(crack, load, info); |
| 670 | } |
| 671 | |
| 672 | /* Ask the finite state machine to take the appropriate steps |
| 673 | given the received message. */ |
| 674 | |
| 675 | msg = fsm_next(fsm, msg); |
| 676 | |
| 677 | if (msg) |
| 678 | message_free(msg); |
| 679 | } |
| 680 | |
| 681 | /* Save the passwords found so far. */ |
| 682 | |
| 683 | if (pwdfound) { |
| 684 | FILE *ofile = fopen(crack->arg->ofname, "w"); |
| 685 | FILE *xfile = 0; |
| 686 | if (crack->arg->exportcsv) |
| 687 | xfile = fopen(crack->arg->xfname, "w"); |
| 688 | |
| 689 | int fields[] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; |
| 690 | |
| 691 | if (ofile) { |
| 692 | pthread_mutex_lock(mutex); |
| 693 | if (xfile) { |
| 694 | ophcrack_export_csv(crack, xfile, fields, ';', 0); |
| 695 | fclose(xfile); |
| 696 | } |
| 697 | ophcrack_save(crack, ofile, 0, 0); |
| 698 | pthread_mutex_unlock(mutex); |
| 699 | |
| 700 | fclose(ofile); |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | /* Display the status. */ |
| 705 | |
| 706 | ophcrack_update(crack); |
| 707 | |
| 708 | if (arg->debug == 0 && !arg->quiet) { |
| 709 | printf("\x1B[2K\r"); |
| 710 | display_status(crack, info); |
| 711 | fflush(stdout); |
| 712 | } |
| 713 | |
| 714 | fflush(arg->lfile); |
| 715 | } |
| 716 | /*-------------------------------------------------------------------------*/ |
| 717 | int main(int argc, char **argv) { |
| 718 | static const char options[] = "aAbBc:Dd:ef:ghiIl:n:o:p:qrsS:t:uvw:x:"; |
| 719 | |
| 720 | arg_t *arg = arg_alloc(); |
| 721 | int c; |
| 722 | |
| 723 | // Add the empty table_path, so that we can specify the absolute |
| 724 | // path with the 'table' keyword in the configuration file. |
| 725 | |
| 726 | list_add_tail(arg->table_path, strdup("")); |
| 727 | |
| 728 | if (argc == 1 && !arg->use_gui) usage(); |
| 729 | |
| 730 | // Load the config file. |
| 731 | |
| 732 | int i = 1; |
| 733 | |
| 734 | while (i < argc && strcmp(argv[i++], "-c") != 0) {}; |
| 735 | if (i < argc) arg->cfname = strdup(argv[i]); |
| 736 | |
| 737 | load_config(arg); |
| 738 | |
| 739 | // Parse the command line. |
| 740 | |
| 741 | #ifdef OSX |
| 742 | // On Mac OS X, double-clicked applications have only one argument |
| 743 | // of the form "-psn...". We therefore do not parse the arguments if |
| 744 | // that one is present. |
| 745 | |
| 746 | if (argc >= 2 && strncmp(argv[1], "-psn", 4) != 0) { |
| 747 | #endif |
| 748 | |
| 749 | while ((c = getopt(argc, argv, options)) != -1) |
| 750 | switch (c) { |
| 751 | case 'a': |
| 752 | arg->auditmode = 0; |
| 753 | break; |
| 754 | |
| 755 | case 'A': |
| 756 | arg->auditmode = 1; |
| 757 | break; |
| 758 | |
| 759 | case 'b': |
| 760 | arg->bforce = 0; |
| 761 | break; |
| 762 | |
| 763 | case 'B': |
| 764 | arg->bforce = 1; |
| 765 | break; |
| 766 | |
| 767 | case 'D': |
| 768 | arg->debug = 1; |
| 769 | arg->verbose = 1; |
| 770 | break; |
| 771 | |
| 772 | case 'd': |
| 773 | list_add_head(arg->table_path, strdup(optarg)); |
| 774 | break; |
| 775 | |
| 776 | case 'e': |
| 777 | arg->showempty = 0; |
| 778 | break; |
| 779 | |
| 780 | case 'f': |
| 781 | arg->hfname = strdup(optarg); |
| 782 | break; |
| 783 | |
| 784 | case 'g': |
| 785 | arg->use_gui = 0; |
| 786 | break; |
| 787 | |
| 788 | case 'h': |
| 789 | usage(); |
| 790 | break; |
| 791 | |
| 792 | case 'i': |
| 793 | arg->hideuname = 1; |
| 794 | break; |
| 795 | |
| 796 | case 'I': |
| 797 | arg->hideuname = 0; |
| 798 | break; |
| 799 | |
| 800 | case 'l': |
| 801 | arg->lfname = strdup(optarg); |
| 802 | break; |
| 803 | |
| 804 | case 'n': |
| 805 | arg->nthreads = atoi(optarg); |
| 806 | break; |
| 807 | |
| 808 | case 'o': |
| 809 | arg->ofname = strdup(optarg); |
| 810 | break; |
| 811 | |
| 812 | case 'p': |
| 813 | arg->preload = atoi(optarg); |
| 814 | break; |
| 815 | |
| 816 | case 'q': |
| 817 | arg->quiet = 1; |
| 818 | break; |
| 819 | |
| 820 | case 'r': |
| 821 | arg->run = 1; |
| 822 | break; |
| 823 | |
| 824 | case 's': |
| 825 | arg->ssave = 0; |
| 826 | break; |
| 827 | |
| 828 | case 'S': |
| 829 | arg->ssave = 1; |
| 830 | arg->sfname = strdup(optarg); |
| 831 | break; |
| 832 | |
| 833 | case 't': |
| 834 | arg->tblstr = strdup(optarg); |
| 835 | break; |
| 836 | |
| 837 | case 'u': |
| 838 | arg->stats = 1; |
| 839 | break; |
| 840 | |
| 841 | case 'v': |
| 842 | arg->verbose = 1; |
| 843 | break; |
| 844 | |
| 845 | case 'w': |
| 846 | arg->samdir = strdup(optarg); |
| 847 | break; |
| 848 | |
| 849 | case 'x': |
| 850 | arg->exportcsv = 1; |
| 851 | arg->xfname = strdup(optarg); |
| 852 | break; |
| 853 | |
| 854 | default: |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | #ifdef OSX |
| 859 | } |
| 860 | #endif |
| 861 | |
| 862 | // If a log file has been provided, then we open it. |
| 863 | |
| 864 | if (arg->lfname != 0) { |
| 865 | arg->lfile = fopen(arg->lfname, "w"); |
| 866 | |
| 867 | if (arg->lfile == 0) { |
| 868 | fprintf(stderr, "Cannot open file %s for writing.\n", arg->lfname); |
| 869 | exit(1); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | // If we are in command line mode, then we check that the output |
| 874 | // file is writable. |
| 875 | |
| 876 | if (!arg->use_gui && arg->ofname) { |
| 877 | FILE *ofile = fopen(arg->ofname, "w"); |
| 878 | |
| 879 | if (ofile == 0) { |
| 880 | fprintf(stderr, "Cannot open file %s for writing.\n", arg->ofname); |
| 881 | exit(1); |
| 882 | } else |
| 883 | fclose(ofile); |
| 884 | } |
| 885 | |
| 886 | // If we are in command line mode, then we check that the csv |
| 887 | // file is writable. |
| 888 | |
| 889 | if (!arg->use_gui && arg->exportcsv) { |
| 890 | FILE *xfile = fopen(arg->xfname, "w"); |
| 891 | |
| 892 | if (xfile == 0) { |
| 893 | fprintf(stderr, "Cannot open file %s for writing.\n", arg->xfname); |
| 894 | exit(1); |
| 895 | } else |
| 896 | fclose(xfile); |
| 897 | } |
| 898 | |
| 899 | // If the session auto-saving is enabled, then we check that the |
| 900 | // session file is writable. |
| 901 | |
| 902 | if (arg->ssave && arg->sfname) { |
| 903 | FILE *sfile = fopen(arg->sfname, "a"); |
| 904 | |
| 905 | if (sfile == 0) { |
| 906 | fprintf(stderr, |
| 907 | "The session file %s you provided either in ", |
| 908 | arg->sfname); |
| 909 | fprintf(stderr, |
| 910 | "the config file or on the command line is not writable.\n"); |
| 911 | exit(1); |
| 912 | } else |
| 913 | fclose(sfile); |
| 914 | } |
| 915 | |
| 916 | // Store the table arguments given on the command line. |
| 917 | |
| 918 | if (arg->tblstr != 0) { |
| 919 | list_t *table_str = arg->table_str_cmd; |
| 920 | char *tmp; |
| 921 | |
| 922 | while ((tmp = strsep(&arg->tblstr, ":")) != 0 && strcmp(tmp, "") != 0) |
| 923 | list_add_tail(table_str, strdup(tmp)); |
| 924 | } |
| 925 | |
| 926 | // Initialise the message queue. |
| 927 | |
| 928 | message_init(); |
| 929 | |
| 930 | // Start ophcrack. Past this point we are in a multi-threaded |
| 931 | // application. The number of threads is generally set equal to the |
| 932 | // number of cores in the CPU plus one. |
| 933 | |
| 934 | ophcrack_t *crack = ophcrack_alloc(arg->nthreads+1, arg); |
| 935 | ophcrack_start(crack); |
| 936 | |
| 937 | // Load the hashes from the pwdump file if one has been provided. |
| 938 | |
| 939 | int maxhid = 0; |
| 940 | int npwds = 0; |
| 941 | |
| 942 | if (arg->hfname != 0) |
| 943 | npwds += load_hashes_pwdump(crack, maxhid); |
| 944 | |
| 945 | assert(crack->maxhid == npwds); |
| 946 | |
| 947 | // Load the hashes from the encrypted SAM if a directory is |
| 948 | // specified and one is found. |
| 949 | |
| 950 | maxhid = crack->maxhid; |
| 951 | |
| 952 | if (arg->samdir != 0) |
| 953 | npwds += load_hashes_sam(crack, maxhid); |
| 954 | |
| 955 | assert(crack->maxhid == npwds); |
| 956 | assert(crack->npwds_total == npwds); |
| 957 | |
| 958 | // Complain if no hashes have been loaded. |
| 959 | |
| 960 | if (!arg->use_gui && npwds == 0) { |
| 961 | fprintf(stderr, "No hash loaded. Use the -f or -w option.\n"); |
| 962 | exit(1); |
| 963 | } |
| 964 | |
| 965 | #ifdef HAVE_GUI |
| 966 | // Start the GUI. |
| 967 | |
| 968 | if (arg->use_gui) { |
| 969 | QApplication app(argc, argv); |
| 970 | OphcrackGUI gui(crack); |
| 971 | |
| 972 | gui.show(); |
| 973 | |
| 974 | return app.exec(); |
| 975 | } |
| 976 | #endif |
| 977 | |
| 978 | // If no tables were specified, then we load those specified in the |
| 979 | // config file. Otherwise, we load the ones specified on the command |
| 980 | // line. |
| 981 | |
| 982 | if (arg->table_str_cmd->size == 0) |
| 983 | load_tables(crack, arg, arg->table_str_conf); |
| 984 | else |
| 985 | load_tables(crack, arg, arg->table_str_cmd); |
| 986 | |
| 987 | // Build the list of enabled tables. |
| 988 | |
| 989 | list_t *tables = crack->tables; |
| 990 | list_t *enabled = crack->enabled; |
| 991 | list_nd_t *nd; |
| 992 | |
| 993 | list_clear(enabled); |
| 994 | |
| 995 | for (nd = tables->head; nd != 0; nd = nd->next) { |
| 996 | table_t *tbl = (table_t*)nd->data; |
| 997 | |
| 998 | if (tbl->enabled) |
| 999 | list_add_tail(enabled, tbl); |
| 1000 | } |
| 1001 | |
| 1002 | // Associate the appropriate tables to each hash. |
| 1003 | |
| 1004 | list_t *hashes = crack->hashes; |
| 1005 | list_nd_t *tnd, *hnd; |
| 1006 | |
| 1007 | for (hnd = hashes->head; hnd != 0; hnd = hnd->next) { |
| 1008 | hash_t *hsh = (hash_t*)hnd->data; |
| 1009 | |
| 1010 | for (tnd = enabled->head; tnd != 0; tnd = tnd->next) { |
| 1011 | table_t *tbl = (table_t*)tnd->data; |
| 1012 | ophcrack_associate(crack, hsh, tbl); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | // Main loop. Launch the preload and brute force, treat the messages |
| 1017 | // and exit when everything is done. |
| 1018 | |
| 1019 | int count = 0; |
| 1020 | fsm = fsm_alloc(crack); |
| 1021 | |
| 1022 | gettimeofday(&tm_main_start, 0); |
| 1023 | tm_main_total.tv_sec = 0; |
| 1024 | fsm_handle_start(fsm); |
| 1025 | |
| 1026 | while (fsm->state != st_wait) { |
| 1027 | handle_messages(crack); |
| 1028 | |
| 1029 | // Wait one second |
| 1030 | |
| 1031 | #ifdef WIN32 |
| 1032 | Sleep(1000); |
| 1033 | #else |
| 1034 | sleep(1); |
| 1035 | #endif |
| 1036 | |
| 1037 | ++count; |
| 1038 | |
| 1039 | // Check if it is time to save the session. |
| 1040 | |
| 1041 | if (arg->ssave && fsm->state & (st_work1 + st_work2) && count >= 30) { |
| 1042 | fsm_ssave(fsm); |
| 1043 | count = 0; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | // Display the results in command line mode |
| 1048 | |
| 1049 | if (!arg->use_gui) { |
| 1050 | if (arg->stats) |
| 1051 | display_stats(crack); |
| 1052 | |
| 1053 | if (!arg->quiet || arg->lfile != stdout) { |
| 1054 | fprintf(arg->lfile, "\n\nResults:\n\n"); |
| 1055 | fprintf(arg->lfile, |
| 1056 | "%-32s %-14s %s\n", "username / hash", "LM password", "NT password"); |
| 1057 | ophcrack_save(crack, arg->lfile, 1, 0); |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if (arg->debug == 0 && !arg->quiet) printf("\n"); |
| 1062 | |
| 1063 | // Close the log file. |
| 1064 | |
| 1065 | if (arg->lfile != stdout) fclose(arg->lfile); |
| 1066 | |
| 1067 | |
| 1068 | /* Save the passwords to the output and csv files. */ |
| 1069 | |
| 1070 | if (!arg->use_gui && arg->ofname) { |
| 1071 | FILE *ofile = fopen(arg->ofname, "w"); |
| 1072 | FILE *xfile = 0; |
| 1073 | if (arg->exportcsv) |
| 1074 | xfile = fopen(arg->xfname, "w"); |
| 1075 | |
| 1076 | int fields[] = {1,1,1,1,1,1,1,1,1,1,1,1,1}; |
| 1077 | |
| 1078 | if (ofile) { |
| 1079 | if (xfile) { |
| 1080 | ophcrack_export_csv(crack, xfile, fields, ';', 0); |
| 1081 | fclose(xfile); |
| 1082 | } |
| 1083 | ophcrack_save(crack, ofile, 0, 0); |
| 1084 | fclose(ofile); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |