Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

时间:2021-07-10 01:07:43

Catalog

. Linux attack vector
. Grsecurity/PaX
. Hardened toolchain
. Default addition of the Stack Smashing Protector (SSP): Compiler Flag: GS
. Automatic generation of Position Independent Executables (PIEs): System Characteristic + Compiler Flag: ASLR
. Default to marking read-only, sections that can be so marked after the loader is finished (RELRO): Compiler Flag + System Characteristic: DEP / NX
. Default full binding at load-time (BIND_NOW)
. Heap Protector
. Pointer Obfuscation
. Built with Fortify Source
. /proc/$pid/maps protection
. ptrace scope
. /dev/mem protection
. Block module loading
. Syscall Filtering
. How To Harden Linux

0. Linux attack vector

0x1: Offset2lib: bypassing full ASLR on 64bit Linux

1. Introduction

Address-Space Layout Randomization (ASLR) is a technique used to thwart attacks which relies on knowing the location of the target code or data. The effectiveness of ASLR hinges on the entirety of the address space layout remaining unknown to the attacker. Only executables compiled as Position Independent Executable (PIE) can obtain the maximum protection from the ASLR technique since all the sections are loaded at random locations.

Offset2lib is a security weakness on the implementation of the ASLR in GNU/Linux when the executable is PIE compiled

A PoC attack is described to illustrate how the weakness can be exploited. The attack bypasses the three most widely adopted and effective protection techniques: No-eXecutable bit (NX), address space layout randomization (ASLR) and stack smashing protector (SSP). The exploit obtaines a remote shell in less than one second.

2. ASLR weakness

It is specific to GNU/Linux and does not affect Windows or Mac OS. It is not a programming error on the code that implements the ASLR, but a weakness on the design
The problem appears when an application is PIE(Position Independent Executable) compiled. The executable image is handled as if it were a shared library, that is, it is loaded at a random location in memory. The GNU/Linux algorithm for loading ASLR objects works as follows

. The first shared object is loaded at a random position (the application).
. The next shared objects are loaded one after the other.

Therefore, a memory leak of an address belonging to the application is enough to de-randomise the memory map of the application. Note that it is not necessary to have a leak of a GOT address (after is has been properly initialised) but just the program counter of the process.
The offset2lib value it is a constant value which may be slightly different on each system, depending on the library version and the order how the libraries have been loaded.

3. Offset2lib attack

The goal of the attack is to obtain an address which belongs to the application code. The following attack exploits a standard stack buffer overflow to obtain the saved-IP address (of the app. code) stored in the stack.
We have structured the attack in 5 steps. Our attack starts by doing an off-line analysis of the target application and its execution environment. The unknown information (hidden by the ASLR) is obtained via brute force, thanks to the forking server architecture of the target. Once we have the full address of the application, the base address of the application is calculated. The last step is to have the memory map of all the libraries, which depends on the GNU/Linux distribution of the target. With the obtained information it is easy to arm a ROP program to get a remote shell. The complete on-line attack may take no more than one second.
The steps to build the attack are:

. Extract static information
. Bruteforce a part of saved-IP
. Calculate the base application address
. Calculate the offset2lib constants
. Obtain mmapped areas
Step Description Result
1a Set the highest 24 bits. These bytes are known off-line. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

1b Set the 12 lower bits. Due to page alignment. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

2 The remaining 28 random bits of the saved-IP are obtained by using the byte-for-byte attack against the saved-IP. Note that only three and a half bytes need to be bruteforced. Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

3 Using the saved-IP calculated in the previous step we can obtain the executable base address as follow:

App_base = (savedIP & 0xFFF)-(CALLER_PAGE_OFFSET << 12)

0x00007F36C6fEB000


4 Calculate the offset2lib for the target libraries. These values are different depending on the system, but quite similar among them. A fast way to obtain these offset2lib values are by executing the application (on local) and print the offsets. The offset2lib does not depends on the application itself. We have calculated the offset2lib for some Linux distributions.
Distribution Libc ver. Offset2lib
CentOS 6.5 2.12 0x5b6000
Debian 7.1 2.13 0x5ac000
Ubuntu 12.04 2.15 0x5e4000
Ubuntu 12.10 2.15 0x5e4000

5 The base address of any library can be calculated by just subtracting the offset2lib. For example to obtain the libc base address we simply do:

Libc_base = App_base - offset2lib

0x00007f36c6a07000


The attack presented here is only a demonstrative example of how to exploit the ASLR offset2lib weakness.

4. Proof of Concept

This is a working example to show how to exploit the offset2lib weakness. You need:

. Small program to obtain the offset2lib value for the libc: get_offset2lib.tgz
. The vulnerable server: vuln-server-64bit.tgz
. The python script to make the attack: exploit-offset2lib-ubuntu-14.04.-LTS.py

Launching the vulnerable server:

. wget http://cybersecurity.upv.es/attacks/offset2lib/vuln-server-64bit.tgz
. tar xvf vuln-server-64bit.tgz
. make
. sudo /etc/init.d/apport stop # avoid creating cores on crashes (speed-up)
. sudo ./server_64_PIE_SSP

Obtaining the offets2lib value of the target system (on another console):

. wget http://cybersecurity.upv.es/attacks/offset2lib/get_offset2lib.tgz
. tar xvf get_offset2lib.tgz
. make -f Makefile.offset2lib
. ./get_offset2lib
//Offset2lib (libc): 0x5b6000

