In order to counter a botnet attack, I am trying to analyze a nginx access.log file to find which user agents are the most frequent, so that I can find the culprits and deny them. How can I do that?
Asked
Active
Viewed 5,699 times
1
-
I think if you chain `awk` and `uniq` you could find the most frequent ip's – Mohammad AbuShady Apr 09 '14 at 09:03
2 Answers
6
Try something like this on your access log, replace with the path to your access log, also keep in mind that some log files would get zipped and new one would be created
sudo awk -F" " '{print $1}' /var/log/nginx/access.log | sort | uniq -dc
EDIT:
Sorry I just noticed you wanted user agent instead of IP
sudo awk -F"\"" '{print $6}' /var/log/nginx/access.log | sort | uniq -dc
To sort ascending append | sort -nr
and to limit to 10 append | head -10
so the final total line would be
sudo awk -F"\"" '{print $6}' /var/log/nginx/access.log | sort | uniq -dc | sort -nr | head -10

Mohammad AbuShady
- 40,884
- 11
- 78
- 89
-
you can chain another sort to sort the order and head to limit to 10, I'll edit my answer – Mohammad AbuShady Jun 26 '14 at 06:59
2
To get user agent
sudo awk -F'"' '/GET/ {print $6}' /var/log/nginx-access.log | cut -d' ' -f1 | sort | uniq -c | sort -rn
awk(1) - selecting full User-Agent string of GET requests
cut(1) - using first word from it
sort(1) - sorting
uniq(1) - count
sort(1) - sorting by count, reversed

Arun
- 1,011
- 11
- 13