Linux程式常見用的一些保護機制
一、NX(Windows中的DEP)
NX:No-eXecute、DEP:Data Execute Prevention
- 也就是資料不可執行,防止因為程式執行出現溢位而使得攻擊者的shellcode可能會在資料區嘗試執行的情況。
- gcc預設開啟,選項有:
gcc -o test test.c // 預設情況下,開啟NX保護
gcc -z execstack -o test test.c // 禁用NX保護
gcc -z noexecstack -o test test.c // 開啟NX保護
二、PIE(ASLR)
PIE:Position-Independent Excutable、ASLR:Address Space Layout Randomization
- fpie/fPIE:需要和選項
-pie
一起使用開啟pie選項編譯可執行檔案使得elf擁有共享庫屬性,可以在記憶體任何地方載入執行。與之相似的還有fpic/fPIC,關於其說明於https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
-fpic
Generate position-independent code (PIC) suitable for use in a shared library, if supported for the target machine. Such code accesses all constant addresses through a global offset table (GOT). The dynamic loader resolves the GOT entries when the program starts (the dynamic loader is not part of GCC; it is part of the operating system). If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC, 28k on AArch64 and 32k on the m68k and RS/6000. The x86 has no such limit.)
Position-independent code requires special support, and therefore works only on certain machines. For the x86, GCC supports PIC for System V but not for the Sun 386i. Code generated for the IBM RS/6000 is always position-independent.
When this flag is set, the macros `__pic__` and `__PIC__` are defined to 1.
-fPIC
If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table.This option makes a difference on AArch64, m68k, PowerPC and SPARC.
Position-independent code requires special support, and therefore works only on certain machines.
When this flag is set, the macros `__pic__` and `__PIC__` are defined to 2.
-fpie
-fPIE
These options are similar to -fpic and -fPIC, but the generated position-independent code can be only linked into executables. Usually these options are used to compile code that will be linked using the -pie GCC option.
-fpie and -fPIE both define the macros `__pie__` and `__PIE__`. The macros have the value 1 for `-fpie` and 2 for `-fPIE`.
- 區別在於fpic/fPIC用於共享庫的編譯,fpie/fPIE則是pie檔案編譯的選項。文件中說 pic(位置無關程式碼)生成的共享庫只能連結於可執行檔案,之後根據自己編譯簡單C程式,pie正常執行,即如網上許多文章說的 pie 選項生成的位置無關程式碼可假定於本程式,但是我也沒看出fpie/fPIE有啥區別,只是巨集定義只為1和2的區別,貌似...
編譯命令(預設不開啟PIE):
gcc -fpie -pie -o test test.c // 開啟PIE
gcc -fPIE -pie -o test test.c // 開啟PIE
gcc -fpic -o test test.c // 開啟PIC
gcc -fPIC -o test test.c // 開啟PIC
gcc -no-pie -o test test.c // 關閉PIE
- 而ASLR(地址空間隨機化),當初設計時只負責棧、庫、堆等段的地址隨機化。ASLR的值存於
/proc/sys/kernel/randomize_va_space
中,如下:
0 - 表示關閉程序地址空間隨機化。
1 - 表示將mmap的基址,stack和vdso頁面隨機化。
2 - 表示在1的基礎上增加棧(heap)的隨機化。(預設)
更改其值方式:echo 0 > /proc/sys/kernel/randomize_va_space
vDSO:virtual dynamic shared object;
mmap:即記憶體的對映。
PIE
則是負責可執行程式的基址隨機。
以下摘自Wiki:
Position-independent executable (PIE) implements a random base address for the main executable binary and has been in place since 2003. It provides the same address randomness to the main executable as being used for the shared libraries.
PIE為ASLR的一部分,ASLR為系統功能,PIE則為編譯選項。
注: 在heap分配時,有mmap()
和brk()
兩種方式,由malloc()
分配記憶體時呼叫,分配較小時brk,否則mmap,128k區別。參考文章:https://blog.csdn.net/gfgdsg/article/details/42709943
三、Canary(棧保護)
Canary對於棧的保護,在函式每一次執行時,在棧上隨機產生一個Canary值。之後當函式執行結束返回時檢測Canary值,若不一致系統則報出異常。
- Wiki:
- Canaries or canary words are known values that are placed between a buffer and control data on the stack to monitor buffer overflows. When the buffer overflows, the first data to be corrupted will usually be the canary, and a failed verification of the canary data will therefore alert of an overflow, which can then be handled, for example, by invalidating the corrupted data. A canary value should not be confused with a sentinel value.
如上所述,Canary值置於緩衝區和控制資料之間,當緩衝區溢位,該值被覆寫,從而可以檢測以判斷是否執行出錯或是受到攻擊。緩解緩衝區溢位攻擊。
- 編譯選項:
gcc -o test test.c //預設關閉
gcc -fno-stack-protector -o test test.c //禁用棧保護
gcc -fstack-protector -o test test.c //啟用堆疊保護,不過只為區域性變數中含有 char 陣列的函式插入保護程式碼
gcc -fstack-protector-all -o test test.c //啟用堆疊保護,為所有函式插入保護程式碼
四、RELRO(RELocation Read Only)
在Linux中有兩種RELRO模式:”Partial RELRO“
和 ”Full RELRO“
。Linux中Partical RELRO預設開啟。
Partial RELRO:
- 編譯命令:
gcc -o test test.c // 預設部分開啟
gcc -Wl,-z,relro -o test test.c // 開啟部分RELRO
gcc -z lazy -o test test.c // 部分開啟 - 該ELF檔案的各個部分被重新排序。內資料段(internal data sections)(如.got,.dtors等)置於程式資料段(program's data sections)(如.data和.bss)之前;
- 無 plt 指向的GOT是隻讀的;
- GOT表可寫(應該是與上面有所區別的)。
Full RELRO:
- 編譯命令:
gcc -Wl,-z,relro,-z,now -o test test.c // 開啟Full RELRO
gcc -z now -o test test.c // 全部開啟 - 支援Partial模式的所有功能;
- 整個GOT表對映為只讀的。
gcc -z norelro -o a a.c // RELRO關閉,即No RELRO
Note:
- .dtors:當定義有.dtors的共享庫被載入時呼叫;
- 在bss或資料溢位錯誤的情況下,Partial和Full RELRO保護ELF內資料段不被覆蓋。 但只有Full RELRO可以緩解GOT表覆寫攻擊,但是相比較而言開銷較大,因為程式在啟動之前需要解析所有的符號。
參考文章:
- RELRO - A (not so well known) Memory Corruption Mitigation Technique
- RELRO
- 透過 checksec 學習 Linux 防溢位攻擊保護措施