|
| 1 | +# ASLR in Depth |
| 2 | + |
| 3 | +Actually, the title is a lie. We're not really going to discuss ASLR in that |
| 4 | +depth yet. We don't really need to. However, what we are going to do is explore |
| 5 | +the effects of ASLR on a diagnostic binary we used in the previous section. |
| 6 | + |
| 7 | +```c |
| 8 | +#define _GNU_SOURCE |
| 9 | +#include <stdlib.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <dlfcn.h> |
| 12 | +#include <unistd.h> |
| 13 | + |
| 14 | + |
| 15 | +int main() { |
| 16 | + puts("This program helps visualise where libc is loaded.\n"); |
| 17 | + int pid = getpid(); |
| 18 | + char command[500]; |
| 19 | + puts("Memory Layout: "); |
| 20 | + sprintf(command, "cat /proc/%d/maps", pid); |
| 21 | + system(command); |
| 22 | + puts("\nFunction Addresses: "); |
| 23 | + printf("System@libc 0x%lx\n", dlsym(RTLD_NEXT, "system")); |
| 24 | + printf("PID: %d\n", pid); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +## ASLR Turned Off |
| 29 | + |
| 30 | +First, make sure ASLR is turned off. |
| 31 | + |
| 32 | +```shell |
| 33 | +ubuntu@ubuntu-xenial:/vagrant/lessons/8_aslr/build$ echo 0 | sudo tee |
| 34 | +/proc/sys/kernel/randomize_va_space |
| 35 | +0 |
| 36 | +``` |
| 37 | + |
| 38 | +Now, play with the binaries in `/vagrant/lessons/8_aslr/build/`. You should |
| 39 | +notice that the addresses the objects are mapped at are more or less constant. |
| 40 | + |
| 41 | +```shell |
| 42 | +ubuntu@ubuntu-xenial:/vagrant/lessons/8_aslr/build$ ./1_reveal_addresses |
| 43 | +This program helps visualise where libc is loaded. |
| 44 | + |
| 45 | +Memory Layout: |
| 46 | +08048000-08049000 r-xp 00000000 00:28 161 |
| 47 | +/vagrant/lessons/8_aslr/build/1_reveal_addresses |
| 48 | +08049000-0804a000 r--p 00000000 00:28 161 |
| 49 | +/vagrant/lessons/8_aslr/build/1_reveal_addresses |
| 50 | +0804a000-0804b000 rw-p 00001000 00:28 161 |
| 51 | +/vagrant/lessons/8_aslr/build/1_reveal_addresses |
| 52 | +0804b000-0806c000 rw-p 00000000 00:00 0 [heap] |
| 53 | +f7e0f000-f7e10000 rw-p 00000000 00:00 0 |
| 54 | +f7e10000-f7fbf000 r-xp 00000000 08:01 256310 |
| 55 | +/lib/i386-linux-gnu/libc-2.23.so |
| 56 | +f7fbf000-f7fc0000 ---p 001af000 08:01 256310 |
| 57 | +/lib/i386-linux-gnu/libc-2.23.so |
| 58 | +f7fc0000-f7fc2000 r--p 001af000 08:01 256310 |
| 59 | +/lib/i386-linux-gnu/libc-2.23.so |
| 60 | +f7fc2000-f7fc3000 rw-p 001b1000 08:01 256310 |
| 61 | +/lib/i386-linux-gnu/libc-2.23.so |
| 62 | +f7fc3000-f7fc6000 rw-p 00000000 00:00 0 |
| 63 | +f7fc6000-f7fc9000 r-xp 00000000 08:01 256309 |
| 64 | +/lib/i386-linux-gnu/libdl-2.23.so |
| 65 | +f7fc9000-f7fca000 r--p 00002000 08:01 256309 |
| 66 | +/lib/i386-linux-gnu/libdl-2.23.so |
| 67 | +f7fca000-f7fcb000 rw-p 00003000 08:01 256309 |
| 68 | +/lib/i386-linux-gnu/libdl-2.23.so |
| 69 | +f7fd4000-f7fd6000 rw-p 00000000 00:00 0 |
| 70 | +f7fd6000-f7fd8000 r--p 00000000 00:00 0 [vvar] |
| 71 | +f7fd8000-f7fd9000 r-xp 00000000 00:00 0 [vdso] |
| 72 | +f7fd9000-f7ffb000 r-xp 00000000 08:01 256300 |
| 73 | +/lib/i386-linux-gnu/ld-2.23.so |
| 74 | +f7ffb000-f7ffc000 rw-p 00000000 00:00 0 |
| 75 | +f7ffc000-f7ffd000 r--p 00022000 08:01 256300 |
| 76 | +/lib/i386-linux-gnu/ld-2.23.so |
| 77 | +f7ffd000-f7ffe000 rw-p 00023000 08:01 256300 |
| 78 | +/lib/i386-linux-gnu/ld-2.23.so |
| 79 | +fffdd000-ffffe000 rw-p 00000000 00:00 0 [stack] |
| 80 | + |
| 81 | +Function Addresses: |
| 82 | +System@libc 0xf7e4ada0 |
| 83 | +PID: 4452 |
| 84 | +``` |
| 85 | + |
| 86 | +```shell |
| 87 | +ubuntu@ubuntu-xenial:/vagrant/lessons/8_aslr/build$ ./4_reveal_addresses64_pie |
| 88 | +This program helps visualise where libc is loaded. |
| 89 | + |
| 90 | +Memory Layout: |
| 91 | +555555554000-555555555000 r-xp 00000000 00:28 158 |
| 92 | +/vagrant/lessons/8_aslr/build/4_reveal_addresses64_pie |
| 93 | +555555754000-555555755000 r--p 00000000 00:28 158 |
| 94 | +/vagrant/lessons/8_aslr/build/4_reveal_addresses64_pie |
| 95 | +555555755000-555555756000 rw-p 00001000 00:28 158 |
| 96 | +/vagrant/lessons/8_aslr/build/4_reveal_addresses64_pie |
| 97 | +555555756000-555555777000 rw-p 00000000 00:00 0 [heap] |
| 98 | +7ffff780a000-7ffff79c9000 r-xp 00000000 08:01 2068 |
| 99 | +/lib/x86_64-linux-gnu/libc-2.23.so |
| 100 | +7ffff79c9000-7ffff7bc9000 ---p 001bf000 08:01 2068 |
| 101 | +/lib/x86_64-linux-gnu/libc-2.23.so |
| 102 | +7ffff7bc9000-7ffff7bcd000 r--p 001bf000 08:01 2068 |
| 103 | +/lib/x86_64-linux-gnu/libc-2.23.so |
| 104 | +7ffff7bcd000-7ffff7bcf000 rw-p 001c3000 08:01 2068 |
| 105 | +/lib/x86_64-linux-gnu/libc-2.23.so |
| 106 | +7ffff7bcf000-7ffff7bd3000 rw-p 00000000 00:00 0 |
| 107 | +7ffff7bd3000-7ffff7bd6000 r-xp 00000000 08:01 2067 |
| 108 | +/lib/x86_64-linux-gnu/libdl-2.23.so |
| 109 | +7ffff7bd6000-7ffff7dd5000 ---p 00003000 08:01 2067 |
| 110 | +/lib/x86_64-linux-gnu/libdl-2.23.so |
| 111 | +7ffff7dd5000-7ffff7dd6000 r--p 00002000 08:01 2067 |
| 112 | +/lib/x86_64-linux-gnu/libdl-2.23.so |
| 113 | +7ffff7dd6000-7ffff7dd7000 rw-p 00003000 08:01 2067 |
| 114 | +/lib/x86_64-linux-gnu/libdl-2.23.so |
| 115 | +7ffff7dd7000-7ffff7dfd000 r-xp 00000000 08:01 2051 |
| 116 | +/lib/x86_64-linux-gnu/ld-2.23.so |
| 117 | +7ffff7fea000-7ffff7fed000 rw-p 00000000 00:00 0 |
| 118 | +7ffff7ff6000-7ffff7ff8000 rw-p 00000000 00:00 0 |
| 119 | +7ffff7ff8000-7ffff7ffa000 r--p 00000000 00:00 0 [vvar] |
| 120 | +7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso] |
| 121 | +7ffff7ffc000-7ffff7ffd000 r--p 00025000 08:01 2051 |
| 122 | +/lib/x86_64-linux-gnu/ld-2.23.so |
| 123 | +7ffff7ffd000-7ffff7ffe000 rw-p 00026000 08:01 2051 |
| 124 | +/lib/x86_64-linux-gnu/ld-2.23.so |
| 125 | +7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0 |
| 126 | +7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack] |
| 127 | +ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 |
| 128 | +[vsyscall] |
| 129 | + |
| 130 | +Function Addresses: |
| 131 | +System@libc 0x7ffff784f390 |
| 132 | +PID: 4446 |
| 133 | +``` |
| 134 | + |
| 135 | +## ASLR Turned On |
| 136 | + |
| 137 | +Now, turn on the ASLR. |
| 138 | + |
| 139 | +```shell |
| 140 | +ubuntu@ubuntu-xenial:/vagrant/lessons/8_aslr/build$ echo 2 | sudo tee |
| 141 | +/proc/sys/kernel/randomize_va_space |
| 142 | +2 |
| 143 | +``` |
| 144 | + |
| 145 | +Before repeating the previous steps on the binaries again, take a look at the |
| 146 | +output from `checksec`. |
| 147 | + |
| 148 | +```shell |
| 149 | +ubuntu@ubuntu-xenial:/vagrant/lessons/8_aslr/build$ checksec * |
| 150 | +[*] '/vagrant/lessons/8_aslr/build/1_reveal_addresses' |
| 151 | + Arch: i386-32-little |
| 152 | + RELRO: Partial RELRO |
| 153 | + Stack: Canary found |
| 154 | + NX: NX enabled |
| 155 | + PIE: No PIE |
| 156 | +[*] '/vagrant/lessons/8_aslr/build/2_reveal_addresses64' |
| 157 | + Arch: amd64-64-little |
| 158 | + RELRO: Partial RELRO |
| 159 | + Stack: Canary found |
| 160 | + NX: NX enabled |
| 161 | + PIE: No PIE |
| 162 | +[*] '/vagrant/lessons/8_aslr/build/3_reveal_addresses_pie' |
| 163 | + Arch: i386-32-little |
| 164 | + RELRO: Partial RELRO |
| 165 | + Stack: Canary found |
| 166 | + NX: NX enabled |
| 167 | + PIE: PIE enabled |
| 168 | +[*] '/vagrant/lessons/8_aslr/build/4_reveal_addresses64_pie' |
| 169 | + Arch: amd64-64-little |
| 170 | + RELRO: Partial RELRO |
| 171 | + Stack: Canary found |
| 172 | + NX: NX enabled |
| 173 | + PIE: PIE enabled |
| 174 | +``` |
| 175 | + |
| 176 | +Notice that the last two have PIE enabled. PIE stands for Position Independent |
| 177 | +Executable. Do you notice any interesting about the results when running these |
| 178 | +binaries with ASLR turned on? |
0 commit comments