How the Tor Network Works: Onion Routing and Internet Anonymity

The Tor network (short for The Onion Router) is a free, open-source anonymity network that allows people to browse the internet without revealing their identity or location. It works by separating identification from routing: instead of connecting directly to a destination server, your traffic is relayed through a series of volunteer-operated servers around the world, with each relay only knowing its immediate neighbor in the chain. No single relay ever knows both who you are and what you are accessing.

Tor was originally developed in the mid-1990s at the U.S. Naval Research Laboratory by mathematicians Paul Syverson and computer scientists Michael Reed and David Goldschlag. The underlying technique — onion routing — was designed to protect U.S. intelligence communications online. The Tor Project was founded as a nonprofit in 2006, and today the network is used by journalists, activists, whistleblowers, privacy-conscious individuals, and anyone who needs to circumvent censorship or surveillance.

From a networking perspective, Tor traffic is ordinary TLS-encrypted TCP traffic. Tor relays are just servers with IP addresses announced in BGP by various autonomous systems worldwide. But the protocol layered on top of that infrastructure creates something remarkable: a system where the network itself cannot determine who is talking to whom.

How Onion Routing Works

The core idea behind Tor is onion routing — a technique where a message is wrapped in multiple layers of encryption, like the layers of an onion. Each relay in the circuit peels off exactly one layer, revealing only the address of the next relay. By the time the message reaches the destination, all layers have been removed, and no relay along the path has seen the full picture.

Here is how it works step by step:

  1. The Tor client (running on your computer) selects three relays from the Tor network: a guard node (entry), a middle relay, and an exit relay.
  2. The client obtains the public encryption key for each relay from the Tor directory.
  3. The client encrypts the message three times, in reverse order — first with the exit relay's key, then with the middle relay's key, then with the guard node's key. This produces a layered ciphertext.
  4. The client sends this fully-wrapped message to the guard node. The guard node decrypts the outer layer using its private key. This reveals the address of the middle relay but not the message content or the final destination.
  5. The guard node forwards the twice-encrypted message to the middle relay. The middle relay decrypts one more layer, revealing the address of the exit relay. The middle relay knows neither the client's IP nor the destination.
  6. The middle relay forwards the singly-encrypted message to the exit relay. The exit relay decrypts the final layer, revealing the original message and the destination server's address. The exit relay sends the request to the destination on behalf of the client.
  7. The response follows the same path in reverse, with each relay adding a layer of encryption. The client strips all three layers to read the response.
Onion Routing: Layer-by-Layer Decryption Client Your computer Guard Node Entry relay Middle Relay Second hop Exit Relay Final hop Server Encrypts 3 layers Layer 3 (Guard key) Layer 2 (Middle key) Layer 1 (Exit key) + Data Peels layer 3 Layer 2 (Middle key) Layer 1 (Exit key) + Data Peels layer 2 Layer 1 (Exit key) + Data Peels layer 1 Plaintext Data What Each Relay Knows Guard Node: Knows client IP + middle relay address. Does NOT know destination or message. Middle Relay: Knows guard + exit addresses. Does NOT know client IP or destination. Exit Relay: Knows middle relay + destination. Does NOT know client IP. Result: No single point in the network knows both source and destination.

The critical insight is the separation of knowledge. The guard node knows who you are (your IP address) but not what you are doing. The exit relay knows what site is being accessed but not who is accessing it. The middle relay knows neither. This is why three hops are used — it is the minimum number needed to ensure that no single relay can link the sender to the destination.

In practice, the encryption is more nuanced than simple layering. During circuit construction, the client negotiates a separate symmetric session key with each relay using ephemeral Diffie-Hellman key exchange (specifically, Curve25519). These session keys are used to encrypt and decrypt data at each hop. The initial key exchange is done using the relay's long-term identity key for authentication, but the session keys provide forward secrecy — even if a relay's long-term key is later compromised, past sessions cannot be decrypted.

Circuit Construction

Before any data can be anonymized, the Tor client must build a circuit — a path through three relays. This is a multi-step process that involves consulting the Tor directory, selecting relays, and negotiating encryption keys with each one.

The Tor Directory System

Tor relies on a set of directory authorities — nine hardcoded, trusted servers operated by known, independent individuals and organizations in the Tor community. These authorities collectively maintain a list of all relays in the network, their capabilities, bandwidth, uptime, and public keys. Every hour, the directory authorities vote on the current state of the network and produce a consensus document — a signed list of all active relays and their properties.

Tor clients download this consensus document periodically (typically every few hours) and use it to select relays for building circuits. The consensus includes each relay's flags — labels like Guard, Exit, Stable, Fast, and HSDir — which indicate the relay's role and quality. A relay earns the Guard flag by demonstrating sustained uptime and sufficient bandwidth. The Exit flag indicates the relay allows traffic to exit to the regular internet.

