2012-11-10 31 views
1

我试图在我的apache访问日志中找到任何空白的用户代理和欺骗用户代理的痕迹。在访问日志中查找空白的用户代理和欺骗UA

下面是从我的访问日志的典型线路:(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 

我能做些什么不同到m让这些命令更强大,更深思熟虑,同时给我提供最好的信息来打击互联网上的机器人和其他渣滓?

回答

1

我不会使用\"作为字段分隔符。 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\" [email protected]"; 
} 

# 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,盐味。

+0

对不起,迟到了,但这很酷。绝对得到书签以供将来使用。谢谢! –