Skip to content

Commit c7154b9

Browse files
authored
Merge pull request #54 from EarthCompass/master
Add EarthC's wp
2 parents a99b014 + b4e60e1 commit c7154b9

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
| [tkmk](players/tkmk/) | 总排名第 9 名 | 赛博厨房(LEVEL 3 非预期) |
9898
| [ReinWD](https://github.com/ReinWD/hackergame2021_writeup/blob/main/writeup.md) | 总排名 560 | 签到,进制十六——参上,去吧!追寻自由的电波,猫咪问答 Pro Max,赛博厨房(LEVEL 0、LEVEL 1、LEVEL2) |
9999
| [tl2cents](players/tl2cents/README.md) [博客](https://tl2cents.github.io/2021/10/31/Hackgame2021/) | 总排名第 70 | FLAG 助力大红包,图之上的信息,Easy RSA,马赛克,Minecraft,超 OI 的 Writeup 模拟器(前两问) |
100+
| [EarthC](players/EarthC/) | 总排名第 26 名 | 灯,等灯等灯, 超 OI 的 Writeup 模拟器(部分) |
100101

101102
## 其他资源
102103

players/EarthC/README.md

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# HackerGame 2021 WP
2+
3+
By [EarthC](https://github.com/EarthCompass)
4+
5+
## 灯,等灯等灯
6+
7+
### Level 0
8+
9+
GF(256) 上面解方程即可
10+
11+
### Level 1 / Level 2
12+
13+
一开始还在想怎么线性规划(?),后来突然意识到,这道题目跟[LWE](https://en.wikipedia.org/wiki/Learning_with_errors)不是一样一样的吗?
14+
15+
原题就成了一个GF(256)上的LWE问题,构造格求解CVP,代码参考https://gist.github.com/hakatashi/2266a5df35cc79de50b86d2419b33a6f
16+
17+
这里放一个解Level 2的代码, Level 1算法完全一致,只需要换一下数据。
18+
19+
```python
20+
blacklist = [(2,2),(3,2),(4,2),(4,3),(2,4),(3,4),(4,4),(4,7),(4,8),(3,8),(2,8),(2,9),(7,2),(7,3),(7,4),(8,3),(9,3),(7,7),(7,8),(7,9),(8,7),(9,7),(9,8),(9,9)]
21+
clicks = []
22+
mmap = [0]*144
23+
for i in range(12):
24+
for j in range(12):
25+
if (i,j) not in blacklist:
26+
tmp = var(f'x_{i}_{j}')
27+
clicks.append(tmp)
28+
mmap[12*i+j] = tmp
29+
30+
def get_click(x,y):
31+
if x>=0 and x<12 and y>=0 and y < 12 and (x,y) not in blacklist:
32+
return mmap[12*x+y]
33+
else:
34+
return 0
35+
36+
ftarget = [189,189,189,189,189,33,33,33,189,189,189,189,189,189,189,33,33,33,189,33,44,189,189,189,189,189,189,189,189,33,33,33,33,189,189,189,189,189,189,189,189,33,189,33,33,189,189,189,189,189,189,33,33,189,189,33,33,33,189,189,189,134,33,33,189,189,189,189,33,33,189,189,189,144,33,33,189,189,189,189,33,189,189,189,189,142,33,33,189,189,189,189,33,33,33,189,189,100,142,33,189,189,189,189,33,33,33,189,189,142,142,189,189,189,189,189,189,33,189,189,189,59,142,33,189,189,189,189,33,189,189,189,189,189,33,33,189,189,189,189,189,189,189,189]
37+
mm = []
38+
for i in range(12):
39+
for j in range(12):
40+
data = 0
41+
data += 31*get_click(i,j)
42+
data += 63*get_click(i-1,j)
43+
data += 63*get_click(i+1,j)
44+
data += 63*get_click(i,j-1)
45+
data += 63*get_click(i,j+1)
46+
47+
data += 63*get_click(i+1,j+1)
48+
data += 63*get_click(i+1,j-1)
49+
data += 63*get_click(i-1,j+1)
50+
data += 63*get_click(i-1,j-1)
51+
52+
data += 127*get_click(i-2,j-2)
53+
data += 127*get_click(i-2,j-1)
54+
data += 127*get_click(i-2,j)
55+
data += 127*get_click(i-2,j+1)
56+
data += 127*get_click(i-2,j+2)
57+
data += 127*get_click(i-1,j+2)
58+
data += 127*get_click(i,j+2)
59+
data += 127*get_click(i+1,j+2)
60+
data += 127*get_click(i+2,j+2)
61+
data += 127*get_click(i+2,j+1)
62+
data += 127*get_click(i+2,j)
63+
data += 127*get_click(i+2,j-1)
64+
data += 127*get_click(i+2,j-2)
65+
data += 127*get_click(i+1,j-2)
66+
data += 127*get_click(i,j-2)
67+
data += 127*get_click(i-1,j-2)
68+
69+
coeff = []
70+
for c in clicks:
71+
coeff.append(data.coefficient(c))
72+
mm.append(coeff)
73+
from sage.modules.free_module_integer import IntegerLattice
74+
75+
# Babai's Nearest Plane algorithm
76+
# from: http://mslc.ctf.su/wp/plaidctf-2016-sexec-crypto-300/
77+
def Babai_closest_vector(M, G, target):
78+
small = target
79+
for _ in range(1):
80+
for i in reversed(range(M.nrows())):
81+
c = ((small * G[i]) / (G[i] * G[i])).round()
82+
small -= M[i] * c
83+
return target - small
84+
85+
n = 144-len(blacklist)
86+
m = 144
87+
q = 256
88+
89+
b_values = ftarget
90+
A_values = mm
91+
92+
A = matrix(ZZ, m + n, m)
93+
for i in range(m):
94+
A[i, i] = q
95+
for x in range(m):
96+
for y in range(n):
97+
A[m + y, x] = A_values[x][y]
98+
lattice = IntegerLattice(A, lll_reduce=True)
99+
print("LLL done")
100+
gram = lattice.reduced_basis.gram_schmidt()[0]
101+
target = vector(ZZ, b_values)
102+
res = Babai_closest_vector(lattice.reduced_basis, gram, target)
103+
print("Closest Vector: {}".format(res))
104+
105+
R = IntegerModRing(q)
106+
M = Matrix(R, A_values)
107+
ingredients = M.solve_right(res)
108+
109+
print("Ingredients: {}".format(ingredients))
110+
print("ok")
111+
cnt = 0
112+
ans = mmap.copy()
113+
for i in range(144):
114+
if ans[i] != 0:
115+
ans[i] = ingredients[cnt]
116+
cnt+=1
117+
print(ans)
118+
119+
```
120+
121+
## 超 OI 的 Writeup 模拟器
122+
123+
### 果然还是逆向比较简单 / 这次没人两小时手做吧
124+
125+
angr全自动
126+
127+
```python
128+
def solve(level):
129+
p = angr.Project(f'challs/{level}.bin',auto_load_libs=False)
130+
131+
cfg = p.analyses.CFGFast()
132+
print(p)
133+
134+
target_addr = 0
135+
for addr,b in cfg.kb.functions.items():
136+
if b.returning and len(list(b.blocks)) == 2 and b.has_return:
137+
print("*"*30)
138+
139+
target_addr = addr
140+
141+
a = claripy.BVS('r0',64)
142+
b = claripy.BVS('r1',64)
143+
add_opt = {
144+
# angr.options.ZERO_FILL_UNCONSTRAINED_REGISTERS,
145+
# angr.options.ZERO_FILL_UNCONSTRAINED_MEMORY,
146+
# angr.options.LAZY_SOLVES,
147+
# angr.options.UNICORN,
148+
# angr.options.DOWNSIZE_Z3,
149+
# angr.options.SIMPLIFY_CONSTRAINTS
150+
}
151+
rm_opt = {
152+
angr.options.SIMPLIFY_MEMORY_WRITES,
153+
angr.options.SIMPLIFY_REGISTER_WRITES,
154+
}
155+
state = p.factory.call_state(target_addr,a,b,add_options=add_opt,remove_options=rm_opt)
156+
157+
158+
simgr = p.factory.simgr(state)
159+
start_time = time.time()
160+
simgr.explore(find=target_addr+33)
161+
print(f"Done in {time.time()-start_time}")
162+
163+
end = simgr.found[0]
164+
165+
print("Solving。。")
166+
try:
167+
s = b''
168+
s+=end.solver.eval(a).to_bytes(8,byteorder='little')
169+
s+=end.solver.eval(b).to_bytes(8,byteorder='little')
170+
171+
print(level,s.decode())
172+
173+
except Exception as e:
174+
print("error",level)
175+
print(e)
176+
```
177+
178+
### 什么叫无情的逆向机器
179+
180+
angr的默认选项会在执行途中尝试化简表达式,导致大部分样本执行时间爆炸,可以手动关闭相关选项加速符号执行(虽然表达式会巨复杂)。
181+
182+
但是z3只能解出少部分题目,剩下的大多数bin会unsat无解。
183+
184+
过于复杂求解卡住还可以理解,但是不知道为啥会unsat,希望大佬教教。
185+

0 commit comments

Comments
 (0)