DoS and DDoS
A denial-of-service (DoS) attack comes from one source. Blocking that one address usually ends it. A distributed attack (DDoS) comes from thousands of addresses at the same time, often spread across many networks and countries, so blocking single sources by hand does not keep up.
Botnets and Amplification
- Botnets: networks of compromised computers, routers, cameras, and other devices that an attacker controls remotely and directs at one target.
- Reflection and amplification: the attacker sends small requests with a forged source address to public services, such as open DNS resolvers or badly configured NTP or memcached servers. Each service sends a much larger answer to the forged address, which is the victim.
The second point is also a reason to keep your own server tidy: a DNS resolver or other UDP service open to the whole internet can be used against someone else.
The Three Main Types
| Type | What it exhausts | Examples |
|---|---|---|
| Volumetric | Bandwidth of the link | UDP floods, amplification |
| Protocol | Connection and state tables | SYN floods, fragment floods |
| Application layer | Web server, application, database | HTTP floods, slow requests |
Volumetric attacks are measured in bits per second, protocol attacks in packets per second, and application-layer attacks in requests per second. An application-layer attack can be small in bandwidth and still stop a site, because each request makes the server do expensive work.
How an Attack Looks from the Server
- Load:
uptimeandtopshow high load or CPU use without a matching rise in real visitors. - Connection counts: far more open or half-open connections than usual.
- Logs: the web server's access log fills with the same URL, the same user agent, or requests from many unfamiliar addresses.
- Errors: the site answers slowly or with 503 Service Unavailable.
- Nothing at all: in a large volumetric attack the link is full before traffic reaches the server, so the server looks calm while nobody can reach it.
Commands to Look Closer
uptime
ss -s
ss -Htn state syn-recv | wc -l
ss -Htn state established | awk '{sub(/:[^:]*$/, "", $NF); print $NF}' | sort | uniq -c | sort -rn | head
The first ss command summarizes sockets, the second counts half-open connections, and the last lists the addresses with the most established connections. -H leaves out the header line, and the awk part strips the port from the peer address in the last column. For the web server's view:
sudo awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
sudo awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
These show the busiest client addresses and the most requested URLs. The awk command guide explains the fields.
Not Every Spike Is an Attack
A link shared on a busy site, an aggressive crawler, a backup job, or a client stuck in a retry loop can look similar. Check the referrers, user agents, and time of day before you block anything.
First Responses
- Record the facts: start time, the target address and port, sample source addresses, and log lines.
- Contact your hosting provider early: a volumetric attack can only be handled upstream of your server, and the facts above help the provider act.
- Rate limit requests: for application-layer traffic, limit requests per client in the web server and answer the rest with 429 Too Many Requests.
- Block clear single sources in the firewall, and change one thing at a time so you can see what helps.
A basic Nginx rate limit:
limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
server {
location / {
limit_req zone=perip burst=20 nodelay;
}
}
Test with sudo nginx -t before reloading. The full response plan is in how to stop a DDoS attack.