久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

linux nx指的是什么

165次閱讀
沒有評論

共計 4827 個字符,預計需要花費 13 分鐘才能閱讀完成。

這篇文章主要介紹了 linux nx 指的是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇 linux nx 指的是什么文章都會有所收獲,下面我們一起來看看吧。

linux nx 是指“No-eXecute”,是 linux 中的一種保護機制,也就是數據不可執行,防止因為程序運行出現溢出而使得攻擊者的 shellcode 可能會在數據區嘗試執行的情況。

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 AArch74 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 AArch74, 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 區別。

三、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 表覆寫攻擊,但是相比較而言開銷較大,因為程序在啟動之前需要解析所有的符號。

關于“linux nx 指的是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“linux nx 指的是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道。

向 AI 問一下細節

丸趣 TV 網 – 提供最優質的資源集合!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2024-02-04發表,共計4827字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 靖远县| 江源县| 安塞县| 涞水县| 依安县| 万源市| 大同市| 深泽县| 寻乌县| 霍林郭勒市| 九寨沟县| 两当县| 金山区| 房山区| 澄城县| 买车| 会宁县| 祁连县| 辛集市| 庄河市| 司法| 湘乡市| 仙居县| 天水市| 黎川县| 大埔县| 神农架林区| 香港| 宝山区| 洪洞县| 牙克石市| 梁河县| 五台县| 贺兰县| 平利县| 华容县| 屏山县| 江陵县| 福泉市| 岱山县| 宁安市|