Configure and Launching the exploit:

. wget http://cybersecurity.upv.es/attacks/offset2lib/exploit-offset2lib-ubuntu-14.04.1-LTS.py
. sed -i 's/\(OFFSET_TO_LIBC\).*=.*/\1=0x5b6000/' exploit-offset2lib-ubuntu-14.04.-LTS.py
. objdump -d server_64_PIE_SSP| grep -A1 vulnerable_function\>$
/*
113b: e8 6d fc ff ff callq dad <vulnerable_function>
1140: 48 8d 45 e0 lea -0x20(%rbp),%rax
*/
. sed -i 's/\(PAGE_NUMBER_OF_NEXT_INSTRUCTION_TO_CALL\).*=.*/\1=0x1/' exploit-offset2lib-ubuntu-14.04.-LTS.py
. sed -i 's/\(OFFSET_SAVED_RIP\).*=.*/\1=0x140/' exploit-offset2lib-ubuntu-14.04.-LTS.py . chmod a+x exploit-offset2lib-ubuntu-14.04.-LTS.py
. ./exploit-offset2lib-ubuntu-14.04.-LTS.py
. ./exploit-offset2lib-ubuntu-14.04.-LTS.py -s localhost -p

5. Recommendations

There are several solutions and workarounds to this weakness

. Prevent the weakness with PaX patch
Among many other security improvements, the PaX (from GRSecurity) patch places the executable at random positions with respect to the other shared objects.
这让"PIE编译后,Binary加载的基址和Libc加载的基址的距离是固定的"的攻击前提不在存在
PaX defines four zones:
) delta_exec: code, data, bss.
) brk: heap.
) delta_mmap: libraries, mapped files, thread stack, shared memory, ...
) delta_stack: user stack.
Pax solution also increases the number of entropy of each zone, even it is able to randomise non-PIE applications. As far as we know it is the most advanced ASLR implementation. Unfortunately, some people think that it is a too complex patch with, may be, too many features (some advanced features may break backward compatibility on some applications). . Fixing Offset2lib weakness
In order to remove the offset2lib weakness, the executable shall be located at a different zone than libraries. We have created a small patch for the current Linux kernel (3.18-rc7) which implements four different zones, so that the executable is not in the same zone than libraries. . Prevent the exploitation with RenewSSP
To successfully bypass the ASLR by exploiting the stack buffer overflow presented in the PoC, the attacker needs to bypass first the stack smashing protector (SSP). If the SSP protection can not be bypassed, then the PoC would fail.
A new technique, called renewSSP can be used to prevent brute force attacks against the SSP. This technique is a variant of the classic SSP where the value of the secret canary is renewed dynamically at key places in the program. This way, the secret is refreshed more often. The SSP value can be renewed per-process preventing the ``byte-for-byte'' attack. The technique is not intrusive, and can be applied by just pre-loading a shared library. The overhead is almost negligible.

0x2: Exploiting "BadIRET" vulnerability (CVE-2014-9322, Linux kernel privilege escalation)

arch/x86/kernel/entry_64.S in the Linux kernel before 3.17.5 does not properly handle faults associated with the Stack Segment (SS) segment register, which allows local users to gain privileges by triggering an IRET instruction that leads to access to a GS Base address from the wrong space.

. With the tested kernel, the vulnerability can be reliably exploited to achieve kernelmode arbitrary code execution.
. SMEP does not prevent arbitrary code execution; SMAP does prevent arbitrary code execution.

1. The vulnerability

In a few cases, when Linux kernel returns to usermode via iret, this instruction throws an exception. The exception handler returns execution to bad_iret function, that does

/* So pretend we completed the iret and took the #GPF in user mode.*/
pushq $
SWAPGS
jmp general_protection

The problematic case is #SS exception. If a kernel is vulnerable (so, before kernel version 3.17.5) and has “espfix” functionality (introduced around kernel version 3.16), then bad_iret executes with a read-only stack – “push” instruction generates a page fault that gets converted into double fault. ; from now on, we focus on pre 3.16 kernel, with no “espfix”.
The vulnerability stems from the fact that the exception handler for the #
SS exception does not fit the “pretend-it-was-#GP-in-userspace” schema well. In comparison with e.g. #GP handler, the #SS exception handler does one extra swapgs instruction.

2. SMEP

SMEP is a feature of Intel processors, starting from 3rd generation of Core processor. If the SMEP bit is set in CR4, CPU will refuse to execute code with kernel privileges if the code resides in usermode pages. Linux enables SMEP by default if available.

Relevant Link:

http://cybersecurity.upv.es/attacks/offset2lib/offset2lib.html
http://labs.bromium.com/2015/02/02/exploiting-badiret-vulnerability-cve-2014-9322-linux-kernel-privilege-escalation/
http://lwn.net/Articles/517475/

1. Grsecurity/PaX

0x1: 什么是Pax

PaX是针对linux kernel的一个加固版本的补丁,它让linux内核的内存页受限于最小权限原则,是一个有效的防御"系统级别0DAY"的方案,第1版的设计和实现诞生于2000年,但是当年Linux内核不收PaX进入upstream是因为很多人觉得PaX不是那么的好维护,之后linux内核推出了LSM(Linux Security Module),LSM利用了一堆CAPABILITY的机制提供了一些限制用户态程序访问控制的接口,SELinux和Apparmor就是基于LSM开发的。需要注意的是LSM并不是一个传统意义上的linux kernel module

. 必须在bootloader启动内核时启动,不能在内核加载完后启动
. 不能同时启动2个LSM的实现,当然后来有人实现了一套LSM Stacking堆栈式调用方式,但是并没有进入Linux内核原生支持

当我们谈到PaX时都会写Grsecurity/PaX,这是因为PaX从一开始就主要关注如何防御和检测memory corruption,后来Grsecurity社区发现PaX和他们所关注的非常类似,所以就合并了,在很长的一段时间里PaX主要关注memorycorruption,而Grsecurity则实现其他的功能包括RBAC,但后来2个社区的工作开始模糊了:包括USERCOPY, STACKLEAK, RANDSTRUCT, etc..都是整个Grsecurity/PaX共同实现的特性

PaX team认为会导致漏洞利用的bug给予了攻击者(区分攻击者和黑客是不同的term)在3个不同层面*问被攻击的进程

. 执行任意代码
. 执行现有代码但打破了原有的执行顺序
. 原有的执行顺序执行现有代码,但加载任意数据

0x2: PaX里vma mirroring的设计

在2003年的晚些时候PaX实现了虚拟内存空间的镜像( vma mirroring),vmamirroring的目的是为了在一组物理页上做特殊文件隐射时有2个不同的线性地址,这2个地址即使在swap-out/swap-in或者COW后依然是不会改变的。这样做的目的为了满足几种场景

. 把可执行的区域在使用SEGMEXEC隐射进入代码段。在32-bit的linux内核里的4GB地址空间是其中3GB给用户空间,1GB给内核空间,而vma mirroring把用户空间的3GB划分成了2个1.5GB分别是给代码段和数据段,在可执行区域里包含的数据的部分(常量字符串,函数指针表等)都会mirroring到数据段里
. 实现可执行区域的地址随机化(RANDEXEC)
. 这个引出了第3种情况,就是SEGMEXEC和RANDEXEC同时激活,这个的效果和PIE+ASLR的效果类似,不同的不是整个elf binary的代码段随机化,而是在mirroring时对代码段和数据段进行随机化

Linux Overflow Vulnerability General Hardened Defense Technology、Grsecurity/PaX

Relevant Link:

https://raw.githubusercontent.com/citypw/DNFWAH/master/4/d4_0x01_DNFWAH_archeological_hacking_on_pax.txt
https://wiki.gentoo.org/wiki/Hardened/PaX_Quickstart
https://linux.cn/article-5750-qqmail.html

2. Hardened toolchain

Hardened toolchain introduces a number of changes to the default behaviour of the toolchain (gcc, binutils, glibc/uclibc) intended to improve security. It supports other initiatives taken by the hardened project; most directly PaX and Grsecurity, but can also be applied to SELinux and RSBAC.

0x1: Check Tools

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzgAAAA2CAIAAACEDKs4AAARcElEQVR4nO2d3bGrOgyF3U86oIbUwcydSSN5owla4Jk+KIb7ALZla8lAAtk+yfrmzJxsx5Yl+QdhE+z+++8/RwghhBBCKoSBGiGEEEJIpTBQI4QQQgipFAZqhBBCCCGVwkCNEEIIIaRSGKgRQgghhFTKBwK1bprneZ7nob26JkIIIYSQb+LqQK3ppiVCCx/yLzztMEfiNyF56hqVd+ouVV7UtBllLhn3WLQlxs7ZDufHu2fKLCq/n256qWWvdd0Vvv8X6n6Bdpi6ppveU/h7W7NE4+9q5XxnTpVvG3DSgLVknqZnKr4ks+IpqCRTNfE7FR2u3YusckgQ59y5gRqanLvJTzhbU3c7rDmbWMbF/tsOoZ+FXipzXkEcPkE5Z5k5D63LvoAWldnM+O7170qZ57VGOxyfJS933duxxzt8ru63zVwEvCnlu1vT4pBS7xtwxfSpZX5yyqp7CnpF5ksVvVh7jUOCLIhAbblXmbp2uavzbRZv8kTn0InJ8lEsvbR9k4os3RU13YT7U+xnMWqKExtQvummeZ6mabfyi3zxWXRcX7thpjR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4cdwqx0wJa2AAAAAElFTkSuQmCC" alt="" />

Relevant Link:

https://github.com/slimm609/checksec.sh
https://wiki.gentoo.org/wiki/Hardened/Toolchain
https://wiki.ubuntu.com/Security/Features

3. Default addition of the Stack Smashing Protector (SSP): Compiler Flag: GS

First developed by Dr Hiroaki Etoh at IBM for the 3.x series of GCC (originally under the name ProPolice) and re-developed in a different way for the 4.x series by RedHat, the Stack Smashing Protector attempts to protect against stack buffer overflows. It causes the compiler to insert a check for stack buffer overflows before function returns. If an attempt is made to exploit a previously unfixed (and probably undiscovered) error that exposes a buffer overflow vulnerability, the application will be killed immediately. This reduces any potential exploit to a denial-of-service.
Normally the compiler must be explicitly directed to switch on the stack protection via compiler options.
类似于windows上的GS编译技术,本质是在函数return返回的时候,检测当前栈空间是否遭到污染
The Stack Smashing Protector (SSP) compiler feature helps detect stack buffer overrun by aborting if a secret value on the stack is changed. This serves a dual purpose in making the occurrence of such bugs visible and as exploit mitigation against return-oriented programming. SSP merely detects stack buffer overruns, they are not prevented. The detection can be beaten by preparing the input such that the stack canary is overwritten with the correct value and thus does not offer perfect protection. The stack canary is native word sized and if chosen randomly, an attacker will have to guess the right value among 2^32 or 2^64 combinations (and revealing the bug if the guess is wrong), or resort to clever means of determining it.

