Christmas CTF - Simple_Bit

역연산 해주면 되는 문제였다.

먼저 ida hexray를 이용해서 함수들을 살펴보자

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
__int64 __fastcall main(__int64 a1, char **a2, char **a3)
{
__int64 result; // rax@4
__int64 v4; // rsi@4
char buf[264]; // [sp+10h] [bp-110h]@1
__int64 canary; // [sp+118h] [bp-8h]@1

canary = *MK_FP(__FS__, 40LL);
memset(buf, 0, 0x100uLL);
printf("Input : ", a2, buf);
fflush(stdout);
buf[(signed int)((unsigned __int64)read(0, buf, 0x100uLL) - 1)] = 0;
if ( (unsigned int)check(buf) )
puts("Correct");
else
puts("Wrong");
result = 0LL;
v4 = *MK_FP(__FS__, 40LL) ^ canary;
return result;
}

signed __int64 __fastcall check(const char *input)
{
signed __int64 result; // rax@17
__int64 v2; // rsi@21
int v3; // [sp+14h] [bp-402Ch]@8
int i; // [sp+18h] [bp-4028h]@1
int j; // [sp+1Ch] [bp-4024h]@2
signed int k; // [sp+20h] [bp-4020h]@7
int l; // [sp+24h] [bp-401Ch]@8
signed int m; // [sp+28h] [bp-4018h]@15
int length; // [sp+2Ch] [bp-4014h]@1
int arr[2048]; // [sp+30h] [bp-4010h]@1
int goal[288]; // [sp+2030h] [bp-2010h]@1
__int64 canary; // [sp+4038h] [bp-8h]@1

canary = *MK_FP(__FS__, 40LL);
memset(arr, 0, sizeof(arr));
memset(goal, 0, 0x2000uLL);
goal[2] = 1;
goal[3] = 1;
goal[4] = 1;
goal[8] = 1;
goal[9] = 1;
goal[11] = 1;
goal[13] = 1;
goal[16] = 1;
goal[20] = 1;
goal[21] = 1;
goal[22] = 1;
goal[28] = 1;
goal[30] = 1;
goal[33] = 1;
goal[35] = 1;
goal[36] = 1;
goal[37] = 1;
goal[40] = 1;
goal[41] = 1;
goal[48] = 1;
goal[49] = 1;
goal[51] = 1;
goal[53] = 1;
goal[54] = 1;
goal[60] = 1;
goal[61] = 1;
goal[63] = 1;
goal[64] = 1;
goal[65] = 1;
goal[67] = 1;
goal[68] = 1;
goal[73] = 1;
goal[76] = 1;
goal[78] = 1;
goal[80] = 1;
goal[85] = 1;
goal[86] = 1;
goal[87] = 1;
goal[89] = 1;
goal[90] = 1;
goal[91] = 1;
goal[95] = 1;
goal[96] = 1;
goal[99] = 1;
goal[100] = 1;
goal[101] = 1;
goal[102] = 1;
goal[104] = 1;
goal[110] = 1;
goal[111] = 1;
goal[116] = 1;
goal[117] = 1;
goal[120] = 1;
goal[121] = 1;
goal[122] = 1;
goal[125] = 1;
goal[126] = 1;
goal[132] = 1;
goal[135] = 1;
goal[140] = 1;
goal[142] = 1;
goal[144] = 1;
goal[145] = 1;
goal[146] = 1;
goal[147] = 1;
goal[149] = 1;
goal[150] = 1;
goal[151] = 1;
goal[154] = 1;
goal[157] = 1;
goal[162] = 1;
goal[167] = 1;
goal[171] = 1;
goal[173] = 1;
goal[174] = 1;
goal[175] = 1;
goal[176] = 1;
goal[179] = 1;
goal[180] = 1;
goal[182] = 1;
goal[184] = 1;
goal[185] = 1;
goal[186] = 1;
goal[191] = 1;
goal[193] = 1;
goal[196] = 1;
goal[198] = 1;
goal[201] = 1;
goal[203] = 1;
goal[204] = 1;
goal[205] = 1;
goal[206] = 1;
goal[208] = 1;
goal[209] = 1;
goal[210] = 1;
goal[216] = 1;
goal[218] = 1;
goal[221] = 1;
goal[222] = 1;
goal[226] = 1;
goal[227] = 1;
goal[228] = 1;
goal[229] = 1;
goal[230] = 1;
goal[231] = 1;
goal[234] = 1;
goal[237] = 1;
goal[239] = 1;
goal[240] = 1;
goal[242] = 1;
goal[245] = 1;
goal[247] = 1;
goal[251] = 1;
goal[254] = 1;
goal[259] = 1;
goal[263] = 1;
goal[264] = 1;
goal[268] = 1;
goal[269] = 1;
goal[271] = 1;
goal[274] = 1;
goal[276] = 1;
goal[277] = 1;
goal[279] = 1;
goal[280] = 1;
goal[282] = 1;
goal[283] = 1;
goal[284] = 1;
goal[287] = 1;
length = strlen(input);
for ( i = 0; i < length; ++i )
{
for ( j = 8 * i; j < 8 * (i + 1); ++j )
{
arr[j] = input[i] & 1;
input[i] >>= 1;
}
}
for ( k = 0; k <= 15; ++k )
{
v3 = 1;
for ( l = 0; l < 8 * length; ++l )
{
if ( arr[l] )
v3 ^= 1u;
arr[l] = v3;
}
shuffle(arr, 8 * length);
}
for ( m = 0; m <= 0x7FF; ++m )
{
if ( arr[m] != goal[m] )
{
result = 0LL;
goto LABEL_21;
}
}
result = 1LL;
LABEL_21:
v2 = *MK_FP(__FS__, 40LL) ^ canary;
return result;
}

