Anyone can run a Tor relay. You install tor, write a few lines of config, and within a few hours your relay is in the consensus. That openness is how the network gets thousands of relays for free, and it's also its oldest weakness: nothing stops one person from starting hundreds.
Running many fake identities in a system that assumes they're independent is called a Sybil attack, after a 2002 paper by John Douceur that borrowed the name from a book about a woman with multiple personalities. In Tor's case, the danger is simple. If one party controls enough relays, they'll sometimes be both your guard and your exit, or your exit and your DNS, and the protections from splitting knowledge across relays disappear.
What a malicious relay can do
It depends on the position.
A malicious exit can see and modify any unencrypted traffic. That's the most common kind of incident, because it pays. Exits have been caught stripping HTTPS redirects (SSL stripping), injecting content into downloads, and replacing cryptocurrency addresses on plain HTTP pages so payments go to the attacker. A study in 2014 called "Spoiled Onions" (Winter and colleagues) built a scanner for this and found dozens of misbehaving exits over several months.
A malicious guard sees your IP address. On its own that tells it little, but paired with exits or with observation elsewhere it's the entry half of a correlation attack.
A malicious HSDir or middle relay is more subtle. Relays that store onion service descriptors can count lookups or, in the old v2 design, collect onion addresses. Middle relays near onion services can help with guard discovery. The v3 onion design fixed most of the HSDir problems by blinding the keys, so an HSDir can't learn which service a descriptor belongs to.
Incidents worth knowing
The 2014 Carnegie Mellon attack is the best-known case. A group of relays joined the network in early 2014, gained the HSDir and Guard flags, and used a traffic confirmation technique based on RELAY_EARLY cells to tag onion service lookups. The Tor Project found and removed them in July 2014. The story, including the later court cases, is in The 2014 Carnegie Mellon Relay Early Attack.
In 2020 an independent researcher who writes as nusenu documented a group tracked as BTCMITM20. It ran large numbers of exit relays, at its peak reportedly close to a quarter of all exit capacity, and used them to strip HTTPS on connections to cryptocurrency-related sites and swap Bitcoin addresses. The Tor Project removed waves of these relays, and the operator kept coming back with new ones for months.
The following year nusenu published research on another group, KAX17, which had run hundreds of relays (mostly non-exit) since around 2017. Its motives were never established. Running that many guards and middles at scale, without obvious profit, is the profile you'd expect from someone interested in deanonymisation research or surveillance rather than theft. The Tor Project removed the relays it could identify.
The common thread: these groups didn't break any cryptography. They just ran relays and waited for clients to pick them.
How the network defends itself
No single mechanism stops Sybils. Several overlapping ones make them expensive.
Bandwidth is measured, not claimed. Clients pick relays in proportion to bandwidth measured by the bandwidth authorities, so to attract a large share of traffic you need to actually provide a large share of capacity. That costs real money.
New relays have to earn trust slowly. The Guard flag needs days of uptime and history. A relay group that wants to be guards has to sit there carrying middle traffic for a while first, which gives the monitoring people time to notice.
Path selection limits overlap. Tor refuses to put two relays from the same /16 IPv4 network or the same declared family into one circuit. Honest operators declare their relays as a family (traditionally with MyFamily, now increasingly with the newer family key scheme), and the authorities allow only two relays per IP address by default. A Sybil operator obviously won't declare a family, so this mainly catches attackers who are lazy about hosting diversity, but plenty are.
People watch. The bad-relays team, volunteers and researchers run exit scanners that fetch known content through every exit and compare results, look for sudden jumps in relays with similar configurations, and track relays with no or fake ContactInfo. When they find something, the directory authorities can mark relays BadExit, restrict them with the MiddleOnly flag, or reject them outright. The directory authorities article covers what those flags do.
Contact info and operator expectations have been tightened. The Tor Project has been moving towards expecting relay operators to be reachable and, for large operators, known to the community. It's a social defence rather than a technical one, and it's controversial with people who think it erodes the anonymity of operators, but it does raise the bar for running hundreds of anonymous relays.
What this means for you as a user
Use HTTPS. Most real-world harm from malicious relays has come from exits tampering with unencrypted connections, and Tor Browser's HTTPS-Only mode closes most of that door. Don't click through certificate warnings over Tor, ever: a warning on a well-known site is exactly what an active exit attack looks like.
Don't try to outsmart path selection with ExitNodes or EntryNodes lists of relays you've decided you trust. You'll make yourself distinctive, and you have no better way of judging relays than the people whose job it is. The guard mechanism, described in guard relays, already does more against Sybils than manual pinning could.
And if you run relays yourself, set a real ContactInfo and declare your family properly. It costs you nothing and it's one of the things that makes the honest majority easy to tell apart from everyone else. The setup is in Running a Tor Relay.