Source: Moeomu’s blog
GS Secure Compilation Protection Principle
Introduction
- After Vistual Studio 2003 (VS 7.0), this compilation option is enabled by default
- Location:
Project -> project Properties -> Configuration Properties -> C/C++ -> Code Generaion -> Buffer Security Check
- GS presses an additional random DWORD into the stack frame when all function calls occur, this random number is called
canary
, this random number isSecurity Cookie
Security Cookie
is located before EBP, the system will also store a copy ofSecurity Cookie
in the.data
memory area- When an overflow occurs in the stack,
Security Cookie
will be flooded first, followed by the EBP and the return address - Before the function returns, the system will perform an additional security verification operation called
Security Check
. - In the security check, the system will compare the value of
Security Cookie
originally stored in the stack frame with the value in the copy of.data
, if the two do not match, it means thatSecurity Cookie
has been destroyed and the stack has overflowed - When an overflow is detected, the system will enter the exception handling process, the function will not return normally, and the ret instruction will not be executed.
- The cost of extra operations and data is a drop in system performance, so GS will not be applied in the following cases.
- the function does not contain a buffer
- the function is defined to have a variable argument list
- the function uses unprotected keyword tags
- the function contains embedded assembly code in the first statement
- the buffer is not of type 8 bytes and is not larger than 4 bytes
- Due to these exceptions, there are still problems, SoEasy VS2005 SP1 introduced a new security marker:
#pragma strict_gs_check
, which can heap any function to add a security cookie to ensure security - Variable reordering.
- According to the type of local variables heap variables in the stack frame to adjust the position of the string moved to the high address of the stack frame to prevent the string overflow when the destruction of other local variables
- Also assigns pointer and string parameters to low addresses in memory
Security Cookie
details
- The first double word of the
.data
section is used as the seed of the cookie, or raw cookie (all functions’ Cooike are generated with this DWORD) - Cookie seed is different for each run
- After the initialization of the stack frame, the system uses the ESP iso or seed as the cookie of the current function as a difference between different functions to increase the randomness of the cookie
- The seed of the cookie is restored by ESP before the function returns
The problem of `Security Cookie
- Attacks based on rewriting function pointers are difficult to defend
- Attacks against exception handling mechanism are difficult to defend against GS
- GS is a protection for stack frames, hard to defend against heap overflow attacks
Use unprotected memory to break GS
Test environment.
- Visual Studio 2008 Professional
- Windows XP SP3
Test code
|
|
In theory, valfunction does not contain more than 4 bytes of buffer, so the stack space of this function should be unprotected, but the actual test is protected, this problem is to be solved
Break GS with dummy function
Only the function checks the stack when it returns, so the process can be hijacked before the function returns
Code
|
|
Description
- Note: The test environment is
Windows XP SP3
, the compiled version isRelease
version, the compiler isVisual Studio 2008
, and the compilation option isDisable compilation optimization/0d
. - The first four bytes of shellcode are the address of the following assembly code, if the system is not
Windows XP SP3
, you need to modify
|
|
- Override the C++ virtual table pointer to make it point to the springboard, and if you need to balance the stack, look for an instruction in the system dynamic link library as a springboard to jump into the shellcode.
- If the return address is a garbage instruction, make it not affect the execution of shellcode as much as possible, here
0x817C
is acmp
instruction, then this assembly instruction try to make it not occur data access exception - This shellcode will pop up a window
Using SEH to break GS
GS does not protect SEH, so you can overwrite SEH to achieve hijacking
Code
|
|
Description
There is a stack overflow vulnerability in the function test, the variable input will be overwritten after strcpy, and strcat will get an illegal address, the function ah will go to the SEH processing process, we can hijack the system process before
security_cookie
check
- Note: The test environment is
Windows 2000 SP4
, the compiled version isRelease
version, the compiler isVisual Studio 2005
, and the compilation option isDisable compilation optimization/0d
. - The reason for using
Windows 2000
is to prevent the effect ofSafeSEH
. - To be completed: Page:277
Positive hard GS (replaces the original cookie in .data)
Code
|
|
Description
- Note: The test environment is
Windows XP SP3
, the compilation version isRelease
version, the compiler isVisual Studio 2008
, and the compilation option isDisable compilation optimization/0d
- When i is negative, it is possible to point to the
.data
section - The test function has a typical stack overflow vulnerability
- Purpose: change the first four bytes of .data (the original cookie) to our fixed value while changing security_cookie(ebp-0x4) in the stack overflow
Details
Calculate security_cookie at the beginning of the function
|
|
verify security_cookie when function will return
|
|