目錄
一、NX(Windows中的DEP)
二、PIE(ASLR)
三、Canary(堆疊保護)
四、RELRO(RELocation Read Only)
首頁 運維 linux運維 linux nx是什麼

linux nx是什麼

Apr 10, 2023 am 11:05 AM
linux

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

linux nx是什麼

本教學操作環境:linux5.9.8系統、Dell G3電腦。

linux nx是什麼?

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 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 exleating, which can then be handled, for. 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影片教學
#

以上是linux nx是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vscode需要什麼電腦配置 vscode需要什麼電腦配置 Apr 15, 2025 pm 09:48 PM

VS Code 系統要求:操作系統:Windows 10 及以上、macOS 10.12 及以上、Linux 發行版處理器:最低 1.6 GHz,推薦 2.0 GHz 及以上內存:最低 512 MB,推薦 4 GB 及以上存儲空間:最低 250 MB,推薦 1 GB 及以上其他要求:穩定網絡連接,Xorg/Wayland(Linux)

Linux體系結構:揭示5個基本組件 Linux體系結構:揭示5個基本組件 Apr 20, 2025 am 12:04 AM

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

notepad怎麼運行java代碼 notepad怎麼運行java代碼 Apr 16, 2025 pm 07:39 PM

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

vscode終端使用教程 vscode終端使用教程 Apr 15, 2025 pm 10:09 PM

vscode 內置終端是一個開發工具,允許在編輯器內運行命令和腳本,以簡化開發流程。如何使用 vscode 終端:通過快捷鍵 (Ctrl/Cmd ) 打開終端。輸入命令或運行腳本。使用熱鍵 (如 Ctrl L 清除終端)。更改工作目錄 (如 cd 命令)。高級功能包括調試模式、代碼片段自動補全和交互式命令歷史。

git怎麼查看倉庫地址 git怎麼查看倉庫地址 Apr 17, 2025 pm 01:54 PM

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

vscode 無法安裝擴展 vscode 無法安裝擴展 Apr 15, 2025 pm 07:18 PM

VS Code擴展安裝失敗的原因可能包括:網絡不穩定、權限不足、系統兼容性問題、VS Code版本過舊、殺毒軟件或防火牆干擾。通過檢查網絡連接、權限、日誌文件、更新VS Code、禁用安全軟件以及重啟VS Code或計算機,可以逐步排查和解決問題。

vscode在哪寫代碼 vscode在哪寫代碼 Apr 15, 2025 pm 09:54 PM

在 Visual Studio Code(VSCode)中編寫代碼簡單易行,只需安裝 VSCode、創建項目、選擇語言、創建文件、編寫代碼、保存並運行即可。 VSCode 的優點包括跨平台、免費開源、強大功能、擴展豐富,以及輕量快速。

vscode 可以用於 mac 嗎 vscode 可以用於 mac 嗎 Apr 15, 2025 pm 07:36 PM

VS Code 可以在 Mac 上使用。它具有強大的擴展功能、Git 集成、終端和調試器,同時還提供了豐富的設置選項。但是,對於特別大型項目或專業性較強的開發,VS Code 可能會有性能或功能限制。

See all articles