Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
1 Overview
The primary learning objective of this assignment is to provide you with firsthand experience in exploiting buffer overflow vulnerabilities and format string issues. Additionally, it aims to deepen your understanding of how operating system countermeasures function in response to these security challenges. All tasks in this assignment can be done on Ubuntu VM as used in the labs. Please refer to Section 2 for submission notes.
2 Submission
You need to submit a lab report (one single PDF file) to describe what you have done and what you have observed with screenshots whenever necessary. In your report, you need to answer all the questions listed in this manual. Please answer each question using at most 200 words. Typeset your report into .pdf format (make sure it can be opened with Adobe Reader) and name it as the format: [Your Name]-[Student ID]- FIT5003-Assignment.pdf.
All source code, if required, should be embedded in your report. Additionally, if a demonstration video is needed, record your screen demonstration along with a voice explanation and upload the video to your Monash Google Drive or any online service that allows video sharing. Include the shared URL of the video in your report. You can use this free tool to make the video:https://monash-panopto.aarnet.edu.au/ ; other tools, such as Zoom, are also fine.
Important notes and penalties:
? It is the student’s responsibility to ensure that the video content is accessible to the teaching staff. Failure to provide accessible video will result in the relevant task being assessed without consideration of the video.
? A part of the submitted video (at a corner) must clearly show your face at all times. Penalties may apply when that’s not the case.
? Late submissions incur a 5-marks deduction per day. For example, if you submit 2 days and 1 hour late, that incurs 15-marks deduction. Submissions more than 7 days late will receive a zero mark.
? If you require extension or special consideration, refer to https://www.monash.edu/students/ admin/assessments/extensions-special-consideration. No teaching team mem-ber is allowed to give you extension or special consideration, so please do not reach out to a teaching team member about this. Follow the guidelines in the aforementioned link.
? The maximum allowed duration for the recorded video is 15 mins. Therefore, only the first 15:00 mins of your submitted video will be marked. Any exceeding video components will be ignored.
? If your device does not have a camera (or for whatever reason you can’t use your device), you can borrow a device from Monash Connect or Library. It’s your responsibility to plan ahead for this. Monash Connect or Library not having available devices for loan at a particular point in time is not a valid excuse.
? You can create multiple video parts at different times, and combine and submit a single video at the end. Make sure that the final video is clear and understandable.
? If any task requires installing new software, you are allowed to do that in advance of recording your video. You do not need to demonstrate software installation in the video.
? You can do (online) research in advance, take notes and make use of them during your video recording. You may also prepare exploit scripts in advance. But you cannot simply copy-paste commands to carry out the tasks without any explanations. Explanations (of what the code does) while completing the tasks are particularly important.
? During video recording, demonstrations of tasks should be performed live. For example, you cannot exploit a buffer overflow in advance and then simply walk through the results on screen. However, you may prepare the commands beforehand and execute them during the demonstration.
? Zero tolerance on plagiarism and academic integrity violations: If you are found cheating, penalties will apply, e.g., a zero grade for the unit. The demonstration video is also used to detect/avoid plagia-rism. University policies can be found at https://www.monash.edu/students/academic/ policies/academic-integrity.
3 Buffer Overflow Vulnerability [65 Marks]
You will be given a program deliberately containing a buffer-overflow vulnerability. Your objective is to de- vise a strategy to exploit this vulnerability and send a remote access to an attacker. Furthermore, beyond ex- ecuting the attacks, you will be guided through an examination of various protection schemes implemented in the operating system designed to thwart buffer overflow vulnerabilities. Your task involves assessing the efficacy of these countermeasures and providing explanations for their success or failure.
3.1 Initial setup
You can execute the tasks using the Ubuntu VM used in the labs. Ubuntu and other Linux distributions have implemented several security mechanisms to make the buffer-overflow attack difficult. To simplify our attacks, we need to disable them first.
Address Space Randomisation. Ubuntu and several other Linux-based systems uses address space ran- domisation to randomise the starting address of heap and stack. This makes guessing the exact addresses difficult; guessing addresses is one of the critical steps of buffer-overflow attacks. In this part, we disable these features using the following commands:
$ sudo sysctl -w kernel .randomize_va_space=0
The StackGuard Protection Scheme. The GCC compiler implements a security mechanism called “Stack Guard” to prevent buffer overflows. In the presence of this protection, buffer overflow will not work. You can disable this protection if you compile the program using the -fno-stack-protector switch. For example, to compile a program example.c with Stack Guard disabled, you may use the following command:
$ gcc -m32 -fno-stack-protector example .c
Non-Executable Stack. Ubuntu used to allow executable stacks, but this has now changed: the binary images of programs (and shared libraries) must declare whether they require executable stacks or not, i.e., they need to mark a field in the program header. Kernel or dynamic linker uses this marking to decide whether to make the stack of this running program executable or non-executable. This marking is done automatically by the recent versions of gcc, and by default, the stack is set to be non-executable. To change that, use the following option when compiling programs:
For executable stack:
$ gcc -m32 -z execstack -o test test .c
For non-executable stack:
$ gcc -m32 -z noexecstack -o test test .c
3.2 The Vulnerable Program
/ * stack .c */
/ * This program has a buffer overflow vulnerability . */ / * Our task is to exploit this vulnerability */
#include
int bof(char *str,int studentID) {
int bufSize; int a = 12;
int b = 18;
bufSize = 12 + studentID%32; char buffer[bufSize];
strcpy(buffer, str); return 1;
}
int main(int argc, char **argv[]) {
if(argc < 2) {
printf("Usage: %s \n", argv[0]); exit(0);
}
int studentID = ; //ENTER YOUR STUDENT ID HERE bof(argv[1],studentID);
printf("Returned Properly\n"); return 1;
}
You need to enter your student ID to the variable studentId. Then, compile the above vulnerable program and make it set-root-uid. You can achieve this by compiling it in the root account and chmod the executable to 4755 (don’t forget to include the execstack and -fno-stack-protector options to turn off the non-executable stack and StackGuard protections):
$ su root
Password (enter your password)
# gcc -m32 -g -o stack -z execstack -fno-stack-protector stack .c
# chmod 4755 stack # exit
The above program has a buffer overflow vulnerability. It takes input from the terminal which is under
user’s control.
3.3 Task 1: Exploiting the Vulnerability [35 Marks]
The objective of this task is to exploit buffer overflow vulnerability in the above provided code (stack.c) and receive a reverse-shell. You need to read Appendix A.1 to investigate how to create a reverse-shell code. Then, you also need to study how to simulate an attacker, who is listening at a specific address/port and waiting for the shell. We refer you to Appendix A.2 for this simulation.
You will need to generate a reverse-shell code using msfvenom and create a payload. Then run the vulnerable program stack with your payload. If your exploit is implemented correctly, the attacker should be able to get the reverse shell.
You will be overflowing the buffer in stack .c, which is compiled with the Stack Guard protection disabled.
$ . /stack {PAYLOAD} // launch the attack by running the vulnerable program
If the attacker obtains the shell successfully, her terminal should be as follows (assuming that she is listening at the port 4444, and the program stack is running at the address 10.0.2.15).
$$ nc -lvp 4444 // listening at the port 4444 Listening on [ 0.0.0.0 ] (family 0, port 4444)
Connection from [ 10.0.2.15 ] port 4444 [tcp/ *] accepted
Please note that, ideally, the attack should work outside the debugger. However, making it work inside GDB is also acceptable. The reason it may not work outside GDB is explained here.
Once the attacker obtains the shell, she can remotely manipulate all the current files where the program stack runs.
Q1 (35 marks): Provide your video demonstration evidence to support and verify that you have performed the attack and it worked successfully. You need to embed the vidoe link to your report so that the teaching team can view and verify your works. In the video, you need to demonstrate following key points:
? The buffer overflow happens and the attacker receives the shell when the victim executes the vulnerable program stack. (10 marks if the attack works during your demonstration video)
? Debug the program stack to investigate the return memory address and local variables (high-light them in your video demo) in the function bof(). (15 marks for the debug demonstra-tion and memory analysis)
? Explain clearly the payload used for exploiting the buffer, i.e. how many NOPs were used and why, how the return address was selected etc..(10 marks for your explanation during the demonstration video)
Please note that providing incorrect student ID will result 0 mark for this task. The full marks only given if you have solid explanation with supporting memory address analysis.
3.4 Task 2: Address Randomisation [5 Marks]
Now, we turn on the Ubuntu’s address randomisation. We run the same attack developed in the above task.
Can you get a shell? If not, what is the problem? How does the address randomisation make your attacks difficult? You can use the following instructions to turn on the address randomisation:
$ sudo /sbin/sysctl -w kernel .randomize_va_space=2
If running the vulnerable code once does not get you the root shell, how about running it for many times? You can run . /stack in the following loop , and see what will happen. If your exploit program is designed properly, you should be able to get the root shell after a while. You can modify your exploit program to increase the probability of success (i.e., reduce the time that you have to wait).
$ sh -c "while [ 1 ]; do . /stack {PAYLOAD}; done;"
Q2 (5 marks): Follow the above steps, and answer the highlight questions. You should describe your observation and explanation briefly. Furthermore, try whether you can obtain root shell again. [Marking scheme: 2 marks for the screenshot and 3 marks for the explanation and solutions].
3.5 Task 3: Stack Guard [20 Marks]
Before working on this task, remember to turn off the address randomisation first, or you will not know
which protection helps achieve the protection.
In our previous tasks, we disabled the “Stack Guard” protection mechanism in GCC when compiling the programs. In this task, you may consider repeating the above task in the presence of Stack Guard. To do that, you should compile the program without the -fno-stack-protector’ option. For this task, you will recompile the vulnerable program, stack.c, to use GCC’s Stack Guard, execute the stack program again, and report your observations. You may report any error messages you observe.
In the GCC 4.3.3 and newer versions, Stack Guard is enabled by default. Therefore, you have to disable Stack Guard using the switch mentioned before. In earlier versions, it was disabled by default. If you use an older GCC version, you may not have to disable Stack Guard.