This project will introduce you to control-flow hijacking vulnerabilities in application software, including buffer overflows.
- Be able to identify and avoid buffer overflow vulnerabilities in native code.
- Understand the severity of buffer overflows and the necessity of standard defenses.
- Gain familiarity with machine architecture and assembly language.
NOTCIE: This repository is the assignment in NCTU course "Network Security 2018". If you are taking this course, please do not duplicate from this repository. All rights reserved.
This project will introduce you to control-flow hijacking vulnerabilities in application software, including buffer overflows. We have provided source code, binary executable, and the assembly code. Try to find vulnerability in these files and exploit it to get the secret flag. There are two flags: flag1 & flag2. If you get flag1, you will score 100, flag2 is just the bonus flag. Have fun!
- Before starting, we should try to use the serve and observe vulnerabilities
# Connect to the server 140.113.194.66:8787 $ nc 140.113.194.66 8787 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice:- In the program
proj3.c, the main function will call functionfunc.- In the function
func, the variableret_addrwill store the return address of its function. This could be a key point to attack this program. - In the function
func, we can see there is a switch-case a while loop and the third of case is going to compareret_addrwith variablesmagic1andmagic2to decide which function to return.
- In the function
- Observe with the program
proj3.asm.- The variable
ret_addrin theproj3.cis store atebp-0x88(line 688). - Above the declaration of
ret_addrin theproj3.c, there is a variablesecretwith struct typeSEC, which size is 32 bytes due to the operation system is 32-bits in the memory stack. - To explain the above mentioned, the struct of
SECinclude an arrayinfowith 32 elements which is 32 Bytes. After converting to hexadecimal-based, the size will become0x20which is mapped toebp-0x64(line 702). - After the array info in the struct
SEC, there is a 4 bytes unsigned long variable calledsecretwhich is a random number. The only way to change the content ofsecretis in the while loop which is in the function func.
- The variable
- In the program
proj3.c, the functionedit_notewith a pointersas parameter which typeSTUis a struct includes variablename(16 bytes),note(16 bytes),stuid(16 bytes), andage(4 bytes). The total sizes are 44 bytes.- In the program
proj3.asm, we can observe that the memory stack pushes 88 bytes for two personssstarted fromebp-0x60.
- In the program
- In the program
- Start to attack by using buffer overflow
- By using buffer overflow to attack the server, we need to find out the input place that can let us input some payload to make the returned address be wrong (e.g., we want to get the content of the variable
magic1, so that we need to return tomagic1’s address). - In the program
proj3.c, the functionmenuis the place to input some payload.- After connecting to the server, we can choose
2to enter the mode of edit info. Then, the server will ask to enter thesecretand there exists a severe problem.# Connect to the server 140.113.194.66:8787 $ nc 140.113.194.66 8787 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice: 2 Please input secret first: - The problem is that the value of integer in C can be positive or negative value. By entering a negative value, we can get the content outside the array’s index.
- The more important thing is that the
secretis stored behind the arrays[0]. So that we can get the content ofsecretby entering-1as id in the mode of view info.# Connect to the server 140.113.194.66:8787 $ nc 140.113.194.66 8787 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice: 1 Please input id: -1 Name: 1 Note: Age: 2928863094 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice: - The value shows after age is the
secret’s value (2928863094) because the address ofsecretisebp-0x60. which is the top 4 bytes befores[0]from above 44 bytes. Notice that thesecret’s value is a random number.
- After connecting to the server, we can choose
- By using buffer overflow to attack the server, we need to find out the input place that can let us input some payload to make the returned address be wrong (e.g., we want to get the content of the variable
- Enter into the edited mode
-
After entering into the edited mode, we can enter the
secret’s value (2928863094) and choose one user arbitrarily.# Connect to the server 140.113.194.66:8787 $ nc 140.113.194.66 8787 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice: 1 Please input id: -1 Name: 1 Note: Age: 2928863094 ---------------------- NS ---------------------- 1. View info 2. Edit info 3. Exit ---------------------- Your choice: 2 Please input secret first: 2928863094 Please input id: 1 Input new note length: -
In the program
proj3.c, we found that we still cannot get the returned address we want if we only enter a positive integer less than 16 (line 124). Therefore, we enter-1because-1is a largest number in 2-based complement system so that we can trace back to the top of address in memory stack. -
After entering
-1, we are at the address ofnotein the structSTU. In that there is a variable name (16 bytes) before note, we can get the address ofnotefroms[0]beebp-0x50. Thus, we need to push 84 bytes ($5 \times 16 + 4 = 84$ ) to get the returned address.
-
- Get the
flag1
proj3.c- The program of server on140.113.194.66:8787proj3.asm- The assembly program ofproj3.cproj3.py- The program using buffer overflow to attackflag1.txt- The content offlag1
- Execute the
main.py# Make sure your current directory is in "src/" $ python proj3.py- If succeed, you will see the following result
[+] Opening connection to 140.113.194.66 on port 8787: Done 2928863094 FLAG{G00D_J0b!} [*] Closed connection to 140.113.194.66 port 8787
- If succeed, you will see the following result
- Show the true plaintext
# Make sure your current directory is in "out/" $ cat flag1.txt FLAG{G00D_J0b!}
NOTICE: You can follow the contributing process CONTRIBUTING.md to join me. I am very welcome any issue!
NOTCIE: This repository is the assignment in NCTU course "Network Security 2018". If you are taking this course, please do not duplicate from this repository. All rights reserved.
