获取中...

-

Just a minute...

emmm又是一道控制流平坦化的题

之前遇到一道关于控制流平坦化的题目,用来差不多一天的时间硬生生的看出来的。这个题放到ida里去看,呜呜呜,什么玩意哇(>人<;)


没去平坦化,800多行实在看不懂。。。

去控制流平坦化

用人家写好的脚本https://github.com/pcy190/deflat
要有python3和angr,需要将deflat.py和题目放在同一个目录下,安装angr时,并不会安装angr-managerment,所以还需要一个am_graph.py

am_graph.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
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
import itertools
from collections import defaultdict

import networkx

from angr.knowledge_plugins import Function


def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * n
return itertools.izip_longest(*args, fillvalue=fillvalue)


def to_supergraph(transition_graph):
"""
Convert transition graph of a function to a super transition graph. A super transition graph is a graph that looks
like IDA Pro's CFG, where calls to returning functions do not terminate basic blocks.
:param networkx.DiGraph transition_graph: The transition graph.
:return: A converted super transition graph
:rtype networkx.DiGraph
"""

# make a copy of the graph
transition_graph = networkx.DiGraph(transition_graph)

# remove all edges that transitions to outside
for src, dst, data in list(transition_graph.edges(data=True)):
if data['type'] == 'transition' and data.get('outside', False) is True:
transition_graph.remove_edge(src, dst)
if transition_graph.in_degree(dst) == 0:
transition_graph.remove_node(dst)

edges_to_shrink = set()

# Find all edges to remove in the super graph
for src in transition_graph.nodes():
edges = transition_graph[src]

# there are two types of edges we want to remove:
# - call or fakerets, since we do not want blocks to break at calls
# - boring jumps that directly transfer the control to the block immediately after the current block. this is
# usually caused by how VEX breaks down basic blocks, which happens very often in MIPS



if len(edges) == 1 and src.addr + src.size == next(iter(edges.keys())).addr:
dst = next(iter(edges.keys()))
dst_in_edges = transition_graph.in_edges(dst)
if len(dst_in_edges) == 1:
edges_to_shrink.add((src, dst))
continue

if any(iter('type' in data and data['type'] not in ('fake_return', 'call') for data in edges.values())):
continue

for dst, data in edges.items():
if isinstance(dst, Function):
continue
if 'type' in data and data['type'] == 'fake_return':
if all(iter('type' in data and data['type'] in ('fake_return', 'return_from_call')
for _, _, data in transition_graph.in_edges(dst, data=True))):
edges_to_shrink.add((src, dst))
break

# Create the super graph
super_graph = networkx.DiGraph()

supernodes_map = {}

function_nodes = set() # it will be traversed after all other nodes are added into the supergraph

for node in transition_graph.nodes():

if isinstance(node, Function):
function_nodes.add(node)
# don't put functions into the supergraph
continue

dests_and_data = transition_graph[node]

# make a super node
if node in supernodes_map:
src_supernode = supernodes_map[node]
else:
src_supernode = SuperCFGNode.from_cfgnode(node)
supernodes_map[node] = src_supernode
# insert it into the graph
super_graph.add_node(src_supernode)

if not dests_and_data:
# might be an isolated node
continue

for dst, data in dests_and_data.items():

edge = (node, dst)

if edge in edges_to_shrink:

if dst in supernodes_map:
dst_supernode = supernodes_map[dst]
else:
dst_supernode = None

src_supernode.insert_cfgnode(dst)

# update supernodes map
supernodes_map[dst] = src_supernode

# merge the other supernode
if dst_supernode is not None:
src_supernode.merge(dst_supernode)

for src in dst_supernode.cfg_nodes:
supernodes_map[src] = src_supernode

# link all out edges of dst_supernode to src_supernode
for dst_, data_ in super_graph[dst_supernode].items():
super_graph.add_edge(src_supernode, dst_, **data_)

# link all in edges of dst_supernode to src_supernode
for src_, _, data_ in super_graph.in_edges([dst_supernode], data=True):
super_graph.add_edge(src_, src_supernode, **data_)

if 'type' in data_ and data_['type'] == 'transition':
if not ('ins_addr' in data_ and 'stmt_idx' in data_):
# this is a hack to work around the issue in Function.normalize() where ins_addr and
# stmt_idx weren't properly set onto edges
continue
src_supernode.register_out_branch(data_['ins_addr'], data_['stmt_idx'], data_['type'],
dst_supernode.addr
)

super_graph.remove_node(dst_supernode)

else:
if isinstance(dst, Function):
# skip all functions
continue

# make a super node
if dst in supernodes_map:
dst_supernode = supernodes_map[dst]
else:
dst_supernode = SuperCFGNode.from_cfgnode(dst)
supernodes_map[dst] = dst_supernode

super_graph.add_edge(src_supernode, dst_supernode, **data)

if 'type' in data and data['type'] == 'transition':
if not ('ins_addr' in data and 'stmt_idx' in data):
# this is a hack to work around the issue in Function.normalize() where ins_addr and
# stmt_idx weren't properly set onto edges
continue
src_supernode.register_out_branch(data['ins_addr'], data['stmt_idx'], data['type'],
dst_supernode.addr
)

for node in function_nodes:
in_edges = transition_graph.in_edges(node, data=True)

for src, _, data in in_edges:
if not ('ins_addr' in data and 'stmt_idx' in data):
# this is a hack to work around the issue in Function.normalize() where ins_addr and
# stmt_idx weren't properly set onto edges
continue
supernode = supernodes_map[src]
supernode.register_out_branch(data['ins_addr'], data['stmt_idx'], data['type'], node.addr)

return super_graph


class OutBranch:
def __init__(self, ins_addr, stmt_idx, branch_type):
self.ins_addr = ins_addr
self.stmt_idx = stmt_idx
self.type = branch_type

self.targets = set()

def __repr__(self):
return "<OutBranch at %#x, type %s>" % (self.ins_addr, self.type)

def add_target(self, addr):
self.targets.add(addr)

def merge(self, other):
"""
Merge with the other OutBranch descriptor.
:param OutBranch other: The other item to merge with.
:return: None
"""

assert self.ins_addr == other.ins_addr
assert self.type == other.type

o = self.copy()
o.targets |= other.targets

return o

def copy(self):
o = OutBranch(self.ins_addr, self.stmt_idx, self.type)
o.targets = self.targets.copy()
return o

def __eq__(self, other):
if not isinstance(other, OutBranch):
return False

return self.ins_addr == other.ins_addr and \
self.stmt_idx == other.stmt_idx and \
self.type == other.type and \
self.targets == other.targets

def __hash__(self):
return hash((self.ins_addr, self.stmt_idx, self.type))


class SuperCFGNode(object):
def __init__(self, addr):
self.addr = addr

self.cfg_nodes = [ ]

self.out_branches = defaultdict(dict)

@property
def size(self):
return sum(node.size for node in self.cfg_nodes)

@classmethod
def from_cfgnode(cls, cfg_node):
s = cls(cfg_node.addr)

s.cfg_nodes.append(cfg_node)

return s

def insert_cfgnode(self, cfg_node):
# TODO: Make it binary search/insertion
for i, n in enumerate(self.cfg_nodes):
if cfg_node.addr < n.addr:
# insert before n
self.cfg_nodes.insert(i, cfg_node)
break
elif cfg_node.addr == n.addr:
break
else:
self.cfg_nodes.append(cfg_node)

# update addr
self.addr = self.cfg_nodes[0].addr

def register_out_branch(self, ins_addr, stmt_idx, branch_type, target_addr):
if ins_addr not in self.out_branches or stmt_idx not in self.out_branches[ins_addr]:
self.out_branches[ins_addr][stmt_idx] = OutBranch(ins_addr, stmt_idx, branch_type)

self.out_branches[ins_addr][stmt_idx].add_target(target_addr)

def merge(self, other):
"""
Merge another supernode into the current one.
:param SuperCFGNode other: The supernode to merge with.
:return: None
"""

for n in other.cfg_nodes:
self.insert_cfgnode(n)

for ins_addr, outs in other.out_branches.items():
if ins_addr in self.out_branches:
for stmt_idx, item in outs.items():
if stmt_idx in self.out_branches[ins_addr]:
self.out_branches[ins_addr][stmt_idx].merge(item)
else:
self.out_branches[ins_addr][stmt_idx] = item

else:
item = next(iter(outs.values()))
self.out_branches[ins_addr][item.stmt_idx] = item

def __repr__(self):
return "<SuperCFGNode %#08x, %d blocks, %d out branches>" % (self.addr, len(self.cfg_nodes),
len(self.out_branches)
)

def __hash__(self):
return hash(('supercfgnode', self.addr))

def __eq__(self, other):
if not isinstance(other, SuperCFGNode):
return False

return self.addr == other.addr

main函数的地址是0x400620
python3 deflat.py polyre 0x400620

1
2
3
Thriumph@ubuntu:~/re$ python3 deflat.py polyre 0x400620
...
Successful! The recovered file: polyre_recovered

生成了一个polyre_recovered文件

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
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
v10 = 0;
memset(s, 0, 0x30uLL);
memset(s1, 0, 0x30uLL);
printf("Input:", 0LL);
v11 = s;
if ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 )
goto LABEL_43;
while ( 1 )
{
__isoc99_scanf("%s", v11);
v6 = 0;
if ( dword_603058 < 10 || (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) == 0 )
break;
LABEL_43:
__isoc99_scanf("%s", v11);
}
while ( 1 )
{
do
v12 = v6;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
v13 = v12 < 64;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 )
;
if ( !v13 )
break;
v14 = s[v6];
do
v15 = v14;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
if ( v15 == 10 )
{
v16 = &s[v6];
*v16 = 0;
break;
}
v17 = v6 + 1;
do
v6 = v17;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
}
for ( i = 0; ; ++i )
{
do
v18 = i;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
do
v19 = v18 < 6; // 循环6次
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
if ( !v19 )
break;
do
v20 = s;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
v4 = *(_QWORD *)&v20[8 * i]; // 取8个字节
v7 = 0;
while ( 1 )
{
v21 = v7;
do
v22 = v21 < 64; // 循环64次
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
if ( !v22 )
break;
v23 = v4;
v24 = v4 < 0;
if ( v4 >= 0 ) // v4大于等于0
{
v27 = v4;
do
v28 = 2 * v27; // 乘2
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
v4 = v28;
}
else // v4小于0
{ // 乘2
v25 = 2 * v4;
do
v26 = v25;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
v4 = v26 ^ 0xB0004B7679FA26B3LL; // 异或0xB0004B7679FA26B3
}
v29 = v7;
do
v7 = v29 + 1;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
}
v30 = 8 * i;
v31 = &s1[8 * i];
if ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 )
LABEL_55:
*(_QWORD *)v31 = v4;
*(_QWORD *)v31 = v4;
if ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 )
goto LABEL_55;
v32 = i + 1;
}
do
v33 = memcmp(s1, &unk_402170, 0x30uLL); // 字符串做对比(48个字节)
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 );
v34 = v33 != 0;
while ( dword_603058 >= 10 && (((_BYTE)dword_603054 - 1) * (_BYTE)dword_603054 & 1) != 0 )
;
if ( v34 )
puts("Wrong!");
else
puts("Correct!");
return v10;
}

大概就是6个循环,每个循环取8个字节,然后循环64次,大于等于0就乘2,小于0乘2后和0xB0004B7679FA26B3进行异或,v4的数据类型int64位的,需要的数是uint64位的,当小于0时候会存在溢出,所以要加上一个中间值0x8000000000000000,当大于等于0的时候,乘2结果为偶数,当小于0是,乘2异或0xB0004B7679FA26B3是个奇数。异或的操作就是为了区分奇偶

把48个字节提取出来

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
#include<cstdio>
char encode[48] = { 0x96, 0x62, 0x53, 0x43, 0x6D, 0xF2, 0x8F, 0xBC, 0x16, 0xEE,0x30, 0x05, 0x78, 0x00, 0x01, 0x52, 0xEC, 0x08, 0x5F, 0x93,
0xEA, 0xB5, 0xC0, 0x4D, 0x50, 0xF4, 0x53, 0xD8, 0xAF, 0x90,
0x2B, 0x34, 0x81, 0x36, 0x2C, 0xAA, 0xBC, 0x0E, 0x25, 0x8B,
0xE4, 0x8A, 0xC6, 0xA2, 0x81, 0x9F, 0x75, 0x55 };
int main() {
long long* e = (long long*)encode;
for (size_t i = 0; i < 6; i++)
{
printf("%llxn", e[i]);
}
printf("n");
for (int i = 0; i < 6; i++) {
for (size_t j = 0; j < 64; j++)
{
if (!(e[i]&1) ) { e[i]= (unsigned long long) e[i]>> 1;}
else {
e[i] = ((unsigned long long)e[i] ^ 0xB0004B7679FA26B3) >> 1;
e[i] |= 0x8000000000000000;
}
}
}
for (size_t i = 0; i < 48; i++)
{
printf("%c", encode[i]);
}
}

参考:https://www.anquanke.com/post/id/188785#h3-22

相关文章
评论
分享
  • 网鼎杯部分wp

    pwnboom1分析远程已经打不通了,远程的偏移和本地的偏移不一样,只能复现一下本地的了。 首先看到流程图,代码量很大,有很大的switch语句和嵌套结构,可能是虚拟机或者是解析器。 从下图看出是一个C语言的解析器。 然后看了...

    网鼎杯部分wp
  • 数字中国创新大赛

    又是自闭的一天。。 game这一题是关于python字节码的题目,之前没有了解过,看了几篇关于python字节码的文章,死磕,手工还原。。 python字节码 12345678910111213141516171819202122...

    数字中国创新大赛
  • hitcontraining_uaf

    一道简单的uaf的题目 保护12345Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX...

    hitcontraining_uaf
Please check the parameter of comment in config.yml of hexo-theme-Annie!