Post

Crackme-3.exe

Crackme-3.exe

Name:

0000 Serial: 96b1b96a9b24f40d8a67748bf4a5bba9

This crackme includes obfuscation, packing and dynamic key. The main goal is to reverse algorithm and generate in accordance with this algo a key. The algo reversing took me about a month of work, but i had rewtitten it from the scratch. The following C++ code is algo i reversed and put into readible C++ code. The python rc6 script, was already presend as a git repo and i used it for my keygen. There is actually a more simpler way - seeking crypto constants which unequivocally determine the encryption algorithm used, but in a real world scenario algorithms are often custom made, so you have to manually reverse engineer them. I will not dive into UPX unpacking as it’s straightforward thing to do and there are already automatic unpackers.

I’ve included the code files in crackme3.rar archive.

RC6 Reversed decryption algorithm

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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "crypto.hpp"

BYTE GetBit(DWORD I, int N){
    int a = I >> (N - 1);
    int b = I >> N;
    b = b << 1;
    int r = a ^ b;
    return r;
}
DWORD LeftRotate(DWORD dwVar, DWORD dwOffset){
    DWORD temp1, temp2;
    temp1 = dwVar >> (W - dwOffset);
    temp2 = dwVar << dwOffset;
    temp2 = temp2 | temp1;
    return temp2;
}

DWORD RightRotate(DWORD dwVar, DWORD dwOffset){
    DWORD temp1, temp2;
    temp1 = dwVar << (W - dwOffset);
    temp2 = dwVar >> dwOffset;
    temp2 = temp2 | temp1;
    return temp2;
}
DWORD OffsetAmount(DWORD dwVar){
    int nLgw = (int)(log((double)W) / log(2.0));
    dwVar = dwVar << (W - nLgw);
    dwVar = dwVar >> (W - nLgw);
    return dwVar;
}


unsigned int block_process_24(unsigned int block){
    unsigned int half_1 = block;
    unsigned int half_2 = block;
    block = ((block + block + 1) * block);
    half_1 = block << 5;
    half_2 = block >> 0x1B;
    return half_1 | half_2;
}

unsigned int block_process_13(unsigned int block_1, unsigned int block_3, unsigned int block_2X, unsigned int block_4X, int id, const std::vector<DWORD>* KE, int round){
    unsigned int half_1;
    unsigned int half_2;
    unsigned int shift;
    unsigned int eax = 0x20;
    unsigned int whole;
    BYTE byte_shift;
    switch (id) {
        case 3: {
            //BLOCK 3
            std::cout << " block 3: " << block_3 << std::endl;
            half_1 = block_3 - KE->at(round * 2 + 1);
            std::cout << " block 3 sub: " << half_1 << std::endl;

            shift = block_2X & 0x1F;
            std::cout << "  block_2X & 0x1F: " << shift << std::endl;
            byte_shift = (shift & 0xFF);
            std::cout << "  byte_shift: " << std::hex << byte_shift << std::endl;

            half_1 = half_1 >> byte_shift;
            std::cout << "  half_1 >> GetBit(shift, 4): " << half_1 << std::endl;

            shift = eax - shift;
            std::cout << "  eax - shift: " << shift << std::endl;

            half_2 = block_3 - KE->at(round * 2 + 1);
            std::cout << "  block_3 - KE->at(round * 2+1): " << half_2 << std::endl;
            byte_shift = (shift & 0xFF);
            std::cout << "  byte_shift: " << std::hex << byte_shift << std::endl;

            half_2 = half_2 << byte_shift;
            std::cout << "  half_2 << GetBit(shift, 4): " << half_2 << std::endl;

            whole = half_1 | half_2;
            std::cout << "  half_1 | half_2: " << whole << std::endl;

            whole = whole ^ block_4X;
            std::cout << "block 3 fin: " << whole << " KE[" << round * 2 + 1 << "] = " << KE->at(round * 2 + 1) << "\n";
            return whole;
        }

        case 1: {
            //BLOCK 1
            std::cout << " block 1: " << block_1 << std::endl;
            half_1 = block_1 - KE->at(round * 2);
            std::cout << " block 1 sub: " << half_1 << std::endl;

            shift = block_4X & 0x1F;
            half_1 = half_1 >> (shift & 0xFF);

            shift = eax - shift;
            half_2 = block_1 - KE->at(round * 2);
            half_2 = half_2 << (shift & 0xFF);

            whole = half_1 | half_2;
            whole = whole ^ block_2X;
            std::cout << "whole block 1: " << whole << " KE[" << round * 2 << "]=" << KE->at(round * 2) << "\n";

            return whole;
        }
    }
}



