Onion services are cheap to attack and expensive to defend. A client sends one small INTRODUCE cell; the service has to decrypt it, do a key exchange, and build a whole new three-hop circuit to the rendezvous point. Flood a service with fake introductions and it spends all its time building circuits to nowhere, legitimate users time out, and the tor process sits at 100% CPU. There's no IP address to block, because every request arrives anonymously. For years, large onion services had basically no answer to this.

Since tor 0.4.8 (2023) there's a real one: proof-of-work. Combined with the older introduction point rate limits, it's what keeps busy services usable during floods. This page covers both, in the order you'd actually set them up.

Proof-of-work

The design, Tor proposal 327, makes clients pay for priority. When the defence is on, the service puts a puzzle seed and a "suggested effort" into its descriptor. A client that wants to connect solves an Equi-X puzzle at that effort level (a puzzle designed so that ordinary CPUs do reasonably well against GPUs and custom hardware) and attaches the solution to its INTRODUCE cell. The service checks the solution cheaply and puts the request into a priority queue ordered by effort. Higher effort gets served first.

The suggested effort adjusts itself. When the queue is empty, it drifts to zero and nobody does any work. When the queue backs up, the service raises the suggested effort in its next descriptor, and honest clients start paying. An attacker who wants to keep crowding out real users now has to solve puzzles at the same rate, which turns a cheap flood into an expensive one.

Tor Browser has supported the client side since version 13, released late in 2023. Users see nothing except a slower first connection during an attack.

Turning it on

In the service's section of /etc/tor/torrc:

HiddenServiceDir /var/lib/tor/mysite/
HiddenServicePort 80 127.0.0.1:8080
HiddenServicePoWDefensesEnabled 1

That's it for most people. Two tuning options control how fast the service pulls requests off the queue:

HiddenServicePoWQueueRate 250
HiddenServicePoWQueueBurst 2500

Those are the defaults: up to 250 introductions per second with bursts to 2500. They represent how many rendezvous circuits your service can realistically handle. If your machine is slow or your backend app chokes earlier, lower them. The point is to keep the service working through the queue at a pace it can sustain, so the queue (and therefore the effort) reflects real overload.

Check the build. The PoW code depends on a separately licensed library, and the service side has needed a tor built with it enabled. Packages from the Tor Project's own repository include it. Run:

tor --list-modules

and look for pow: yes. If it says no, your distribution built tor without it, and the option will be ignored with a warning in the log. This one catches people out because the torrc line looks like it worked.

Introduction point rate limiting

Older, cruder, still useful. Since tor 0.4.2, a service can ask its introduction points to rate-limit the INTRODUCE2 cells they forward to it. Excess requests get dropped at the introduction point before they ever reach your server:

HiddenServiceEnableIntroDoSDefense 1
HiddenServiceEnableIntroDoSRatePerSec 25
HiddenServiceEnableIntroDoSBurstPerSec 200

The rate and burst shown are the defaults. They apply per introduction point, so with the default three intro points your effective ceiling is roughly three times that.

The weakness is that this limit is blind. It drops attackers and real users alike, at random. During a flood, it keeps your tor process alive but most legitimate users still fail to connect. PoW is the better tool because it discriminates; rate limiting is the backstop that stops a flood from flattening the service while PoW effort climbs. I run both. Some operators turn intro rate limiting off once PoW is in place, reasoning that it fights the PoW queue for the same job. There's no single right answer; watch your metrics and see which behaves better under your traffic.

Limits inside established circuits

Floods don't only come as introductions. A client that's connected can open lots of streams on one circuit. Two options handle that:

HiddenServiceMaxStreams 20
HiddenServiceMaxStreamsCloseCircuit 1

The first caps concurrent streams per rendezvous circuit. The second tears down the whole circuit when a client exceeds the cap, instead of just refusing the extra stream. Pick the number by looking at what a real browser does with your site; a modern page with lots of assets can open a good number of streams in parallel, and you don't want to cut off real users on a heavy page.

Watching what's happening

Tor has a Prometheus-style metrics endpoint:

MetricsPort 127.0.0.1:9035
MetricsPortPolicy accept 127.0.0.1

Point a scraper at http://127.0.0.1:9035/metrics and you get onion service counters, including PoW-related ones such as the current suggested effort and the queue size. The exact metric names have shifted between releases, so grep the output rather than copying names from an old dashboard. Keep the port bound to localhost; there's nothing useful in it for the public and a few things you'd rather not publish.

Without metrics you're guessing. With them, you'll see an attack begin as a rising queue and climbing effort well before users start complaining.

Other things that help

More introduction points spread the load: HiddenServiceNumIntroductionPoints accepts values up to 20 for v3 services, though it also means more circuits for your tor to maintain. Beyond that, the next step is more tor processes, which is what Onionbalance gives you. Every backend gets its own queue and its own CPU.

Keep tor up to date. Much of the DoS work over the last few years has been small fixes inside tor itself, in how it handles circuit-building failures and memory pressure, that never needed a torrc option. A service running a years-old distro package is missing all of that, which is the main reason our Nginx setup guide installs tor from the Tor Project's repository.

And make the web application cheap to serve. If a single page view costs your backend a second of CPU, an attacker who gets past every Tor-level defence still wins at the application layer. Caching, a static front page and rate limits in the app itself do more than most people expect. There's no client IP to key them on, but HiddenServiceExportCircuitID haproxy makes tor pass a per-circuit ID to your front proxy using the PROXY protocol, which gives you something to count.

The full option list is in the tor manual, and the Tor Project's onion service DoS guidelines are short and worth reading. The glossary entry on proof of work has the short version if you need to explain this to someone else.

#dos-protection #proof-of-work #equi-x #torrc #onion-services #ddos