Skip to main content

Run Your Own Onion Service Without Leaking Yourself

Updated 7 min readMarkdown
Run Your Own Onion Service Without Leaking Yourself

REN Field Notes #001

Setting up an onion service takes about ten minutes. Setting one up that doesn't quietly hand out your server's real IP takes a bit longer, and that's the part most guides skip. Tor does its job fine. The leaks almost always come from the stuff you put behind it.

This is the way I do it on Debian. Nothing exotic, no magic scripts, just a box that only ever talks to the world through Tor.


0. Before you touch anything

Use a box that does nothing else. Not your VPS that also hosts your clearnet blog, not the home server with your Plex on it. If the same machine answers on a public IP and on an onion, sooner or later someone matches the two (same favicon, same error page, same TLS cert, same odd 404). Scanners index all of that, and favicon-hash searches are a thing people actually use.

Fresh Debian, SSH with keys only, and a firewall that allows SSH in and nothing else:

apt update && apt install ufw
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw enable

An onion service needs zero inbound ports. Tor builds outbound circuits to its introduction points, so port 80 never has to be open to the internet. If you see a guide telling you to open 80/443 for your hidden service, close the tab.

1. Install tor

Debian's own package works. I prefer the Tor Project repo because it tracks current stable releases and you get fixes sooner:

apt install apt-transport-https gpg wget
wget -qO- https://deb.torproject.org/torproject.org/A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89.asc | gpg --dearmor > /usr/share/keyrings/deb.torproject.org-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/deb.torproject.org-keyring.gpg] https://deb.torproject.org/torproject.org trixie main" > /etc/apt/sources.list.d/tor.list
apt update && apt install tor deb.torproject.org-keyring

Swap trixie for your release codename (lsb_release -cs tells you). Check the fingerprint on the Tor Project's support page yourself instead of trusting a forum post. Yes, including this one.

2. The web server, bound to localhost only

This is where people get burned. Debian's nginx ships a default site listening on every interface. Kill it first:

apt install nginx
rm /etc/nginx/sites-enabled/default

Then a site that only exists on 127.0.0.1:

# /etc/nginx/sites-available/onion
server {
    listen 127.0.0.1:8080 default_server;
    server_name _;

    root /var/www/onion;
    index index.html;

    access_log off;
    error_log /var/log/nginx/onion_error.log crit;

    location / {
        try_files $uri $uri/ =404;
    }
}

And in the http { } block of /etc/nginx/nginx.conf:

server_tokens off;
ln -s /etc/nginx/sites-available/onion /etc/nginx/sites-enabled/onion
mkdir -p /var/www/onion && echo ok > /var/www/onion/index.html
nginx -t && systemctl reload nginx
ss -tlnp | grep nginx

That last line should show 127.0.0.1:8080 and nothing on 0.0.0.0 or [::]. If it shows anything public, stop and fix it before going further.

3. The onion itself

Add this to /etc/tor/torrc:

HiddenServiceDir /var/lib/tor/mysite/
HiddenServicePort 80 127.0.0.1:8080
systemctl restart tor
cat /var/lib/tor/mysite/hostname

That's your 56-character v3 address. v3 is the default and the only option now; v2 was removed from tor years ago (0.4.6), so if a tutorial mentions HiddenServiceVersion 2 or a 16-character address, it's a fossil.

Test it from any machine running tor (apt install torsocks if you don't have it):

torsocks curl -s http://youraddress.onion/

4. The leaks nobody warns you about

Status pages. Here's the classic. Every connection that comes through Tor reaches your web server from 127.0.0.1. So anything you "restricted to localhost" (nginx stub_status, Apache's /server-status, PHP-FPM's status page, some admin panel that trusts local IPs) is now open to every visitor. Apache's server-status on onion sites has leaked client requests and vhost names more than once. Remove these, don't just restrict them.

Headers and error pages. server_tokens off is the minimum. If there's PHP behind it, set expose_php = Off in php.ini so X-Powered-By goes away. Use your own 404/500 pages. Default error pages are fingerprints, and a stack trace with a file path or hostname in it is a gift.

Outbound requests. Your onion box shouldn't call anything on the clearnet directly. Gravatar lookups, link previews, webhooks, "check for updates", fetching remote images, sending mail: every one of those tells some third party your real IP, and the timing lines up nicely with activity on your onion. Either turn the feature off or force it through Tor. Watching ss -tnp for a day is educational.

External assets. Google Fonts, CDNs, analytics, a jQuery from some public CDN. They leak your visitors (worse for them) and they break for anyone on Tor Browser's Safest setting. Self-host everything.

Time. Set the box to UTC (timedatectl set-timezone UTC). The HTTP Date header is always GMT anyway, but your app, your logs and anything it renders ("posted at 03:14") will happily show local time otherwise. Keep the clock synced too, tor is fussy about skew.

Certificates. Don't reuse a clearnet TLS cert on the onion. Certificate Transparency logs are public and searchable, and a cert listing both names is a signed confession. Plain HTTP over an onion is already end-to-end encrypted by Tor; you don't need HTTPS for that.

Uploads. If users (or you) upload images, strip EXIF on the server. GPS coordinates in a JPEG have outed more people than any Tor exploit.

5. Your keys are your address

Everything that makes your onion yours lives in the HiddenServiceDir: hs_ed25519_secret_key, hs_ed25519_public_key and hostname. Lose the secret key and the address is gone forever. Leak it and someone else can be you.

Back the directory up, encrypted, somewhere offline:

tar czf - -C /var/lib/tor mysite | gpg --symmetric --cipher-algo AES256 -o mysite-keys.tgz.gpg

When restoring, ownership and permissions matter or tor refuses to start:

chown -R debian-tor:debian-tor /var/lib/tor/mysite
chmod 700 /var/lib/tor/mysite

6. Vanity address (optional, and a bit vain)

mkp224o brute-forces keys until the address starts with what you want. Do it on a different machine, not the server:

apt install gcc libc6-dev libsodium-dev make autoconf git
git clone https://github.com/cathugger/mkp224o && cd mkp224o
./autogen.sh && ./configure && make
./mkp224o -d keys -n 1 ren

Each extra character multiplies the work by 32. Five or six characters is quick on a normal CPU, eight starts to hurt, beyond that bring a lot of cores and patience. Copy the generated folder's contents into your HiddenServiceDir, fix ownership like above, restart tor.

Two warnings. A vanity prefix trains people to check only the first few characters, which is exactly what phishing clones exploit. And never use a key someone else "generated for you".

7. Bits for when it gets serious

  • Client authorization. Private service for a few people? Drop .auth files into HiddenServiceDir/authorized_clients/ (one line each: descriptor:x25519:PUBKEY). Without the matching private key, clients can't even fetch your descriptor.
  • DoS defences. tor 0.4.8 added proof-of-work for onion services: HiddenServicePoWDefensesEnabled 1, if your build includes it. There's also HiddenServiceEnableIntroDoSDefense 1 for rate limiting at the intro points.
  • Vanguards. Recent tor already has vanguards-lite built in. For a high-value service, look at the full vanguards add-on to make guard discovery attacks more expensive.
  • OnionBalance. One address, several backend servers. It publishes a combined descriptor so traffic spreads across instances and one box going down doesn't take you offline. Supports v3.
  • Never enable HiddenServiceNonAnonymousMode / single-hop mode unless you genuinely don't care if the server location is known. It's for big sites that already have a public face, not for you.

Quick sanity checklist

  • ss -tlnp shows nothing listening publicly except SSH
  • No status pages, no default error pages, no version headers
  • Box doesn't make clearnet requests of its own
  • Timezone UTC, clock synced
  • Keys backed up offline, encrypted
  • Nothing on the box is shared with a clearnet identity

That's it. Tor is the easy part. Most onions that got unmasked weren't beaten by some genius attack on the network, they were beaten by a misconfigured Apache, a forgotten outbound request or an admin who reused something. Don't be that admin.

Questions, corrections, war stories: drop them below.


Originally published as REN Field Notes #001 on Ren Forums. The forum and later notes also go out on Telegram at t.me/RenForums.

About the author

  • Official account of Ren Forums. Practical OPSEC, Tor, Tails and defensive security write-ups. Forum: https://ren.st Telegram: https://t.me/RenForums

No comments yet. Be the first to comment.