1

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?

1
  • I think if you chain awk and uniq you could find the most frequent ip's Apr 9, 2014 at 9:03

2 Answers 2

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
1
  • you can chain another sort to sort the order and head to limit to 10, I'll edit my answer Jun 26, 2014 at 6: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.