Leanpub Header

Skip to main content

How Tor Works

A Complete Technical Deep Dive into The Onion Router

How Tor Works
This book is 100% completeLast updated on 2026-11-16
+15,187 words in the last 30 days

Go beyond the basics and discover what really happens inside Tor. This book takes you from the cryptographic principles behind onion routing to the network architecture, protocol messages and source code that power the system. Backed by official specifications and academic research, it offers a detailed, practical look at how Tor works under the hood.

Minimum price

$25.00

$35.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
257
Pages
About

About

About the Book

This book explains The Onion Router (Tor) from the ground up. It begins with the networking and cryptographic foundations Tor depends on, walks through the design of the Tor network and then descends into the actual protocol messages, state machines and source code that make Tor work in production. Every claim is tied to a primary source such as the official Tor specifications, the Tor source tree, or peer-reviewed research. Citations appear as bracketed numbers that resolve to the bibliography at the end of this book.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience spanning software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. His work focuses on understanding complex systems, uncovering security weaknesses and developing practical solutions that make technology more secure, reliable and effective.

Through Steve Publications, he collaborates with experienced researchers, engineers and industry experts to create practical, technically rigorous books on various topics not limited to self-improvement, software development, cybersecurity, cloud computing, DevOps, artificial intelligence and enterprise technologies. His mission is to make complex technical subjects accessible through clear explanations, real-world examples and hands-on exercises that help IT professionals, students and technology enthusiasts build valuable, practical skills.

Steve Publications embraces AI as a supporting tool for translation, proofreading and editorial refinement, not as a substitute for original research, technical expertise, or hands-on experience. Some contributors choose to publish under pseudonyms to protect their privacy or maintain professional boundaries.

Behind every book is a commitment to technical accuracy, practical relevance and continuous improvement. Our publications are written, reviewed and maintained by experienced professionals, with support from a private community of more than 420 engineers and researchers. Rather than prioritizing volume or mass-produced AI-generated content, we focus on delivering reliable, useful resources that readers can apply in real-world environments.

We also welcome experienced professionals who want to share their knowledge by writing or reviewing books, particularly in specialized and emerging technology fields. If you have expertise to contribute, we invite you to get in touch and explore opportunities to collaborate.

Our commitment is simple: To deliver books that respect our readers' time, support their professional growth and provide lasting practical value. If a book does not meet your expectations, Leanpub offers a 60-day money-back guarantee.

Contents

Table of Contents

A Complete Technical Deep Dive into The Onion Router

  1. How to Use This Book
  2. Conventions and Scope

Part I: Foundations

Chapter 1: Why Anonymity and Tor

  1. The Problem of Network Surveillance
  2. What Tor Protects
  3. Threat Models and Adversaries
  4. What Tor Does Not Protect
  5. Anonymity Sets and Metrics
  6. Why Circuits? Why Three Hops?
  7. The Tor Ecosystem at a Glance
  8. History in One Paragraph

Chapter 2: Networking Foundations

  1. The Internet Protocol Stack
  2. IP Addresses and Ports
  3. TCP: Reliable Byte Streams
  4. TLS: Encryption for the Transport
  5. SOCKS: The Proxy Protocol Tor Clients Speak
  6. DNS: Name Resolution and Its Privacy Implications
  7. UDP, ICMP and Tor’s Limitations
  8. Summarizing the Network Stack View of Tor

Chapter 3: Cryptographic Foundations

  1. Symmetric Encryption
  2. Asymmetric Encryption and Public Key Cryptography
  3. Elliptic Curves and Curve25519
  4. Hash Functions
  5. Message Authentication Codes (MACs) and AEAD
  6. Key Derivation Functions (KDFs)
  7. Digital Signatures
  8. Diffie-Hellman Key Exchange
  9. Perfect Forward Secrecy (PFS)
  10. Randomness
  11. Putting the Primitives Together: A Hybrid Encryption Pattern
  12. Which Primitives Does Tor Use Where?

Chapter 4: Threat Modeling and Anonymity Systems

  1. Adversarial Models in Anonymity Research
  2. The Honest Majority Assumption and Its Limits
  3. Attacks on Anonymity Networks
  4. Architectural Patterns for Anonymity
  5. The Threat Model Summary for Tor

Chapter 5: The History of Onion Routing

  1. 1981: Chaum’s Untraceable Electronic Mail
  2. The 1990s: Anonymous Remailers and Mixmaster
  3. 1996-1997: The First Onion Routing Design
  4. 2000-2002: The Second Generation Redesign at NRL
  5. 2004: The Tor Paper
  6. 2004-2010: Growth, Onion Services and Censorship Circumvention
  7. 2010-Present: Modern Tor, v3 Onion Services and Ongoing Research
  8. Lessons from History

Chapter 6: Tor Design Goals and System Architecture

  1. Design Goals
  2. The Tor Network: Major Components
  3. The Overall Architecture
  4. Data Flow: A User’s Request Through Tor
  5. Terminology Table
  6. How the Book Builds on This Architecture

Part II: The Tor Network

Chapter 7: Relays, Exits, Authorities and HSDirs

  1. What Is a Relay?
  2. Relay Identity and Keys
  3. Relay Descriptors
  4. The Different Roles of a Relay
  5. Guard Relays
  6. Exit Relays
  7. Directory Authorities in Depth
  8. Hidden Service Directories
  9. Bridges
  10. How Relay Types Interact

