-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrack_demo.c
More file actions
60 lines (54 loc) · 1.74 KB
/
Copy pathcrack_demo.c
File metadata and controls
60 lines (54 loc) · 1.74 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Demo didáctica de cracking de SHA-256 sin sal contra wordlist en memoria.
//
// Uso:
// ./crack_demo <hash_hex> [<wordlist_path>]
//
// Ejemplo:
// echo -n "letmein" | sha256sum
// ./crack_demo 1c8bfe8f801d79745c4631d09fff36c82aa37fc4cce4fc946683d7b336b63032
//
// NO usar en producción. Solo ilustrativo: passwords reales tienen salt
// y un KDF caro (bcrypt/argon2id) por encima.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
void hex(const unsigned char *buf, char *out) {
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++)
sprintf(out + 2 * i, "%02x", buf[i]);
out[64] = '\0';
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Uso: %s <hash_hex> [wordlist]\n", argv[0]);
return 1;
}
const char *target = argv[1];
const char *wlpath = argc > 2 ? argv[2] : "/usr/share/wordlists/rockyou.txt";
FILE *fp = fopen(wlpath, "r");
if (!fp) { perror("fopen"); return 1; }
char line[256];
long tried = 0;
while (fgets(line, sizeof line, fp)) {
size_t n = strcspn(line, "\r\n");
line[n] = '\0';
if (n == 0) continue;
unsigned char digest[SHA256_DIGEST_LENGTH];
SHA256((unsigned char *) line, n, digest);
char hexdigest[65];
hex(digest, hexdigest);
if (strcmp(hexdigest, target) == 0) {
printf("[+] Encontrado tras %ld intentos: \"%s\"\n", tried + 1, line);
fclose(fp);
return 0;
}
tried++;
if (tried % 100000 == 0) {
fprintf(stderr, "\r[*] %ld passwords probados...", tried);
fflush(stderr);
}
}
printf("\n[-] No encontrado tras %ld intentos.\n", tried);
fclose(fp);
return 1;
}