nginx web安全二

來源:互聯網
上載者:User

標籤:nginx安全   iptables限制ip   web串連數限制   ip限制   

最近在分析nginx日誌發現,有很多可以ip訪問網站根目錄,如下:

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

GET / - - 58.60.170.219 HTTP/1.1 [Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)]

從00:00--6:00這個ip的記錄已經達到10萬條左右,如何解決此問題呢?

本來nginx上有相關模組limit_conn 100 及limit_req 12 burst 10做了一層限制,但是效果不是很明顯。因為之前日誌也有過類似ip,導致503錯誤很多,但這個ip並沒有產生503錯誤很多,這說明limit模組配置不太合適。

limit具體參數的調整就不說了,但是對於limit_conn 100這個模組有些疑問:(1)同一時間有100個ip在訪問(2)產生100個串連;請明白的具體說下,不勝感激!!

既然nginx無法完全限制,我們就換個思路,結合iptables限制特定時間內ip的串連數來實現。

iptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --name httpuser --rcheck --seconds 60 --hitcount 20 -j DROPiptables -A INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --name httpuser --set -j ACCEPT

測試過程:

當我把--hitcount設定為8時:

[[email protected] webbench-1.5]# ab -c 10 -n 20-t 60  http://192.168.3.124/1.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.3.124 (be patient)
apr_socket_recv: Connection timed out (110)
Total of 8 requests completed

20個請求只有8個是成功的。

緊接著在60s內再發送請求:

[[email protected] webbench-1.5]# ab -c 10 -n 20 -t 60  http://192.168.3.124/1.html
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/


Benchmarking 192.168.3.124 (be patient)
apr_socket_recv: Connection timed out (110)

已經無法建立串連了,但過了60s後就可以正常測試了。

用netstat -ant |grep ESTABLISHED |grep 192.168.3.124:80|wc -l ,發現ESTABLISHED狀態的個數在8個以內。

當將當--hitcount設定為20時,發現ESTABLISHED狀態的個數在20以內,大致10個左右,而且網頁訪問速度也有提高。


還有一種寫法是:

#iptables -I INPUT -p tcp --dport 80 -i eth0 -m state --state NEW -m recent --name httpuser --set#iptables -A INPUT -m recent --update --name httpuser --seconds 60 --hitcount 20 -j LOG --log-prefix 'HTTP attack:'#iptables -A INPUT -m recent --update --name httpuser --seconds 60 --hitcount 20 -j DROP

1.先插入INPUT鏈的最前面,對於80連接埠的建立串連,建立一個名為httpuser的清單( /proc/net/xt_recent/httpuser ),並且將記錄加1

2.在INPUT鏈最後插入後,匹配第二條,在60s內建立的串連叨叨20,會開始記錄日誌(預設iptables日誌寫在/var/log/message下,我們可以設定將其分離,下面講到),並有HTTP attack標識

3.在INPUT鏈最後插入後,繼續匹配第三條,串連達到20後,後續建立的串連會被iptables drop掉。

但是這種寫法適用於INPUT鏈預設規則為ACCEPT的,否則都會DROP,這點需要注意,建議用第一種方法。

另日誌配置:

在/etc/rsyslog.conf中加入:

kern.=warn /var/log/kern-warn-log

防止日誌過大,需要截斷日誌:

vim /etc/logrotate.d/syslog

/var/log/kern-warn-log
{
    rotate 50
    postrotate
    /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}


ps:1.--rcheck --update --set 參數的區別,這裡不多說了。

 2.iptables和nginx配合使用,可以減少大量可疑ip的訪問,不僅增加了安全性,也增加了nginx的抗並發數,一舉兩得啊

 3.本次實驗用ab和webbench測試發現,它們的每次request應該都是建立一個串連,這樣才會符合我們iptables的設定,也符合測試中的request 成功數。這讓我對ab和

webbench這兩個工具又加深了認知。

nginx web安全二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.