pwnable之codemap

来源:互联网 发布:delphi登陆淘宝联盟 编辑:程序博客网 时间:2024/06/01 08:20

pwnable之codemap

题目

made by YourButterfly

I have a binary that has a lot information inside heap.How fast can you reverse-engineer this?(hint: see the information inside EAX,EBX when 0x403E65 is executed)download: http://pwnable.kr/bin/codemap.exessh codemap@pwnable.kr -p2222 (pw:guest)

题目大意是在codemap这个程序,运行时堆中有大量信息,并且提示我们在程序运行至0x403e65时观察eax,ebx。

ssh连上远程服务器,有三个文件,分别是codemap.c,codemap.exe,readme。

readme提示我们逆向 codemap.exe。然后连上本地的 9021端口,守护进程会询问第二大和第三大的chunk内容是什么

codemap@ubuntu:~$ lscodemap.c  codemap.exe  readmecodemap@ubuntu:~$ cat readmereverse engineer the 'codemap.exe' binary, then connect to codemap daemon(nc 0 9021),the daemon will ask you some question, provide the correct answer to get flag.codemap@ubuntu:~$ nc 0 9021What is the string inside 2nd biggest chunk? :aaWait for 10 seconds to prevent brute-forcing...aaWhat is the string inside 3rd biggest chunk? :Wait for 10 seconds to prevent brute-forcing...Nah, wrongcodemap@ubuntu:~$ 

ida分析。

ida分析codemap.exe

在0x403E65下断点,debugger->start process,此时eax的值为0xAD4E,EBX指向的是“FItZrTIUzoAHpx”,

这里写图片描述

下面这一部分主要是在准备当前chunks的大小,

这里写图片描述

在申请空间后通过rand()%62的方式从字符串
“abcdefghijklmnopqrstubwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890”
中获取字符,共15次,也就是获得的串长度为15。接下来就是题目提示的部分,eax,ebx分别对应的是chunks大小,和内容。这里记录值比当前eax大就更新记录的chunks大小,和内容,这在这个1000次的循环里意味着寻找最大chunks的大小和内容。

这里写图片描述

需要的是第二大的chunks和第三大的chunks的内容。
人工没办法再1000次循环中找,只能是让程序自己跑了。可以用ida的idc或idapthon脚本跑。当时不会。临时看了下ida权威指南,也从网上搜了一下。

#coding:utf-8import idcfrom idaapi import *max_eax = 0second_eax = 0third_eax = 0max_ebx = 0second_ebx = 0third_ebx = 0ft=0sd=0td=0#AddBpt(0x263E65).text:00403E65(在这一句下断点)jbe     short loc_403E6D#在题目提示的地方前下一个断点。StartDebugger("","","")#启动具有默认参数的调试器for count in xrange(999):     code = GetDebuggerEvent(WFNE_SUSP|WFNE_CONT, -1) # 恢复执行,等待断点    eax = GetRegValue("EAX")    ebx = GetRegValue("EBX")    if max_eax < eax :        td=sd        sd=ft        ft=count        third_eax = second_eax        third_ebx = second_ebx        second_eax = max_eax        second_ebx = max_ebx        max_eax = eax;          max_ebx = ebx;      elif second_eax < eax :        td=sd        sd=count        third_eax = second_eax        third_ebx = second_ebx        second_eax = eax        second_ebx = ebx    elif third_eax < eax:        td=count        third_eax = eax        third_ebx = ebxMessage("max eax: %d, ebx: %x, count %d, second eax: %d, ebx: %x, count %d, third eax: %d, ebx: %x, count %d\n" % (max_eax, max_ebx, ft, second_eax, second_ebx, sd, third_eax, third_ebx, td))

虽说chunks的大小和内容是随机的,但srand(),rand()并不是真的随机,每次程序运行seed值都是一样的,实际上是伪随机,每次跑出的结果都是一样的。运行结果每次第一二三大的chunks其count值,大小,内容都相同,也说明了这一点。

参考链接
https://hex-rays.com/products/ida/debugger/scriptable.shtml
http://blog.csdn.net/qq_19550513/article/details/72846279