Sub topic 10
Hash flooding attacks and seeded hashes
This chapter assumes you know how a hash table maps keys to buckets with a modulo, and that you have read the earlier chapters on collisions and bucket mapping.
Why a predictable hash is a weakness
A hash table is fast on average. Each bucket holds a few keys, so a lookup takes constant time. If many keys land in one bucket, that bucket becomes a long chain or probe sequence. Lookups in it take linear time, and inserting n such keys costs about n² steps in total.
Ordinary data rarely does this. An attacker can make it happen on purpose. If the hash function is public and has no secret, the attacker can compute many keys that share a bucket before sending them. Examples include query parameters, JSON object keys, and HTTP headers. This is a hash flooding attack. A small request can then keep a server's CPU busy, which makes it a denial of service.
Seeded hashing
The defense is to add a secret. The program picks a random seed (a secret key) when it starts and mixes it into every hash. The attacker cannot predict which keys collide without knowing the seed. Keys that collide under one seed will normally spread out under another.
The example below uses the weak sum hash from the earlier chapter. Every permutation of the same letters has the same character sum, so they all share one bucket.
import hashlib
import os
from itertools import permutations
key = os.urandom(16) # secret seed, chosen per process
def sum_hash(data: bytes, buckets: int) -> int:
return sum(data) % buckets
def seeded_bucket(data: bytes, buckets: int) -> int:
digest = hashlib.blake2b(data, key=key, digest_size=8).digest()
return int.from_bytes(digest, "big") % buckets
attack = ["".join(p).encode() for p in permutations("abcdefg")] # 5040 keys
print(len(attack), "attack keys")
print(len({sum_hash(k, 1024) for k in attack}), "bucket used by sum_hash")
print(len({seeded_bucket(k, 1024) for k in attack}), "buckets used when seeded")The sum hash puts all 5040 keys in one bucket. The seeded hash spreads them over about a thousand of the 1024 buckets. The exact count changes on every run because the seed is new each time.
Gotchas
- The seed must be secret and unpredictable. Generate it from
os.urandomor an equivalent. A fixed constant or a timestamp gives the attacker something to guess. - A seed on a weak function is not enough. If the function is simple, such as FNV-1a with a seed XORed in at the start, an attacker may still find collisions that hold for every seed. Production languages use keyed functions designed for this, such as SipHash. Python's built-in
hash()for strings is randomized per process for this reason. - A per-process seed changes bucket order. Iteration order can differ between runs. Never store seeded hash values on disk or send them to other machines, and never depend on that order.
- Seeding is not authentication. It stops an attacker from planning collisions. It does not prove a message is genuine. HMAC covers that, in a later chapter.
Exercise
A web service uses a hash table keyed by request parameter names, with a public, unseeded hash function. Why can an attacker slow it down, and what single change removes the attacker's advantage?
Show answer
The attacker can compute many parameter names that fall in the same bucket, which makes each insert and lookup scan a long chain. Choosing a secret random seed at startup and using a keyed hash such as SipHash or BLAKE2b with that key means the attacker cannot predict which names collide.