Relay Selection

When building a circuit, the client does not choose relays uniformly at random. Instead, selection is bandwidth-weighted: relays with more bandwidth are more likely to be chosen, which balances load across the network and ensures that high-capacity relays carry a proportional share of traffic. Without bandwidth weighting, a small number of fast relays would be underutilized while slow relays would be overwhelmed.

The client also applies several constraints:

Building the Circuit Incrementally

The circuit is built one hop at a time, which is essential for security:

  1. The client connects to the guard node via a TLS connection and performs a Diffie-Hellman key exchange to establish a shared session key. This creates a one-hop circuit.
  2. Through this encrypted tunnel, the client sends a request to the guard node asking it to extend the circuit to the middle relay. The guard node connects to the middle relay, and the client negotiates a separate session key with the middle relay. The guard node cannot read this negotiation because it is encrypted end-to-end between the client and the middle relay.
  3. Through the two-hop tunnel, the client extends the circuit one more time to the exit relay, negotiating a third session key. Neither the guard nor the middle relay can read this negotiation.

At the end of this process, the client has a three-hop circuit with three independent session keys. Each relay only knows the key it shares with the client. The entire construction takes a few hundred milliseconds under good network conditions.

Circuit Lifetime and Rotation

Circuits are not permanent. Tor rotates circuits regularly to limit the window of vulnerability:

Pre-built circuits are maintained so that new connections can be served immediately without the delay of circuit construction.

Types of Tor Relays

Not all relays in the Tor network are equal. Each serves a distinct role, and the security properties of the network depend on having a diverse, distributed set of each type.

Guard (Entry) Nodes

The guard node is the first relay in a circuit and the only relay that sees the client's real IP address. Because of this sensitive position, Tor is very selective about which relays earn the Guard flag — they must demonstrate high uptime, adequate bandwidth, and stability over a sustained period. The client uses a small, persistent set of guard nodes (called its guard set) for months at a time rather than rotating through many guards.

This persistence might seem counterintuitive — wouldn't using a different entry point every time be safer? The answer is no. If you rotate guards frequently, then over time you will inevitably connect through a malicious guard. Once you do, a malicious guard that also controls (or can observe) the exit relay can correlate your traffic by timing analysis. By sticking with a small set of guards, you either always avoid the malicious guard or always use it — the latter is bad, but the probability of that outcome is proportional to the fraction of malicious guards in the network, which is low. Frequent rotation would make correlation a certainty given enough time.

Middle Relays

Middle relays are the workhorses of the Tor network. They sit between the guard and the exit, forwarding encrypted traffic in both directions. A middle relay knows only the addresses of the relay before it and the relay after it — it never sees the client's IP or the destination. Running a middle relay carries essentially no legal risk, making it the easiest way to contribute to the Tor network.

Exit Relays

The exit relay is the last hop in a circuit. It decrypts the final layer of onion encryption and sends the traffic to the destination server on the open internet. It is the only relay that sees the actual content of unencrypted requests (such as plain HTTP). The destination server sees the exit relay's IP address as the source of the connection.

Each exit relay publishes an exit policy — a list of destination IP addresses and ports it is willing to connect to. Many exit relays restrict their policy to common ports like 80 (HTTP) and 443 (HTTPS) and block ports associated with email (to prevent spam) or peer-to-peer file sharing.

Exit relay operators bear the greatest legal burden in the Tor ecosystem. Because the traffic appears to originate from their IP address, they may receive abuse complaints, DMCA takedown notices, or law enforcement inquiries for traffic they merely forwarded. Most exit relay operators maintain a Tor exit notice page on port 80 explaining that the IP is a Tor exit relay, and organizations like the EFF provide legal guidance for operators.

Bridges

Bridges are unlisted entry points into the Tor network. Unlike guard nodes, whose IP addresses are published in the public Tor consensus, bridge addresses are distributed privately. This makes bridges essential for users in countries where Tor is actively blocked — censors cannot easily block bridges if they do not know the IP addresses.

Bridges can use pluggable transports — protocol obfuscation layers that make Tor traffic look like something else entirely. The most widely used pluggable transport, obfs4, makes Tor traffic indistinguishable from random noise. Others, like meek, tunnel Tor traffic inside HTTPS connections to major cloud providers (like Cloudflare or Amazon), making it look like ordinary web traffic to a CDN. Censors would have to block the entire cloud provider to block the bridge — a costly tradeoff.

Directory Authorities

The nine directory authorities are the backbone of Tor's trust model. They are hardcoded into the Tor client source code and operated by trusted community members in multiple jurisdictions. Each authority independently measures the network, and a consensus is reached by majority vote. If a majority of directory authorities are compromised, the network's integrity is at risk — but distributing them across different countries and operators makes this extremely difficult.

