我正在尝试执行一个非常简单的缓冲区溢出攻击。我对此几乎是个新手。所以,如果这个问题很愚蠢,请原谅我:-)
代码:
#include<stdio.h>
#include<stdlib.h>
int i, n;
void confused(int i)
{
printf("**Who called me? Why am I here?? *** %x\n ", i);
}
void shell_call(char *c)
{
printf(" ***Now calling \"%s\" shell command *** \n", c);
system(c);
}
void victim_func()
{
int a[4];
printf("Enter n: "); scanf("%d",&n);
printf("~~~~~~~~~~~~~ values and address of n locations ~~~~~~~~~~");
for (i = 0;i <n ;i++)
printf ("\n a[%d] = %x, address = %x", i, a[i], &a[i]);
printf("\nEnter %d HEX Values \n", n);
// Buffer Overflow vulnerability HERE!
for (i=0;i<n;i++) scanf("%x",&a[i]);
printf("Done reading junk numbers\n");
}
int main()
{
victim_func();
printf(“\n done”);
return 0;
}
当我使用objdump来获取函数地址时,我有以下内容:
main(): 0x804854d
Address of main() where printf() is called: 0x8048563
victim_func(): 0x8048455
confused(): 0x8048414
现在,我想要的是从victim_func()跳转到函数' confused()‘,方法是使那里的缓冲区溢出,并将返回地址重写到confused()的地址。并且我想从confused()返回到main中的printf()语句,然后正常退出。因此,我提供了以下输入
Enter n: 7
Enter 7 HEX values:
1
2
3
4
5
8048414 (This is to jump to confused)
8048563 (this is to jump to printf() in main)
尽管程序从该printf语句输出"Done“,但它跳回victim_func()并输出"Enter n:”
我做错了什么?任何帮助都将不胜感激!
附言:我不确定我是否把问题做对了。如果需要更多的信息,请让我知道。
发布于 2011-09-08 15:36:16
缓冲区溢出攻击要比这复杂得多。首先,您需要了解汇编程序才能执行此操作。在反汇编您想要作为目标的程序和函数之后,您需要在它执行该函数时确定堆栈布局。这是一个使用visual studio的缓冲区溢出的示例,但原理是相同的。
#include "stdafx.h"
#include <math.h>
volatile double test;
double function3()
{
test++;
return exp(test);
}
double function2()
{
return log(test);
}
double function1()
{
int a[5] = {0};
a[7] = (int)&function3;
return exp(function2());
}
int _tmain(int argc, _TCHAR* argv[])
{
double a = function1();
test = a;
return a;
}
多亏了反汇编,我们知道在函数保存堆栈帧指针之前,在function1中分配了一个。后面的值是返回地址,如果完成,function1将转到该地址。
00401090 55 push ebp <- we save the stack pointer
00401091 8B EC mov ebp,esp
00401093 83 EC 1C sub esp,1Ch <- save space to allocate a[5]
00401096 B8 CC CC CC CC mov eax,0CCCCCCCCh
0040109B 89 45 E4 mov dword ptr [ebp-1Ch],eax <- crt debug init a[5]
0040109E 89 45 E8 mov dword ptr [ebp-18h],eax
004010A1 89 45 EC mov dword ptr [ebp-14h],eax
004010A4 89 45 F0 mov dword ptr [ebp-10h],eax
004010A7 89 45 F4 mov dword ptr [ebp-0Ch],eax
004010AA 89 45 F8 mov dword ptr [ebp-8],eax
004010AD 89 45 FC mov dword ptr [ebp-4],eax
由此我们可以得出结论,如果我们用不同的地址覆盖a7,函数将不会返回到main,而是返回我们在a7中写入的任何地址。
希望这能有所帮助。
发布于 2011-09-08 15:39:02
您没有向我们展示带有ai地址的程序输出。我怀疑编译器正在做一些事情,比如将堆栈上的数据对齐到16。它可能比您预期的返回地址更远。
https://stackoverflow.com/questions/7344226
复制相似问题