CentOS 7 下使用iptables服務

禁止Filewalld開機啟動

為了防止與iptables沖突拭宁,您必須先禁止Filewalld開機啟動。

執(zhí)行如下命令瓣俯,查看服務狀態(tài)宿百。

systemctl status firewalld


當服務處于active狀態(tài)若贮,運行以下命令關(guān)閉Firewalld服務。

systemctl stop firewalld

執(zhí)行如下命令,禁止Filewalld開機啟動辕羽。

systemctl disable firewalld

1 安裝iptables

執(zhí)行如下命令,安裝iptables觅闽。

yum install -y iptables-services

2 啟動iptables并設置為開機啟動

? ? ?執(zhí)行如下命令珊楼,啟動iptables。

systemctl start iptables

? ? 執(zhí)行如下命令绪爸,查看iptables是否成功啟動湾碎。

systemctl status iptables

系統(tǒng)顯示類似如下,說明iptables已經(jīng)成功啟動奠货。

執(zhí)行如下命令胜茧,設置iptables開機啟動。

systemctl enable iptables.service

創(chuàng)建規(guī)則腳本仇味,自己新建一個規(guī)則腳本iptablescript.sh

#!/bin/sh

#

#https://sharadchhetri.com/2013/06/15/how-to-protect-from-port-scanning-and-smurf-attack-in-linux-server-by-iptables/

# Script is for stoping Portscan and smurf attack

# June 15, 2013 by Sharad Chhetri

### first flush all the iptables Rules

iptables -F

# INPUT iptables Rules

# Accept loopback input

iptables -A INPUT -i lo -p all -j ACCEPT

# allow 3 way handshake

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

### DROPspoofing packets

iptables -A INPUT -s 10.0.0.0/8 -j DROP

iptables -A INPUT -s 169.254.0.0/16 -j DROP

iptables -A INPUT -s 172.16.0.0/12 -j DROP

iptables -A INPUT -s 127.0.0.0/8 -j DROP

iptables -A INPUT -s 192.168.0.0/24 -j DROP

iptables -A INPUT -s 224.0.0.0/4 -j DROP

iptables -A INPUT -d 224.0.0.0/4 -j DROP

iptables -A INPUT -s 240.0.0.0/5 -j DROP

iptables -A INPUT -d 240.0.0.0/5 -j DROP

iptables -A INPUT -s 0.0.0.0/8 -j DROP

iptables -A INPUT -d 0.0.0.0/8 -j DROP

iptables -A INPUT -d 239.255.255.0/24 -j DROP

iptables -A INPUT -d 255.255.255.255 -j DROP

#for SMURF attack protection

iptables -A INPUT -p icmp -m icmp --icmp-type address-mask-request -j DROP

iptables -A INPUT -p icmp -m icmp --icmp-type timestamp-request -j DROP

iptables -A INPUT -p icmp -m icmp -m limit --limit 1/second -j ACCEPT

# Droping all invalid packets

iptables -A INPUT -m state --state INVALID -j DROP

iptables -A FORWARD -m state --state INVALID -j DROP

iptables -A OUTPUT -m state --state INVALID -j DROP

# flooding of RST packets, smurf attack Rejection

iptables -A INPUT -p tcp -m tcp --tcp-flags RST RST -m limit --limit 2/second --limit-burst 2 -j ACCEPT

# Protecting portscans

# Attacking IP will be locked for 24 hours (3600 x 24 = 86400 Seconds)

iptables -A INPUT -m recent --name portscan --rcheck --seconds 86400 -j DROP

iptables -A FORWARD -m recent --name portscan --rcheck --seconds 86400 -j DROP

# Remove attacking IP after 24 hours

iptables -A INPUT -m recent --name portscan --remove

iptables -A FORWARD -m recent --name portscan --remove

# These rules add scanners to the portscan list, and log the attempt.

iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"

iptables -A INPUT -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP

iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j LOG --log-prefix "portscan:"

iptables -A FORWARD -p tcp -m tcp --dport 139 -m recent --name portscan --set -j DROP

# Allow the following ports through from outside

iptables -A INPUT -p tcp -m tcp --dport 25 -j ACCEPT

iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT

iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT

iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

# Allow ping means ICMP port is open (If you do not want ping replace ACCEPT with REJECT)

iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# Lastly reject All INPUT traffic

iptables -A INPUT -j REJECT

################# Below are for OUTPUT iptables rules #############################################

## Allow loopback OUTPUT

iptables -A OUTPUT -o lo -j ACCEPT

iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow the following ports through from outside

