> For the complete documentation index, see [llms.txt](https://x7331.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://x7331.gitbook.io/notes/htb/api-attacks/ssrf.md).

# SSRF

> [CWE-918: Server-Side Request Forgery (SSRF)](https://cwe.mitre.org/data/definitions/918.html)

* When an API fetches a remote resource without validating the user-supplied URL.
* The attacker can force the app to send a request to an unexpected destination, such as localhost, bypassing firewalls or VPNs.

The web API stores the path of the files using the `file` URI scheme, which is used to represent local file paths (Figure 1).&#x20;

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FJf7yNGxuJtkUDLY1Q4TI%2Fhtb_api_ssrf_1.png?alt=media&amp;token=4eb0b6a7-9a95-4e18-a36a-ce21cbd6d468" alt=""><figcaption><p>Figure 1: Finding a potential vulnerable-to-SSRF endpoint.</p></figcaption></figure>

This path is stored under the `/supplier-companies/current-user` endpoint (Figure 2).

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FSVci0v5cMwytUExJslJM%2Fhtb_api_ssrf_2.png?alt=media&amp;token=d525e4b7-e60d-4780-9c0f-6452ba42f863" alt=""><figcaption><p>Figure 2: The URI path is now stored in the application.</p></figcaption></figure>

The `/api/v1/supplier-companies` `PATCH` endpoint allows the user to modify the URI path (Figure 3); this should not be the case as it should be dynamically set only by the value defined by the `/api/v1/supplier-companies/certificates-of-incorporation` `POST` endpoint.

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FJDlrb2k5aOzyQr7krSrI%2Fhtb_api_ssrf_3.png?alt=media&amp;token=5984c64b-a20e-4863-baa6-59c9630a286b" alt=""><figcaption><p>Figure 3: The URI path can be manually modified by the user.</p></figcaption></figure>

An attacker could leverage this flaw and read a local file, such as `/etc/passwd` (Figure 4).

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2F9azvYRin5eLiNPtRvP3p%2Fhtb_api_ssrf_4.png?alt=media&amp;token=11df4173-9bf4-4fd8-ab5f-758a5a060256" alt=""><figcaption><p>Figure 4: Leveraging SSRF to read a local file.</p></figcaption></figure>

## Exercise

<figure><img src="https://3960676229-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FmjLkek16kB60c2WFd5lf%2Fuploads%2FlwRL6agquUTEFTDvhiO5%2Fhtb_api_ssrf_ex.png?alt=media&amp;token=28c05215-826c-44e0-a6ca-0010a9209ab7" alt=""><figcaption><p>Figure 5: Exploiting another SSRF vulnerability.</p></figcaption></figure>
