本文主要是介绍pwnable 笔记 Toddler's Bottle - coin1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这题考察算法 (分治法) 通过二分法查找便可以解出
题目比较坑的一点是要求在30秒内完成,如果去nc pwnable.kr 9007的话速度会非常慢,一般猜到第四十多次就超时了,为解决这个问题,需要把脚本放在pwnable服务器上去执行
$ ssh fd@pwnable.kr -p2222
password: guest
$ cd /tmp
$ vim a.py
$ python a.py
解题脚本:(好久没编程了,写个二分法手都生了...)
#!/usr/bin/python
__author__ = "TaQini"from pwn import *
import redef getNC():r = target.readline() #number and changesNC = re.findall("[0-9]+",r)return int(NC[0]), int(NC[1])def guess(start, end):coin = ""for i in xrange(start, end+1):coin += str(i) + " "# print "coin " + cointarget.sendline(coin)weight = target.read()# print "weight " + str(weight)return weightdef binsearch():for i in range(100):N, C = getNC()cnt = 0# print "N= " + str(N) + " C=" + str(C)Left = 0Right = N - 1while (Left <= Right):Mid = (Left + Right)/2# print "guess " + str(Left) + "-" + str(Mid)cnt += 1if cnt > C:# print "Hit!"weight = guess(Left,Mid)breakelse:weight = guess(Left,Mid)# print "trial= " + str(cnt)# print "and C= " + str(C)if (eval(weight) + 1) % 10: # fake coin not hereLeft = Mid + 1else:Right = Midprint "hit!",(i),target = remote("127.0.0.1",9007)
target.read() #rule of game
binsearch()
print target.read()
这篇关于pwnable 笔记 Toddler's Bottle - coin1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!