Chapter 8: The Directory System and Consensus

  1. The Documents of the Directory System
  2. The Consensus: What It Is and Why It Matters
  3. How the Consensus Is Built
  4. The Role of Directory Caches
  5. Verification and Trust: The Chain of Trust
  6. Consensus Format: An Example
  7. Directory Protocol: How Clients Download Documents
  8. What Can Go Wrong in the Directory System?

Chapter 9: Relay Discovery, Path Selection and Guards

  1. The Building Blocks of Path Selection
  2. Guard Selection
  3. Middle Relay Selection
  4. Exit Relay Selection
  5. Circuit Purpose and Path Length
  6. Bandwidth Weighting and Path Stability
  7. Avoiding Bad Paths
  8. Internal Circuits vs Exit Circuits
  9. Bridges and Path Selection in Censored Environments
  10. Putting It All Together: Building a Path

Chapter 10: Bootstrapping a Tor Client

  1. The Bootstrap State Machine
  2. Bootstrap in Detail
  3. Bootstrap Status Events
  4. Bootstrap Failures and Recovery
  5. Bootstrap Time and Performance
  6. What Happens After Bootstrap

Part III: Circuits and Protocols

Chapter 11: The Cell and the OR Connection

  1. The Cell Format
  2. The Cell Command Set
  3. The OR Connection
  4. The Channel Abstraction in the Source Code
  5. Cell Flow: A Simple Example
  6. Why Fixed Size Cells Matter

Chapter 12: Circuit Construction and the ntor Handshake

  1. The Telescoping Circuit Model
  2. The ntor Handshake
  3. ntor-v3
  4. Circuit Construction in Detail: A Sequence Diagram
  5. Key Derivation for Circuit Layers
  6. Circuit IDs and Circuit State
  7. Circuit Failure and Replacement
  8. Putting It Together: From Path to Circuit

Chapter 13: Relay Cells, Layered Encryption and Data Flow

  1. The RELAY Cell
  2. Layered Encryption of Relay Cells
  3. The Encryption Primitives
  4. A Simple Educational Example of Layered Encryption
  5. Stream Multiplexing
  6. Flow Control
  7. DNS Resolution Through the Circuit
  8. Circuit Teardown
  9. From Cells to Applications

Part IV: Traffic and Applications

Chapter 14: Streams, Multiplexing and Flow Control

  1. Streams: The Abstraction Between Applications and Circuits
  2. Stream States and Lifecycle
  3. Mapping Streams to Circuits
  4. Circuit Multiplexing and the Circuitmux
  5. Flow Control in Depth
  6. Performance Considerations
  7. Security Considerations

Chapter 15: DNS Resolution, Exit Policies and Traffic Classification

  1. DNS Resolution in Tor
  2. DNS Privacy and the DNSPort
  3. Exit Policies
  4. Exit Policy and Path Selection
  5. Exit Relay Risks and Responsibilities
  6. Traffic Classification and Censorship
  7. The Future of DNS in Tor

Chapter 16: The Tor Browser Bundle

  1. What Is Tor Browser?
  2. How Tor Browser Integrates with the Tor Client
  3. Fingerprinting Resistance
  4. Security Levels
  5. Identity Management
  6. Tor Browser and the Tor Network

Part V: Onion Services

Chapter 17: Onion Services: Design and Cryptography

  1. The Goal of Onion Services
  2. The Participants in an Onion Service Connection
  3. v2 vs v3 Onion Services
  4. The v3 Onion Address
  5. The v3 Key Hierarchy
  6. Onion Service Descriptors
  7. Introduction Points
  8. Client Authorization
  9. The Onion Service Protocol at a Glance

Chapter 18: The Onion Service Rendezvous Protocol

  1. The Basic Idea
  2. Step-by-Step Protocol
  3. The hs-ntor Handshake in Detail
  4. Security Properties of the Rendezvous Protocol
  5. Rendezvous Failures and Recovery
  6. From Rendezvous to Communication

Part VI: Security and Threats

Chapter 19: Traffic Analysis and Correlation

  1. The Fundamental Attack: End-to-End Correlation
  2. Timing Attacks
  3. Passive Correlation and Statistical Methods
  4. Correlation Through the Middle
  5. The Limits of Traffic Analysis Defenses

Chapter 20: Website Fingerprinting and Countermeasures

  1. How Website Fingerprinting Works
  2. Types of Website Fingerprinting Attacks
  3. Tor’s Defenses Against Website Fingerprinting
  4. The Tradeoff Between Privacy and Performance

Chapter 21: Sybil Attacks, Malicious Relays and DoS

  1. Sybil Attacks
  2. Malicious Relays
  3. Denial of Service Attacks
  4. Censorship and Censorship Circumvention

Chapter 22: Operational Security and Best Practices

  1. Opsec for Tor Users
  2. Opsec for Relay Operators
  3. The Human Factor
  4. Summary of Threats and Defenses

Part VII: Implementation and Code

Chapter 23: The Tor Codebase

  1. Overview
  2. Repository and Build System
  3. Directory Structure
  4. Source File Map
  5. Key Data Structures
  6. Key Functions and Execution Paths
  7. Testing and Debugging
  8. Contributing to Tor

Chapter 24: Educational Implementations

  1. Introduction
  2. Implementation 1: Layered Onion Encryption
  3. Implementation 2: Circuit Establishment and Cell Relay
  4. Implementation 3: Ntor Handshake (Rust)
  5. Implementation 4: Onion Service Rendezvous (Go)

Chapter 25: Building and Running Tor

  1. Building Tor from Source
  2. Running a Tor Relay
  3. Contributing to Tor

Appendix A: Glossary

Appendix B: Bibliography

Appendix C: Index

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub