溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

linux nx指的是什么

發(fā)布時(shí)間:2023-04-19 10:56:05 來源:億速云 閱讀:114 作者:iii 欄目:建站服務(wù)器

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

linux nx是指“No-eXecute”,是linux中的一種保護(hù)機(jī)制,也就是數(shù)據(jù)不可執(zhí)行,防止因?yàn)槌绦蜻\(yùn)行出現(xiàn)溢出而使得攻擊者的shellcode可能會(huì)在數(shù)據(jù)區(qū)嘗試執(zhí)行的情況。

Linux程序常見用的一些保護(hù)機(jī)制

一、NX(Windows中的DEP)

NX:No-eXecute、DEP:Data Execute Prevention

  • 也就是數(shù)據(jù)不可執(zhí)行,防止因?yàn)槌绦蜻\(yùn)行出現(xiàn)溢出而使得攻擊者的shellcode可能會(huì)在數(shù)據(jù)區(qū)嘗試執(zhí)行的情況。

  • gcc默認(rèn)開啟,選項(xiàng)有:

gcc -o test test.c      // 默認(rèn)情況下,開啟NX保護(hù)
gcc -z execstack -o test test.c  // 禁用NX保護(hù)
gcc -z noexecstack -o test test.c  // 開啟NX保護(hù)

二、PIE(ASLR)

PIE:Position-Independent Excutable、ASLR:Address Space Layout Randomization

  • fpie/fPIE:需要和選項(xiàng)-pie一起使用開啟pie選項(xiàng)編譯可執(zhí)行文件使得elf擁有共享庫屬性,可以在內(nèi)存任何地方加載運(yùn)行。與之相似的還有fpic/fPIC,關(guān)于其說明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`.

  • 區(qū)別在于fpic/fPIC用于共享庫的編譯,fpie/fPIE則是pie文件編譯的選項(xiàng)。文檔中說 pic(位置無關(guān)代碼)生成的共享庫只能鏈接于可執(zhí)行文件,之后根據(jù)自己編譯簡(jiǎn)單C程序,pie正常運(yùn)行,即如網(wǎng)上許多文章說的 pie 選項(xiàng)生成的位置無關(guān)代碼可假定于本程序,但是我也沒看出fpie/fPIE有啥區(qū)別,只是宏定義只為1和2的區(qū)別,貌似...
    編譯命令(默認(rèn)不開啟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       // 關(guān)閉PIE

  • 而ASLR(地址空間隨機(jī)化),當(dāng)初設(shè)計(jì)時(shí)只負(fù)責(zé)棧、庫、堆等段的地址隨機(jī)化。ASLR的值存于/proc/sys/kernel/randomize_va_space中,如下:

0 - 表示關(guān)閉進(jìn)程地址空間隨機(jī)化。
1 - 表示將mmap的基址,stack和vdso頁面隨機(jī)化。
2 - 表示在1的基礎(chǔ)上增加棧(heap)的隨機(jī)化。(默認(rèn))

更改其值方式:echo  0 >  /proc/sys/kernel/randomize_va_space

vDSO:virtual dynamic shared object;
mmap:即內(nèi)存的映射。
PIE 則是負(fù)責(zé)可執(zhí)行程序的基址隨機(jī)。
以下摘自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為系統(tǒng)功能,PIE則為編譯選項(xiàng)。
注: 在heap分配時(shí),有mmap()brk()兩種方式,由malloc()分配內(nèi)存時(shí)調(diào)用,分配較小時(shí)brk,否則mmap,128k區(qū)別。

三、Canary(棧保護(hù))

??Canary對(duì)于棧的保護(hù),在函數(shù)每一次執(zhí)行時(shí),在棧上隨機(jī)產(chǎn)生一個(gè)Canary值。之后當(dāng)函數(shù)執(zhí)行結(jié)束返回時(shí)檢測(cè)Canary值,若不一致系統(tǒng)則報(bào)出異常。

  • 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值置于緩沖區(qū)和控制數(shù)據(jù)之間,當(dāng)緩沖區(qū)溢出,該值被覆寫,從而可以檢測(cè)以判斷是否運(yùn)行出錯(cuò)或是受到攻擊。緩解緩沖區(qū)溢出攻擊。

  • 編譯選項(xiàng):

gcc -o test test.c                       //默認(rèn)關(guān)閉
gcc -fno-stack-protector -o test test.c  //禁用棧保護(hù)
gcc -fstack-protector -o test test.c     //啟用堆棧保護(hù),不過只為局部變量中含有 char 數(shù)組的函數(shù)插入保護(hù)代碼
gcc -fstack-protector-all -o test test.c //啟用堆棧保護(hù),為所有函數(shù)插入保護(hù)代碼

四、RELRO(RELocation Read Only)

在Linux中有兩種RELRO模式:”Partial RELRO“”Full RELRO“。Linux中Partical RELRO默認(rèn)開啟。

Partial RELRO:

  • 編譯命令:
    gcc -o test test.c  // 默認(rèn)部分開啟
    gcc -Wl,-z,relro -o test test.c // 開啟部分RELRO
    gcc -z lazy -o test test.c // 部分開啟

  • 該ELF文件的各個(gè)部分被重新排序。內(nèi)數(shù)據(jù)段(internal data sections)(如.got,.dtors等)置于程序數(shù)據(jù)段(program's data sections)(如.data和.bss)之前;

  • 無 plt 指向的GOT是只讀的;

  • GOT表可寫(應(yīng)該是與上面有所區(qū)別的)。

Full RELRO:

  • 編譯命令:
    gcc -Wl,-z,relro,-z,now -o test test.c    // 開啟Full RELRO
    gcc -z now -o test test.c  // 全部開啟

  • 支持Partial模式的所有功能;

  • 整個(gè)GOT表映射為只讀的。

gcc -z norelro -o a a.c // RELRO關(guān)閉,即No RELRO

Note:

  • .dtors:當(dāng)定義有.dtors的共享庫被加載時(shí)調(diào)用;

  • 在bss或數(shù)據(jù)溢出錯(cuò)誤的情況下,Partial和Full RELRO保護(hù)ELF內(nèi)數(shù)據(jù)段不被覆蓋。 但只有Full RELRO可以緩解GOT表覆寫攻擊,但是相比較而言開銷較大,因?yàn)槌绦蛟趩?dòng)之前需要解析所有的符號(hào)。

關(guān)于“l(fā)inux nx指的是什么”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“l(fā)inux nx指的是什么”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI