实验吧-2

有一个程序加密得到如下密文

又是pyc文件,反编译之后:

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
# 2018.10.23 17:05:34 CST
# Embedded file name: ./rev200.py
import sys
from hashlib import md5
import base64
from time import time
from datetime import datetime
UC_KEY = '123456789'

def authcode(string, operation = 'DECODE', key = UC_KEY, expiry = 0):
ckey_length = 4
if key == '':
key = md5(UC_KEY.encode('utf-8')).hexdigest()
else:
key = md5(key.encode('utf-8')).hexdigest()
keya = md5(key[0:16].encode('utf-8')).hexdigest()
keyb = md5(key[16:32].encode('utf-8')).hexdigest()
if ckey_length == 0:
keyc = ''
elif operation == 'DECODE':
keyc = string[0:ckey_length]
elif operation == 'ENCODE':
keyc = md5(str(datetime.now().microsecond).encode('utf-8')).hexdigest()[-ckey_length:]
else:
return
cryptkey = keya + md5((keya + keyc).encode('utf-8')).hexdigest()
key_length = len(cryptkey)
if operation == 'DECODE':
string = base64.b64decode(string[ckey_length:])
elif operation == 'ENCODE':
if expiry == 0:
string = '0000000000' + md5((string + keyb).encode('utf-8')).hexdigest()[0:16] + string
else:
string = '%10d' % (expiry + int(time())) + md5((string + keyb).encode('utf-8')).hexdigest()[0:16] + string
else:
return
string_length = len(string)
result = ''
box = range(256)
rndkey = [0] * 256
for i in range(256):
rndkey[i] = ord(cryptkey[i % key_length])

j = 0
for i in range(256):
j = (j + box[i] + rndkey[i]) % 256
tmp = box[i]
box[i] = box[j]
box[j] = tmp

a = j = 0
for i in range(string_length):
a = (a + 1) % 256
j = (j + box[a]) % 256
tmp = box[a]
box[a] = box[j]
box[j] = tmp
result += chr(ord(string[i]) ^ box[(box[a] + box[j]) % 256])

if operation == 'DECODE':
if not result[0:10].isdigit() or int(result[0:10]) == 0 or int(result[0:10]) - int(time()) > 0:
if result[10:26] == md5(result[26:].encode('utf-8') + keyb).hexdigest()[0:16]:
return result[26:]
else:
return ''
else:
return ''
else:
return keyc + base64.b64encode(result)


if __name__ == '__main__':
if len(sys.argv) < 3:
exit(1)
ex = 20
for i in range(1, len(sys.argv), 2):
a = sys.argv[i]
b = sys.argv[i + 1]
if a == '-t':
ex = int(b)
elif a == '-e':
encoded = authcode(b, 'ENCODE', expiry=ex)
print encoded
elif a == '-d':
decoded = authcode(b, 'DECODE', expiry=ex)
print decoded
# okay decompyling reverse300.pyc
# decompiled 1 files: 1 okay, 0 failed, 0 verify failed
# 2018.10.23 17:05:34 CST

要求是输入至少3个参数,第一个输入-d的话就是解密,第二个参数就输入题目给的字符串就行了,不知道第三个参数该输入啥,直接把if len(sys.argv) < 3: exit(1)给nop掉了。

运行发现程序没有结果,可能是解密函数出了一些问题:

1
2
3
4
5
6
7
8
9
10
if operation == 'DECODE':
if not result[0:10].isdigit() or int(result[0:10]) == 0 or int(result[0:10]) - int(time()) > 0:
if result[10:26] == md5(result[26:].encode('utf-8') + keyb).hexdigest()[0:16]:
return result[26:]
else:
return ''
else:
return ''
else:
return keyc + base64.b64encode(result)

最里面的return result[26:]可能就是正确结果,把所有return替换成这个之后运行得到flag。

Keylead(ASIS CTF 2015)

下载下来是个7z文件,后缀改为7z解压后用ida打开,从主函数发现是一个掷🎲小游戏,和上一篇里的🎲要求掷的数字也是一样的,不过这个要用gdb调试,无奈gdb鶸,只能用ida看看输出flag的函数:

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
  v11 = 0;
