iptables 禁止端口和開放端口
1.首先介紹一下指令和相關配置文件
- 啟動指令:service iptables start
- 重啟指令:service iptables restart
- 關閉指令:service iptables stop
- 然后是相關配置:/etc/sysconfig/iptables
- 如何操作該配置呢?
- vim /etc/sysconfig/iptables
- 然后進去修改即可,修改完了怎么辦?這里很多人會想到/etc/rc.d/init.d/iptables save指令,但是一旦你這么干了你剛才的修改內容就白做了。。。
- 具體方法是:
- 只修改/etc/sysconfig/iptables 使其生效的辦法是修改好后先service iptables restart,然后才調用/etc/rc.d/init.d/iptables save,
- 因為/etc/rc.d/init.d/iptables save會在iptables服務啟動時重新加載,要是在重啟之前直接先調用了/etc/rc.d/init.d/iptables save那么你
- 的/etc/sysconfig/iptables 配置就回滾到上次啟動服務的配置了,這點必須注意!!!
2.下面介紹一些指令用法(主要還是man iptables看下相關資料才行)
- -A:指定鏈名
- -p:指定協議類型
- -d:指定目標地址
- --dport:指定目標端口(destination port 目的端口)
- --sport:指定源端口(source port 源端口)
- -j:指定動作類型
3.如果我不像修改文件直接打命令可以嗎,當然沒問題,步驟如下:
- 例如我給SSH加放行的語句:
- 添加input記錄: iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- 添加output記錄: iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
- 最后注意需要再執行一下 /etc/init.d/iptables save,這樣這兩條語句就保存到剛才那個/etc/sysconfig/iptables 文件中了。
4.接下來說明一下步驟,如果機器不在我身邊,我只能SSH進去做iptables規則,那么我必須注意每一步,千萬別搞錯了,否則就SSH鏈接不上都有可能!
- 首先要做的是給咱的SSH進行ACCEPT配置,以免直接無法連接的情況發生:
- 1.如果SSH端口是22(這里不建議用默認端口最好改掉SSH端口)
- iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
- 注意要/etc/rc.d/init.d/iptables save,以下每一步都最好執行一遍此語句,以下不再累述。
- 2.vim /etc/sysconfig/iptables確定是否已經加入配置,可以的話執行service iptables restart重啟后生效
- 3.下面是很危險的操作,如果你第一步沒做就會直接可能導致你連不上SSH,此步驟前切記執行第一步!!!
- iptables -P INPUT DROP
- iptables -P OUTPUT DROP
- iptables -P FORWARD DROP
- 這個步驟是把所有不符合自己配置的規則ACCEPT的連接全部DROP掉,執行完以后如果咱SSH還沒掉,那么謝天謝地,安全了,重啟下iptables后繼續下面的配置!
- 4.下面咱就不細說了,具體就是看自己服務器要開放哪些端口或者是要訪問哪些端口來做具體的配置,下面是我自己的機器的配置:
- /etc/sysconfig/iptables文件配置如下:
- # Generated by iptables-save v1.4.7 on Fri Mar 2 19:59:43 2012
- *filter
- :INPUT DROP [0:0]
- :FORWARD DROP [0:0]
- :OUTPUT DROP [8:496]
- -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
- #ping使用的端口
- -A INPUT -p icmp -j ACCEPT
- -A INPUT -i lo -j ACCEPT
- -A INPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT
- -A INPUT -s 192.168.2.200/32 -d 192.168.2.200/32 -j ACCEPT
- #允許服務器自己的SSH(對外部請求來說服務器是目標所以使用--dport)
- -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
- #80端口不用說了吧,服務器網站訪問端口
- -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
- -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT
- -A INPUT -p tcp -m tcp --dport 11211 -j ACCEPT
- -A INPUT -p tcp -m tcp --dport 11212 -j ACCEPT
- -A FORWARD -j REJECT --reject-with icmp-host-prohibited
- #53端口是DNS相關,TCP和UDP都要配置
- -A INPUT -p tcp -m tcp --dport 53 -j ACCEPT
- -A INPUT -p udp -m udp --dport 53 -j ACCEPT
- #ping使用的端口
- -A OUTPUT -p icmp -j ACCEPT
- -A OUTPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT
- -A OUTPUT -s 192.168.2.200/32 -d 192.168.2.200/32 -j ACCEPT
- #允許服務器SSH到其他機器(使用外部端口就使用--dport)
- -A OUTPUT -p tcp -m tcp --dport 22 -j ACCEPT
- #允許服務器自己的SSH(自已為源輸出就使用--sport)
- -A OUTPUT -p tcp -m tcp --sport 22 -j ACCEPT
- #訪問外部網站80端口(使用外部端口就使用--dport)
- -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
- #如果服務器需要訪問外部網站,那么OUTPUT也需要配置53端口(使用外部端口就使用--dport)
- -A OUTPUT -p tcp -m tcp --dport 53 -j ACCEPT
- -A OUTPUT -p udp -m udp --dport 53 -j ACCEPT
- #如果有訪問外部郵箱,那么打開郵箱相關端口(使用外部端口就使用--dport)
- -A OUTPUT -p tcp -m tcp --dport 465 -j ACCEPT
- -A OUTPUT -p tcp -m tcp --dport 25 -j ACCEPT
- -A OUTPUT -p tcp -m tcp --dport 110 -j ACCEPT
- #服務器網站訪問端口(自已為源輸出就使用--sport)
- -A OUTPUT -p tcp -m tcp --sport 80 -j ACCEPT
- -A OUTPUT -p tcp -m tcp --sport 3306 -j ACCEPT
- -A OUTPUT -p tcp -m tcp --sport 11211 -j ACCEPT
- -A OUTPUT -p tcp -m tcp --sport 11212 -j ACCEPT
- COMMIT
- # Completed on Fri Mar 2 19:59:43 2012
5.可能有時候需要刪除規則,最簡單就是修改一下/etc/sysconfig/iptables然后service iptables restart,最后/etc/rc.d/init.d/iptables save即可。
當然也可以使用指令完成:
- 在網上找了一下,刪除規則的方法:
- 語法是: iptables -D chain rulenum [options]
- 其中: chain 是鏈的意思,就是INPUT FORWARD 之類的
- rulenum 是規則的編號。從1 開始。可以使用 --line-numbers 列出規則的編號
- 所以,例如上面要刪除一個INPUT鏈的規則的話可以這樣:iptables -D INPUT 3
- 意思是刪除第3條規則。
- 還有第二種方法。第二種辦法是 -A 命令的映射,不過用-D替換-A。當你的鏈中規則很復雜,而你不想計算它們的編號的時候這就十分有用了。也就是說,你如何用iptables -A.... 語句定義了一個規則,則刪除此規則時就用 -D 來代替- A 其余的都不變即可。
- ======================
- 說一下上面的 --line-numbers 選項,如下面的命令:
- iptables -L INPUT --line-numbers 列出INPUT 鏈所有的規則
- num target prot opt source destination
- 1 REJECT tcp -- anywhere anywhere tcp dpt:microsoft-ds reject-with icmp-port-unreachable
- 2 REJECT tcp -- anywhere anywhere tcp dpt:135 reject-with icmp-port-unreachable
- 3 REJECT tcp -- anywhere anywhere tcp dpt:netbios-ssn reject-with icmp-port-unreachable
- ...
- ...
- 刪除指定行規則:
- [root@localhost rc.d]# iptables -D INPUT 4
6.最后補充一下,如果想針對某IP進行單獨開放端口可以如下配置:
- 如果我需要對內網某機器單獨開放mysql端口,應該如下配置:
- iptables -A INPUT -s 192.168.2.6 -p tcp -m tcp --dport 3306 -j ACCEPT
- iptables -A OUTPUT -s 192.168.2.6 -p tcp -m tcp --sport 3306 -j ACCEPT
7.徹底禁止某IP訪問:
- #屏蔽單個IP的命令是
- iptables -I INPUT -s 123.45.6.7 -j DROP
- #封整個段即從123.0.0.1到123.255.255.254的命令
- iptables -I INPUT -s 123.0.0.0/8 -j DROP
- #封IP段即從123.45.0.1到123.45.255.254的命令
- iptables -I INPUT -s 124.45.0.0/16 -j DROP
- #封IP段即從123.45.6.1到123.45.6.254的命令是
- iptables -I INPUT -s 123.45.6.0/24 -j DROP
- 指令I是insert指令 但是該指令會insert在正確位置並不像A指令看你自己的排序位置,因此用屏蔽因為必須在一開始就要加載屏蔽IP,所以必須使用I命令加載,然后注意執行/etc/rc.d/init.d/iptables save進行保存后重啟服務即可
1、關閉所有的 INPUT FORWARD OUTPUT 只對某些端口開放。
下面是命令實現:
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
再用命令 iptables -L -n 查看 是否設置好, 好看到全部 DROP 了
這樣的設置好了,我們只是臨時的, 重啟服務器還是會恢復原來沒有設置的狀態
還要使用 service iptables save 進行保存
看到信息 firewall rules 防火牆的規則 其實就是保存在 /etc/sysconfig/iptables
可以打開文件查看 vi /etc/sysconfig/iptables
2、
下面我只打開22端口,看我是如何操作的,就是下面2個語句
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
再查看下 iptables -L -n 是否添加上去, 看到添加了
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:22
現在Linux服務器只打開了22端口,用putty.exe測試一下是否可以鏈接上去。
可以鏈接上去了,說明沒有問題。
最后別忘記了保存 對防火牆的設置
通過命令:service iptables save 進行保存
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
針對這2條命令進行一些講解吧
-A 參數就看成是添加一條 INPUT 的規則
-p 指定是什么協議 我們常用的tcp 協議,當然也有udp 例如53端口的DNS
到時我們要配置DNS用到53端口 大家就會發現使用udp協議的
而 --dport 就是目標端口 當數據從外部進入服務器為目標端口
反之 數據從服務器出去 則為數據源端口 使用 --sport
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
3、禁止某個IP訪問
1台Linux服務器,2台windows xp 操作系統進行訪問
Linux服務器ip 192.168.1.99
xp1 ip: 192.168.1.2
xp2 ip: 192.168.1.8
下面看看我2台xp 都可以訪問的
192.168.1.2 這是 xp1 可以訪問的,
192.168.1.8 xp2 也是可以正常訪問的。
那么現在我要禁止 192.168.1.2 xp1 訪問, xp2 正常訪問,
下面看看演示
通過命令 iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
這里意思就是 -A 就是添加新的規則, 怎樣的規則呢? 由於我們訪問網站使用tcp的,
我們就用 -p tcp , 如果是 udp 就寫udp,這里就用tcp了, -s就是 來源的意思,
ip來源於 192.168.1.2 ,-j 怎么做 我們拒絕它 這里應該是 DROP
好,看看效果。好添加成功。下面進行驗證 一下是否生效
一直出現等待狀態 最后 該頁無法顯示 ,這是 192.168.1.2 xp1 的訪問被拒絕了。
再看看另外一台 xp 是否可以訪問, 是可以正常訪問的 192.168.1.8 是可以正常訪問的
4、如何刪除規則
首先我們要知道 這條規則的編號,每條規則都有一個編號
通過 iptables -L -n --line-number 可以顯示規則和相對應的編號
num target prot opt source destination
1 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
2 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
3 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
多了 num 這一列, 這樣我們就可以 看到剛才的規則對應的是 編號2
那么我們就可以進行刪除了
iptables -D INPUT 2
刪除INPUT鏈編號為2的規則。
再 iptables -L -n 查看一下 已經被清除了。
5、過濾無效的數據包
假設有人進入了服務器,或者有病毒木馬程序,它可以通過22,80端口像服務器外傳送數據。
它的這種方式就和我們正常訪問22,80端口區別。它發向外發的數據不是我們通過訪問網頁請求
而回應的數據包。
下面我們要禁止這些沒有通過請求回應的數據包,統統把它們堵住掉。
iptables 提供了一個參數 是檢查狀態的,下面我們來配置下 22 和 80 端口,防止無效的數據包。
iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
可以看到和我們以前使用的:
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
多了一個狀態判斷。
同樣80端口也一樣, 現在刪掉原來的2條規則,
iptables -L -n --line-number 這個是查看規則而且帶上編號。我們看到編號就可以
刪除對應的規則了。
iptables -D OUTPUT 1 這里的1表示第一條規則。
當你刪除了前面的規則, 編號也會隨之改變。看到了吧。
好,我們刪除了前面2個規則,22端口還可以正常使用,說明沒問題了
下面進行保存,別忘記了,不然的話重啟就會還原到原來的樣子。
service iptables save 進行保存。
Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
其實就是把剛才設置的規則寫入到 /etc/sysconfig/iptables 文件中。
6、DNS端口53設置
下面我們來看看如何設置iptables來打開DNS端口,DNS端口對應的是53
大家看到我現在的情況了吧,只開放22和80端口, 我現在看看能不能解析域名。
hostwww.google.com 輸入這個命令后,一直等待,說明DNS不通
出現下面提示 :
;; connection timed out; no servers could be reached
ping 一下域名也是不通
[root@localhost ~pingwww.google.com
ping: unknown hostwww.google.com
我這里的原因就是 iptables 限制了53端口。
有些服務器,特別是Web服務器減慢,DNS其實也有關系的,無法發送包到DNS服務器導致的。
下面演示下如何使用 iptables 來設置DNS 53這個端口,如果你不知道 域名服務端口號,你
可以用命令 : grep domain /etc/services
[root@localhost ~grep domain /etc/services
domain 53/tcp # name-domain server
domain 53/udp
domaintime 9909/tcp # domaintime
domaintime 9909/udp # domaintime
看到了吧, 我們一般使用 udp 協議。
好了, 開始設置。。。
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
這是我們 ping 一個域名,數據就是從本機出去,所以我們先設置 OUTPUT,
我們按照ping這個流程來設置。
然后 DNS 服務器收到我們發出去的包,就回應一個回來
iptables -A INPUT -p udp --sport 53 -j ACCEPT
同時還要設置
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
好了, 下面開始測試下, 可以用 iptables -L -n 查看設置情況,確定沒有問題就可以測試了
[root@localhost ~iptables -L -n
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:22 state ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:80 state ESTABLISHED
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:53
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp spt:53
可以測試一下 是否 DNS 可以通過iptables 了。
[root@localhost ~hostwww.google.com
www.google.comis an alias forwww.l.google.com.
www.l.google.comis an alias for www-china.l.google.com.
www-china.l.google.com has address 64.233.189.104
www-china.l.google.com has address 64.233.189.147
www-china.l.google.com has address 64.233.189.99
正常可以解析 google 域名。
ping 方面可能還要設置些東西。
用 nslookup 看看吧
[root@localhost ~nslookup
>www.google.com
Server: 192.168.1.1
Address: 192.168.1.1#53
Non-authoritative answer:
www.google.comcanonical name =www.l.google.com.
www.l.google.com canonical name = www-china.l.google.com.
Name: www-china.l.google.com
Address: 64.233.189.147
Name: www-china.l.google.com
Address: 64.233.189.99
Name: www-china.l.google.com
Address: 64.233.189.104
說明本機DNS正常, iptables 允許53這個端口的訪問。
7、iptables對ftp的設置
現在我開始對ftp端口的設置,按照我們以前的視頻,添加需要開放的端口
ftp連接端口有2個 21 和 20 端口,我現在添加對應的規則。
[root@localhost rootiptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@localhost rootiptables -A INPUT -p tcp --dport 20 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 20 -j ACCEPT
好,這樣就添加完了,我們用瀏覽器訪問一下ftp,出現超時。
所以我剛才說 ftp 是比較特殊的端口,它還有一些端口是 數據傳輸端口,
例如目錄列表, 上傳 ,下載 文件都要用到這些端口。
而這些端口是 任意 端口。。。 這個 任意 真的比較特殊。
如果不指定什么一個端口范圍, iptables 很難對任意端口開放的,
如果iptables允許任意端口訪問, 那和不設置防火牆沒什么區別,所以不現實的。
那么我們的解決辦法就是 指定這個數據傳輸端口的一個范圍。
下面我們修改一下ftp配置文件。
我這里使用vsftpd來修改演示,其他ftp我不知道哪里修改,大家可以找找資料。
[root@localhost rootvi /etc/vsftpd.conf
在配置文件的最下面 加入
pasv_min_port=30001
pasv_max_port=31000
然后保存退出。
這兩句話的意思告訴vsftpd, 要傳輸數據的端口范圍就在30001到31000 這個范圍內傳送。
這樣我們使用 iptables 就好辦多了,我們就打開 30001到31000 這些端口。
[root@localhost rootiptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[root@localhost rootiptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT
[root@localhost rootservice iptables save
最后進行保存, 然后我們再用瀏覽器范圍下 ftp。可以正常訪問
用個賬號登陸上去,也沒有問題,上傳一些文件上去看看。
看到了吧,上傳和下載都正常。。 再查看下 iptables 的設置
[root@localhost rootiptables -L -n
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:20
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:30001:31000
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy DROP)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:21
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spt:20
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp spts:30001:31000
這是我為了演示ftp特殊端口做的簡單規則,大家可以添加一些對數據包的驗證
例如 -m state --state ESTABLISHED,RELATED 等等要求更加高的驗證
轉自:http://hi.baidu.com/beijiqieys/item/5ee9bbcf94f0a712b67a24cf