centos 7系統iptables防火牆和連接埠配置

來源:互聯網
上載者:User

一,連接埠配置

開啟redis連接埠,修改防火牆設定檔     vi /etc/sysconfig/iptables

加入連接埠配置   

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379 -j ACCEPT

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 6379:7000 -j ACCEPT 

//開放6379到7000之間的連接埠

service iptables save

service iptables restart 

CentOS 7.0預設使用的是firewall作為防火牆,這裡改為iptables防火牆。

1、關閉firewall:

systemctl stop firewalld.service #停止firewall

systemctl disable firewalld.service #禁止firewall開機啟動

防範DDOS攻擊指令碼

#防止SYN攻擊 輕量級預防
iptables -N syn-flood
iptables -A INPUT -p tcp --syn -j syn-flood
iptables -I syn-flood -p tcp -m limit --limit 3/s --limit-burst 6 -j RETURN
iptables -A syn-flood -j REJECT
#防止DOS太多串連進來,可以允許外網網卡每個IP最多15個初始串連,超過的丟棄
iptables -A INPUT -i eth0 -p tcp --syn -m connlimit --connlimit-above 15 -j DROP
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

#用Iptables抵禦DDOS (參數與上相同)
iptables -A INPUT  -p tcp --syn -m limit --limit 12/s --limit-burst 24 -j ACCEPT
iptables -A FORWARD -p tcp --syn -m limit --limit 1/s -j ACCEPT
##########################################################
防範CC攻擊

當apache網站受到嚴重的cc攻擊,我們可以用iptables來防止web伺服器被CC攻擊,實現自動屏蔽IP的功能。
1.系統要求
(1)LINUX 核心版本:2.6.9-42ELsmp或2.6.9-55ELsmp(其它核心版本需要重新編譯核心,比較麻煩,但是也是可以實現的)。
(2)iptables版本:1.3.7
2. 安裝
安裝iptables1.3.7和系統核心版本對應的核心模組kernel-smp-modules-connlimit
3. 配置相應的iptables規則

樣本如下:

(1)控制單個IP的最大並發串連數
iptables -I INPUT -p tcp --dport 80 -m connlimit  --connlimit-above 50 -j REJECT #允許單個IP的最大串連數為 30
#預設iptables模組不包含connlimit,需要自己單獨編譯載入,

(2)控制單個IP在一定的時間(比如60秒)內允許建立立的串連數
iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --update --seconds 60 --hitcount 30 -j REJECT iptables -A INPUT -p tcp --dport 80 -m recent --name BAD_HTTP_ACCESS --set -j ACCEPT
#單個IP在60秒內只允許最多建立30個串連

4. 驗證

(1)工具:flood_connect.c(用來類比攻擊)
(2)查看效果:

使用
watch 'netstat -an | grep:21 | grep<類比攻擊客戶機的IP>| wc -l'

即時查看類比攻擊客戶機建立起來的串連數,
使用
watch 'iptables -L -n -v | \grep<類比攻擊客戶機的IP>'

查看類比攻擊客戶機被 DROP 的資料包數。
5.注意
為了增強iptables防止CC攻擊的能力,最好調整一下ipt_recent的參數如下:
#cat/etc/modprobe.conf options ipt_recent ip_list_tot=1000 ip_pkt_list_tot=60
#記錄1000個IP地址,每個地址記錄60個資料包 #modprobe ipt_recent

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.