std::string hts(char n){
    std::stringstream ss;
    ss << n;
    return ss.str();
}

class cypher_blocs{
public:
    std::vector<unsigned int>block_1;
    std::vector<unsigned int>block_2;
    std::vector<unsigned int>block_3;
    std::vector<unsigned int>block_4;

    cypher_blocs() {};
    cypher_blocs(int size) {
        this->block_1;
        this->block_2;
        this->block_3;
        this->block_4;
    };
    ~cypher_blocs() {};
};

template <typename T>
void output(std::vector <T>* x){
    for (int i : *x)    {
        std::cout << std::hex << i << " ";
    }
    std::cout << "\n\n";
}


int main(){
    std::vector<DWORD> key_exp;
    key_exp = key_expansion();

    cypher_blocs blocks(4);
    char* passbuff2 = new char[32];
    //std::vector<char> pass {'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F','0' ,'1' ,'2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' ,'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D' };
    //std::vector<char> pass{ 'A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A','A' };

    std::string password_x;
    std::vector<char> pass{};
    std::cout << "[+] Enter pass: ";
    //std::cin >> password_x;
    password_x = "B7E151629E3779B9595921D2AAAAAAAA";
    // ciphered: 96b1b96a 9b24f40d 8a67748b f4a5bba9
    //  6ec62c47 4d403d52 f9aed716 ee529ac7
    // B7E15162 9E3779B9 595921D2 AAAAAAAA

    for (char i : password_x) {
        pass.push_back(i);
    }

    std::vector<unsigned long> XORpass{};
    std::vector<unsigned long> cube;

    output(&pass);

    // std::cout << " enter pass: \n";
    // std::cin >> passbuff;
    std::cout << "\n";
    unsigned int op1, op2, op3, op4;

    std::cout << "[+] CHAR TO HEX: " << std::endl;

    for (int c = 0; c < 32; ++c)    {
        if (pass[c] >= 65 && pass[c] <= 70) {  // chars  A-F
            pass.at(c) = pass[c] - 55;
        }
        else if (pass[c] >= 97 && pass[c] <= 102) { // chars  a-f
            pass.at(c) = pass[c] - 87;
        }
        else if (pass[c] >= 48 && pass[c] <= 57) {  // numbers 0-9
            pass.at(c) = pass[c] - 48;
        }
    }
    output(&pass);

    std::cout << "\n\n[+] SCALING TO BYTE: \n";
    for (int a = 0; a < 32; a++) {
        int b = a;
        int row = 4;

        if (b % 2 == 0) {
            op1 = pass[a];
            op2 = pass[a + 1];
            op1 = op1 * 16;
            op3 = (op2 + op1);
            XORpass.push_back(op3);
        }
    }
    output(&XORpass);

    std::cout << "\n[+] SCALING TO DWORD\n";
    unsigned long tmp = XORpass.at(0);
    for (unsigned long i = 0; i < XORpass.size(); i++){
        tmp = tmp << 8;
        tmp += XORpass.at(i);
        if (i % 4 == 4 - 1)  cube.push_back(tmp);
    }
    output(&cube);
    std::vector<DWORD> shit = { 0x3020100,0x7060504,0xB0A0908 ,0xF0E0D0C };
    std::vector<unsigned int> shiftedArray;

    unsigned long block_4X;
    unsigned long block_2X;
    unsigned long exchange;

    {
        //normal rc6 rot
        // reverse rc6 rot
        /*exchange = cube.at(1)  ;
        cube.at(1) = cube.at(3);
        cube.at(3) = cube.at(0);
        cube.at(0) = exchange ;*/
    }

    output(&cube);

    // Значение не фиксированное зависит от юзернейма
    // расчет по адресу loc_4A685B:

    std::cout << "[!] last val in KE: " << key_exp.at(43) << std::endl;
    std::cout << "cubes: " << cube.at(0) << " " << cube.at(1) << " " << cube.at(2) << " " << cube.at(3) << std::endl;

    cube.at(1) += key_exp.at(0); // 2nd block : AAAAAAAA
    cube.at(3) += key_exp.at(1); // 3rd block : CCCCCCCC

    std::cout << "\n\n\n[!] LOOK HERE!!! <--- " << std::endl;
    output(&cube); // must be 0B7E15162 9E3779B9 595921D2 AAAAAAAA

    std::cout << "\n[+] BLOCK 2 + KE[43] = " << cube.at(1) << "\n";
    std::cout << "\n[+] BLOCK 4 + KE[42] = " << cube.at(3) << "\n";

    std::cout << "\n[+] SHUFFLE 4 BLOCKS:\n";
    int  ctr = 1;
    while (ctr++ && ctr != 21) {
      //  std::cout << "\nCYCLE: " << std::dec << ctr << " " << " exchange: " << std::hex << exchange << "\n";
        
        block_4X = LeftRotate((cube.at(1) * (2 * cube.at(1) + 1)), (DWORD)5);
        block_2X = LeftRotate((cube.at(3) * (2 * cube.at(3) + 1)), (DWORD)5);
       
        std::cout << "block_4X: " << block_4X << "  " << " block_2X: " << block_2X << "\n\n";
    
       cube.at(0) = (LeftRotate(cube.at(0) ^ block_4X, OffsetAmount(block_2X)) + key_exp.at(2*ctr));
       cube.at(2) = (LeftRotate(cube.at(2) ^ block_2X, OffsetAmount(block_4X)) + key_exp.at(2 * ctr + 1));

        DWORD temp = cube.at(0);
        cube.at(0) = cube.at(1);
        cube.at(1) = cube.at(2);
        cube.at(2) = cube.at(3);
        cube.at(3) = temp;

        output(&cube);
    }
    std::cout << "\n\n[+] FINAL RES: ";

    output(&cube);

    cube.at(0) += key_exp.at(2 * R + 2);
    cube.at(2) += key_exp.at(2 * R + 3);
 
    std::cout << "\n\n[+] FINAL RES: ";
    output(&cube);

    std::cout << "[+] ROT: " << std::endl;
    DWORD x = cube.at(0);
    cube.at(0) = cube.at(1);
    cube.at(1) = cube.at(3);
    cube.at(3) = x;
    output(&cube);

    std::cout << "[+] XOR: " << std::endl;

    for (int i = 0; i < 4; i++) {

        cube.at(i) = cube.at(i) ^ shit.at(i);

    }
    output(&cube);

    /*
    e15c97cb 6dc42d47 f2a4de1e 4a463856
    6DC42D47 4A463856 F2A4DE1E E15C97CB
    */
    pass.clear();
    cube.clear();
    return 0;
}

