Linux bash 遍历 apache access_log 文件并发送邮件

Linux bash to iterate over apache access_log files and send mail

我需要一个 linux bash 脚本,如果在 apache 日志中进行的搜索中出现任何结果,它会向我发送电子邮件。

我有一个非常简单的方法(句子)来研究SQL注入攻击,它只是搜索SQLi中使用的一些关键字。这是:

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

所以现在我希望能够在多个 access_log 中启动它(对于每个网站 - 我拥有的虚拟主机)并在找到结果时给我发电子邮件。

示意图:

我有 apache access_log 个文件,每个虚拟主机一个:

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

以及我正在谈论的 bash 过程的方案:

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;

有人知道如何实现这个脚本吗?

提前致谢

你的计划很好,只需要编码即可。 试试这个:

#!/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