_DWORD *__fastcall shuffle(_DWORD *arr, signed int length)
{
_DWORD *result; // rax@1
int i; // [sp+18h] [bp-8h]@1
int r; // [sp+1Ch] [bp-4h]@2

result = (_DWORD *)(unsigned int)(length - 1);
for ( i = length - 1; i >= 0; --i )
{
r = rand() % length;
result = (_DWORD *)(unsigned int)i;
if ( i != r )
{
arr[i] ^= arr[r]; // swap(arr[i], arr[r])
arr[r] ^= arr[i];
result = &arr[i];
*result ^= arr[r];
}
}
return result;
}

루틴을 정리해보면 유저에게 입력을 받고 그 글자를 2진수로 바꾼다음 뒤집어서 넣는다.
그리고 어떠한 연산을 한 뒤에 배열을 섞어준다. 이걸 16번 반복하고 goal배열과 결과를 비교해본다.

해당 프로그램에서 shufflerand()를 사용하고 있는 것을 볼 수 있지만 srand를 사용하지 않았기에 rand는 항상 동일한 값이 나오며 이것을 이용해서 우리는 shuffle을 역연산 하는 행위도 할 수 있다.

solve.py

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
from ctypes import *
import sys

def shuffle(arr, rand):
rand = rand[::-1]
for i in range(len(rand)):
arr[i], arr[rand[i]] = arr[rand[i]], arr[i]

length = 36

libc = CDLL('libc.so.6')

rands = []
for i in range(16):
rand = []
for j in range(length * 8):
rand.append(libc.rand() % (length * 8))
rands.append(rand)

arr = [0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1]
arr += [0] * (0x800 - len(arr))

for i in range(15, -1, -1):
shuffle(arr, rands[i])
for j in range(8 * length - 1, -1, -1):
if j == 0:
arr[j] = arr[j] ^ 1
else:
arr[j] = arr[j] ^ arr[j-1]

bins = ''.join(map(str, arr))
output = ''
for i in range(0, len(bins), 8):
output += chr(int(bins[i:i+8][::-1], 2))

print output # XMAS{S1mp1e_diff3r3nt1cal_c0d1ng_:p}
Share