[逆向,栈溢出]《0day安全:软件漏洞分析技术》2.3.2控制程序的执行流程 复现
记录一下。
程序使用dev-cpp编译运行,注意要修改为32位debug,安装的ollydbg只能调试32位的应用程序。
C源代码,来自书上。多了两个头文件string.h和stdlib.h。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define PASSWORD "1234567"
int verify_password(char *password)
{
int authenticated;
char buffer[8];
authenticated = strcmp(password, PASSWORD);
strcpy(buffer,password);
return authenticated;
}
int main()
{
int valid_flag = 0;
char password[1024];
FILE *fp;
if(!(fp=fopen("password.txt","rw+")))
{
exit(0);
}
fscanf(fp,"%s", password);
valid_flag = verify_password(password);
if(valid_flag)
{
printf("incorrect password!\n");
}
else
{
printf("Congratulation!\n");
}
return 0;
}
在同目录下新建一个password.txt文件。
将生成的exe文件拖到ollydbg中调试。
找到verify_password函数的汇编指令,在函数的开始按F2设置断点。
如图所示,函数的起始地址是00401500。
找到调用verify_password函数的代码。
程序在004015A6处调用了verify_password函数,返回地址是004015AB,我们的目的是为了栈中的这个返回地址,将其修改为验证成功处的地址,也就是004015CA。
这个时候运行程序,运行到verify_password的起始处,触发断点,然后按F8单步运行。
strcpy的两个参数分别是EBP+8和EBP-14,EBP+8处存放的是verify_password的参数,现在看看栈的内容。
参数;
返回地址;
old EBP;
还有SUB ESP, 28开辟的栈空间。
buffer在EBP-14处,我们需要覆盖返回地址,需要计算buffer到返回地址的长度,函数刚开始PUSH EBP,然后MOV EBP,ESP。
也就是EBP指向了old EBP,那么返回地址的地址就是EBP+4,而buffer的地址是EBP-14,两者相差18,也就是24,在password.txt文件中,前24个字符填写4321,重复6次,然后填写验证成功处的地址,就是004015CA,注意要逆序。
从栈帧中也可以看出:
EBP的值是0028FA98,EBP+4保存着返回地址,也就是004015AB。
使用notepad++的插件HexEditor修改password.txt:
继续运行程序,或者在cmd中运行程序,可以看到程序打印了Congratulation!
补充:
1,有很多十六进制编辑器,notepad++需要下载插件HexEditor,然后放到安装目录中的plugins中。
2,编译程序的时候选择debug,32位,当然也可以选择64位,可以使用gdb或x64dbg调试,ollydbg目前所知只能调试32位程序。