This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
session:09 [2020/07/08 13:10] Rareş-Mihail VISALOM (67101) [Resources] |
session:09 [2020/07/19 12:49] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | = 0x08. Return Oriented Programming | + | ====== 0x08. Return Oriented Programming |
- | == Resources | + | ===== Resources |
[[https:// | [[https:// | ||
Line 10: | Line 10: | ||
- | === Executable Space Protection | + | === PLT and GOT === |
- | + | ||
- | The **executable space protection** is an instance of the **principle of least privilege**, | + | |
- | + | ||
- | The mechanism can be (and was) implemented in many different ways, the most common in Linux being: | + | |
- | + | ||
- | **NX bit:** This is the easiest method, and involves an extra bit added to each page table entry that specifies if the memory page should be executable or not. This is current implementation in 64-bit processors where page table entries are 8-bytes wide. | + | |
- | + | ||
- | **Physical Address Extension (PAE):** Besides the main feature that allows access to more than 4GB of memory, the PAE extension for 32-bit processor also adds a NX bit in its page table entries. | + | |
- | + | ||
- | **Emulation: | + | |
- | + | ||
- | < | + | |
- | This security feature gets in the way of **just-in-time (JIT)** compilers, which need to produce and write code at runtime, and that is later executed. Since a JIT compiler cannot run in this kind of secured environment, | + | |
- | + | ||
- | * Slides: [[http:// | + | |
- | * Paper: [[http:// | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | There are of course other implementations in different hardening-oriented projects such as: OpenBSD [[http:// | + | |
- | + | ||
- | === Bypassing NX | + | |
- | + | ||
- | **ret-to-plt/ | + | |
- | + | ||
- | **Return Oriented Programming (ROP).** This is a generalization of the ret-to-* approach that makes use of existing code to execute almost anything. As this is probably one of the most common types of attacks, it will be discussed in depth in a future section. | + | |
- | + | ||
- | **mprotect().** If the application is using '' | + | |
- | + | ||
- | < | + | |
- | Today we will talk about the first 2 methods to bypass NX. **mprotect()** will be introduced in the next sessions. | + | |
- | </ | + | |
- | + | ||
- | === Address Space Layout Randomization (ASLR) | + | |
- | + | ||
- | Address Space Layout Randomization (ASLR) is a security feature that maps different memory regions of an executable at random addresses. This prevents buffer overflow-based attacks that rely on known addresses such as the stack (for calling into shellcode), or dynamically linked libraries (for calling functions that were not already linked with the target binary). Usually, the sections that are randomly mapped are: the stack, the heap, the VDSO page, and the dynamic libraries. The code section can also be randomly mapped for [[http:// | + | |
- | + | ||
- | <note important> | + | |
- | Linux allows 3 options for its ASLR implementation that can be configured using the ''/ | + | |
- | * **0**: deactivated | + | |
- | * **1**: random stack, vdso, libraries; heap is after code section; random code section (only for PIE-linked binaries) | + | |
- | * **2**: random heap too | + | |
- | + | ||
- | </ | + | |
- | + | ||
- | Make sure you reactivate ASLR after the previous section of the tutorial, by one of the two options below. | + | |
- | + | ||
- | If you disabled ASLR system-wide, | + | |
- | + | ||
- | <code bash> | + | |
- | ~$ sudo bash -c 'echo 2 > / | + | |
- | </ | + | |
- | + | ||
- | If you disabled ASLR at shell level, simply **close the shell** such as issuing the '' | + | |
- | + | ||
- | We can easily demonstrate the effects on shared libraries by running '' | + | |
- | + | ||
- | ==== PLT and GOT | + | |
ASLR is not the only feature that prevents the compiler and the linker from solving some relocations before the binary is actually running. Shared libraries can also be combined in different ways, so the first time you actually know the address of a shared library is while the loader is running. The ASLR feature is orthogonal to this - the loader could choose to assign address to libraries in a round-robin fashion, or could use ASLR to assign them randomly. | ASLR is not the only feature that prevents the compiler and the linker from solving some relocations before the binary is actually running. Shared libraries can also be combined in different ways, so the first time you actually know the address of a shared library is while the loader is running. The ASLR feature is orthogonal to this - the loader could choose to assign address to libraries in a round-robin fashion, or could use ASLR to assign them randomly. | ||
Line 159: | Line 101: | ||
- | === Return Oriented Programming | + | ==== Return Oriented Programming |
{{ : | {{ : | ||
- | ==== Motivation | + | === Motivation |
In the previous sessions we discussed '' | In the previous sessions we discussed '' | ||
< | < | ||
Line 190: | Line 132: | ||
This kind of conflict can be resolved using Return Oriented Programming, | This kind of conflict can be resolved using Return Oriented Programming, | ||
- | ==== NOP analogy | + | === NOP analogy |
While '' | While '' | ||
Let's explore an example: | Let's explore an example: | ||
Line 263: | Line 205: | ||
- | ==== Gadgets & ROP chains | + | === Gadgets & ROP chains |
Now that we have a sort of neutral primitive equivalent to a NOP let's actually do something useful. | Now that we have a sort of neutral primitive equivalent to a NOP let's actually do something useful. | ||
The building blocks of ROP payloads are called gadgets. These are blocks of instructions that end with a ' | The building blocks of ROP payloads are called gadgets. These are blocks of instructions that end with a ' | ||
Line 365: | Line 307: | ||
- | === Some useful ninja tricks | + | ==== Some useful ninja tricks |
- | ==== Memory spraying | + | === Memory spraying |
Let's take the following prog: | Let's take the following prog: | ||
<code c> | <code c> | ||
Line 472: | Line 414: | ||
- | ==== Vulnerable function identification | + | === Vulnerable function identification |
As you can see from above, the base pointer gets trashed so backtracing is not possible | As you can see from above, the base pointer gets trashed so backtracing is not possible | ||
<code bash> | <code bash> | ||
Line 542: | Line 484: | ||
- | ==== ROP payload debugging | + | === ROP payload debugging |
When you know what the offending function is, disassemble it and break on " | When you know what the offending function is, disassemble it and break on " | ||
<code bash> | <code bash> | ||
Line 607: | Line 549: | ||
- | ==== checksec in peda | + | === checksec in peda === |
<code bash> | <code bash> | ||
gdb-peda$ checksec | gdb-peda$ checksec | ||
Line 618: | Line 560: | ||
- | ==== gadget finding in peda | + | === gadget finding in peda === |
Apart from **objdump** which only finds aligned instructions, | Apart from **objdump** which only finds aligned instructions, | ||
<code bash> | <code bash> | ||
Line 667: | Line 609: | ||
</ | </ | ||
- | ==== Anti-anti-debugging and others | + | === Anti-anti-debugging and others |
There can be various annoyances in binaries: **ptrace** calls for anti-debugging, | There can be various annoyances in binaries: **ptrace** calls for anti-debugging, | ||
These can all be deactivated using **unptrace** (for ptrace) and **deactive** in peda. | These can all be deactivated using **unptrace** (for ptrace) and **deactive** in peda. | ||
- | == Challenges | + | ===== Challenges |
- | === 00. Tutorial - Bypass NX Stack with return-to-libc | + | ==== 00. Tutorial - Bypass NX Stack with return-to-libc |
Go to the '' | Go to the '' | ||
Line 761: | Line 703: | ||
- | === 01. Challenge - ret-to-libc | + | ==== 01. Challenge - ret-to-libc |
Looks good! Let's get serious and do something useful with this. | Looks good! Let's get serious and do something useful with this. | ||
Line 784: | Line 726: | ||
</ | </ | ||
- | === 02. Challenge - no-ret-control | + | ==== 02. Challenge - no-ret-control |
Go to the '' | Go to the '' | ||
Line 792: | Line 734: | ||
Alter the execution of '' | Alter the execution of '' | ||
- | === 03. Challenge - ret-to-plt | + | ==== 03. Challenge - ret-to-plt |
Go to the '' | Go to the '' | ||
Line 850: | Line 792: | ||
- | === 04. Challenge - Gadget tutorial | + | ==== 04. Challenge - Gadget tutorial |
This task requires you to construct a payload using gadgets and calling the functions inside such that it will print | This task requires you to construct a payload using gadgets and calling the functions inside such that it will print | ||
Line 862: | Line 804: | ||
stage B!stage A! | stage B!stage A! | ||
</ | </ | ||
- | === Bonus Challenge - Echo service | + | ==== Bonus Challenge - Echo service |
This task is a network service that can be exploited. Run it locally and try to exploit it. You'll find that if you call system("/ | This task is a network service that can be exploited. Run it locally and try to exploit it. You'll find that if you call system("/ | ||