0x1: Description

Compilers implement this feature by selecting appropriate functions, storing the stack canary during the function prologue and checking the value at the epilogue, invoking a failure handler if it was changed. For instance, consider the code:

void foo(const char* str)
{
char buffer[];
strcpy(buffer, str);
}

SSP automatically illustratively transforms that code into this:

/*
Note how buffer overruns are undefined behavior and the compilers tend to optimize these checks away if you wrote them yourself, this only works robustly because the compiler did it itself.
*/
extern uintptr_t __stack_chk_guard;
noreturn void __stack_chk_fail(void);
void foo(const char* str)
{
//在最靠近return函数返回的位置设置canary(SSP Check)
uintptr_t canary = __stack_chk_guard;
char buffer[];
strcpy(buffer, str);
if ( (canary = canary ^ __stack_chk_guard) != )
__stack_chk_fail();
}

Note how the secret value is stored in a global variable (initialized at program load time) and is copied into the stack frame, and how the it is safely erased from the stack as part of check. Since stacks grow downwards on many architectures, the canary gets overwritten whenever input to strcpy is at least 16 characters. The caller return pointer exploited in return-oriented programming attacks is not accessed until after the value was validated, thus defusing such attacks.

0x2: Implementation

Run-time support needs only two components: A global variable and a check failure handler. For instance, a minimal implementation could be:

#include <stdint.h>
#include <stdlib.h> #if UINT32_MAX == UINTPTR_MAX
#define STACK_CHK_GUARD 0xe2dee396
#else
#define STACK_CHK_GUARD 0x595e9fbd94fda766
#endif uintptr_t __stack_chk_guard = STACK_CHK_GUARD; __attribute__((noreturn))
void __stack_chk_fail(void)
{
#if __STDC_HOSTED__
abort();
#elif __is_myos_kernel
panic("Stack smashing detected");
#endif
}

Note how the secret guard value is hard-coded rather than being decided during program load. You should have the program loader (the bootloader in the case of the kernel) randomize the values. You can do this by putting the guard value in a special segment that the loader knows to randomize.

0x3: compile flag

GCC 4.1 中三个与堆栈保护有关的编译选项

. -fstack-protector: 启用堆栈保护,不过只为局部变量中含有 char 数组的函数插入保护代码
. -fstack-protector-all: 启用堆栈保护,为所有函数插入保护代码
. -fno-stack-protector: 禁用堆栈保护

Relevant Link:

http://en.wikipedia.org/wiki/Buffer_overflow_protection
http://wiki.osdev.org/Stack_Smashing_Protector
https://www.ibm.com/developerworks/cn/linux/l-cn-gccstack/
http://www.linuxfromscratch.org/hints/downloads/files/ssp.txt

4. Automatic generation of Position Independent Executables (PIEs): System Characteristic + Compiler Flag: ASLR

Standard executables have a fixed base address, and they must be loaded to this address otherwise they will not execute correctly. Position Independent Executables can be loaded anywhere in memory much like shared libraries, allowing PaX 's Address Space Layout Randomisation (ASLR) to take effect. This is achieved by building the code to be position-independent, and linking them as ELF shared objects.
类似于windows上的ASLR技术,通过将栈地址空间随机化,在很大程度上提高了黑客的shellcode的编写难度

In computing, position-independent code (PIC) or position-independent executable (PIE) is a body of machine code that, being placed somewhere in the primary memory, executes properly regardless of its absolute address.

. PIC is commonly used for shared libraries, so that the same library code can be loaded in a location in each program address space where it will not overlap any other uses of memory (for example, other shared libraries)
. PIC was also used on older computer systems lacking an MMU,[] so that the operating system could keep applications away from each other even within the single address space of an MMU-less system.

Position-independent code can be executed at any memory address without modification. This differs from relocatable code, where a link editor or program loader modifies a program before execution, so that it can be run only from a particular memory location.

0x1: Technical details

Procedure calls inside a shared library are typically made through small procedure linkage table stubs, which then call the definitive function. This notably allows a shared library to inherit certain function calls from previously loaded libraries rather than using its own versions.
Data references from position-independent code are usually made indirectly, through global offset tables (GOTs), which store the addresses of all accessed global variables. There is one GOT per compilation unit or object module, and it is located at a fixed offset from the code (although this offset is not known until the library is linked). When a linker links modules to create a shared library, it merges the GOTs and sets the final offsets in code. It is not necessary to adjust the offsets when loading the shared library later.
Position independent functions accessing global data start by determining the absolute address of the GOT given their own current program counter value.

0x2: compile flag

. -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 and 32k on the m68k and RS/. 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/ is always position-independent.
When this flag is set, the macros __pic__ and __PIC__ are defined to . . -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 the 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 . . -fpie
. -fPIE
These options are similar to -fpic and -fPIC, but generated position independent code can be only linked into executables. Usually these options are used when -pie GCC option is used during linking.
-fpie and -fPIE both define the macros __pie__ and __PIE__. The macros have the value for -fpie and for -fPIE.