Tor Relay Types and Their Knowledge Guard / Entry Node Knows: Client IP, middle relay Cannot see: destination, data Must have Guard flag Persistent (2-3 months) High uptime required Middle Relay Knows: guard + exit addresses Cannot see: client, destination, data Most common relay type No legal risk Easiest to operate Exit Relay Knows: middle relay, destination Cannot see: client IP Publishes exit policy Sees unencrypted traffic Highest legal exposure Bridge Knows: Client IP, next relay Cannot see: destination, data IP not in public consensus Pluggable transports (obfs4) Anti-censorship tool Directory Authority Publishes: relay list, consensus Does not carry user traffic 9 hardcoded servers Hourly consensus vote Multi-jurisdiction Network Stats (2024) ~7,000 relays ~2,000 bridges ~2 million daily users ~300 Gbit/s capacity

Tor and the Network Layer

From the perspective of BGP and internet routing, Tor traffic is completely invisible. Tor relays are just servers with ordinary IP addresses, hosted in data centers around the world, their prefixes announced by autonomous systems like any other server. A Tor relay in Germany might be in Hetzner (AS24940), another in the United States might be in AWS (AS16509), and a third in France might be in Scaleway (AS12876).

When you connect to the Tor network, your traffic first goes from your ISP to the guard node — traversing however many autonomous systems are in the AS path between them, crossing Internet Exchange Points, passing through transit providers. The connection from the guard to the middle relay and from the middle relay to the exit relay each independently traverse the BGP routing fabric. A single Tor circuit may cross a dozen or more AS boundaries.

On the wire, a connection to a Tor relay looks like an ordinary TLS connection to a server on port 443 (or port 9001, the default Tor ORPort). An ISP monitoring a user's traffic sees a TLS connection to a known IP address — and since the IP addresses of non-bridge relays are public, the ISP can determine that the user is connecting to the Tor network. However, the ISP cannot see which sites the user is visiting, because the actual destination is hidden behind two more encrypted hops.

This is where the relationship between Tor and BGP becomes particularly interesting for security. The diversity of autonomous systems hosting Tor relays is a critical factor in the network's security. If a single AS hosts a disproportionate share of guard and exit relays, the operator of that AS can observe both ends of many circuits and potentially perform traffic correlation attacks. Tor's relay selection algorithm takes this into account — it avoids placing two relays from the same /16 network or the same AS family in the same circuit.

AS-Aware Path Selection and AS Diversity

Research into Tor's security has increasingly focused on AS-level adversaries — entities that control an autonomous system and can passively observe all traffic entering and leaving it. Unlike a single malicious relay, an AS-level adversary can observe traffic at the network layer without operating any Tor relays at all.

Consider this scenario: a user's ISP is AS-A, and the destination website is hosted in AS-B. If AS-A and AS-B happen to be the same network, or if a transit AS carries traffic on both the client-to-guard and exit-to-destination paths, that AS can see both ends of the communication and correlate them by timing, even though the Tor circuit itself passes through three separate relays in three different ASes.

This is not hypothetical. Academic papers like "Users get routed: traffic correlation on Tor by realistic adversaries" (Johnson et al., 2013) demonstrated that large transit ASes — particularly Tier-1 networks like those with ASNs you can look up in the looking glass — are in a position to observe significant fractions of Tor circuit endpoints.

To address this, researchers have proposed AS-aware path selection — modifying Tor's relay selection to consider the AS-level paths between the client, the guard, the exit, and the destination. Instead of only avoiding relays in the same AS, the circuit builder would also avoid circuits where the same AS appears on the network-layer path between the client and the guard and on the path between the exit and the destination. This requires knowledge of BGP routing tables — the very data that tools like this BGP looking glass provide.

The practical challenges are significant. BGP paths are not static — they change due to routing events, peering changes, and traffic engineering. A circuit that is AS-safe when built may become vulnerable minutes later when a BGP update shifts the path through a different transit AS. Nevertheless, AS diversity remains one of the most important security properties of the Tor network, and the Tor Project actively monitors the distribution of relays across autonomous systems.

Performance: Why Tor Is Slow

Users of Tor immediately notice its performance cost. A typical web page that loads in 200 milliseconds over a direct connection might take 2-5 seconds through Tor. Several factors contribute to this:

The Tor Project has made significant improvements over the years. Congestion control (introduced in Tor 0.4.7, 2022) replaced the old fixed-window flow control with an algorithm inspired by TCP Vegas, allowing relays to dynamically adjust their sending rate based on observed round-trip times. This dramatically improved throughput for bulk transfers. Single-stream downloads that previously topped out at 1-2 Mbit/s can now reach 10+ Mbit/s on circuits with sufficient relay capacity.