v7 = 0;
while ( v11 != 1 )
{
putchar(byte_601680[14 * v11++]);
do
{
while ( v7 <= 1 )
putchar(byte_601680[20 * v11 - 8 * v7++]);
v1 = v7 + 1;
v2 = 3 * v11;
putchar(byte_601680[2 * v2 + 11 + v1]);
v2 *= 5;
v8 = v1 - 1;
putchar(byte_601680[2 * v2 + v8]);
v12 = v2 / 3;
while ( 1 )
{
while ( 1 )
{
LABEL_12:
if ( !v8 )
{
putchar(byte_601680[2 * v12]);
v15 = v12 ^ 2;
while ( 2 )
{
for ( i = 1; i <= 9; ++i )
putchar(byte_601680[10 * (i % 2) + 3 + v15]);
v13 = v15 + 1;
LABEL_44:
putchar(byte_601680[v13 / 3]);
if ( i == 10 )
{
v13 += 2 * v13 + 13;
i = 19;
goto LABEL_33;
}
if ( i == 2 )
{
i = v13-- + 2;
goto LABEL_4;
}
if ( (unsigned int)(i - 22) <= 0xA )
{
v14 = v13 - 3;
for ( j = i - v14; ; j = 8 )
{
LABEL_58:
putchar(byte_601680[v14 + 1]);
v16 = v14 + 1;
if ( j == 11 )
{
v13 = (v16 + 14) / 2;
i = 11 * (v13 / 6);
goto LABEL_44;
}
if ( j != 13 )
break;
v12 = v16 + 8;
v8 = 2;
LABEL_51:
while ( 2 )
{
putchar(byte_601680[v12 - 10]);
if ( v8 == v12 )
{
v4 = v8 + 2;
v5 = v12 + 2;
putchar(byte_601680[v5 / 2 + v4 / 5]);
i = v4 / 2;
putchar(byte_601680[i / 5 + v5]);
v13 = v5 + i - 1 + v5;
while ( 1 )
{
LABEL_33:
putchar(byte_601680[v13 - 19]);
if ( i == 2 )
{
v3 = v13 + 1;
putchar(byte_601680[v3 / 2 - 3]);
v13 = v3 / 5 + 2;
i = 2;
goto LABEL_44;
}
if ( i <= 2 )
break;
if ( i == 10 )
{
v12 = v13 - 31;
v8 = 9;
goto LABEL_51;
}
if ( i != 19 )
goto LABEL_44;
i = 2;
}
if ( i == 1 )
{
v8 = 17;
v12 = v13 % 5 - 17 + v13;
LABEL_20:
putchar(byte_601680[v12 - v8 + 9]);
v12 += ~v8++;
continue;
}
goto LABEL_44;
}
break;
}
if ( v8 != 9 )
{
if ( v8 != 2 )
goto LABEL_12;
j = 2;
v16 = v12 - 18;
LABEL_64:
v6 = 9 * j;
putchar(byte_601680[v16 / 2 + v6 + v6 % 10]);
v11 = v16 + 1;
v7 = v6 % 10;
goto LABEL_21;
}
putchar(byte_601680[v12 - 16]);
v14 = v12 / 2;
}
if ( j == 8 )
{
v15 = v16 + 1;
i = 7;
continue;
}
goto LABEL_64;
}
goto LABEL_4;
}
}
if ( v8 == 2 )
break;
if ( v12 == 7 )
{
putchar(byte_601680[21 - v8]);
v13 = 49;
i = v8 / 3;
goto LABEL_33;
}
if ( v8 != 3 )
goto LABEL_20;
putchar(byte_601680[2 * (v12 / 3)]);
v13 = v12 / 3;
i = 9;
do
{
while ( 1 )
{
if ( v13 == 10 )
{
putchar(byte_601680[i + 8]);
v14 = 9;
j = i + 1;
goto LABEL_58;
}
if ( v13 == 11 )
{
putchar(byte_601680[i / 7]);
v13 = i-- - 11;
}
LABEL_4:
if ( i != 9 )
break;
putchar(byte_601680[v13 * v13 + 7]);
v13 = v13 * v13 + 1;
i = 10;
}
}
while ( i != 13 );
putchar(byte_601680[2 * v13 + 12]);
v8 = 3;
v12 = 3 * v13;
}
putchar(byte_601680[3 * v12 + 1]);
v0 = v12 * v12;
putchar(byte_601680[v0 - 15]);
v11 = v0 - 15;
v7 = 4;
putchar(byte_601680[4]);
LABEL_21:
if ( v7 != 4 )
break;
putchar(byte_601680[v11 + 6]);
v12 = v11 - 3;
v8 = 3;
}
}
while ( v7 != 8 );
putchar(byte_601680[2 * v11 + 32]);
}

