2012-11-10 30 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

對不起,遲到了,但這很酷。絕對得到書籤以供將來使用。謝謝! –