File: zipcrack.c

package info (click to toggle)
fcrackzip 1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 632 kB
  • sloc: ansic: 2,723; sh: 743; makefile: 121; perl: 81
file content (278 lines) | stat: -rw-r--r-- 6,870 bytes parent folder | download | duplicates (6)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * zipcrack.c - where the 'crack' routine is located
 */

/*
 * guiding comments for gencrack.pl
 *
 * MTH cpmask init_cpmask crack_cpmask load_img
 *
 * LCL init_crack_pw crack_pw
 * LOD load_zip
 *
 * ARC ARCH_i386
 * DEF USE_MULT_TAB
 * DEF TARGET_CPU 0 $ 5 $ 6
 * DFT TARGET_CPU==COMPILE_CPU && USE_MULT_TAB
 * DEP defined(__i386__) || defined(__i386) || defined(i386)
 *
 * DEF USE_MULT_TAB
 * DFT USE_MULT_TAB
 * DEP 1
 *
 * END
 */

#include "crc32.h"

#undef USE_GCC_X86_ASM
#define USE_GCC_X86_ASM (ARCH_i386 && USE_GCC_ASM && USE_MULT_TAB)

static void init_crack_pw (void)
{
#if USE_MULT_TAB
  u16 t;
  for (t = 0; t < 16384; t++)
    mult_tab[t] = ((t*4+3) * (t*4+2) >> 8) & 0xff;
#endif
}

static int crack_pw (gen_func genfunc, callback_func cbfunc)
{
  int changed = -1;
  int crack_count = 0;
  u32 key_stack[(MAX_PW+1) * 3] = { 0x12345678UL, 0x23456789UL, 0x34567890UL };
  u32 *sp;
#if USE_GCC_X86_ASM
  int dummy; /* dummy output.  */
#endif

  sp = 0; /* to calm down dumb compilers */

  do
    {
      int count = file_count;
      int count2 = 0;
      u32 key0, key1, key2;
      u8 *p;
      u8 *b = files;
      
      if (changed < 0)
        {
          changed = strlen (pw);
          pw_end = pw + changed;
          sp = key_stack + changed * 3;
        }
      
      sp -= changed * 3;
      p = (u8 *)pw_end - changed;
      
      if (++crack_count >= 1000000 && verbosity)
        {
          printf ("checking pw %-40.40s\r", pw), fflush (stdout);
          crack_count = 0;
        }
      
#  if USE_GCC_X86_ASM && TARGET_CPU
      asm(
"        movl (%7),%0\n"
"        movl 4(%7),%1\n"
"        movl 8(%7),%2\n"
"        xorl %3,%3\n"
"        movb (%5),%b3\n"
"1:      xorb %b0,%b3\n"
"        shrl $8,%0\n"
"        incl %5\n"
"        xorl %c6(,%3,4),%0\n"
"        movb %b0,%b3\n"
"        addl $12,%7\n"
"        movl %0,(%7)\n"
"        addl %1,%3\n"
"        imul $134775813,%3\n"
"        leal 1(%3),%1\n"
"        shrl $24,%3\n"
"        movl %1,4(%7)\n"
"        xorb %b2,%b3\n"
"        shrl $8,%2\n"
"        xorl %c6(,%3,4),%2\n"
"        movb (%5),%b3\n"
"        orb  %b3,%b3\n"
"        movl %2,8(%7)\n"
"        jnz  1b\n"
         : "=c" (key0), "=a" (key1), "=b" (key2), "=&d" (dummy), "=D" (sp)
         : "S" (p), "i" (crc_32_tab), "D" (sp)
      );
#  else
      key0 = *sp++;
      key1 = *sp++;
      key2 = *sp++;
      do {
        *sp++ = key0 = crc32 (key0, *p++);
        *sp++ = key1 = (key1 + (u8)key0) * 134775813 + 1;
        *sp++ = key2 = crc32 (key2, key1 >> 24);
      } while (*p);
      
      sp -= 3;
#  endif
      
      do
        {
          u8 target, pre_target;
          
#         if USE_GCC_X86_ASM && TARGET_CPU
            asm(
"              movl $-12,%%edi\n"
"              pushl %%ebx\n"
"              addl $12,%3\n"
"              pushl %%ecx\n"
"              xorl %%edx,%%edx\n"
"              pushl %%ebp\n"
"              jmp  2f\n"
"1:            shrl $2,%%edx\n"
"              movb %c8(%%edx),%%dl\n"
"              xorb -1(%3,%%edi),%%dl\n"
#if TARGET_CPU == 6
"              movzbl %%dl,%%ebp\n"
#else
"              movl %%edx,%%ebp\n"
#endif
"              xorb %b4,%%dl\n"
"              shrl $8,%4\n"
#if TARGET_CPU == 6
"              movzbl %%dl,%%edx\n"
#else
"              andl $0xff,%%edx\n"
#endif
"              xorl %c7(,%%edx,4),%4\n"
#if TARGET_CPU == 6
"              movzbl %b4,%%edx\n"
#else
"              movb %b4,%%dl\n"
#endif
"              addl %5,%%edx\n"
"              imul $134775813,%%edx\n"
"              leal 1(%%edx),%5\n"
"              shrl $24,%%edx\n"
#if TARGET_CPU == 6
"              xorl %6,%%edx\n"
"              shrl $8,%6\n"
"              movzbl %%dl,%%edx\n"
#else
"              xorb %b6,%%dl\n"
"              shrl $8,%6\n"
#endif
"              xorl %c7(,%%edx,4),%6\n"
"2:            \n"
#if TARGET_CPU == 6
"              movzwl %w6,%%edx\n"
#else
"              movw %w6,%%dx\n"
#endif
"              shrl $2,%%edx\n"
"              movb %c8(%%edx),%%dl\n"
"              xorb (%3,%%edi),%%dl\n"
#if TARGET_CPU == 6
"              movzbl %%dl,%%ebp\n"
#else
"              movl %%edx,%%ebp\n"
#endif
"              xorb %b4,%%dl\n"
"              shrl $8,%4\n"
#if TARGET_CPU == 6
"              movzbl %%dl,%%edx\n"
#else
"              andl $0xff,%%edx\n"
#endif
"              xorl %c7(,%%edx,4),%4\n"
#if TARGET_CPU == 6
"              movzbl %b4,%%edx\n"
#else
"              movb %b4,%%dl\n"
#endif
"              addl %5,%%edx\n"
"              imul $134775813,%%edx\n"
"              leal 1(%%edx),%5\n"
"              shrl $24,%%edx\n"
#if TARGET_CPU == 6
"              xorl %6,%%edx\n"
"              shrl $8,%6\n"
"              movzbl %%dl,%%edx\n"
#else
"              xorb %b6,%%dl\n"
"              shrl $8,%6\n"
#endif
"              xorl %c7(,%%edx,4),%6\n"
"              addl $2,%%edi\n"
#if TARGET_CPU == 6
"              movzwl %w6,%%edx\n"
#else
"              movw %w6,%%dx\n"
#endif
"              jne  1b\n"
#if TARGET_CPU == 6
"              movzwl %w6,%6\n"
#else
"              andl $0xffff,%6\n"
#endif
"              movb -1(%3),%1\n"
"              shrl $2,%6\n"
"              movl %%ebp,%k2\n"
"              popl %%ebp\n"
"              popl %%ecx\n"
"              xorb %c8(%6),%1\n"
"              popl %%ebx\n"
               : "=S" (b), "=a" (target), "=d" (pre_target)
               : "S" (b), "c" (key0), "a" (key1), "b" (key2),
                 "i" (crc_32_tab), "i" (mult_tab)
               : "edi"
            );
#         else
#           if !USE_MULT_TAB
              u16 t;
#           endif
            u32 kez0, kez1, kez2;
            u8 *e = b + FILE_SIZE - 1;
          
            kez0 = key0, kez1 = key1, kez2 = key2;
            do
              {
#               if USE_MULT_TAB
                  pre_target = *b++ ^ mult_tab [(u16)(kez2) >> 2];
#               else
                  t = kez2 | 2;
                  pre_target = *b++ ^ (u8)(((u16) (t * (t^1)) >> 8));
#               endif
                kez0 = crc32 (kez0, pre_target);
                kez1 = (kez1 + (u8)kez0) * 134775813 + 1;
                kez2 = crc32 (kez2, kez1 >> 24);
              }
            while (b < e);
            
#           if USE_MULT_TAB
              target = *b++ ^ mult_tab [(u16)(kez2) >> 2];
#           else
              t = kez2 | 2;
              target = *b++ ^ (u8)(((u16) (t * (t^1)) >> 8));
#           endif
#         endif

          /*printf ("pw=%s, t1=%02x, t2=%02x (%02x, %02x)\n", pw, target, pre_target, b[0], b[1]);*/
          
          if (target != *b++)
            goto out;
          
          if (pre_target == *b++)
            count2++;
        }
      while(--count);
      
      if ((changed = cbfunc (pw, 0)))
         return changed;
      
      out: ;
    }
  while ((changed = genfunc ()));
  
  return 0;
}