I have a modified log file listing all the IP addresses which have accessed my server.

Next to the IP address on each line is a date/time stamp.

In my quest to filter bots and malicious activity, I would like to check if any IP addresses have made more than 5 requests in less than 15 seconds.

I have already posted a question on how to measure this from the first request, but I would like to take this further.

I am currently learning (G)AWK, and I would like to learn more. If possible, I would like this to be completed in (G)AWK and explained as explicitly as possible, so that I can study the script and, hopefully in the future, I will be able to write similar scripts myself.

While I understand this can most likely be done in Python, I am not currently learning Python.

Sample input

Here is a full (working) modified (to obfuscate the IP addresses) version of my log file: log-file.csv.

And, below is a [further modified] sample of the file, in case you prefer to test your results against that instead:


The desired output

I would like you to produce a document named bot-list.txt, containing a list of IP addresses which have made 5 requests or more in less than 15 seconds at any time (not necessarily the first 5). I may want to adjust the frequency/duration at a later date.

Because the log file is large, I don't feel it would be appropriate to post the whole thing here. However, the output of the (modified) sample above is only 2 IP addresses. Thus, it will be considerably more reliable to test the full log file.

Please be explicit in how you achieved the result. As an added bonus, if you do use any obscure technique/function, I would be pleased if you can point to a reference where I can learn more about this technique.

So, to be clear:

  • Search for each unique IP in log-file.csv
  • Where there are 5 or more instances of this IP, calculate the difference in seconds between them using the date/time stamp on each line.
  • Isolate IP addresses where 5 pages (or more) are accessed in less than 15 seconds.
  • Append these IP address to bot-list.txt.
  • If possible, complete the task in a single (G)AWK script.

Further advice on how to improve on this concept would be welcome.

What I have tried

I honestly don't know how to compare rows in GAWK, but it is clear from some of the answers in my previous question, that it is possible.

Currently, I am reading through the book "Effective AWK Programming". It is very interesting, but I struggle with some of it. I am also looking into associative arrays, but as I don't come from a programming background, I am picking this knowledge up slowly.

I cannot find anything which solves my specific problem and videos on this subject are few and far between.

If anyone can point to helpful resources which can help me to solve problems like this, I would be appreciative.

My attempt at comparing the dates:

egrep "" log-file.csv | awk 'BEGIN{FS="|"; ORS=" "} NR==1 || NR==5 {print $1,$2}' | sed -e 's/[\/:]/\ /g' -e 's/Jan/1/g' -e 's/Feb/2/g' -e 's/Mar/3/g' -e 's/Apr/4/g' -e 's/May/5/g' -e 's/Jun/6/g' -e 's/Jul/7/g' -e 's/Aug/8/g' -e 's/Sep/9/g' -e 's/Oct/10/g' -e 's/Nov/11/g' -e 's/Dec/12/g' | awk '{print $3,$2,$1,$4,$5,$6 "," $10,$9,$8,$11,$12,$13","$14}' | awk -F, '{d2=mktime($2);d1=mktime($1);print d2-d1, $3}'  | awk '{if($1<15)print $2}' >> bot-list.txt

Unfortunately, as you can see, I could not do the whole thing in (G)AWK, but I would appreciate it if you can.

(This is my second time using Stack Exchange. I have attempted to follow advice on how to lay out my question, I am happy to receive further advice if I have made any errors).

Thank you.

  • 1
    I'm sorry this has been dowvoted while other questions with no attempt/research get upvoted here. Anyway, the main hassle here is the timestamp. It will be a major pain to verify if two timestamps in that format are 15 seconds apart. It would be so much easier if it was in Epoch time. Do you have control over the server to change this bit? – Quasímodo Nov 4 '20 at 20:50
  • Hi @Quasímodo , I do have control over the server, and I am happy to make adjustments, but I will warn that I am not a programmer or sever admin, so my knowledge of servers is fairly limited. The data originates from nginx. – Bumbling Badger Nov 4 '20 at 21:40
  • 1
    I wouldn't worry about it since it's trivial to convert date+time to epoch seconds with GNU awk as seen in the answers to your previous question – Ed Morton Nov 4 '20 at 21:46
  • 1
    You said it will be considerably more reliable to test the full log file. but you didn't tell us what the expected output would be for the full log file so we can't know if a text passed or failed. The script in my answer output 126 IP addresses from your full log file but idk if that's the right or wrong number we should get. – Ed Morton Nov 4 '20 at 22:24
  • @EdMorton You make a good point. My logic here is flawed. I guess what I meant to say was that given the small number of results that could be gleaned from my sample, the log file could provide more meaningful results, however, as you correctly stated, it is difficult to know, when answering the question when you have hit the mark. My thoughts: To take meaningful results, it would seem to me to be best to run a script against a large data sample. But, here lies the issue... How do I provide an accurate desired outcome on a large sample if I need a script to do it accurately? Advice welcome. – Bumbling Badger Nov 5 '20 at 6:45

A simple, brute-force approach that reads all of your IP addresses and timestamps after converting to epoch seconds into memory (the secs[] array) then once the whole file has been read loops through the array one IP address at a time looking at the difference in timestamps every 5 entries apart to see if any are less than 15 secs:

$ cat tst.awk
BEGIN { FS="|" }
    monthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",t[2])+2)/3
    currSecs = mktime(t[3] " " monthNr " " t[1] " " t[4] " " t[5] " " t[6])

    secs[$2][++count[$2]] = currSecs
    range = 5
    for (ip in secs) {
        for (beg=1; beg<=(count[ip]-range)+1; beg++) {
            end = beg + range - 1
            if ( (secs[ip][end] - secs[ip][beg]) < 15 ) {
                print ip

$ awk -f tst.awk file

It does mean you need to have enough memory to store the list of IP addresses and epoch seconds but your file would have to be massive (billions of lines) for that to become an issue.

EDIT: per the comments under your question your input file isn't in the correct order (incrementing date+time) so we can do this to fix that:

$ awk -F'|' '
        monthNr = (index("JanFebMarAprMayJunJulAugSepOctNovDec",t[2])+2)/3
        currSecs = mktime(t[3] " " monthNr " " t[1] " " t[4] " " t[5] " " t[6])

        print currSecs, NR, $0
' log-file.csv | sort -k1,1n -k2,2n | cut -d' ' -f3- > sorted-log-file.csv

and then run the above script on THAT to get the following 50 IP addresses output:

$ awk -f tst.awk sorted-log-file.csv | sort
  • Thank you. After fixing the log file, your script now delivers 55 IPs. I compared this to the previous script (for the last question) which delivered 40 on this particular log file. – Bumbling Badger Nov 8 '20 at 3:49
  • You're welcome, see stackoverflow.com/help/someone-answers for what to do next. – Ed Morton Nov 8 '20 at 13:21
  • I just want to check that the script matches the current format. From memory the script matched 128 IPs. Since the script was reordered by date, the same script matches 55. The reason could be that, due to my error, there were false positives or that, because the data is fresh, the user behaviour has changed. I realise from our previous discussions that this makes it tricky to measure as I cannot easily judge the performance of the script. For this reason, If you feel the new date order doesn't negatively affect the outcome of the script, I am happy to mark your solution as the answer. – Bumbling Badger Nov 8 '20 at 21:39
  • 1
    The script is accurate. – Ed Morton Nov 8 '20 at 22:46

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.