Threat Model: What Tor Protects Against

Understanding Tor's security requires understanding its threat model — the set of adversaries it is designed to defend against, and the ones it explicitly does not claim to defeat.

What Tor Defends Against

What Tor Does Not Defend Against

Tor Browser

The Tor Browser is a modified version of Mozilla Firefox that comes pre-configured to route all traffic through the Tor network. But it does far more than simply proxy traffic — it implements dozens of anti-fingerprinting protections that are essential for anonymity on the modern web.

Key protections include:

The goal is to make every Tor Browser user look identical to websites. If all users have the same browser version, window size, fonts, and JavaScript behavior, a fingerprint reveals nothing about the individual. This is called uniformity as anonymity — you are anonymous because you look like everyone else.

Onion Services (Hidden Services)

Tor is not only used to access the regular internet anonymously — it also supports onion services (formerly called hidden services), which are servers that can be accessed only through the Tor network. Onion service addresses end in .onion and consist of a 56-character base32-encoded string derived from the service's public key (in the v3 onion service protocol).

Onion services provide mutual anonymity: neither the client nor the server knows the other's IP address. This is achieved through a sophisticated protocol involving rendezvous points:

  1. The onion service builds circuits to several relays and designates them as introduction points. It publishes these introduction points (signed with its identity key) to a distributed hash table stored on relays with the HSDir flag.
  2. A client wanting to access the onion service looks up the introduction points in the distributed hash table, then builds its own circuit to a relay and designates it as a rendezvous point.
  3. The client sends a rendezvous cookie to the onion service through an introduction point. The onion service connects to the rendezvous point through its own three-hop circuit.
  4. The rendezvous point connects the two circuits together, and encrypted data flows between client and server — each through a separate three-hop circuit. The total path is six hops long.

Onion services provide several unique properties: they are resistant to censorship (no IP address to block), they provide end-to-end encryption without needing a TLS certificate from a CA, and they authenticate the server cryptographically (the .onion address is derived from the server's public key, so connecting to an address proves you are talking to the holder of the corresponding private key).

Onion Service Connection via Rendezvous Point Client G M E Rendezvous Point Client's circuit Introduction Point rendezvous cookie Onion Service G M E Service's circuit publishes intro points 6-hop total: 3 (client) + 3 (service) Neither side knows the other's IP

Tor Browser and HTTPS: Defense in Depth

A common question is whether HTTPS is still necessary when using Tor, since Tor already encrypts traffic. The answer is emphatically yes. Tor encrypts traffic within the Tor network — between your client and the exit relay. But the connection from the exit relay to the destination server is a normal internet connection. If the destination uses plain HTTP, the exit relay and anyone between the exit relay and the destination can read the traffic in the clear.

HTTPS provides end-to-end encryption between your browser and the destination server. Combined with Tor, this means:

With both Tor and HTTPS, no single point in the entire chain sees both your identity and your traffic content. This is defense in depth — the two systems complement each other.

Tor and Censorship Resistance

In countries where internet access is heavily censored — such as China, Iran, Russia, and Turkmenistan — governments actively try to block Tor. Their methods have grown increasingly sophisticated:

The Network by the Numbers

As of 2024, the Tor network consists of approximately:

The geographic and AS distribution of relays is critical to the network's security. Germany and the Netherlands historically host the largest share of Tor relay bandwidth, partly due to favorable hosting costs and legal environments. Organizations like DFN (AS680) (the German Research Network) and hosting providers like Hetzner (AS24940) host significant numbers of relays. The Tor Project publishes relay metrics at metrics.torproject.org, including per-AS relay counts.

See It in Action

Tor relay IP addresses are publicly listed, which means you can look them up in a BGP looking glass to see which autonomous systems host them, what AS paths lead to them, and how they are connected to the rest of the internet. This gives you a concrete view of the infrastructure that underpins the anonymity network.

Try these lookups to explore the BGP-level view of Tor infrastructure:

You can also look up any specific Tor relay IP address to see its BGP route, origin AS, and AS path. The looking glass will show you the same routing information that the global internet uses to deliver traffic to and from that relay — the very routing fabric on top of which Tor's anonymity layer operates. Every Tor circuit you build traverses this BGP infrastructure, crossing autonomous system boundaries at each hop, relying on the same BGP routing that carries all other internet traffic.

See BGP routing data in real time

Open Looking Glass
More Articles
How Tor Onion Services Work: .onion Addresses and the Rendezvous Protocol
How VPNs Work: Tunneling Protocols and Encryption
How Tailscale Works: Building a Mesh VPN on WireGuard
How WireGuard Works: Modern VPN Protocol Explained
What is BGP? The Internet's Routing Protocol Explained
What is an Autonomous System (AS)?