linux nx指的是什么

发布时间:2023-04-19 10:56:05 作者:iii
来源:亿速云 阅读:114

这篇文章主要介绍了linux nx指的是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇linux nx指的是什么文章都会有所收获,下面我们一起来看看吧。

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

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

一、NX(Windows中的DEP)

NX:No-eXecute、DEP:Data Execute Prevention

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

-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`.

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

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值,若不一致系统则报出异常。

  如上所述,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:

Full RELRO:

gcc -z norelro -o a a.c // RELRO关闭,即No RELRO

Note:

关于“linux nx指的是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“linux nx指的是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. unix/linux指的是什么
  2. 怎么使用NoMachine NX交付远程Mac、Linux和Windows桌面

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

linux

上一篇:Linux下如何查看MySQL端口

下一篇:linux单用户模式怎么进入

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》