Position Independent Executables (PIE) are an output of the hardened package build process. A PIE binary and all of its dependencies are loaded into random locations within virtual memory each time the application is executed. This makes Return Oriented Programming (ROP) attacks much more difficult to execute reliably.

要实现PIE安全加固技术,需要操作系统和编译器同时提供支持,即操作系统提供基础能力,编译器基于此对待编译的程序进行处理

. Linux kernel features
Address Space Layout Randomization (ASLR) can help defeat certain types of buffer overflow attacks. ASLR can locate the base, libraries, heap, and stack at random positions in a process's address space, which makes it difficult for an attacking program to predict the memory address of the next instruction
) echo value > /proc/sys/kernel/randomize_va_space
) vim /etc/sysctl.conf : kernel.randomize_va_space = value : sysctl -p . compiler flag
-fpie、-fpic

Relevant Link:

http://en.wikipedia.org/wiki/Position-independent_code
https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
https://securityblog.redhat.com/2012/11/28/position-independent-executables-pie/

5. Default to marking read-only, sections that can be so marked after the loader is finished (RELRO): Compiler Flag + System Characteristic: DEP / NX

There are several sections that need to be writable by the loader before the application starts, but do not need to be writable by the application itself later. Setting relro instructs the linker to record which sections this applies to, and the loader will mark them read-only before passing or returning execution control to the application. Typical sections affected include .ctors, .dtors, .jcr, .dynamic and .got, although the exact list varies according to arch.

The difference between Partial RELRO and Full RELRO is that the Global Offset Table (and Procedure Linkage Table) which act as kind-of process-specific lookup tables for symbols (names that need to point to locations elsewhere in the application or even in loaded shared libraries) are marked read-only too in the Full RELRO. Downside of this is that lazy binding (only resolving those symbols the first time you hit them, making applications start a bit faster) is not possible anymore.

0x1: GOT Overwrite Demo

// Include standard I/O declarations
#include <stdio.h>
// Include string declarations
#include <string.h> // Program entry point
int main(int argc, char** argv)
{
// Terminate if program is not run with three parameters.
if (argc != )
{
// Print out the proper use of the program
puts("./a.out <size> <offset> <string>");
// Return failure
return -;
} // Convert size to an integer
int size = atoi(argv[]);
// Convert offset to an integer
int offset = atoi(argv[]);
// Place string into its own string on the stack
char* str = argv[];
// Declare a 256 byte buffer on the stack
char buffer[]; // Print the location of the buffer for calculating the offset.
printf("Buffer:\t\t%8x\n", &buffer); // Fill the buffer with the letter 'A'.
memset(buffer, , - );
// Null-terminate the buffer.
buffer[] = ;
// Attempt to copy the specified string into the specified location.
strncpy(buffer + offset, str, size);
// Print out the buffer.
printf("%s", buffer); // Return success
return ;
}
//gcc -g -O0 -Wl,-z,norelro -fno-stack-protector -o a.out a.c

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABOwAAABFCAIAAAB2e+7nAAALAUlEQVR4nO3dW5KjNgAFUNbFgliPVqPN6DObmD/ng5ckBKZNe8Z0n1Opiueah4Ckem4Lm64DAACAj/fnz5///vvvX48CAAAATlBiAQAAuA0lFgAAgNv4ZyU2pMfj8Xg84vAPdg4AAMAtNUrsWC/fWi77kMYdLC/qN2ZDfKzWd5Y4hX67cBFeN/ftYrN9SOVJWnd+vLWdI7pk3uarm5vW36w+xBT6kK6Ncogf9ZuKzYV7fckhfuGcN6/R1QsHAAC/T7PEfutfqVslKKS5ED6rSEOcluzXdbq16Q5xKY1DnLvAt7amkJqdeIgp5ENf9pmPs6l5RN801EuHvV19vDiXN/vNv1Oofa1l1xfu4pJfPTnN5b/5/zgAAPjRnpbYca4ohaGcoO3n6cm84m3DYtpxXXvcRV9u8mhWqg+pPcM5xG2el9h1SDGlOOQDiuMhpdAXk6jFMJbGuRlO1e7WBadO1dzRmSPKD6Nx5tcD2pTr1y9cY/X2YX5Z+QuFfOdPVtyMc532nI9sPcrTM9vnj+jkkiHFuJn/P32NdsL9awQAAJyaiR3i/PfpqasN8ZF1tnUCdBvuzcTGYWxxm3d3/pbf/tt8vXZf3wq9dstlonYe2zTFOi+Rb2l53YcY4+bDu3P/LHa+LR47Ozo+okp95vPqu6nBr1649uprETtVD/fGXx7mMCxV/PD42+PMRpj98uL8TGzzwl1bcr3juJyLr7aTLf+0xB5dIwAA4GSJrapI8edl6rERdoclNg5nSuxe4elDak/o5bN/xYRoMZ7pX8uwty+KDz2uG13ez4fah7isNBzv6Cs3Eldnvq+aZTlT9+KFe7J6a677pM3Fzcd/dAZ2xnmxxDYv3MUlt0P66jWqw8NrBAAA/LsSu3P/cKP3t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uJjcPY4jbvNivT3pxlvXZf3wq9Ht0yUTuPbZpinZfIt7S87kOMcfPh3bl/FjvflsOdHR0fUaU+83n13dTgVy9ce/W1LH/njc8AUChK7PpzPfupKRQKhULhQThqdaFykfLP06aaYfGq3MWYnyix/U7h60NqfwA1n4ktJkSL8dQlfvtiWnup6PUJy4fah7isNBzvaP+IWodSLNhXzbKcTX3xwj1ZfYjPPuULAC8rSmzxY27+8SMUCoVC4UE4v/VXSuxaMI/mA7fzjfMQjr5CqbHLZTu73XKqqfm62cRl3l2b5zPrzePLvR3tHFHT8Zl/etQXSuzOjeAA8K3KbyfOftxMP7OEQqFQKDwIZ8+7UFfMzs3LN8O8s61fvXQwE1ntPV9yeav+YuBp41kbXfe4GcbhBGn9TVdZ4Zy7aXuo2btPSmzziPY0zvx+n3z1wh2sPk+YA8B7rCU2//hO9atUoVAoFApb4U6z6qt5x34bH4f5fosl49AVN9ZWoxvj1nxgfU9tVlPr/WwfZzMlKfQhPR4pdNkdw82KuG61eGfzQJ36vuW9HZ2e4dw5841jap66r1y4xnVfEiUWgHeqv50YAF7wd1pLyH/hygdol9jDG5gB4CIlFoBvUH/DLz/eNJXrmgPwtymxAAAA3IYSCwAAwG0osVzzaP0DAADwHkos12wrqxILAAC8jRLLNUosAADwF/2zEusxCT+EEgsAAPxFY4n9H4v7RsDLXZQ1AAAAAElFTkSuQmCC" alt="" />

这个示例DEMO展示了一个内存复制导致的任意虚拟内存覆盖的漏洞,黑客可以借此覆盖进程的GOT表,以此实现函数劫持的目的
首先获取GOT虚拟内存地址
readelf -a a.out

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeYAAAAoCAIAAACkZM7oAAAE0UlEQVR4nO2d24GEIAxFrcuCqIdqaIZi3A+VBAiKwywT8J4vJ8tDQgiIkl2Wn7Nav+14u0L4RGjcduJMrb4JhS0aRdimeevPsozbNmdCFc6wsll1qhAV8pLaG0ccAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPXs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/zk0WjBBSeT9ik+aRbo/PQ3jntoVXqY0bPV3uPgNf6URj4RqFwOc+Bxi57jIDC8wmbNH9kPsNQMNdjvTPqj+mW7PMNtfcIeK0fheElFQpDcJlC1BfVAYXnEzZpfj09s/dR5PHzSnkIQ1kh76i9ccRNgsIBqVAoRbnkj9SqAwrPJ2zTvHHertY7a/1+YfYys85WyZtddo+A1/phetfy2KtSuBFiyufW++sWzSJ8rHl6J2HZ3rXYxQoRFfLG2rsv8bWgMCKwQmGAryzGCCg8o7BR8+Hf49EnQPHeqOZ19rV9zl17j4DXA6AwIrBC4bIIHxgNElB4QmGb5ikW/v4vI0IVx1622Ptq+O1+7m9r7xDwGgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIPyB/5skzNTgX30AAAAAElFTkSuQmCC" alt="" />

计算偏移量并进行溢出POC测试

(gdb) x 0x0600a58
0x600a58 <_GLOBAL_OFFSET_TABLE_>: 0x006008c0
(gdb) p -(0x1c6f0940 - 0x0600a58) % 0x80000000
$ =
(gdb) r \$\$\$\$
Starting program: /zhenghan/relro/a.out \$\$\$\$
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
Buffer: ffffe800 Program received signal SIGSEGV, Segmentation fault.
0x0000003e72e78fc9 in strncpy () from /lib64/libc.so.

Here, we see that we can overwrite the GOT entry for printf with our string. The program crashes because it’s trying to jump to memory that is not mapped.

0x2: RELRO: RELocation Read-Only

To prevent the above exploitation technique, we can tell the linker to resolve all dynamically linked functions at the beginning of execution and make the GOT read-only.

gcc -g -O0 -Wl,-z,relro,-z,now -fno-stack-protector -o b.out a.c 

首先获取GOT虚拟内存地址
readelf -a a.out

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdwAAAAkCAIAAACsdxxiAAAEdklEQVR4nO2dbaLsEAyGrcuCrMdqbMZien8oQpnR6jWJ8z6/Ojk+DtGUlFSp9Wjrj4C3GkKVRMdxHIczQeJtI2mzzHH4tZ2h8KqObvZhrI/ZjDsO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wmzQXhw4vTMHAP7EONivTOPVLmSd6LqyKx9Su+CYBickKGQui/Sn6XENtxPOBXEQEfb630RmTpeMQ+PNznqRNf+TvAK/jC85RgKCV+C2I7DsJlShMTlYMxtw3B+lsJZ68OFCWXG0nlb5b9slOf0LgfSs1wWpwyFlJa9gPvid8LblsE4b0MJlviQm7H0GDI56vapffk0fR0MI8byE5afwsqOZhkBZ/cTkjvyyc2ZPnKWg6OWPkrOc+V3ggLLrH1S73JgGDFWtHAchv+8FGEjGPUNbPoadPhoQKri9ClP6HQBv/Wr/rb2Ob0DAAAAAAAAAAAAbMk/Td71fsSfzDAAAAAASUVORK5CYII=" alt="" />

