Linux bash to iterate over apache access_log files and send mail

你说的曾经没有我的故事 提交于 2019-12-13 22:26:17

问题


I need a linux bash script which send me an email if any results appear in searches made in the apache logs.

I have a very simple method (sentence) to look into SQL Injection attacks, which simply searches for some keywords used in SQLi. Is this:

#tail -50000 /var/vhosts/site.com/logs/access_log | egrep -i "select%20|union%20|'|1=1"

So now I would like to be able to launch it in several access_log (for each website - virtual host I have) and send me an email in case of finding results.

In a schematic way:

I have the apache access_log files, one for each virtual host:

/var/vhosts/website_1/access_log
/var/vhosts/website_2/access_log
etc...

And the scheme of the bash process I'm talking:

for each access_log file (one by virtual host)
    result = tail -50000 /var/www/vhosts/site.com/logs/access_log | egrep -i "select%20|union%20|'|1=1"
    if any line appear in the result then
        send mail(myemail@site.com, 'Warning!: Possible attack in virtual_host_i')
end;

Does anyone know how to implement this script?

Thanks in advance


回答1:


You have a good plan, just need to code it. Try this:

#!/bin/bash
for file in $(ls /var/vhosts/*/access_log);  do 
  result=""   #reset the result variable
  result=$(tail -50000 "${file}" | egrep -i "(select )|(union )|'|(1=1)")
  if [[ ! -z $result ]]; then
    echo "file ${file} contains suspicious lines:"
    echo $result
    # or enter your command for mailing the result
    # for example:
    # echo ${result} | mail -s ${file} youremail@site.com
    # check man page for your mail command!
  fi
done


来源:https://stackoverflow.com/questions/49180232/linux-bash-to-iterate-over-apache-access-log-files-and-send-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!