std::vector<DWORD>& key_expansion(){

    DWORD P32 = 0xB7E15163;
    DWORD Q32 = 0x9E3779B9;
    DWORD Z32 = 0x61C88647;		// user for key expansion on the very 1sst round
    DWORD USER = 0x30303030;        // equal to 0000
    DWORD UTEMP_1, UTEMP_2;
    DWORD KEA = 0; // block2
    DWORD KEB = 0; // block3
    DWORD KEA_KEB_1, KEA_KEB_2;

    std::vector<DWORD> KE_TABLE{};

    std::vector<DWORD>KE{};
    KE.push_back(P32);
    int scale = 0;
    for (int i = 1; i <= KE_LENGTH; i++) {
        if (scale % 5 == 0) std::cout << "\n";
        KE.push_back(KE.at(i - 1) - Z32);
        std::cout << std::hex << KE.at(i) << ", ";
        scale++;
    }

    std::cout << "\n[+] Key expansion round 2 with 2 additional blocks.\n";
    scale = 0;

    for (int j = 0; j < 3; j++) {
        std::cout << "\n[+] Key expansion CYCLE - " << j << std::endl;

        for (int i = 0; i < KE_LENGTH; i++) {
            if (scale % 5 == 0) std::cout << "\n";

            DWORD SHL;
            DWORD SHR;

            SHL = (KE.at(i) + KEA + KEB) << 3;
            SHR = (KE.at(i) + KEA + KEB) >> 0x1D;

            KE.at(i) = SHR | SHL;

            // on 2nd iteration must be KEB not kea
            KEB = KE.at(i);

            UTEMP_1 = USER + KEA + KEB;
            KEA_KEB_1 = (KEA + KEB) & 0x1F;
            UTEMP_1 = UTEMP_1 << KEA_KEB_1;

            UTEMP_2 = USER + KEA + KEB;
            KEA_KEB_2 = 0x20 - ((KEA + KEB) & 0x1F);
            UTEMP_2 = UTEMP_2 >> KEA_KEB_2;

            USER = UTEMP_2 | UTEMP_1;
            // on 2nd iteration must be KEA and 2nd part of key
            KEA = USER;

            std::cout << KEA << " :" << KEB << ", \n";
            scale++;

            if (j == 2) {
                KE_TABLE.push_back(KEB);
            }
        }
    }


    std::cout << "\n[+] Key expansion table:\n\n";

    scale = 0;
    for (DWORD x : KE_TABLE)
    {
        if (scale % 5 == 0) std::cout << "\n";
        std::cout << std::hex << x << ", ";
        scale++;
    }
    std::cout << "\nlen: " << scale << std::endl;
    return KE_TABLE;
}

