File: brute.c

package info (click to toggle)
crack 5.0a-19
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,248 kB
  • sloc: ansic: 7,445; perl: 1,375; sh: 1,066; makefile: 216
file content (208 lines) | stat: -rw-r--r-- 3,005 bytes parent folder | download | duplicates (9)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
   # This program was written by and is copyright Alec Muffett 1991,
   # 1992, 1993, 1994, 1995, and 1996, and is provided as part of the
   # Crack v5.0 Password Cracking package.
   #
   # The copyright holder disclaims all responsibility or liability with
   # respect to its usage or its effect upon hardware or computer
   # systems, and maintains copyright as set out in the "LICENCE"
   # document which accompanies distributions of Crack v5.0 and upwards.
 */

#include <stdio.h>
#include <stdlib.h>

#define MAXLEN 64

char *cipher;
char *pwset[MAXLEN];
char pwbuffer[MAXLEN];

void
brute(int charno, int length)
{
    char *p;
    int doit;

    if (charno == (length - 1))
    {
	doit = 1;
    }
    else
    {
	doit = 0;
    }

    pwbuffer[charno + 1] = '\0';

    if (!pwset[charno])
    {
	fprintf(stderr, "wtf?\n");
	exit(1);
    }

    for (p = pwset[charno]; *p; p++)
    {
	pwbuffer[charno] = *p;

	if (doit)
	{
#if BRUTEGEN
	    printf("%s\n", pwbuffer);
#else
	    if (!strcmp(cipher, (char *) crypt(pwbuffer, cipher)))
	    {
		printf("brute: guessed %s == '%s'\n", cipher, pwbuffer);
		exit(0);
	    }
#endif
	}
	else
	{
	    brute(charno + 1, length);
	}
    }
}

char *
expand(char *string)
{
    int i;
    int j;
    char *c;
    char *retval;
    int a;
    int b;
    char block[256];            /* std crypt() is restricted to 128, bwtf.  md5 aint. */

    for (i = 0; i < 256; i++)
    {
	block[i] = '\0';
    }

    c = string;

    while (*c)
    {
	i = 0;
	j = -1;

	if ((*c == '\\') && c[1])
	{
	    c++;
	    i = *c & 0xff;
	    c++;
	} else
	{
	    i = *c & 0xff;
	    c++;
	}

	if (*c == '-' && c[1])
	{
	    c++;

	    if ((*c == '\\') && c[1])
	    {
		c++;
		j = *c & 0xff;
		c++;
	    } else
	    {
		j = *c & 0xff;
		c++;
	    }
	}

	if (i < j)
	{
	    while (i <= j)
	    {
		block[i++] = 1;
	    }
	}
	else
	{
	    block[i] = 1;	    
	}
    }

    j = 0;

    for (i = 0; i < 256; i++)
    {
	if (block[i])
	{
	    j++;
	}
    }

    if (!j)
    {
	fprintf(stderr, "brute: cannot permit empty set: %s\n", string);
	exit(1);
    }

    c = retval = malloc(j + 1);

    for (i = 0; i < 256; i++)
    {
	if (block[i])
	{
	    *(c++) = i;
	}
    }

    *c = '\0';

    return retval;
}

/* brute ciphertext '[set]' ... */

main(int argc, char **argv)
{
    int i;
    int j;

#if BRUTEGEN
    if (argc < 2)
    {
	fprintf(stderr, "Usage:\t%s charset ...\n", argv[0]);
	exit(1);
    }

    i = 1;
#else
    if (argc < 3)
    {
	fprintf(stderr, "Usage:\t%s ciphertext charset ...\n", argv[0]);
	exit(1);
    }

    cipher = argv[1];
    fprintf(stderr, "brute: ciphertext: %s\n", cipher);

    i = 2;
#endif

    j = 0;
    while ((i < argc) && (i < (MAXLEN - 1)))
    {
	fprintf(stderr, "brute: set %d: %s -> ", j, argv[i]);
	pwset[j] = expand(argv[i]);
	fprintf(stderr, "'%s'\n", pwset[j]);
	i++;
	j++;
    }

    pwset[j] = (char *) 0;

    brute(0, j);

#if BRUTEGEN
    exit(0);
#else
    exit(1); /* if cracking, exit with error unless cracked */
#endif
}