简体   繁体   English

C中的堆栈溢出漏洞利用

[英]Stack Overflow Exploit in C

The question is actually about stack overflows in C. I have an assigment that I can not get done for the life of me, I've looked at everything in the gdb and I just cant figure it. 问题实际上是关于C中的堆栈溢出。我有一个不能完成我的生活的分配,我已经查看了gdb中的所有内容,我只是无法想象它。

The question is the following: 问题如下:

int i,n;

void confused()
{
    printf("who called me");
    exit(0);
}

void shell_call(char *c)
{
    printf(" ***Now calling \"%s\" shell command *** \n",c);
    system(c);
    exit(0);
}

void victim_func()
{
    int a[4];
    printf("[8]:%x\n", &a[8]);
    printf("Enter n: "); scanf("%d",&n);
    printf("Enter %d HEX Values \n",n);
    for(i=0;i<n;i++) scanf("%x",&a[i]);
    printf("Done reading junk numbers\n");
}

int main()
{
    printf("ls=736c --- ps = 7370 --- cal = 6c6163\n");
    printf("location of confused %x \n", confused);
    printf("location of shell_call %x \n", shell_call);
    victim_func();
    printf("Done, thank you\n");

}

Ok, so I managed to get the first question correctly, which is to arbitrarily call one of the two functions not explicitly called in the main path. 好的,所以我设法正确地得到了第一个问题,即任意调用主路径中未明确调用的两个函数之一。 By the way, this has to be done while running the program without any modifications. 顺便说一下,这必须在运行程序时完成而不做任何修改。 I did this by running the program, setting N to 7 , which gets me to the Function Pointer of the victim_func frame, I write a[7] with the memory address of confused or shell_call , and it works. 我这样做是通过运行程序,将N设置为7 ,这使我进入victim_func帧的函数指针,我写a[7]与内存地址confused或shell_call ,它的工作原理。 (I have a 64 bit machine, thats why I have to get it to 7, since the EBI pointer is 2 ints wide, instead of 1) (我有一台64位机器,这就是为什么我必须把它变为7,因为EBI指针是2英寸宽,而不是1)

My question is the following, how could I control which argument gets passed to the shell_code funcion? 我的问题如下,我怎样才能控制哪个参数传递给shell_code函数? ie. 即。 how do I write a string to char* c . 如何将string写入char* c The whole point is executing unix commands like ps etc, by running only the program. 整点是通过仅运行程序来执行像ps等的unix命令。

I figured writing the EBI pointer with the hex representation of ps and setting the arg list of shell_call to that, but that didn't work. 我想用写PS的十六进制表示的EBI指针和设置的ARG列表shell_call到这一点,但没有奏效。 I also tried inputing argsv arguments and setting the arg list of shell_call to the arg_list of main, but didn't work either. 我也尝试输入argsv参数并将argsv的arg列表设置为shell_callarg_list ,但也没有用。

I think the second version should work, but I believe I'm not setting the arg list of the new stack frame correctly ( I did it by writing a[8] to 0 , since its the first part of the function pointer, and writing a[9]=736c and a[10]=0000 , but its probably not right since those are the parameters of victim_func . So how do I access the parameters of shell_call ? 我认为第二个版本应该工作,但我相信我没有正确设置新堆栈帧的arg列表(我通过将a[8]写入0 ,因为它是函数指针的第一部分,并且写入a[9]=736ca[10]=0000 ,但它可能不对,因为那些是victim_func的参数。那么如何访问shell_call的参数?

I probably shouldn't do your homework for you. 我可能不应该为你做功课。 But the basically: 但基本上:

You need to get a character buffer somewhere in memory to store the string you want to execute. 您需要在内存中的某处获取字符缓冲区来存储您要执行的字符串。 Obviously, you can do this the same way you are getting the other functions called (ie you put the text on the stack as well). 显然,您可以像调用其他函数一样(即将文本也放在堆栈中)。 After you have that written, you need to write a pointer to it on to the stack in the location that the shell_code function expects to find its arguments. 编写完成后,需要在shell_code函数期望找到其参数的位置写入指向堆栈的指针。

The best way to figure this out without me doing all of the work for you is to write down your stack/memory contents on a piece of paper/whiteboard. 在没有我完成所有工作的情况下解决这个问题的最好方法是在一张纸/白板上写下你的堆栈/内存内容。 Write down how it would look if you called shell_code normally from inside the program. 如果从程序内部正常调用shell_code,请记下它的外观。 Then write down what the stack looks like inside victum_func and figure out which things to change to get it to look like it would look "naturally" (of course keeping in mind some things are "don't cares" like the return address). 然后在victum_func中写下堆栈的样子,找出要改变的东西,让它看起来像“自然”一样(当然要记住一些东西“不关心”,如返回地址)。

That's all the charity you're gonna get from me today! 这就是你今天要从我这里得到的所有慈善事业! :-P :-P

SoapBox already did a great job of leading you in the right direction. SoapBox已经做了很好的工作,引领你朝着正确的方向前进。

For more information; 欲获得更多信息; http://www.skullsecurity.org/wiki/index.php/Example_4 http://www.skullsecurity.org/wiki/index.php/Example_4

You need to manipulate the stack-frame of the caller ( main() ), and arrange it in such a way that returning to shell_call() from the epilog of the overflowed victim_func() the latter could find a settled stack as it was been called by the main. 你需要操作调用者(的堆栈帧main()并以这样的方式返回来安排它shell_call()从溢出的收尾victim_func()后者可以找到一个解决堆栈,因为它是被主要叫做。

In doing so you probably have to mangle the frame-pointer in the stackframe of the victim, that will be restored in %ebp by means of leave . 这样做你可能不得不在受害者的堆栈框架中破坏框架指针,这将通过leave在%ebp中恢复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM