February 7, 2023

西湖论剑 逆向

BabyRe

tips:
_initterm, _initterm_e
atexit:简单来说,就是程序正常退出之前,会调用atexit注册的函数,调用顺序是栈式的,即先注册后调用。

函数调用顺序:sub_401000(初始化,注册sub_405770) -> sub_401170(初始化,检查输入全为数字) -> sub_401050(调用sub_401230,注册sub_4057B0) -> sub_401230(处理后续使用的表) -> sub_4010A0(调用hook函数,注册sub_4057F0) -> sub_4012B0(将GetLastError函数hook为sub_4019D0,此函数修改了哈希常量) -> sub_401670(调用base加密并cmp) -> sub_4015C0(调用hash并cmp) -> sub_405770(RC4并cmp,最终输出结果)

整个加密流程为:将输入base8编码,然后取[16:112]与密文对比(即前6字节和后面的不可知),base8后的前16字节哈希后与密文对比,最后取输入的最后6字节作为RC4密钥加密[:112]
可以根据RC4爆破6字节密钥,获得base8后的密文

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<stdio.h>
#include<string.h>
#include<stdlib.h>

void rc4_init(unsigned char*s,unsigned char*key, unsigned long Len)
{
int i=0,j=0;
unsigned char k[256]={0};
unsigned char tmp=0;
for(i=0;i<256;i++) {
s[i]=i;
k[i]=key[i%Len];
}
for(i=0;i<256;i++) {
j=(j+s[i]+k[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
}

void rc4_crypt(unsigned char*s,unsigned char*Data,unsigned char*enc,unsigned long Len)
{
int i=0,j=0,t=0;
unsigned long k=0;
unsigned char tmp;
for(k=0;k<Len;k++)
{
i=(i+1)%256;
j=(j+s[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
t=(s[i]+s[j])%256;
Data[k]=s[t]^enc[k];
}
}

int main()
{
unsigned char enc[] =
{
0x3F, 0x95, 0xBB, 0xF2, 0x57, 0xF1, 0x7A, 0x5A, 0x22, 0x61,
0x51, 0x43, 0xA2, 0xFA, 0x9B, 0x6F, 0x44, 0x63, 0xC0, 0x08,
0x12, 0x65, 0x5C, 0x8A, 0x8C, 0x4C, 0xED, 0x5E, 0xCA, 0x76,
0xB9, 0x85, 0xAF, 0x05, 0x38, 0xED, 0x42, 0x3E, 0x42, 0xDF,
0x5D, 0xBE, 0x05, 0x8B, 0x35, 0x6D, 0xF3, 0x1C, 0xCF, 0xF8,
0x6A, 0x73, 0x25, 0xE4, 0xB7, 0xB9, 0x36, 0xFB, 0x02, 0x11,
0xA0, 0xF0, 0x57, 0xAB, 0x21, 0xC6, 0xC7, 0x46, 0x99, 0xBD,
0x1E, 0x61, 0x5E, 0xEE, 0x55, 0x18, 0xEE, 0x03, 0x29, 0x84,
0x7F, 0x94, 0x5F, 0xB4, 0x6A, 0x29, 0xD8, 0x6C, 0xE4, 0xC0,
0x9D, 0x6B, 0xCC, 0xD5, 0x94, 0x5C, 0xDD, 0xCC, 0xD5, 0x3D,
0xC0, 0xEF, 0x0C, 0x29, 0xE5, 0xB0, 0x93, 0xF1, 0xB3, 0xDE,
0xB0, 0x70, 0
};

char key[8];
unsigned char box[256], dec[150];
int flag = 0;
for(int i = 100000; i < 1000000; i++)
{
int len = 0;
itoa(i, key, 10);
rc4_init(box, key, strlen(key));
rc4_crypt(box, dec, enc, strlen(enc));
for(int k = 0; dec[k]; k++)
{
if(dec[k]<'0'||dec[k]>'9')
break;
len++;
}
if(len == strlen(enc))
{
printf("%s\n",dec);
printf("%d",i);
break;
}
}

return 0;
}
/*
1523306115230466162304651523346214431471150310701503207116032063140334661543446114434066142304661563446615430464
807391
*/

对输出base8解码,打表或者直接逆都可以,得到明文561516915572239428449843076691286116796614,再接上key,得到561516915572239428449843076691286116796614807391,即为flag。

Berkeley

ebpf程序

strings得源码(在1.8k+行的地方):

1
2
3
4
5
6
7
8
9
10
for(int i=0; i<256; i++)
unsigned char uc1 = flag[i/8];
unsigned char uc2 = ~(flag[i/8] + arr[i%8]);
output[i] = key[uc1 ^ uc2];
output[i] = key[output[i] ^ key[i]];
for(int i=0; i<256; i++)
if(output[i] != cipher[i])
bpf_map_delete_elem(&execs, &pid);
int BPF_KRETPROBE(LBB0_2)
cipher

逆向得exp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
key = [0xC1, 0xD1, 0x02, 0x61, 0xD6, 0xF7, 0x13, 0xA2, 0x9B, 0x20, 0xD0, 0x4A, 0x8F, 0x7F, 0xEE, 0xB9, 0x00, 0x63, 0x34, 0xB0, 0x33, 0xB7, 0x8A, 0x8B, 0x94, 0x60, 0x2E, 0x8E, 0x21, 0xFF, 0x90, 0x82, 0xD5, 0x87, 0x96, 0x78, 0x22, 0xB6, 0x48, 0x6C, 0x45, 0xC7, 0x5A, 0x16, 0x80, 0xFD, 0xE4, 0x8C, 0xBF, 0x01, 0x1F, 0x4B, 0x79, 0x24, 0xA0, 0xB4, 0x23, 0x4D, 0x3B, 0xC5, 0x5D, 0x6F, 0x0D, 0xC9, 0xD4, 0xCA, 0x55, 0xE0, 0x39, 0xAD, 0x2B, 0xCD, 0x2C, 0xEC, 0xC2, 0x6B, 0x30, 0xE6, 0x0C, 0xA8, 0x9A, 0x2F, 0xF6, 0xE8, 0xBB, 0x32, 0x57, 0xFB, 0x0B, 0x9D, 0xF2, 0x3F, 0xB5, 0xF9, 0x59, 0xE5, 0x10, 0xCF, 0x51, 0x41, 0xE9, 0x50, 0xDF, 0x26, 0x74, 0x58, 0xCB, 0x64, 0x54, 0x73, 0xAB, 0xF4, 0xB2, 0x9F, 0x18, 0xF8, 0x4E, 0xFE, 0x08, 0x1D, 0x4F, 0x49, 0xD3, 0xAC, 0x38, 0x12, 0x77, 0x11,
0x69, 0x07, 0x1C, 0x99, 0xB3, 0xE7, 0x3D, 0x05, 0xD8, 0xFC, 0x70, 0x46, 0x93, 0x09, 0x65, 0x89, 0xB1, 0xC6, 0x52, 0xFA, 0xD2, 0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x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01]
tmp = []
for i in range(256):
tmp.append(key.index(cipher[i])^key[i])
for i in range(0,256,8):
for j in range(0x20,0x7f):
for k in range(8):
if((j^(j+arry[k])^0xff)&0xff!=key.index(tmp[i+k])):
break
else:
print(chr(j),end='')
#71c2ac98ac8d99a2e8a95111449a7393

被打

看到一个提取ir的方法,根据Lu1u师傅的博客操作即可

Dual personality

天堂之门 (Heaven’s Gate)

简而言之就是在32位程序里执行64位代码。sub_401120用于做出这种转换。
自己patch程序的32位标志为64位标志即可用64位IDA打开反汇编。

结合两个程序的代码自己看流程即可,数据可以调试取得,注意反调试的地方。(好吧!我调不对,我硬看的x)
加密一共有3段,第一段是加,第二段是移位,第三段是异或,但是我明明改了反调试的地方,最后一段取的异或数据还是不对啊啊啊啊啊,不知道哪里有问题(可能是后面改了,但是断点断不到64位代码里所以有点问题)
只能patch输出用交互取加密后的密文,然后跟前两段加密后的结果异或,得到用于异或的数据。

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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdint.h>

#define rol(x,i) ((x<<i)|(x>>(64-i)))&0xffffffffffffffff
#define ror(x,i) ((x>>i)|(x<<(64-i)))&0xffffffffffffffff

int main()
{
unsigned char enc[] = {0xAA, 0x4F, 0xF, 0xE2, 0xE4, 0x41, 0x99, 0x54, 0x2C, 0x2B, 0x84, 0x7E, 0xBC, 0x8F, 0x8B, 0x78, 0xD3, 0x73, 0x88, 0x5E, 0xAE, 0x47, 0x85, 0x70, 0x31, 0xB3, 0x9, 0xCE, 0x13, 0xF5, 0xD, 0xCA, 0};
unsigned char my_enc[] = {37, 39, 98, 178, 84, 125, 194, 91, 4, 119, 146, 106, 4, 215, 13, 10, 90, 4, 127, 130, 74, 4, 119, 130, 74, 4, 119, 130, 74, 4, 119, 130, 74, 13, 10};
unsigned char my_dec[32];
/*
x = 0x5DF966AE - 0x21524111
for(i = 0; i < 8; i++)
{
(dword)input[i] += x;
x ^= input[i];
}
rol8(input[0], 12)
rol8(input[1], 34)
rol8(input[2], 56)
rol8(input[3], 14)

^????
*/
uint32_t x = 0x5DF966AE - 0x21524111;
uint32_t* t32_d = (uint32_t*)my_dec;
uint32_t* t32_e = (uint32_t*)my_enc;
uint64_t* t64_d = (uint64_t*)my_dec;
uint64_t* t64_e = (uint64_t*)my_enc;
int i;

//encrypt
for(i = 0; i < 32; i++)
my_dec[i] = 'h';

for(i = 0; i < 8; i++)
{
t32_d[i]+=x;
x^=t32_d[i];
}

t64_d[0] = rol(t64_d[0], 12);

//printf("%#x,", t64_d[0]^t64_e[0]);
//0x4a827704

//decrypt
uint32_t* t32 = (uint32_t*)enc;
uint64_t* t64 = (uint64_t*)enc;

for(i = 0; i < 8; i++)
t32[i]^=0x4a827704;

t64[0] = ror(t64[0], 12);
t64[1] = ror(t64[1], 34);
t64[2] = ror(t64[2], 56);
t64[3] = ror(t64[3], 14);

x = 0x5DF966AE - 0x21524111;
uint32_t t;
for(i = 0; i < 8; i++)
{
t = t32[i];
t32[i]-=x;
x^=t;
}

for(i = 0; i < 32; i++)
{
printf("%c", enc[i]);
}

return 0;
}
//6cc1e44811647d38a15017e389b3f704

EasyVT

每次都会因为很傻逼的问题卡半年。。。

根据main函数里的指令对比常量表即可,wp很多我就不抄一遍了……
啊,我真是越来越懒了

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
#include <stdio.h>
#include <stdint.h>
#include <string.h>

void decrypt (uint32_t* v, uint32_t* k) {
uint32_t v0 = v[0], v1 = v[1];
uint32_t delta = 0xC95D6ABF;
uint32_t sum = 0x20000000;
uint32_t k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
for (int i=0; i<32; i++) sum -= delta;
for (int i=0; i<32; i++) {
sum += delta;
v1 -= (k2 + (v0 >> 5)) ^ (sum + v0) ^ (k3 + 16 * v0);
v0 += (k0 + (v1 >> 5)) ^ (sum + v1) ^ (k1 + 16 * v1);
}

v[0]=v1;
v[1]=v0;
}


void rc4_init(unsigned char*s,unsigned char*key, unsigned long Len)
{
int i=0,j=0;
unsigned char k[256]={0};
unsigned char tmp=0;
for(i=0;i<256;i++) {
s[i]=i;
k[i]=key[i%Len];
}
for(i=0;i<256;i++) {
j=(j+s[i]+k[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
}

void rc4_crypt(unsigned char*s,unsigned char*Data,unsigned long Len)
{
int i=0,j=0,t=0;
unsigned long k=0;
unsigned char tmp;
for(k=0;k<Len;k++)
{
i=(i+1)%256;
j=(j+s[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
t=(s[i]+s[j])%256;
Data[k]=s[t];
}
}
/*
op: 27,19,21,25,20,23,18,22,24,26
rc4_key: '04e52c7e31022b0b'
5,1,3,4,2,swap,rc4,swap,TEA
k1_ = key_tea[1]; // 102030h, 40506070h, 8090A0B0h, 0C0D0E0F0h
k0_ = key_tea[3];
k3_ = key_tea[2];
k2_ = key_tea[0];
*/
int main()
{
unsigned char enc[] =
{
0x94, 0x39, 0x07, 0x5C, 0xB3, 0x5C, 0x80, 0x0D, 0x86, 0xA5,
0xDD, 0x87, 0x8E, 0xFB, 0x17, 0x03, 0x29, 0xEF, 0x20, 0x65,
0xAF, 0x87, 0x49, 0x5A, 0xA4, 0xC2, 0x2D, 0xEB, 0x0E, 0x47,
0xCF, 0x38, 0
};

uint32_t k[4]= {0xC0D0E0F0, 0x40506070, 0x102030, 0x8090A0B0};
int i;

for(i = 0; i < 8; i+=2)
decrypt((uint32_t*)enc+i, k);

unsigned char key[]={"04e52c7e31022b0b"};
unsigned char s[256], box[8];

rc4_init(s,key,strlen(key));
rc4_crypt(s,box,8);
for(i = 0; i < 32; i++)
enc[i]^=box[i%8];

uint32_t *enc32 = (uint32_t*)enc;
for(i = 0; i < 8; i+=2)
{
uint32_t tmp = enc32[i];
enc32[i] = enc32[i+1];
enc32[i+1] = tmp;
}
printf("%s",enc);
return 0;
}
//81920c3758be43705ba154bb8f599846

关于本文

本文作者 云之君, 许可由 CC BY-NC 4.0.