mirror of
https://github.com/nganhkhoa/CTF-All-In-One.git
synced 2024-12-24 03:01:15 +07:00
update gcc argc
This commit is contained in:
parent
1231d2e3d1
commit
bcb1c6f683
@ -13,7 +13,7 @@
|
||||
- [1.5.5 静态链接](doc/1.5.5_static_link.md)
|
||||
- [1.5.6 动态链接](doc/1.5.6_dynamic_link.md)
|
||||
- [1.5.7 内存管理](doc/1.5.7_memory.md)
|
||||
- [1.5.8 glibc malloc](doc/glibc_malloc.md)
|
||||
- [1.5.8 glibc malloc](doc/1.5.8_glibc_malloc.md)
|
||||
- [1.6 密码学基础](doc/1.6_crypto_basic.md)
|
||||
- [1.7 Android 安全基础](doc/1.7_android_basic.md)
|
||||
|
||||
@ -28,6 +28,7 @@
|
||||
- [2.8 zio](doc/2.8_zio.md)
|
||||
- [2.9 metasploit](doc/2.9_metasploit.md)
|
||||
- [2.10 binwalk](doc/2.10_binwalk.md)
|
||||
- [2.11 Burp Suite](doc/2.11_burpsuite.md)
|
||||
|
||||
- [三、分类专题篇](doc/3_topics.md)
|
||||
- [3.1 Reverse](doc/3.1_reverse.md)
|
||||
@ -40,7 +41,7 @@
|
||||
- [四、技巧篇](doc/4_tips.md)
|
||||
- [4.1 AWD模式](doc/4.1_AWD.md)
|
||||
- [4.2 Linux 命令行技巧](doc/4.2_Linux_terminal_tips.md)
|
||||
- [4.3 GCC 安全编译参数](doc/4.3_gcc.md)
|
||||
- [4.3 GCC 堆栈保护技术](doc/4.3_gcc.md)
|
||||
|
||||
- [五、高级篇](doc/5_advanced.md)
|
||||
- [5.1 Fuzz 测试](doc/5.1_fuzz.md)
|
||||
|
@ -14,7 +14,7 @@
|
||||
* [1.5.5 静态链接](doc/1.5.5_static_link.md)
|
||||
* [1.5.6 动态链接](doc/1.5.6_dynamic_link.md)
|
||||
* [1.5.7 内存管理](doc/1.5.7_memory.md)
|
||||
* [1.5.8 glibc malloc](doc/glibc_malloc.md)
|
||||
* [1.5.8 glibc malloc](doc/1.5.8_glibc_malloc.md)
|
||||
* [1.6 密码学基础](doc/1.6_crypto_basic.md)
|
||||
* [1.7 Android 安全基础](doc/1.7_android_basic.md)
|
||||
* [二、工具篇](doc/2_tools.md)
|
||||
@ -28,6 +28,7 @@
|
||||
* [2.8 zio](doc/2.8_zio.md)
|
||||
* [2.9 metasploit](doc/2.9_metasploit.md)
|
||||
* [2.10 binwalk](doc/2.10_binwalk.md)
|
||||
* [2.11 Burp Suite](doc/2.11_burpsuite.md)
|
||||
* [三、分类专题篇](doc/3_topics.md)
|
||||
* [3.1 Reverse](doc/3.1_reverse.md)
|
||||
* [3.2 Crypto](doc/3.2_crypto.md)
|
||||
@ -38,7 +39,7 @@
|
||||
* [四、技巧篇](doc/4_tips.md)
|
||||
* [4.1 AWD模式](doc/4.1_AWD.md)
|
||||
* [4.2 Linux 命令行技巧](doc/4.2_Linux_terminal_tips.md)
|
||||
* [4.3 GCC 安全编译参数](doc/4.3_gcc.md)
|
||||
* [4.3 GCC 堆栈保护技术](doc/4.3_gcc.md)
|
||||
* [五、高级篇](doc/5_advanced.md)
|
||||
* [5.1 Fuzz 测试](doc/5.1_fuzz.md)
|
||||
* [5.2 Pin 动态二进制插桩](doc/5.2_pin.md)
|
||||
|
@ -12,6 +12,6 @@
|
||||
- [1.5.5 静态链接](1.5.5_static_link.md)
|
||||
- [1.5.6 动态链接](1.5.6_dynamic_link.md)
|
||||
- [1.5.7 内存管理](1.5.7_memory.md)
|
||||
- [1.5.8 glibc malloc](doc/glibc_malloc.md)
|
||||
- [1.5.8 glibc malloc](1.5.8_glibc_malloc.md)
|
||||
- [1.6 密码学基础](1.6_crypto_basic.md)
|
||||
- [1.7 Android 安全基础](1.7_android_basic.md)
|
||||
|
1
doc/2.11_burpsuite.md
Normal file
1
doc/2.11_burpsuite.md
Normal file
@ -0,0 +1 @@
|
||||
# Burp Suite
|
@ -10,3 +10,4 @@
|
||||
- [2.8 zio](2.8_zio.md)
|
||||
- [2.9 metasploit](2.9_metasploit.md)
|
||||
- [2.10 binwalk](2.10_binwalk.md)
|
||||
- [2.11 Burp Suite](2.11_burpsuite.md)
|
||||
|
197
doc/4.3_gcc.md
197
doc/4.3_gcc.md
@ -1 +1,196 @@
|
||||
# GCC 安全编译参数
|
||||
# GCC 堆栈保护技术
|
||||
|
||||
- [技术简介](#技术简介)
|
||||
- [编译参数](#编译参数)
|
||||
- [保护技术检测](#保护技术检测)
|
||||
|
||||
|
||||
## 技术简介
|
||||
Linux 中有各种各样的安全防护,其中 ASLR 是由内核直接提供的,通过系统配置文件控制。NX,Canary,PIE,RELRO 等需要在编译时根据各项参数开启或关闭。未指定参数时,使用默认设置。
|
||||
|
||||
#### CANARY
|
||||
启用 CANARY 后,函数开始执行的时候会先往栈里插入 canary 信息,当函数返回时验证插入的 canary 是否被修改,如果是,就停止运行。
|
||||
|
||||
#### FORTIFY
|
||||
FORTIFY 的选项 `-D_FORTIFY_SOURCE` 往往和优化 `-O` 选项一起使用,以检测缓冲区溢出的问题。
|
||||
|
||||
下面是一个简单的例子:
|
||||
```c
|
||||
#include<string.h>
|
||||
void main() {
|
||||
char str[3];
|
||||
strcpy(str, "abcde");
|
||||
}
|
||||
```
|
||||
```text
|
||||
$ gcc -O2 fortify.c
|
||||
$ checksec --file a.out
|
||||
RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
|
||||
Partial RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH No 0 0a.out
|
||||
|
||||
$ gcc -O2 -D_FORTIFY_SOURCE=2 fortify.c
|
||||
In file included from /usr/include/string.h:639:0,
|
||||
from fortify.c:1:
|
||||
In function ‘strcpy’,
|
||||
inlined from ‘main’ at fortify.c:4:2:
|
||||
/usr/include/bits/string3.h:109:10: warning: ‘__builtin___memcpy_chk’ writing 6 bytes into a region of size 3 overflows the destination [-Wstringop-overflow=]
|
||||
return __builtin___strcpy_chk (__dest, __src, __bos (__dest));
|
||||
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
$ checksec --file a.out
|
||||
RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
|
||||
Partial RELRO Canary found NX enabled PIE enabled No RPATH No RUNPATH Yes 2 2a.out
|
||||
```
|
||||
开启优化 `-O2` 后,编译没有检测出任何问题,checksec 后 FORTIFY 为 No。当配合 `-D_FORTIFY_SOURCE=2`(也可以 `=1`)使用时,提示存在溢出问题,checksec 后 FORTIFY 为 Yes。
|
||||
|
||||
#### NX
|
||||
No-eXecute,表示不可执行,其原理是将数据所在的内存页标识为不可执行,如果程序产生溢出转入执行 shellcode 时,CPU 会抛出异常。其绕过方法是 ret2libc。
|
||||
|
||||
#### PIE
|
||||
PIE(Position Independent Executable)需要配合 ASLR 来使用,以达到可执行文件的加载时地址随机化。简单来说,PIE 是编译时随机化,由编译器完成;ASLR 是加载时随机化,有操作系统完成。
|
||||
|
||||
我们通过实际例子来探索一下 PIE 和 ASLR:
|
||||
```c
|
||||
#include<stdio.h>
|
||||
void main() {
|
||||
printf("%p\n", main);
|
||||
}
|
||||
```
|
||||
首先我们关闭 ASLR,使用 `-pie` 进行编译:
|
||||
```
|
||||
# echo 0 > /proc/sys/kernel/randomize_va_space
|
||||
# gcc -m32 -pie random.c -o a.out
|
||||
# checksec --file a.out
|
||||
RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
|
||||
Partial RELRO No canary found NX enabled PIE enabled No RPATH No RUNPATH No 0 2 a.out
|
||||
|
||||
# ./a.out
|
||||
0x5655553d
|
||||
# ./a.out
|
||||
0x5655553d
|
||||
```
|
||||
我们虽然开启了 `-pie`,但是 ASLR 被关闭,入口地址不变。
|
||||
```
|
||||
# ldd a.out
|
||||
linux-gate.so.1 (0xf7fd7000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7dd9000)
|
||||
/lib/ld-linux.so.2 (0xf7fd9000)
|
||||
# ldd a.out
|
||||
linux-gate.so.1 (0xf7fd7000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7dd9000)
|
||||
/lib/ld-linux.so.2 (0xf7fd9000)
|
||||
```
|
||||
可以看出动态链接库地址也不变。然后我们开启 ASLR:
|
||||
```
|
||||
# echo 2 > /proc/sys/kernel/randomize_va_space
|
||||
# ./a.out
|
||||
0x5665353d
|
||||
# ./a.out
|
||||
0x5659753d
|
||||
# ldd a.out
|
||||
linux-gate.so.1 (0xf7727000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7529000)
|
||||
/lib/ld-linux.so.2 (0xf7729000)
|
||||
# ldd a.out
|
||||
linux-gate.so.1 (0xf77d6000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf75d8000)
|
||||
/lib/ld-linux.so.2 (0xf77d8000)
|
||||
```
|
||||
入口地址和动态链接库地址都变得随机。
|
||||
|
||||
接下来关闭 ASLR,并使用 `-no-pie` 进行编译:
|
||||
```
|
||||
# echo 0 > /proc/sys/kernel/randomize_va_space
|
||||
# gcc -m32 -no-pie random.c -o b.out
|
||||
# checksec --file b.out
|
||||
RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
|
||||
Partial RELRO No canary found NX enabled No PIE No RPATH No RUNPATH No 0 2 b.out
|
||||
|
||||
# ./b.out
|
||||
0x8048406
|
||||
# ./b.out
|
||||
0x8048406
|
||||
# ldd b.out
|
||||
linux-gate.so.1 (0xf7fd7000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7dd9000)
|
||||
/lib/ld-linux.so.2 (0xf7fd9000)
|
||||
# ldd b.out
|
||||
linux-gate.so.1 (0xf7fd7000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7dd9000)
|
||||
/lib/ld-linux.so.2 (0xf7fd9000)
|
||||
```
|
||||
入口地址和动态库都是固定的。下面开启 ASLR:
|
||||
```
|
||||
# echo 2 > /proc/sys/kernel/randomize_va_space
|
||||
# ./b.out
|
||||
0x8048406
|
||||
# ./b.out
|
||||
0x8048406
|
||||
# ldd b.out
|
||||
linux-gate.so.1 (0xf7797000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf7599000)
|
||||
/lib/ld-linux.so.2 (0xf7799000)
|
||||
# ldd b.out
|
||||
linux-gate.so.1 (0xf770a000)
|
||||
libc.so.6 => /usr/lib32/libc.so.6 (0xf750c000)
|
||||
/lib/ld-linux.so.2 (0xf770c000)
|
||||
```
|
||||
入口地址依然固定,但是动态库变为随机。
|
||||
|
||||
所以在分析一个 PIE 开启的二进制文件时,只需要关闭 ASLR,即可使 PIE 和 ASLR 都失效。
|
||||
|
||||
> ASLR(Address Space Layout Randomization)
|
||||
>
|
||||
>关闭:`# echo 0 > /proc/sys/kernel/randomize_va_space`
|
||||
>
|
||||
>部分开启(将 mmap 的基址,stack 和 vdso 页面随机化):`# echo 1 > /proc/sys/kernel/randomize_va_space`
|
||||
>
|
||||
>完全开启(在部分开启的基础上增加 heap的随机化:`# echo 2 > /proc/sys/kernel/randomize_va_space`
|
||||
|
||||
#### RELOAD
|
||||
RELRO 设置符号重定向表为只读或在程序启动时就解析并绑定所有动态符号,从而减少对 GOT(Global Offset Table)的攻击。
|
||||
|
||||
|
||||
## 编译参数
|
||||
各种安全技术的编译参数如下:
|
||||
|
||||
安全技术 | 完全开启 | 部分开启 | 关闭
|
||||
--- | --- | --- |
|
||||
Canary | -fstack-protector-all | -fstack-protector | -fno-stack-protector
|
||||
NX | -z noexecstack | | -z execstack
|
||||
PIE | -pie | | -no-pie
|
||||
RELRO | -z now | -z lazy | -z norelro
|
||||
|
||||
关闭所有保护:
|
||||
```text
|
||||
gcc hello.c -o hello-L -fstack-protector -z execstack -no-pie -z norelro
|
||||
```
|
||||
开启所有保护:
|
||||
```text
|
||||
gcc hello.c -o hello-S -fstack-protector-all -z noexecstack -pie -z now
|
||||
```
|
||||
|
||||
- FORTIFY
|
||||
- `-D_FORTIFY_SOURCE=1`:仅在编译时检测溢出
|
||||
- `-D_FORTIFY_SOURCE=1`:在编译时和运行时检测溢出
|
||||
|
||||
|
||||
## 保护机制检测
|
||||
有许多工具可以检测二进制文件所使用的编译器安全技术。下面介绍常用的几种:
|
||||
|
||||
#### checksec
|
||||
```text
|
||||
$ checksec --file /bin/ls
|
||||
RELRO STACK CANARY NX PIE RPATH RUNPATH FORTIFY Fortified Fortifiable FILE
|
||||
Partial RELRO Canary found NX enabled No PIE No RPATH No RUNPATH Yes 5 15 /bin/ls
|
||||
```
|
||||
|
||||
#### peda 自带的 checksec
|
||||
```text
|
||||
$ gdb /bin/ls
|
||||
gdb-peda$ checksec
|
||||
CANARY : ENABLED
|
||||
FORTIFY : ENABLED
|
||||
NX : ENABLED
|
||||
PIE : disabled
|
||||
RELRO : Partial
|
||||
```
|
||||
|
@ -2,4 +2,4 @@
|
||||
|
||||
- [4.1 AWD模式](4.1_AWD.md)
|
||||
- [4.2 Linux 命令行技巧](4.2_Linux_terminal_tips.md)
|
||||
- [4.3 GCC 安全编译参数](4.3_gcc.md)
|
||||
- [4.3 GCC 堆栈保护技术](4.3_gcc.md)
|
||||
|
Loading…
Reference in New Issue
Block a user