avatar

pwnable.kr-coin1-二分法

分析

连上去发现是玩一个找假币的游戏,真币重10,假币重9,可以使用天平秤重,会随机给出币的数量与可以称重的次数,次数用完后输入假币的编号即可,值得注意的是编号是从0开始的,需要进行100次,然后只有60s的时间,所以我们必须使用脚本

1
2
3
4
5
6
7
8
- Example -
[Server] N=4 C=2 # find counterfeit among 4 coins with 2 trial
[Client] 0 1 # weigh first and second coin
[Server] 20 # scale result : 20
[Client] 3 # weigh fourth coin
[Server] 10 # scale result : 10
[Client] 2 # counterfeit coin is third!
[Server] Correct!

解题思路很容易想到二分法,我们只要挨个平分称重就可以定位出硬币了

不过由于网络延迟的原因,题目提示我们可以通过其他题目连上去,然后在本地弄

exp

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
from pwn import *
import re

def solve(start, end):
global p,c
mid = (start + end) // 2
full = (end - start) // 2 * 10
s = str.encode(' '.join(map(lambda x: str(x), range(start, mid))))
p.sendline(s)
r = p.recvline()
real = int(r)
c -= 1
if c >= 0:
if real < full:
return start if mid - start == 1 else solve(start, mid)
else:
return mid if end - mid == 1 else solve(mid, end)



def worker():
global p,c
r = str(p.recv())
pattern = re.compile(r'N=(\d+) C=(\d+)')
n,c = list(map(lambda x: int(x),pattern.match(r).groups()))
result = solve(0,n)
while c>=0:
p.sendline(str(result))
r = p.recvline()
c -= 1


p = remote('pwnable.kr', 9007)
p.recvuntil("Ready? starting in 3 sec... -\n\t\n")
for i in range(100):
print("round {}".format(i))
worker()
flag = p.recv()
print(flag)

发现其实已经有不少了,所以参考着写了一份

文章作者: 0bs3rver
文章链接: http://yoursite.com/2021/04/25/pwnable-kr-coin1-%E4%BA%8C%E5%88%86%E6%B3%95/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 0bs3rver的小屋