sys和cc攻擊防範分享

調整系統參數擋攻擊

第一個參數tcp_synack_retries = 0是關鍵,表示迴應第二個握手包(SYN+ACK包)給客戶端IP後,如果收不到第三次握手包(ACK包)後,不進行重試,加快回收“半連接”,不要耗光資源。

不修改這個參數,模擬攻擊,10秒後被攻擊的80端口即無法服務,機器難以ssh登錄; 用命令netstat -na |grep SYN_RECV檢測“半連接”hold住180秒;

修改這個參數爲0,再模擬攻擊,持續10分鐘後被攻擊的80端口都可以服務,響應稍慢些而已,只是ssh有時也登錄不上;檢測“半連接”只hold住3秒即釋放掉。

修改這個參數爲0的副作用:網絡狀況很差時,如果對方沒收到第二個握手包,可能連接服務器失敗,但對於一般網站,用戶刷新一次頁面即可。這些可以在高峯期或網絡狀況不好時tcpdump抓包驗證下。

根據以前的抓包經驗,這種情況很少,但爲了保險起見,可以只在被tcp洪水攻擊時臨時啓用這個參數。

tcp_synack_retries默認爲5,表示重發5次,每次等待30~40秒,即“半連接”默認hold住大約180秒。詳細解釋:

The tcp_synack_retries setting tells the kernel how many times to retransmit the SYN,ACK reply to
an SYN request. In other words, this tells the system how many times to try to establish a passive
TCP connection that was started by another host.
This variable takes an integer value, but should under no circumstances be larger than 255 for the
same reasons as for the tcp_syn_retries variable. Each retransmission will take aproximately 30-40
seconds. The default value of the tcp_synack_retries variable is 5, and hence the default timeout
of passive TCP connections is aproximately 180 seconds.

之所以可以把tcp_synack_retries改爲0,因爲客戶端還有tcp_syn_retries參數,默認是5,即使服務器端沒有重發SYN+ACK包,客戶端也會重發SYN握手包。詳細解釋:

The tcp_syn_retries variable tells the kernel how many times to try to retransmit the initial SYN
packet for an active TCP connection attempt.
This variable takes an integer value, but should not be set higher than 255 since each
retransmission will consume huge amounts of time as well as some amounts of bandwidth. Each
connection retransmission takes aproximately 30-40 seconds. The default setting is 5, which
would lead to an aproximate of 180 seconds delay before the connection times out.

第二個參數net.ipv4.tcp_max_syn_backlog = 200000也重要,具體多少數值受限於內存。

以下配置,第一段參數是最重要的,第二段參數是輔助的,其餘參數是其他作用的:
# vi /etc/sysctl.conf

使配置生效:
# sysctl -p

注意,以下參數面對外網時,不要打開。因爲副作用很明顯,具體原因請google,如果已打開請顯式改爲0,然後執行sysctl -p關閉。因爲經過試驗,大量TIME_WAIT狀態的連接對系統沒太大影響:

爲了處理大量連接,還需改大另一個參數:
# vi /etc/security/limits.conf 

在底下添加一行表示允許每個用戶都最大可打開409600個文件句柄(包括連接):
*                –       nofile          409600

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章