我正在尝试在我的 apache 访问日志中查找任何空白用户代理和欺骗用户代理的痕迹。
这是我的访问日志中的典型行:(IP 和域已编辑)
x.x.x.x - - [10/Nov/2012:16:48:38 -0500] "GET /YLHicons/reverbnation50.png HTTP/1.1" 304 - "http://www.example.com/newaddtwitter.php" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/534.7 ZemanaAID/FFFF0077"
对于空白用户代理,我正在尝试这样做:
awk -F\" '($6 ~ /^-?$/)' /www/logs/www.example.com-access.log | awk '{print $1}' | sort | uniq
为了查找有关 UA 的信息,我正在运行此命令:(提供每个独特 UA 的点击量)
awk -F\" '{print $6}' /www/logs/www.example.com-access.log | sort | uniq -c | sort -fr
我可以采取哪些不同的措施来使这些命令更强大、更经过深思熟虑,同时为我提供最好的信息来打击机器人和互联网上的其他败类?
最佳答案
我不会使用 \"
作为字段分隔符。CLF 的构造足够好,如果您以空格分隔,字段 12 就是用户代理的开始。如果 $12 = = '""'
,用户代理为空。
请记住,awk
可以接受标准输入。因此,您可以通过以下方式“实时”监控您的 Apache 日志:
$ tail -F /path/to/access.log | /path/to/awkscript
请记住,以这种方式调用时,awk 脚本将永远不会到达其 END
。但是您可以在 Apache 将行添加到日志中时对其进行处理。
这样的事情可能会有所帮助。添加您认为合适的内容。
#!/usr/bin/awk -f
BEGIN {
mailcmd="Mail -s \"Security report\" <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7e091b1c131f0d0a1b0c3e1b061f130e121b501d1113" rel="noreferrer noopener nofollow">[email protected]</a>";
}
# Detect empty user-agent
$12 == "" {
report="Empty user agent from " $1 "\n";
}
# Detect image hijacking
$7 ~ /\.(png|jpg)$/ && $11 !~ /^http:\/\/www.example.com\// {
report=report "Possible hijacked image from " $1 " (" $11 " -> " $7 ")\n";
}
# Detect too many requests per second from one host
thissecond != $4 {
delete count;
thissecond=$4;
}
{
count[$1]++;
for (ip in count) {
if (count[ip] > 100) {
report=report "Too many requests from " $1 "\n";
delete(count[ip]); # Avoid too many reports
}
}
}
# Send report, if there is one
length(report) {
print report | mailcmd; # Pipe output through a command.
close(mailcmd); # Closing the pipe sends the mail.
report=""; # Blank the report, ready for next.
}
请注意,对特定秒内的请求进行计数的作用不大;如果您有大量来自中国或防火墙后面的大学/企业网络的流量,那么许多请求可能看起来来自单个 IP 地址。而且 Mail
命令并不是处理通知的好方法。我将其放在这里仅用于演示目的。 YMMV,加盐调味。
关于apache - 在访问日志中查找空白用户代理和欺骗性 UA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326605/