This experiment requires attacks on 5 code with different security vulnerabilities, and the attack is divided into two parts:
Part 1:code injection Attacks (buffer overflow attack)
Part 2:return-oriented programming (ROP Attack)
Be sure to read the write Up:http://csapp.cs.cmu.edu/3e/attacklab.pdf carefully before you do the problem, and it will be a lot clearer after the experiment.
Document description
ctarget: An executable program that is susceptible to code injection attacks.
rtarget: An executable program that is susceptible to return-oriented programming attacks.
cookie.txt: A 8-bit hexadecimal code that is used to verify the unique identifier of the identity.
farm.c: The source code of the target "gadget farm" for generating return-oriented programming attacks.
hex2raw: A tool that generates attack strings.
Part 1:code Injection Attackslevel 1
The first question is to attack a method called Test, which has a getbuf method that asks to return from Getbuf without returning it to test, but instead returns to the touch method.
We can find that Getbuf is an input string method, then we just fill up the stack of this method, and then fill in the address of the touch method, then we can achieve the effect of jumping to touch!
So we're going to do two steps:
First determine the buffer size of the Getbuf method, and disassemble the Gets method to view:
The buffer that gets this method is 40 bytes.
Next we look at the address of the touch method:
The address is 4017c0, then we need to enter the string to get:
The previous 40 bytes are false, the key is the last 8 bits, fill in the time need to pay attention to the order of the small end.
Then use Hex2raw to generate bytecode, successfully invade the garbage code!
Level 2
According to the topic instruction, this problem we want to do is also jump, this time we jump is touch2 function, see what TOUCH2 function Long, find here need us to pass a parameter, according to experiment explanation we know this parameter position in%rdi. So we need to inject a piece of our own intrusion code.
Here's my idea:
1. Write a piece of code to the parameters (that is, everyone different cookie), put in the register%rip, and then touch2 the address of the stack, in order to return through the RET can jump to ret.
2. The first problem is then used in the same way as the vulnerability of buffer overflow to return the GETBUF function to the starting position of the code above, that is, the beginning of the buffer execution code.
I wrote the compilation, the first immediate number is my cookie, the second one is TOUCH2 's address:
Use the GCC and objdump commands to get the machine code:
Then need to find the real address of the buffer, we need gdb in the getbuf break point to see the value of%RSP, but here I met a small problem, I two times before and after the execution of the program to see%RSP value is not the same, resulting in my first experiment never pass, not able to understand why this change, If you know you can leave a message to me.
Here, with the latest RSP values, you can invade the junk code again!
Level 3
This topic is similar to the above, the request is to jump to TOUCH3, also need to pass in a parameter, but this time is to pass in an address, rather than a direct cookie:
You can see the TOUCH3 and call the Hexmatch, here we need to note that we store the address of the cookie, can not be placed in the buffer, because the Hexmatch in the array will also overwrite our cookie, my idea is to say a cookie on the buffer, That is, the return address 8 bytes above, namely%rsp+30.
Assembly Code:
BYTE code::
Results:
Part 2:return-oriented Programming
Our previous questions were all about the buffer overflow topic, but from here on, the Rtarget file has defensive measures against the above attacks, such as stack address randomization or buffer code being set to be non-executable, thus breaking the possibility of this attack, which is now the way many systems take, But the attackers did not stop there, and the ROP was born. The general meaning of the ROP attack is to use the existing code to make a way of attacking by constantly jumping and piecing together the desired results.
The following is the bytecode for some of the instructions given in the lab manual, which we need to select in the Rtarget file to perform the previous Level2 and level3 attacks.
Level 2:
According to the topic tip, the first question of the code can be found in this code:
My idea is to first pop the cookie to Rax, then use the MOV instruction to move the Rax to RDI, and finally execute the TOUCH2 by overwriting the return address.
By observing the commands we want here:
Respectively is the%rax (pop), C7 (Movq%rax,%rdi)
The address is 0X4019AB,0X4019C5,TOUCH2, respectively, and the address is 0X4017EC.
So the code we put together comes out:
Bingo!
Level 3:
This problem is slightly difficult, the recent major illness, and so on after the resumption of the challenge.
Csapp lab:attack lab--teach you to write viruses (big mistake