SSRF, or Server-Side Request Forgery, appears wherever a site makes a request on the server's behalf to an address supplied by the user. The features look harmless and useful: importing an image by URL, link previews, webhook checks, generating a PDF from another page. The catch is that the request leaves from inside your network, not from the outside world. Without validation, an attacker turns your server into a proxy for reaching internal systems.
What the server can reach and the attacker cannot
Many services stay unprotected on the assumption that only the server can reach them. On a VPS, a password-less admin panel, Redis or an internal API may listen on 127.0.0.1. Office networks have printers, cameras and accounting servers on 192.168.x.x. Cloud platforms expose instance metadata at 169.254.169.254, which on some platforms can even hand out access keys. An outside attacker cannot connect to these directly, but through SSRF your server does it for them. Some libraries also accept schemes other than http: give PHP's file_get_contents the value file:///etc/passwd and it reads a local file.
Vulnerable code and attack payloads
<?php
// image-import.php?url=... : VULNERABLE
$data = file_get_contents($_GET['url']);
file_put_contents('/var/www/example/uploads/' . md5($_GET['url']) . '.jpg', $data);
Instead of an image link, the attacker sends values like these. The last two defeat a naive "block the string 127.0.0.1" check: an IP can be written as a decimal number, and an innocent-looking hostname can resolve to an internal address.
?url=http://127.0.0.1:8080/admin
?url=http://192.168.1.10/
?url=http://169.254.169.254/
?url=file:///etc/passwd
?url=http://2130706433/ (127.0.0.1 written as a decimal number)
?url=http://local.misol.uz/ (a name whose DNS points to 127.0.0.1)
Building a proper defence
If the feature only talks to a few known services, an allowlist of domains is best. When arbitrary URLs are required, layer the checks: allow only http and https; resolve the hostname yourself and reject any private or reserved address; connect to exactly the IP you checked, so DNS cannot change between check and connection (DNS rebinding); and disable redirects, because a 302 can bounce the server to an internal address.
<?php
function fetch_external(string $url): ?string
{
$p = parse_url($url);
$scheme = strtolower($p['scheme'] ?? '');
if (!in_array($scheme, ['http', 'https'], true) || empty($p['host'])) {
return null;
}
$host = $p['host'];
$port = $p['port'] ?? ($scheme === 'https' ? 443 : 80);
// Resolve the name ourselves and reject private/reserved addresses
$ips = gethostbynamel($host);
if (!$ips) {
return null;
}
foreach ($ips as $ip) {
if (!filter_var($ip, FILTER_VALIDATE_IP,
FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
return null;
}
}
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => false, // no redirects
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_RESOLVE => ["$host:$port:{$ips[0]}"], // pin the checked IP
CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
CURLOPT_CONNECTTIMEOUT => 3,
CURLOPT_TIMEOUT => 10,
]);
$body = curl_exec($ch);
return $body === false ? null : $body;
}
FILTER_FLAG_NO_PRIV_RANGE and FILTER_FLAG_NO_RES_RANGE reject ranges such as 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 127.0.0.0/8 and 169.254.0.0/16. Depending on the PHP version some rarer ranges may not be covered, so check the official documentation for critical projects. The example handles IPv4 only for simplicity.
Example: a classifieds site in Tashkent
A car classifieds site lets sellers add photos by URL. It runs on a VPS that also hosts an internal moderator dashboard on 127.0.0.1:8080, left without a password because it is "not reachable from outside". A user submits that address as a photo link; the server downloads the page and stores it as an "image", and the attacker reads the dashboard HTML, including users' phone numbers. The fix came from both sides: fetch_external plus a check that the result is a real image, and a password on the dashboard. Even localhost-only services need authentication, because SSRF breaks exactly that trust.
Network-level hardening
On a VPS, run the URL-fetching component in a separate process or container with no access to the internal network, and know which services listen only on localhost, since those are the targets:
# Each of these must be rejected by your feature
curl "https://example.uz/image-import.php?url=http://127.0.0.1/"
curl "https://example.uz/image-import.php?url=file:///etc/passwd"
curl "https://example.uz/image-import.php?url=http://2130706433/"
# On a VPS: which services listen only on localhost?
sudo ss -tlnp | grep 127.0.0.1
- Use short timeouts and a response size limit.
- Never echo internal responses in error messages.
- Log outbound requests with the user who triggered them.
- Put authentication on internal services such as Redis and Elasticsearch.
Shared hosting usually has few internal services, but SSRF can still be used to attack other sites or read files. If you deploy your own services on a sayt.uz VPS, run these checks before launch.