명령어 : echo 0 > /proc/sys/kernel/randomize_va_space
옵션
0 : ASLR 해제
1 : 랜덤 스택 & 랜덤 라이브러리 설정
2 : 랜덤 스택 & 랜덤 라이브러리 설정 & 랜덤 힙 설정
❯ checksec --proc ASLR
* System-wide ASLR (kernel.randomize_va_space): Full (Setting: 2)
Description - Make the addresses of mmap base, heap, stack and VDSO page randomized.
This, among other things, implies that shared libraries will be loaded to random
addresses. Also for PIE-linked binaries, the location of code start is randomized.
See the kernel file 'Documentation/sysctl/kernel.txt' for more details.
* Does the CPU support NX: Yes
COMMAND PID RELRO STACK CANARY NX/PaX PIE FORTIFY
| Memory area | 첫번째 실행 | 두번째 실행 |
|---|---|---|
| Stack | 0x7ffea624c000 ~ 0x7ffea626d000 | 0x7ffc75b23000 ~ 0x7ffc75b44000 |
| Libc | 0x7fb51e026000 ~ 0x7fb51e618000 | 0x7f6adb39b000 ~ 0x7f6adb98d000 |
| Heap | 0x0229e000 ~ 0x022bf000 | 0x01198000 ~ 0x011b9000 |
/proc : process의 줄임말이며, 이 디렉토리에 프로세스의 정보들이 저장됩니다.
/proc/self : 현재 실행되고 있는 프로세스의 정보가 담겨있는 디렉토리입니다.
/proc/self/maps : 현재 실행되고 있는 프로세스의 주소 맵입니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *global = "Lazenca.0x0";
int main(){
char *heap = malloc(100);
char *stack[] = {"LAZENCA.0x0"};
printf("[Heap] address: %p\n", heap);
printf("[Stack] address: %p\n", stack);
printf("[libc] address: %p\n",**(&stack + 3));
printf("[.data] address: %p\n",global);
gets(heap);
return 0;
}
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7fffffffe550
[libc] address: 0x7ffff7dee083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7fffffffe550
[libc] address: 0x7ffff7dee083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7fffffffe550
[libc] address: 0x7ffff7dee083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7ffd20331870
[libc] address: 0x7f151ca9e083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7fff1cc94850
[libc] address: 0x7f54005fb083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5555555592a0
[Stack] address: 0x7ffe4e92c970
[libc] address: 0x7f5810012083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x55a5438c42a0
[Stack] address: 0x7ffc850a2de0
[libc] address: 0x7f06e06c9083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x56535a9f52a0
[Stack] address: 0x7fff8c206c00
[libc] address: 0x7f43326d1083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x5610becc32a0
[Stack] address: 0x7ffca8604b70
[libc] address: 0x7f09e251e083
[.data] address: 0x555555556004
^C
root# ./ASLR
[Heap] address: 0x55a5438c42a0
[Stack] address: 0x7ffc850a2de0
[libc] address: 0x7f06e06c9083
[.data] address: 0x55a5438c1004
^C
root# ./ASLR
[Heap] address: 0x56535a9f52a0
[Stack] address: 0x7fff8c206c00
[libc] address: 0x7f43326d1083
[.data] address: 0x56535a9f2004
^C
root# ./ASLR
[Heap] address: 0x5610becc32a0
[Stack] address: 0x7ffca8604b70
[libc] address: 0x7f09e251e083
[.data] address: 0x5610becc0004
^C
Checksec.sh - line 285
# check for system-wide ASLR support
aslrcheck() {
# PaX ASLR support
if !(cat /proc/1/status 2> /dev/null | grep -q 'Name:') ; then
echo -n -e ':\033[33m insufficient privileges for PaX ASLR checks\033[m\n'
echo -n -e ' Fallback to standard Linux ASLR check'
fi
if cat /proc/1/status 2> /dev/null | grep -q 'PaX:'; then
printf ": "
if cat /proc/1/status 2> /dev/null | grep 'PaX:' | grep -q 'R'; then
echo -n -e '\033[32mPaX ASLR enabled\033[m\n\n'
else
echo -n -e '\033[31mPaX ASLR disabled\033[m\n\n'
fi
else
# standard Linux 'kernel.randomize_va_space' ASLR support
# (see the kernel file 'Documentation/sysctl/kernel.txt' for a detailed description)
printf " (kernel.randomize_va_space): "
if /sbin/sysctl -a 2>/dev/null | grep -q 'kernel.randomize_va_space = 1'; then
echo -n -e '\033[33mOn (Setting: 1)\033[m\n\n'
printf " Description - Make the addresses of mmap base, stack and VDSO page randomized.\n"
printf " This, among other things, implies that shared libraries will be loaded to \n"
printf " random addresses. Also for PIE-linked binaries, the location of code start\n"
printf " is randomized. Heap addresses are *not* randomized.\n\n"
elif /sbin/sysctl -a 2>/dev/null | grep -q 'kernel.randomize_va_space = 2'; then
echo -n -e '\033[32mOn (Setting: 2)\033[m\n\n'
printf " Description - Make the addresses of mmap base, heap, stack and VDSO page randomized.\n"
printf " This, among other things, implies that shared libraries will be loaded to random \n"
printf " addresses. Also for PIE-linked binaries, the location of code start is randomized.\n\n"
elif /sbin/sysctl -a 2>/dev/null | grep -q 'kernel.randomize_va_space = 0'; then
echo -n -e '\033[31mOff (Setting: 0)\033[m\n'
else
echo -n -e '\033[31mNot supported\033[m\n'
fi
printf " See the kernel file 'Documentation/sysctl/kernel.txt' for more details.\n\n"
fi
}
sysctl -a | grep 'kernel.randomize_va_space = '
root$ sysctl -a | grep 'kernel.randomize_va_space = '
sysctl: permission denied on key 'fs.protected_hardlinks'
sysctl: permission denied on key 'fs.protected_symlinks'
sysctl: permission denied on key 'kernel.cad_pid'
kernel.randomize_va_space = 2
sysctl: permission denied on key 'kernel.unprivileged_userns_apparmor_policy'
sysctl: permission denied on key 'kernel.usermodehelper.bset'
sysctl: permission denied on key 'kernel.usermodehelper.inheritable'
sysctl: permission denied on key 'net.ipv4.tcp_fastopen_key'
sysctl: permission denied on key 'net.ipv6.conf.all.stable_secret'
sysctl: permission denied on key 'net.ipv6.conf.default.stable_secret'
sysctl: permission denied on key 'net.ipv6.conf.ens33.stable_secret'
sysctl: permission denied on key 'net.ipv6.conf.lo.stable_secret'