-
Notifications
You must be signed in to change notification settings - Fork 1
/
crack.c
85 lines (82 loc) · 2.68 KB
/
crack.c
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int sim_resol(unsigned char *iv, unsigned char *key, int byte_index, unsigned char out) {
unsigned char s[256], tmp_key[8];
unsigned char a, s_1, k;
int i, j;
memcpy(tmp_key, iv, 3);
memcpy(&tmp_key[3], key, 5);
for (i=0; i < sizeof(s)/sizeof(s[0]); i++)
s[i] = i;
j=k=0;
for (i=0; i < byte_index+3; i++) {
j = (unsigned char)(j+s[i]+tmp_key[k]);
a = s[i];
s[i] = s[j];
s[j] = a;
if (++k >= 8)
k = 0;
}
for (i=0; i < sizeof(s)/sizeof(s[0]); i++)
if (s[i] == out)
s_1 = i;
if ((s[0] != byte_index + 3) || (s[1] != 0))
return -1;
else
return (s_1-j-s[byte_index+3]);
}
void print_counts(unsigned char *counts, unsigned char byte_index) {
int i;
printf("\n---- begin counts (%d) ---- \n", byte_index);
for(i = 0; i < 256; i++)
printf("%d ", *(counts+i));
printf("\n---- end counts ---- \n");
}
int main(int argc, char* argv[]) {
FILE *fd;
size_t read, len = 0;
int i;
unsigned char tmp, out, byte_index, max, max_index, plain;
unsigned char key[5], counts[256], tmp_line[256];
plain = 'C';
memset(key, 0, 5);
for (byte_index=0; byte_index < sizeof(key)/sizeof(key[0]); byte_index++) {
memset(counts, 0, 256);
fd = fopen (argv[1],"r");
if (fd == NULL) {
printf ("can not open file\n");
return -1;
}
read = fread (tmp_line, sizeof(unsigned char), 143, fd);
while (read !=0 ){
//Process read packet and update counts array
if ((tmp_line[24] == byte_index+3) && (tmp_line[25] == 255))
{
out = plain^tmp_line[27];
//printf("tmp: %d %d %d %d\n", tmp_line[24], byte_index, tmp_line[25], out);
tmp = sim_resol(&tmp_line[24], key, byte_index, out);
if ((tmp >= 0) && (tmp <= 255))
counts[tmp]++;
}
read = fread (tmp_line, sizeof(unsigned char), 143, fd);
}
//Process counts array and find key[byte_index]
max=0;
max_index=0;
for (i=0; i<256; i++)
{
if (counts[i] > max){
max_index=i;
max = counts[i];
}
}
key[byte_index]=(unsigned char)max_index;
//print_counts(counts, byte_index);
fclose (fd);
}
//printf("Key (decimal value): %d %d %d %d %d\n", key[0], key[1], key[2], key[3], key[4]);
//printf("Key (character val): '%c' '%c' '%c' '%c' '%c'\n", key[0], key[1], key[2], key[3], key[4]);
printf("%s\n", key);
return 0;
}