Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdint.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <getopt.h>
- #include <assert.h>
- #include <errno.h>
- #include <time.h>
- #include <limits.h>
- typedef struct {
- #define KEYLEN 256
- uint32_t state[KEYLEN];
- } KEY;
- typedef struct {
- #define BUFLEN 16
- uint32_t version;
- unsigned char master_pass[BUFLEN];
- } HEADER;
- typedef struct {
- unsigned char site[BUFLEN * 2];
- unsigned char user[BUFLEN];
- unsigned char pass[BUFLEN];
- } ENTRY;
- unsigned int startTime, timeElapsed;
- unsigned int startSeed;
- float seedsPerSecond;
- unsigned char master[BUFLEN] = "foo";
- unsigned int percentilModulo = (unsigned int)(UINT_MAX * .1);
- enum
- {
- SITE,
- USER,
- PASS
- };
- char * const add_opts[] =
- {
- [SITE] = "site",
- [USER] = "username",
- [PASS] = "password",
- NULL
- };
- static struct option longopts[] =
- {
- { "help", no_argument, 0, 'h' },
- { "crack", no_argument, 0, 'c' },
- { "seqsize", required_argument, 0, 's' },
- { "vlevel", required_argument, 0, 'v' },
- { "sfrom", required_argument, 0, 'f' },
- { 0, 0, 0, 0 }
- };
- void help()
- {
- fprintf(stderr,
- "\nMilitary-Grade Password Cracker\n"
- "Usage: ./passwd_cracker [options] [suboptions] <database>\n\n"
- "--crack\n"
- "\tBrute force the database\n\n"
- "--sfrom 0-4294967295 (default 0)\n"
- "\tSpecify where to begin in range of seed values. Useful for a distributed attack among multiple computers/cpus.\n\n"
- "--seqsize 3-5 (default 5)\n"
- "\tLess than 3 will result in too many false-positives, more than 5 may cause false-negatives.\n\n"
- "--vlevel 1-5 (default 1)\n"
- "\tVerbosity level. Shows progress at %%10, %%1, %%.1, %%.01, or %%.001 intervals.\n\n"
- );
- }
- void derive_key(KEY *key, uint32_t fakeSeed)
- {
- int i = 0;
- srand(fakeSeed);
- for (i = 0; i < KEYLEN; i++)
- key->state[i] = rand() & 0xffff;
- }
- void rc4_encrypt(KEY *key, unsigned char *data, const size_t len)
- {
- uint32_t i = 0, t = 0, x = 0, y = 0;
- uint32_t state[KEYLEN];
- memcpy(&state, key->state, sizeof(state));
- for (; i < len; i++)
- {
- x = (x + 1) % KEYLEN;
- y = (y + state[x]) % KEYLEN;
- t = state[x];
- state[x] = state[y];
- state[y] = t;
- t = (state[x] + state[y]) % KEYLEN;
- data[i] = state[t] ^ data[i];
- }
- }
- int crack(char *db, uint32_t fakeSeed, KEY key, HEADER hdr, ENTRY entry, char *byteSequence, int seqSize)
- {
- derive_key(&key, fakeSeed);
- rc4_encrypt(&key, hdr.master_pass, BUFLEN);
- rc4_encrypt(&key, entry.site, sizeof(entry.site));
- rc4_encrypt(&key, entry.user, sizeof(entry.user));
- rc4_encrypt(&key, entry.pass, sizeof(entry.pass));
- if(
- ((memmem(hdr.master_pass,sizeof(hdr.master_pass),byteSequence,seqSize) != NULL)) ||
- ((memmem(entry.site,sizeof(entry.site),byteSequence,seqSize) != NULL)) ||
- ((memmem(entry.user,sizeof(entry.user),byteSequence,seqSize) != NULL)) ||
- ((memmem(entry.pass,sizeof(entry.pass),byteSequence,seqSize) != NULL))
- )
- {
- printf("\n%-32s\t%-16s\t%-16s\n", "SITE", "USERNAME", "PASSWORD");
- printf("--------------------------------");
- printf("--------------------------------");
- printf("----------------\n");
- printf("%-32s\t%-16s\t%-16s\n", entry.site, entry.user, entry.pass);
- printf("Seed value: %u\n", fakeSeed);
- printf("Master Pass: %s\n", hdr.master_pass);
- }
- else if(fakeSeed > 1 && fakeSeed % percentilModulo == 0)
- {
- timeElapsed = time(0) - startTime;
- seedsPerSecond = (float)(fakeSeed-startSeed)/timeElapsed;
- printf("%u seeds of %u tried in %i seconds, %.2f seeds per second \n", fakeSeed - startSeed, UINT_MAX - startSeed, timeElapsed, seedsPerSecond);
- printf("%.3f percent complete, ~%.2f hours left\n", ((float)(fakeSeed-startSeed)/UINT_MAX) * 100, ((UINT_MAX-(fakeSeed-startSeed))/seedsPerSecond) / 3600);
- }
- return 0;
- }
- int main(int argc, char **argv)
- {
- char *db = NULL, *site = NULL, *user = NULL, *pass = NULL;
- char *subopt, *value;
- int opts = 0, idx = 0, ret = 0;
- int _init = 0, _crack = 0, _add = 0;
- uint32_t fakeSeed = 0;
- startSeed = 0;
- char *byteSequence;
- int seqSize = 5;
- KEY key;
- HEADER hdr;
- ENTRY entry;
- FILE *dbh;
- while (1)
- {
- if ((opts = getopt_long_only(argc, argv, "", longopts, &idx)) == -1)
- break;
- switch (opts)
- {
- case 0:
- if (longopts[idx].flag)
- break;
- case 'h':
- help();
- return 0;
- case 'c':
- _crack++;
- break;
- case 's':
- seqSize = atoi(optarg);
- break;
- case 'f':
- startSeed = (unsigned int)strtoul(argv[1], NULL, 0);
- fakeSeed = startSeed;
- break;
- case 'v':
- switch (atoi(optarg))
- {
- case 1:
- percentilModulo = (unsigned int)(UINT_MAX * .1);
- break;
- case 2:
- percentilModulo = (unsigned int)(UINT_MAX * .01);
- break;
- case 3:
- percentilModulo = (unsigned int)(UINT_MAX * .001);
- break;
- case 4:
- percentilModulo = (unsigned int)(UINT_MAX * .0001);
- break;
- case 5:
- percentilModulo = (unsigned int)(UINT_MAX * .00001);
- break;
- default:
- percentilModulo = (unsigned int)(UINT_MAX * .1);
- }
- break;
- default:
- abort();
- }
- }
- if (optind == argc)
- {
- fprintf(stderr, "Error: database required\n");
- return -1;
- }
- assert(db = strdup(argv[optind]));
- if (_crack)
- {
- if ((dbh = fopen(db, "r")) == NULL)
- return errno;
- fread(&hdr, sizeof(hdr), 1, dbh);
- fread(&entry, sizeof(entry), 1, dbh);
- startTime = time(0);
- byteSequence = calloc(sizeof(char),seqSize);
- while( fakeSeed < UINT_MAX )
- {
- if ((ret = crack(db, fakeSeed, key, hdr, entry, byteSequence, seqSize)) != 0)
- fprintf(stderr, "Error: %s\n", strerror(ret));
- fakeSeed++;
- }
- fclose(dbh);
- return ret;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment