What Is a Stack Overflow?
The stack is an abstract data type often used in computer science. Objects on the stack have a feature: The last object placed on the stack is always taken out first. This feature is often referred to as the last-in-first-out (LIFO) queue. Some operations are defined in the stack. The two most important ones are PUSH and POP. The PUSH operation adds an element to the top of the stack. The POP operation is the opposite, removing one element from the top of the stack and reducing the size of the stack by one.
- Chinese name
- Stack overflow
- Applied discipline
- computer science
- Category
- High-level language
- Technology
- Procedures and functions
- RAM
- Contiguous memory
- Address
- Fixed address
- Field
- Computer security
- The stack is an abstract data type often used in computer science. Objects on the stack have a feature: The last object placed on the stack is always taken out first. This feature is often referred to as the last-in-first-out (LIFO) queue. Some operations are defined in the stack. The two most important ones are PUSH and POP. The PUSH operation adds an element to the top of the stack. The POP operation is the opposite, removing one element from the top of the stack and reducing the size of the stack by one.
- The stack overflow is caused by too many function calls, resulting in the call stack not being able to accommodate the return addresses of these calls, which is usually generated in recursion. Stack overflow is most likely caused by infinite recursion, but it may also be just too many stack levels.
Stack overflow stack overflow
- Stack overflow is to ignore the size of the local data block allocated in the stack, write too much data to the data block, causing the data to exceed the boundary, and the result overwrites other data. It can be understood as embedding a piece of code in a long string, and overwriting the return address of the procedure with the address of this code, so that when the procedure returns, the program will start executing this self-written code.
- For example, the following program:
- #include <stdio.h>
- int main ()
- {
- char name [8];
- printf ("Please type your name:");
- gets (name);
- printf ("Hello.% s!", name);
- return 0;
- }
- Compile and execute, enter ipxodiAAAAAAAAAAAAAAAA, after executing gets (name), the stack is as follows:
- Bottom of memory top of memory
- name EBP ret
- <------- [ipxodiAA] [AAAA] [AAAA] ............
- ^ & name
- Top of stack bottom of stack
- Because the name string we entered is too long, the name array can't hold it, so we have to continue writing 'A' to the top of the memory. If we apply for dynamic memory in advance, we can avoid stack overflow. In this example, because the growth direction of the stack is opposite to that of the memory, these 'A's cover the old elements of the stack. 'EBP ret' is covered by 'A'. When main returns, it will use the ASCII code of 'AAAA': 0x41414141 as the return address. The CPU will try to execute the instruction at 0x41414141, and the result will be an error. This is a stack overflow!
Stack overflow resolution
- Ability to monitor the behavior of the four functions malloc, memset, memcpy, and free (the stack is not detected, and generally there are fewer stack overflows, which is easy to check. In addition, new and delete cannot monitor them due to their limited level). If an out-of-bounds operation is found, print it out and continue execution. In other words, the detection tool does not affect the behavior of the program. [1]
Stack overflow stack area
- The stack is a contiguous block of memory that holds data. A register named Stack Pointer (SP) points to
- Stack overflow
Stack overflow stack overflow attack
JMP ESP Stack overflow using JMP ESP
- Its use format is NNNNNNRSSSSS, where N = NOP, R = RET (address of jmp esp), and S = ShellCode. It is to cover the buffer with NOP (empty instruction, do nothing) until the original EIP position, we fill in the jmp esp address of a core dll in the system, followed by our ShellCode. Under normal circumstances, when the function returns, the RET instruction is executed. This is equal to the POP EIP. The saved EIP value of the original program is restored, and the interrupted return is completed. But here, we overwrite the saved EIP value and rewrite it to the address of jmp esp. In this way, after POP EIP, EIP = jmp esp address, and the stack pointer ESP will go down to point to the beginning of ShellCode. The program continues to execute. At this time, the content in the EIP is jmp esp, and the system executes jmp esp, which just jumps to our ShellCode.
If the ShellCode is an open port, then we can connect to it remotely; if the ShellCode is downloaded and executed, then we can let the target machine download and execute a file on the webpage, as long as you want to reach the function, you can think of ways to achieve. [2]
JMP EBX Stack overflow using JMP EBX
- Its utilization format is NNNNN JESSSSSS. Here N = NOP, J = Jmp 04, E = jmp ebx address, and S = ShellCode. Here the position of J and E is the key, E is the entry position for error handling, and J is in front of it. In the first way, we know to overwrite the return address with another address. But what if it is an invalid address? The data pointed there may not be readable, or may not be executed, what happens? In fact, I believe everyone has encountered it, that is, the system will pop up a dialog box to report an error, we click OK, and the operation will be terminated. This is because as a system-level program, there is a sound error handling mechanism inside. Simply put, if an error occurs during runtime, windows will jump to a place dedicated to handling errors, corresponding to different errors, and executing different code. The code executed above is popping up a dialog box to report an error. So here we intentionally overwrite the returned address with a wrong address. In this case, Windows will jump to the entry that handles the error, and ebx points to the first 4 bytes of the entry! Then if we overwrite the error entry with the address of jmp ebx, it will jump to the first 4 bytes. How to jump to ShellCode? Here we write jmp 04, haha, skip back 4 bytes, just skip the overlay value and reach our ShellCode. [2]