…有点烦,还是先学习下gdb吧。

学习后发现用ida远程调试就可以了,并不一定要用gdb,调试时把所有跳转到失败情况的jle之类的nop掉,这样运行到最后就会输出flag了。

分道扬镳

题目说是一个迷宫,ida打开,用字符串查看地图:

1
2
3
4
5
6
7
8
********
* * *
* * ** *
* * ** *
* * #* *
* **** *
* *
********

找到调用这个地图的函数:

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
  v5 = 0;
memset(&v6, 0, 0x74u);
v7 = 0;
v8 = 0;
strcpy(&v2, "********* * ** * ** ** * ** ** * #* ** **** ** *********");
v1 = &v3;
printf("Please input your key:\n");
gets(&v5);
if ( strlen(&v5) != 22 )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(1);
}
v11 = 0;
do
{
v10 = *(&v5 + v11);
if ( v10 != 'k' && v10 != 'j' && v10 != 'h' && v10 != 'l' )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(2);
}
v9 = *(&v5 + v11);
switch ( v9 )
{
case 'h':
if ( --v1 < &v2 || v1 > &v4 || (result = (char *)*v1, result == (char *)'*') )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(3);
}
if ( *v1 == '#' )
{
LABEL_41:
printf("Good!\n");
system("pause");
exit(0);
}
break;
case 'j':
v1 += 8;
if ( v1 < &v2 || v1 > &v4 || *v1 == '*' )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(3);
}
result = (char *)*v1;
if ( result == (char *)'#' )
goto LABEL_41;
break;
case 'k':
v1 -= 8;
if ( v1 < &v2 || v1 > &v4 || *v1 == '*' )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(3);
}
result = v1;
if ( *v1 == '#' )
goto LABEL_41;
break;
default:
if ( ++v1 < &v2 || v1 > &v4 || *v1 == '*' )
{
printf("Sorry you are wrong!\n");
system("pause");
exit(4);
}
result = v1;
if ( *v1 == '#' )
goto LABEL_41;
break;
}
++v11;
}
while ( v11 < 25 );
return result;

分析得j是向下,k是向上,h向左,l向右,不能走到*上,走到#就走出了迷宫,结合地图得到答案。

10000000

ida打开,main函数:

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
__main();
v17 = 0;
memset(&v18, 0, 0x10u);
memset(&v4, 0, 0x14u);
v4 = -26;
v5 = -20;
v6 = -31;
v7 = -25;
v8 = -70;
v9 = -12;
v10 = -27;
v11 = -13;
v12 = -12;
v13 = -12;
v14 = -27;
v15 = -13;
v16 = -12;
v19 = 0;
puts(asc_403024);
scanf("%s", &v17);
LOBYTE(v19) = 0;
while ( *((_BYTE *)&v17 + v19) )
*((_BYTE *)&v17 + v19++) |= 0x80u;
if ( !strcmp((const char *)&v17, &v4) )
printf("good");
else
printf("wrong");
return 0;

逻辑很简单,输入|=0x80后与v4相等就ok了,不过不清楚|可不可以逆运算,来一发爆破吧:

1
2
3
4
5
6
7
8
import string
v4=[0xe6,0xec,0xe1,0xe7,0xba,0xf4,0xe5,0xf3,0xf4,0xf4,0xe5,0xf3,0xf4]
flag=""
for i in range(len(v4)):
for j in string.printable:
if ord(j)|0x80==v4[i]:
flag+=j
print flag

需要注意的是ida打开时v4的值默认显示的是十进制负数……如果用这个来爆破是不会得到结果的,转换成十六进制后变成正数(鶸也不知道为啥),随后就可以得到flag了。

文章目录
  1. 1. 有一个程序加密得到如下密文
  2. 2. Keylead(ASIS CTF 2015)
  3. 3. 分道扬镳
  4. 4. 10000000
|