First, the experimental description
A buffer overflow is a scenario in which a program attempts to write to a buffer beyond the pre-allocated fixed-length data. This vulnerability could be exploited by malicious users to alter the flow control of a program, or even execute arbitrary fragments of code. This vulnerability occurs because of a temporary shutdown of the data buffer and the return address, which causes the return address to be rewritten.
II. Preparation of the experiment
System User name Shiyanlou, password Shiyanlou
The lab building provides 64-bit Ubuntu Linux, and in this experiment we need to operate in 32-bit environments to facilitate the observation of assembly statements, so we need to do some preparation before the experiment.
sudo apt-get update
sudo apt-get install lib32z1 libc6-dev-i386
sudo apt-get install Lib32readline-gplv2-dev
2. 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 the tab completion, so enter "/bin/bash" Using bash:
3.1 Initial Setup
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
I wonder if this has any effect.
3.3 Vulnerability Procedures
Save the following code as a "stack.c" file and save it to the/tmp directory. The code is as follows:
/* 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>IntBoF(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 ( "R"); Fread (str, sizeof (char), 517, Badfile); BOF (str); Span class= "hljs-built_in" >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
。。。 Unfortunately the experiment failed ... have tried to do it again
Buffer Overflow Vulnerability Experiment