View difference between Paste ID: qVbZrCBN and 9mve8xWV
SHOW: | | - or go back to the newest paste.
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <unistd.h>
5
#include <crypt.h>
6-
#include <sys/time.h>
6+
#include <time.h>
7
8
int guesses = 0;
9
10
char *
11
brute(char *dict, int dictlen, char hash[13], int len) {
12
    unsigned char indeces[len];
13
    char guess[len + 1];
14
    int cur;
15
    char *res;
16
17
    memset(indeces, 0, len);
18
    cur = 0;
19
    guess[len] = 0;
20
    while (cur >= 0) {
21
        if (cur == len) {
22
            res = crypt(guess, hash);
23
            guesses++;
24
            if (!memcmp(res, hash, 13)) {
25
                res = malloc(len + 1);
26
                memcpy(res, guess, len + 1);
27
                return res;
28
            }
29
            cur--;
30
        } else if (indeces[cur] == dictlen + 1) {
31
            cur--;
32
        } else {
33
            guess[cur] = dict[indeces[cur]];
34
            indeces[cur]++;
35
            cur++;
36
            indeces[cur] = 0;
37
        }
38
    }
39
    return NULL;
40
}
41
42
char dict[] = "abcdefghijklmnopqrstuvwxyz";
43
44
int
45
main() {
46
    char *res;
47
    char *pass;
48
    char hash[13];
49
    char *salt;
50-
    struct timeval start, finish;
50+
    struct timespec start, finish;
51
    float spent;
52
    float rate;
53
54
    printf("Password: ");
55
    scanf("%ms", &pass);
56
    printf("Salt: ");
57
    scanf("%ms", &salt);
58
59
    res = crypt(pass, salt);
60
    memcpy(hash, res, 13);
61
    printf("Hash is %s\n", res);
62
63
    clock_gettime(CLOCK_BOOTTIME, &start);
64
    res = brute(dict, sizeof(dict) - 1, hash, strlen(pass));
65
    clock_gettime(CLOCK_BOOTTIME, &finish);
66
    spent = (finish.tv_sec - start.tv_sec);
67
    spent += (finish.tv_nsec - start.tv_nsec) / 1000000000.0;
68
    rate = guesses / spent;
69
    printf("Spent %f sec, rate %f g/s\n", spent, rate);
70
    if (res) {
71
        printf("Found: %s\n", res);
72
    } else {
73
        printf("Not found\n");
74
    }
75
    free(pass);
76
    free(salt);
77
    free(res);
78
    return 0;
79
}