How to Analyze Distributed Denial-of-Service (DDos) Attack

Information Security

What is DDoS Attack?

As per Wikipedia, denial-of-service (DoS) or distributed denial-of-service (DDoS) attack is an attempt to make a machine or network resource unavailable to its intended users.

In this small post I would like to show a few useful commands to use if someone is experiencing a DDoS attack. In my case, there is an nginx as a front-end server. The access log format looks like this:

log_format main '$remote_addr — $remote_user [$time_local] "$host" "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" -> $upstream_response_time';

In the log file we’ll see something like this: — - [14/Sep/2014:22:51:03 +0400] «» «GET / HTTP/1.1» 200 519 «» «Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.191602; .NET CLR 3.5.191602; .NET CLR 3.0.191602» "-" -> 0.003

Analyzing DDoS Attack

tail -f /var/log/nginx/nginx.access.log | cut -d ' ' -f 1 | logtop This command allows to see a bigger picture: the distribution of unique IPs sending requests, the number of requests from one IP, etc.

The main thing here is that all of this operates in real-time and we can monitor the situation, as well as make necessary changes in the configuration. For example, we can ban the top 20 of the most active IPs via iptables, or limit the geography of requests for some time in nginx with the help of GeoIP (

Once you run the command, it will display (and will update in real-time) something like the following:

3199 elements in 27 seconds (118.48 elements/s) 1 337 12.48/s 2 308 11.41/s 3 304 11.26/s 4 284 10.52/s 5 275 10.19/s 6 275 10.19/s 7 270 10.00/s 8 230 8.52/s 9 182 6.74/s 10 172 6.37/s

In the given case, the columns mean:

  • 1 — the sequence number
  • 2 — the number of requests from the given IP
  • 3 — the number of requests per second from the given IP
  • 4 — the IP itself

At the very top you will see the summery for all of the requests. We can see that IP sends 12,48 requests per second. During the last 27 seconds it has sent 337 requests.

Let’s review it in details:

tail -f /var/log/nginx/nginx.access.log — continuously read the end of the log-file.

cut -d ‘ ’ -f 1 — split the string into “substrings” with the help of a delimiter that is defined in –d flag (in the given case, it’s a space). -f 1 flag means that we only want to show the field with “1” as a sequence number only (in the given case, the field will contain the IP address sending a request).

logtop counts the number of equal strings (i.e., IPs), sorts them in descending order and prints to screen in the form of a list, adding statistics at the same time (in Debian we can do it via aptitude from a standard repository).

grep "&key=" /var/log/nginx/nginx.access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -n | tail -n 30 That will show the distribution of a string by the IP in the log.

In my case, we were to gather the statistics regarding one IP using the &key=… parameter in a request.

We are going to see something of the kind:

31 47 51 53 803 822 912 1856 1867 1878

  • 1st column is the number of string entries (IP in our case)
  • 2nd column is the IP address itself

We can see that IP has sent 1,878 requests (Later we will see in Whois that this IP belongs to Google and is not “harmful”)

Let’s take a closer look at it:

grep “&key=” /var/log/nginx/nginx.access.log we find all the strings in the log that contain “&key=” substring (no matter in what part of the string it’s located)

cut -d ‘ ’ -f 1 (see the previous example) derive an IP address

sort — sort lines (it’s necessary for the correct operation of the next command)

uniq -c — show unique lines and count the number of times the line occurred in the input (-c flag)

sort -n - sort by comparing according to string numerical value (-n flag)

tail -n 30 — derive the last 30 lines (-n 30 flag; we can define a random number of lines)

All the mentioned above requests are provided for Debian and Ubuntu, but I think the commands will look pretty much the same in other Linux distros.



    Ropes — Fast Strings

    Most of us work with strings one way or another. There’s no way to avoid them — when writing code, you’re doomed to concatinate strings every day, split them into parts and access certain characters by index. We are used to the fact that strings are fixed-length arrays of characters, which leads to certain limitations when working with them. For instance, we cannot quickly concatenate two strings. To do this, we will at first need to allocate the required amount of memory, and then copy there the data from the concatenated strings.