(gdb) x 0x0600a58
0x600a58 <_GLOBAL_OFFSET_TABLE_>: 0x006008c0
(gdb) p -(0x7fffffffe800 - 0x0600a58) % 0x80000000
$ = -
(gdb) r - hello
Starting program: /zhenghan/relro/a.out - hello
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x2aaaaaaab000
Buffer: ffffe7f0 Program received signal SIGSEGV, Segmentation fault.
0x0000003e72e78fc9 in strncpy () from /lib64/libc.so.

Here, we see that we cannot overwrite the GOT entry for printf with our string. The program crashes because it trying to write to a memory segment that is read-only.

要实现RELRO安全加固技术,需要操作系统和编译器同时提供支持,即操作系统提供基础能力,编译器基于此对待编译的程序进行处理

. Linux kernel features
The Data Execution Prevention (DEP) feature prevents an application or service from executing code in a non-executable memory region. Hardware-enforced DEP works in conjunction with the NX (Never eXecute) bit on compatible CPUs.
) You cannot disable the DEP feature. . compiler flag
relro

Relevant Link:

http://larrythecow.org/universe/archives/2011-07-16.html.gz
https://isisblogs.poly.edu/2011/06/01/relro-relocation-read-only/
http://docs.oracle.com/cd/E37670_01/E36387/html/ol_kernel_sec.html
http://www.win.tue.nl/~aeb/linux/hh/protection.html

6. Default full binding at load-time (BIND_NOW)

To reduce the time between starting an application and actually being able to use it, most software is built with "lazy binding". This means that references to functions in shared libraries are resolved when they are actually used for the first time, rather than when the application is loaded. The hardened toolchain changes this behaviour so that by default it will set the "BIND_NOW" flag, which causes the loader to sort out all of these links before starting execution. It improves the effectiveness of RELROintro

Relevant Link:

https://wiki.ubuntu.com/Security/Features

7. Heap Protector

The GNU C Library heap protector (both automatic via ptmalloc and manual) provides corrupted-list/unlink/double-free/overflow protections to the glibc heap memory manager (first introduced in glibc 2.3.4). This stops the ability to perform arbitrary code execution via heap memory overflows that try to corrupt the control structures of the malloc heap memory areas.
This protection has evolved over time, adding more and more protections as additional corner-cases were researched. As it currently stands, glibc 2.10 and later appears to successfully resist even these hard-to-hit conditions.

8. Pointer Obfuscation

Some pointers stored in glibc are obfuscated via PTR_MANGLE/PTR_UNMANGLE macros internally in glibc, preventing libc function pointers from being overwritten during runtime.

Relevant Link:

http://udrepper.livejournal.com/13393.html

9. Built with Fortify Source

Programs built with "-D_FORTIFY_SOURCE=2" (and -O1 or higher), enable several compile-time and run-time protections in glibc:

. expand unbounded calls to "sprintf", "strcpy" into their "n" length-limited cousins when the size of a destination buffer is known (protects against memory overflows).
. stop format string "%n" attacks when the format string is in a writable memory segment.
. require checking various important function return codes and arguments (e.g. system, write, open).
. require explicit file mask when creating new files.

10. /proc/$pid/maps protection

With ASLR, a process's memory space layout suddenly becomes valuable to attackers. The "maps" file is made read-only except to the process itself or the owner of the process. Went into mainline kernel with sysctl toggle in 2.6.22. The toggle was made non-optional in 2.6.27, forcing the privacy to be enabled regardless of sysctl settings (this is a good thing).

11. ptrace scope

A troubling weakness of the Linux process interfaces is that a single user is able to examine the memory and running state of any of their processes. For example, if one application was compromised, it would be possible for an attacker to attach to other running processes (e.g. SSH sessions, GPG agent, etc) to extract additional credentials and continue to immediately expand the scope of their attack without resorting to user-assisted phishing or *s.
In Ubuntu 10.10 and later, users cannot ptrace processes that are not a descendant of the debugger. The behavior is controllable through the/proc/sys/kernel/yama/ptrace_scope sysctl, available via Yama(centos kernel not support)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbIAAAAmCAIAAAAukYmqAAAF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BwUBatEM8HblIWhyYde3hICdwpWew9emx0yaws6uGz1fCgzMtaFg+qpWYOZMtyykcgCEILz8uiZfx+xgdYlooCFbo579Fy5tmiC7nWHiIH2ncUqubV1aJhfAvQ9LRCASmcSxtkpzlQn1SlYxkjoyVckNKciO/u2qKjcWAml+wpnF0jG49g4CxZPAixGAMGzkgG6+FDMxCK5vyX3zhwuE8oQcCjp796B7Koa9AyJCzUreq9f5umrDRiO9nUQfXRTVsctWVxXbJA+d31mI/VQqzvtcn99onX6CGmOqoNFY8/ULk3L4v1EcQY6VI3Sembgn7TOV5aUYq5EH0dqez+uplfrEk27AQDwYnb50Rajluafba5gDo4k0tofJjeo1u2yboucHUbsT8BaF1RIZ4NXDNWfYgJoolzCUVzGLouHH071CfKJcOjJ9+J4+vckHd7YcHH4J7fGjwALm2OnnL+2kAjjmP02OP2Sxma9MsvYN0TuBdzjUf84z+N9dyeVF79zyI/L0Y3efTqwP0+V3lEWSSEXE739fBbCzRlkRBCFJRFQghRUBYJIURBWSSEEAVlkRBCFIYsuv6fBhJCyJ8AyiL8p4GEEPInGGyiKYuEkL8IZZEQQhSURUIIUVAWCSFEQVkkhBCF9U30+/81NyGEXAJf5yaEEAVlkRBCFJRFQghRUBYJIURBWfwcNvRDCDkLZfFzoCwS8itQFj8HyiIhv8E/r2I16SHcxsUAAAAASUVORK5CYII=" alt="" />

12. /dev/mem protection

Some applications (Xorg) need direct access to the physical memory from user-space. The special file /dev/mem exists to provide this access. In the past, it was possible to view and change kernel memory from this file if an attacker had root access. The CONFIG_STRICT_DEVMEM kernel option was introduced to block non-device memory access (originally named CONFIG_NONPROMISC_DEVMEM).

13. Block module loading

In Ubuntu 8.04 LTS and earlier, it was possible to remove CAP_SYS_MODULES from the system-wide capability bounding set, which would stop any new kernel modules from being loaded. This was another layer of protection to stop kernel rootkits from being installed. The 2.6.25 Linux kernel (Ubuntu 8.10) changed how bounding sets worked, and this functionality disappeared. Starting with Ubuntu 9.10, it is now possible to block module loading again by setting "1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5Piq/qpha7ndb1jXQ8T9YlSUc2lGef7Lr06TxVfZYVSt1Y2HIzrLKmMU2ewOjIS31iiyiMouefFV91CaYfoTF4Nd/dxWqA/5YpktGGtu1XFU17hjxPfduLEQAqj7RxpLUGf1eZX1/APcqmi9VGrxO8W376fpxk9Ua+Jb9snTsVZ8YXO/w3xlZMahwwPllOYlxydyq+atrJlest6IDuF4l4XX8358x3rYIldFNtwmPMj2q7s+RoX1w1dSkPnEk7U77LyVZyvARdkuA8CwZ1LG+SnaqhNqtRxOUdKS6gmISbFk9vOdTubS3oIx3diKL4wdHWlX8ez/fCAtA791INcoVgfGCrFF6SiyKVKJcbOd116dYD4yvo+BQOuXvGStqjWRMuYP0pNl7fU6kKvXnLk+2d9CDkfj+Wisu21usLqncje5rI4in4KWS1d58U3b9r0KYfUBCk8J2qX6f1qCKW8cd5mS2kHoIn8pgVZ8RMYgoE7YwIfu4iWap91LqAOruQSsg/Tu3UpL3RO6YwOwtCVKe+T1Maj3tq484mtD/3spxiyPr6KQcrFVAxDHj217zivBOQt4JcsIK/22NR6Z4ZrxjvApdBo9/lhhnqM5+h6n7fzgi6RVfDrxZK5Rw9/m/wZojUR4hyRj4DiSwgRNB8s4J3uFii+hBCyAIovIYQsgOJLCCELoPgSQsgCKL6EELKA7g/r8AknIYTcAxTf/CXsJR/sJ4SQjweKb+d3OgghhDyAkfi+3BdtCSHkE6D4EkLIArjtQAghC1AeuKGfryWEEPIo8EfNlJ+vJYQQ8hj4JQtCCFkAxZcQQhZA8SWEkAVQfAkhZAEU38/hQP8IIa8JxfdzoPgS8kZQfD8Hii8hbwT/6/jPgeJLyPvwH6/USA2tE6iAAAAAAElFTkSuQmCC" alt="" />

14. Syscall Filtering

Programs can filter out the availability of kernel syscalls by using the seccomp_filter interface. This is done in containers or sandboxes that want to further limit the exposure to kernel interfaces when potentially running untrusted software.

15. How To Harden Linux

要实现系统级的安全加固,防御Error Based DOS、Buffer Overflow攻击,需要从Linux内核配置和编译器的编译选项两个方面同时入手,但是大多数时候,编译器选项是我们没法控制的,我们只能从系统内核配置这个角度入手

0x1: Ubuntu

. echo  > /proc/sys/kernel/randomize_va_space
. echo > /proc/sys/kernel/yama/protected_sticky_symlinks
. echo > /proc/sys/kernel/yama/protected_nonaccess_hardlinks
. echo > /proc/sys/kernel/yama/ptrace_scope
. echo > /proc/sys/kernel/modules_disabled: 需要慎重,这个Kernel选项禁止root用户加载LKM
. echo > /proc/sys/kernel/kptr_restrict
. echo > /proc/sys/kernel/dmesg_restrict: 需要慎重,这个Kernel选项禁止root用户查看内核调试信息

0x2: Centos

Relevant Link:

https://fedoraproject.org/wiki/SecurityBasics
https://wiki.ubuntu.com/Security/Features
http://wiki.centos.org/HowTos/Custom_Kernel
https://www.usenix.org/legacy/event/sec08/tech/full_papers/dalton/dalton_html/
https://www.blackhat.com/presentations/bh-usa-04/bh-us-04-silberman/bh-us-04-silberman-paper.pdf
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.208.9387&rep=rep1&type=pdf
http://www.informit.com/articles/article.aspx?p=2036582&seqNum=6

Copyright (c) 2015 LittleHann All rights reserved