Now here’s the keygen for crackme3. The main caveat is the ending xor operation perforemed on the resulting dword values, such that: 0x3020100, 0x7060504, 0xB0A0908, 0xF0E0D0C.

RC6 key generator

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
import math
import sys

#rotate right input x, by n bits
def ROR(x, n, bits = 32):
    mask = (2**n) - 1
    mask_bits = x & mask
    return (x >> n) | (mask_bits << (bits - n))

#rotate left input x, by n bits
def ROL(x, n, bits = 32):
    return ROR(x, bits - n,bits)

#convert input sentence into blocks of binary
#creates 4 blocks of binary each of 32 bits.
def blockConverter(sentence):
    encoded = []
    res = ""
    for i in range(0,len(sentence)):
        if i%4==0 and i!=0 :
            encoded.append(res)
            res = ""
        temp = bin(ord(sentence[i]))[2:]
        if len(temp) <8:
            temp = "0"*(8-len(temp)) + temp
        res = res + temp
        #print('res', res)
    encoded.append(res)
    return encoded

#converts 4 blocks array of long int into string
def deBlocker(blocks):
    s = ""
    for ele in blocks:
        temp =bin(ele)[2:]
        if len(temp) <32:
            temp = "0"*(32-len(temp)) + temp
        for i in range(0,4):
            s=s+chr(int(temp[i*8:(i+1)*8],2))
    return s

#generate key s[0... 2r+3] from given input string userkey
def generateKey(userkey, c):
    r=20
    w=32
    b=len(userkey)
    modulo = 2**32
    s=(2*r+4)*[0]
    s[0]=0xB7E15163
    for i in range(1,2*r+4):
        s[i]=(s[i-1]+0x9E3779B9)%(2**w)
    k = 0

    #encoded = blockConverter(userkey)
    b = []
    for i in userkey:
        b.append(bin(ord(i))[2::].zfill(8))
    b = b[::-1]
    c = []
    d = 1
    buff = ''
    for i in range(len(b),0,-1):
        if i == 1:
            buff = b[i-1] + buff
            c.append(buff)
        elif d%4 == 0:
            buff = b[i-1] + buff
            c.append(buff)
            buff = ''
            d = 1
        elif d % 4 != 0:
            buff = b[i-1] + buff
            d += 1
            
    encoded = c[::-1]
    #print ('encoded', encoded)
    enlength = len(encoded)
    l = enlength*[0]
    for i in range(1,enlength+1):
        l[enlength-i]=int(encoded[i-1],2)
    #print(l)
    v = 3*max(enlength,2*r+4)
    A=B=i=j=0
    for index in range(0,v):
        # print('l =', hex(l[j]), 's =', hex(s[i]), end = ' ')
        A = s[i] = ROL((s[i] + A + B)%modulo,3,32)
        B = l[j] = ROL((l[j] + A + B)%modulo,(A+B)%32,32)
        # print('A =', hex(A), 'B =', hex(B))
        i = (i + 1) % (2*r + 4)
        j = (j + 1) % enlength
    #print(hex(A), hex(B))
    return s

