DEV Community

SeoMavares
SeoMavares

Posted on Edited on Originally published at dev.to

Why curl gets a 403 while your browser gets a 200

I had a PHP script that worked everywhere except one site. Same URL, same headers, same cookies:

curl    → 403
browser → 200
Enter fullscreen mode Exit fullscreen mode

I spent an afternoon rotating User-Agents before I understood the problem. The site wasn't reading my headers. It was reading my handshake.

The fingerprint is in the first packet

Before any HTTP exists, the client sends a ClientHello: which ciphers it supports, in which order, which curves, which extensions, in which order, plus some random "GREASE" values to keep parsers honest.

Hash all that and you get JA3/JA4. It's cheap to compute, stable across requests, and almost impossible to fake with a generic HTTP client — because PHP's OpenSSL binding won't let you choose the TLS 1.3 cipher order, the curves, the GREASE values, or the extension order.

Those are exactly the fields the fingerprint is made of. So curl and file_get_contents() always look like curl and file_get_contents().

So I wrote the handshake myself

NativeHttp opens a plain TCP socket — no SSL wrapper — and speaks TLS 1.3 on its own. OpenSSL is only borrowed for the primitives: X25519, AES-GCM, ChaCha20-Poly1305 and certificate checks.

The ClientHello is assembled byte by byte with a Chrome-oriented profile: GREASE, SNI, supported_groups, ALPS, compress_certificate, and padding to 512 bytes. Then the RFC 8446 key schedule, the encrypted records, and HTTP/2 on top: preface, SETTINGS, HEADERS, DATA, flow control and a full HPACK decoder.

That was the fun part. The part that actually took me days was certificate verification.

The bug that would have made it useless

TLS verification is three checks: is the chain trusted, does the hostname match, and did the server sign the handshake?

The first two are a few lines of OpenSSL. The third one stopped me cold: modern CAs sign with RSA-PSS, and PHP's openssl_verify() does not expose that padding variant. No flag, no constant. It's just not there.

Skipping the check was tempting — everything would have "worked". It would also mean accepting any certificate a man-in-the-middle hands you, as long as the chain looks right. That's the one attack TLS exists to prevent.

So I implemented EMSA-PSS by hand: RSA exponentiation, unmask with MGF1, recompute the hash, compare. It's about 40 lines and it's the reason ext-gmp is worth having installed.

Install

composer require nativehttp/client
Enter fullscreen mode Exit fullscreen mode

One object, one method, an array back. No PSR-18 adapter needed, no dependencies:

use NativeHttp\Client;

$http = new Client(['timeout' => 20]);

$r = $http->get('https://httpbin.org/get');

$r['status'];       // 200
$r['http_version']; // "2"
$r['body'];         // string
$r['headers'];      // ['content-type' => 'application/json', ...]
Enter fullscreen mode Exit fullscreen mode

What I actually use it for

Scraping links out of a page. No DOM library required, just regex over the body:

$html = $http->get('https://example.com/catalog')['body'];

preg_match_all('/href="([^"]+\.mp4)"/i', $html, $m);

foreach (array_unique($m[1]) as $link) {
    echo $link, "\n";
}
Enter fullscreen mode Exit fullscreen mode

Downloading a file once you have its direct URL:

$video = $http->get($mp4Url);

if ($video['status'] === 200) {
    file_put_contents('video.mp4', $video['body']);
}
Enter fullscreen mode Exit fullscreen mode

Following whatever redirect the CDN throws at you — the final URL comes back in url:

$r = $http->get('https://example.com/stream/abc');
echo $r['url']; // https://cdn12.example.com/hls/abc.mp4
Enter fullscreen mode Exit fullscreen mode

Calling a JSON API with custom headers, in either shape:

// map
$r = $http->request('POST', 'https://api.example.com/items', [
    'Content-Type' => 'application/json',
    'Authorization' => 'Bearer ' . $token,
], json_encode(['name' => 'foo']));

// list
$r = $http->get('https://api.example.com/items', [
    'Accept: application/json',
]);
Enter fullscreen mode Exit fullscreen mode

Watching the handshake when something doesn't add up:

$http = new Client(['logger' => fn(string $msg) => print($msg . "\n")]);

$http->get('https://tls.peet.ws/api/all');

// TCP connected to tls.peet.ws:443
// ClientHello sent (512 bytes)
// ServerHello: cipher=aes-128-gcm
// TLS 1.3 handshake complete (alpn=h2)
Enter fullscreen mode Exit fullscreen mode

Headers come back lowercased and already decompressed, so gzip just works without you asking.

What it doesn't do yet

Three things worth knowing before you build on it: no ML-KEM (so the fingerprint is close to Chrome's, not identical), no session resumption (every request pays a full handshake, ~1s), and no HTTP/3.

The full list — and the test suite, RFC 7541 vectors included — is in the repo: https://github.com/SeoMavares/native-http

If you've fought the same 403, I'd like to know what finally worked for you.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the kind of debugging story I trust, because the wrong fix is so tempting: rotating User-Agents for an afternoon while the real signal was in the ClientHello all along. The JA3/JA4 point is the one most people miss — by the time HTTP exists, the server has already decided what you are. The part I respect most is refusing to skip the third verification check. Implementing EMSA-PSS by hand with RSA exponentiation and MGF1, instead of accepting any certificate just to make the demo work, is exactly the discipline that separates a toy client from one you can actually use. With no ML-KEM yet, how close does the fingerprint stay to current Chrome in practice, and what was the first site where the hand-rolled handshake broke in a way the test suite did not catch?