# SMTP = 25

# DNS =53

# HTTP = 80

# HTTPS = 443

# SSH = 22

### You can also add or remove port no. as per your requirement

iptables -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT

iptables -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT

iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

iptables -A OUTPUT -p tcp -m tcp --dport 443 -j ACCEPT

iptables -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT

# Allow pings

iptables -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT

# Lastly Reject all Output traffic

iptables -A OUTPUT -j REJECT

## Reject Forwarding? traffic

iptables -A FORWARD -j REJECT

把腳本放入如下目錄

/etc/rc.d/init.d/iptablescript.sh

3. 執(zhí)行如下命令,將該腳本標記為可執(zhí)行文件(添加可執(zhí)行的權(quán)限)

chmod +x /etc/rc.d/init.d/iptablescript.sh

4. 執(zhí)行如下命令將/etc/rc.d/rc.local文標記為可執(zhí)行文件

在centos7中,/etc/rc.d/rc.local文件的權(quán)限被降低了,開機的時候執(zhí)行在自己的腳本是不能起動一些服務的,執(zhí)行下面的命令可以文件標記為可執(zhí)行的文件

chmod +x /etc/rc.d/rc.local

5. 打開/etc/rc.d/rc.local文件,在最后面添加如下腳本

/etc/rc.d/init.d/iptablescript.sh

設置完成后呻顽,執(zhí)行如下命令,重啟實例驗證配置丹墨。

systemctl reboot

iptables常用命令

# 啟動iptables? ? ? ? ? ? systemctl start iptables

# 查看iptables狀態(tài)? ? ? ? systemctl status iptables

# 停止iptables? ? ? ? ? ? ? systemctl stop iptables

# 重啟iptables? ? ? ? ? ? ? systemctl restart iptables

# 重載iptables? ? ? ? ? ? ? ? ? systemctl reload iptables


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末廊遍,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子贩挣,更是在濱河造成了極大的恐慌喉前,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,386評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件王财,死亡現(xiàn)場離奇詭異卵迂,居然都是意外死亡,警方通過查閱死者的電腦和手機绒净,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評論 3 394
  • 文/潘曉璐 我一進店門见咒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人挂疆,你說我怎么就攤上這事改览∠卖幔” “怎么了?”我有些...
    開封第一講書人閱讀 164,704評論 0 353
  • 文/不壞的土叔 我叫張陵宝当,是天一觀的道長视事。 經(jīng)常有香客問我,道長庆揩,這世上最難降的妖魔是什么俐东? 我笑而不...
    開封第一講書人閱讀 58,702評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮订晌,結(jié)果婚禮上虏辫,老公的妹妹穿的比我還像新娘。我一直安慰自己腾仅,他們只是感情好乒裆,可當我...
    茶點故事閱讀 67,716評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著推励,像睡著了一般鹤耍。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上验辞,一...
    開封第一講書人閱讀 51,573評論 1 305
  • 那天稿黄,我揣著相機與錄音,去河邊找鬼跌造。 笑死杆怕,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的壳贪。 我是一名探鬼主播陵珍,決...
    沈念sama閱讀 40,314評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼违施!你這毒婦竟也來了互纯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,230評論 0 276
  • 序言:老撾萬榮一對情侶失蹤磕蒲,失蹤者是張志新(化名)和其女友劉穎留潦,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辣往,經(jīng)...
    沈念sama閱讀 45,680評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡兔院,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,873評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了站削。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坊萝。...
    茶點故事閱讀 39,991評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出屹堰,到底是詐尸還是另有隱情肛冶,我是刑警寧澤街氢,帶...
    沈念sama閱讀 35,706評論 5 346
  • 正文 年R本政府宣布扯键,位于F島的核電站,受9級特大地震影響珊肃,放射性物質(zhì)發(fā)生泄漏荣刑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,329評論 3 330
  • 文/蒙蒙 一伦乔、第九天 我趴在偏房一處隱蔽的房頂上張望厉亏。 院中可真熱鬧,春花似錦烈和、人聲如沸爱只。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,910評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽恬试。三九已至,卻和暖如春疯暑,著一層夾襖步出監(jiān)牢的瞬間训柴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,038評論 1 270
  • 我被黑心中介騙來泰國打工妇拯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留幻馁,地道東北人。 一個月前我還...
    沈念sama閱讀 48,158評論 3 370
  • 正文 我出身青樓越锈,卻偏偏與公主長得像仗嗦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子甘凭,可洞房花燭夜當晚...
    茶點故事閱讀 44,941評論 2 355