in ,

Analyzing the Attacks on my Website, Hacker News

Analyzing the Attacks on my Website, Hacker News
    

      

      

            Pluralsight profile image Pluralsight profile image Pluralsight                                                     

            

Pluralsight profile image               jeremycmorgan profile imagePluralsight profile image Jeremy Morgan                                      twitter logo Pluralsight profile image              github logo              Feb 8

        Pluralsight profile image Note: Pluralsight is currently having a Free Weekend , so you can take any of their courses this weekend for free. Including a huge Security library !! Pluralsight profile imagePluralsight profile image I was casually doing a security audit on my blog ( JeremyMorgan.com recently and decided to look a little deeper into my security logs. With a bit of Linux command line kung fu, some Golang, and Google sheets, I was able to get a pretty good idea of ​​where the attacks are coming from. Pluralsight profile image To start, I’m using CentOS to host my site, so I checked out / var / log / secure. This log is where authentication logs are stored on my server. Pluralsight profile image This is what the log file looks like: Pluralsight profile imagePluralsight profile imagePluralsight profile imagePluralsight profile imagePluralsight profile image and with 348, lines it’s not likely I’m going to manually look around much. Let’s automate this a bit. Pluralsight profile imageAnalyzing attacks on my website   

     Getting the IP Address of attackers

I wanted to extract the IP address of attackers from this file. That way I can block them. Pluralsight profile imagePluralsight profile image I started to mess around with Linux commands until I came up with this script . Pluralsight profile image

What it does is pretty simple, it’s going to look for these strings:

declare -a badstrings=(“Failed password for invalid user”                 “input_userauth_request: invalid user”                 “pam_unix (sshd: auth): check pass; user unknown”                 “input_userauth_request: invalid user”                 “does not map back to the address”                 “pam_unix (sshd: auth): authentication failure”                 “input_userauth_request: invalid user”                 “reverse mapping checking getaddrinfo for”                 “input_userauth_request: invalid user”                 )

These are strings that identify logs of failed attacks. If they put in the wrong username or tried some other form of attack, it would have one of these strings. Pluralsight profile image

So we loop through that list and search for these strings, then extract an IP address from the line the string exists in. Pluralsight profile image

cat / var / log / secure | grep "$ i" | grep -E -o "([0-9] {1,3} [.]) {3} [0-9] {1,3}" | awk '{print $ 0}' | sort | uniq>> "temp.txt"

It then dumps the IP into a (temp.txt) file. It will do this for all of the messages I have in my "badstrings" list. Pluralsight profile image

That text file had a ton of duplicates in it, so I removed the duplicates and put only the unique IPs into a file: Pluralsight profile image

# grab unique ips from temp and put them in a file cat "temp.txt" | sort | uniq> "badguyips.txt" # remove the temp file rm "temp.txt"

Cool, now I have a list of IP addresses ready to go. Pluralsight profile imagePluralsight profile image

Yikes, I have (1, IP addresses here. Pluralsight profile imageAnalyzing attacks on my website      

  Blocking Them

Now I want to block these IP addresses. Since I'm running iptables, I can just drop them with this simple script: Pluralsight profile image #! / bin / bash jeremycmorgan profile image $ line jeremycmorgan profile image - j jeremycmorgan profile image DROP ($ input)

Cool. Now the attackers blocked from my server. Pluralsight profile image

Then I got curious. Where the heck are these attacks coming from? Pluralsight profile imageAnalyzing attacks on my website      

  Getting their Location Data

Since I have a list of IP addresses, I thought I'd run them against a database like Maxmind

to find some location information. So I did just that.

Pluralsight profile image I wrote this Golang program called "find the bad guys" that would go through the text file of IP addresses, and look up their location information, then write it to a series of text files.

Pluralsight profile image I wrote out locations based on: Pluralsight profile image (Continent) (Countries) (Cities) (Subdivisions of Cities)