def encrypt(sentence,s):
    encoded = blockConverter(sentence)
    enlength = len(encoded)
    A = int(encoded[0],2)
    B = int(encoded[1],2)
    C = int(encoded[2],2)
    D = int(encoded[3],2)
    #print(hex(A),hex(B),hex(C),hex(D))
    orgi = []
    orgi.append(A)
    orgi.append(B)
    orgi.append(C)
    orgi.append(D)
    r=20
    w=32
    modulo = 2**32
    lgw = 5
    B = (B + s[0])%modulo
    D = (D + s[1])%modulo 
    #print(hex(A),hex(B),hex(C),hex(D))
    for i in range(1,r+1):
        t_temp = (B*(2*B + 1))%modulo 
        t = ROL(t_temp,lgw,32)
        u_temp = (D*(2*D + 1))%modulo
        u = ROL(u_temp,lgw,32)
        tmod=t%32
        umod=u%32
        A = (ROL(A^t,umod,32) + s[2*i])%modulo 
        C = (ROL(C^u,tmod,32) + s[2*i+ 1])%modulo
        (A, B, C, D)  =  (B, C, D, A)
    A = (A + s[2*r + 2])%modulo 
    C = (C + s[2*r + 3])%modulo
    cipher = []
    cipher.append(A)
    cipher.append(B)
    cipher.append(C)
    cipher.append(D)
    return orgi,cipher

def decrypt(esentence,s):
    encoded = blockConverter(esentence)
    enlength = len(encoded)
    A = int(encoded[0],2)
    B = int(encoded[1],2)
    C = int(encoded[2],2)
    D = int(encoded[3],2)
    r=20
    #print(hex(A),hex(B),hex(C),hex(D), hex(s[2*r+3]), hex(s[2*r+2]))
    cipher = []
    cipher.append(A)
    cipher.append(B)
    cipher.append(C)
    cipher.append(D)
    
    w=32
    modulo = 2**32
    lgw = 5
    C = (C - s[2*r+3])%modulo
    A = (A - s[2*r+2])%modulo
    #print(hex(A),hex(B),hex(C),hex(D))
    for j in range(1,r+1):
        i = r+1-j
        (A, B, C, D) = (D, A, B, C)
        u_temp = (D*(2*D + 1))%modulo
        u = ROL(u_temp,lgw,32)
        t_temp = (B*(2*B + 1))%modulo 
        t = ROL(t_temp,lgw,32)
        tmod=t%32
        umod=u%32
        C = (ROR((C-s[2*i+1])%modulo,tmod,32)  ^u)  
        A = (ROR((A-s[2*i])%modulo,umod,32)   ^t) 
    D = (D - s[1])%modulo 
    B = (B - s[0])%modulo
    orgi = []
    orgi.append(A)
    orgi.append(B)
    orgi.append(C)
    orgi.append(D)
    return cipher,orgi

key=input('key<16:')
#encoded = ['01100101','01100100 01100011 01100010 01100001']
b = []
for i in key:
    b.append(bin(ord(i))[2::].zfill(8))
b = b[::-1]
c = []
d = 1
buff = ''
for i in range(len(b),0,-1):
    if i == 1:
        buff = b[i-1] + buff
        c.append(buff)
    elif d%4 == 0:
        buff = b[i-1] + buff
        c.append(buff)
        buff = ''
        d = 1
    elif d % 4 != 0:
        buff = b[i-1] + buff
        d += 1
 
sentence = input('sentence<16: ')

sentence = sentence + " "*(16-len(sentence))

s = generateKey(key, c)

k = 0
sentence = sentence[:16]
sentence = deBlocker([3084996962, 2654435769, 1499013586, 538976288])

orgi,cipher = encrypt(sentence,s)
esentence = deBlocker(cipher)

print(hex(cipher[0]^0x3020100)[2::].zfill(8),end='')
print(hex(cipher[1]^0x7060504)[2::].zfill(8),end='')
print(hex(cipher[2]^0xB0A0908)[2::].zfill(8),end='')
print(hex(cipher[3]^0xF0E0D0C)[2::].zfill(8),end='')

orgi,decipher =decrypt(esentence,s)
print("\nDECIPHER\n")
print(hex(decipher[0])[2::].zfill(8),end='')
print(hex(decipher[1])[2::].zfill(8),end='')
print(hex(decipher[2])[2::].zfill(8),end='')
print(hex(decipher[3])[2::].zfill(8),end='')

This post is licensed under CC BY 4.0 by the author.