首页服务器linux nx指的是什么

linux nx指的是什么

时间2023-04-26 10:27:02发布访客分类服务器浏览1442
导读:这篇文章主要介绍了linux nx指的是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇linux nx指的是什么文章都会有所收获,下面我们一起来看看吧。 linux nx是指“N...
这篇文章主要介绍了linux nx指的是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇linux nx指的是什么文章都会有所收获,下面我们一起来看看吧。

linux nx是指“No-eXecute”,是linux中的一种保护机制,也就是数据不可执行,防止因为程序运行出现溢出而使得攻击者的shellcode可能会在数据区尝试执行的情况。

Linux程序常见用的一些保护机制

一、NX(Windows中的DEP)

NX:No-eXecute、DEP:Data Execute Prevention

  • 也就是数据不可执行,防止因为程序运行出现溢出而使得攻击者的shellcode可能会在数据区尝试执行的情况。

  • gcc默认开启,选项有:

gcc-otesttest.c//默认情况下,开启NX保护
gcc-zexecstack-otesttest.c//禁用NX保护
gcc-znoexecstack-otesttest.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

	Generateposition-independentcode(PIC)suitableforuseinasharedlibrary,ifsupportedforthetargetmachine.Suchcodeaccessesallconstantaddressesthroughaglobaloffsettable(GOT).ThedynamicloaderresolvestheGOTentrieswhentheprogramstarts(thedynamicloaderisnotpartofGCC;
    itispartoftheoperatingsystem).IftheGOTsizeforthelinkedexecutableexceedsamachine-specificmaximumsize,yougetanerrormessagefromthelinkerindicatingthat-fpicdoesnotwork;
    inthatcase,recompilewith-fPICinstead.(Thesemaximumsare8kontheSPARC,28konAArch74and32konthem68kandRS/6000.Thex86hasnosuchlimit.)

	Position-independentcoderequiresspecialsupport,andthereforeworksonlyoncertainmachines.Forthex86,GCCsupportsPICforSystemVbutnotfortheSun386i.CodegeneratedfortheIBMRS/6000isalwaysposition-independent.

	Whenthisflagisset,themacros`__pic__`and`__PIC__`aredefinedto1.

-fPIC

	Ifsupportedforthetargetmachine,emitposition-independentcode,suitablefordynamiclinkingandavoidinganylimitonthesizeoftheglobaloffsettable.ThisoptionmakesadifferenceonAArch74,m68k,PowerPCandSPARC.

	Position-independentcoderequiresspecialsupport,andthereforeworksonlyoncertainmachines.

	Whenthisflagisset,themacros`__pic__`and`__PIC__`aredefinedto2.

-fpie
-fPIE

	Theseoptionsaresimilarto-fpicand-fPIC,butthegeneratedposition-independentcodecanbeonlylinkedintoexecutables.Usuallytheseoptionsareusedtocompilecodethatwillbelinkedusingthe-pieGCCoption.

	-fpieand-fPIEbothdefinethemacros`__pie__`and`__PIE__`.Themacroshavethevalue1for`-fpie`and2for`-fPIE`.

  • 区别在于fpic/fPIC用于共享库的编译,fpie/fPIE则是pie文件编译的选项。文档中说 pic(位置无关代码)生成的共享库只能链接于可执行文件,之后根据自己编译简单C程序,pie正常运行,即如网上许多文章说的 pie 选项生成的位置无关代码可假定于本程序,但是我也没看出fpie/fPIE有啥区别,只是宏定义只为1和2的区别,貌似...
    编译命令(默认不开启PIE):

gcc-fpie-pie-otesttest.c//开启PIE
gcc-fPIE-pie-otesttest.c//开启PIE
gcc-fpic-otesttest.c//开启PIC
gcc-fPIC-otesttest.c//开启PIC
gcc-no-pie-otesttest.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-otesttest.c//默认关闭
gcc-fno-stack-protector-otesttest.c//禁用栈保护
gcc-fstack-protector-otesttest.c//启用堆栈保护,不过只为局部变量中含有char数组的函数插入保护代码
gcc-fstack-protector-all-otesttest.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指的是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

linux

若转载请注明出处: linux nx指的是什么
本文地址: https://pptw.com/jishu/9146.html
linux中bc的作用是什么 Linux下如何查看MySQL端口

游客 回复需填写必要信息