1. 程式人生 > >Linux下iptables 禁止埠和開放埠

Linux下iptables 禁止埠和開放埠

iptables 禁止埠和開放埠(轉載)

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 一下域名也是不通
[[email protected] ~pingwww.google.com
ping: unknown hostwww.google.com

這裡的原因就是 iptables 限制了53埠。

有些伺服器,特別是Web伺服器減慢,DNS其實也有關係的,無法傳送包到DNS伺服器導致的。

下面演示下如何使用 iptables 來設定DNS 53這個埠,如果你不知道 域名服務埠號,你

可以用命令 : grep domain /etc/services

[[email protected] ~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 檢視設定情況,確定沒有問題就可以測試了

[[email protected] ~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 了。

[[email protected] ~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 看看吧

[[email protected] ~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 埠,現在新增對應的規則。

[[email protected] rootiptables -A INPUT -p tcp --dport 21 -j ACCEPT
[[email protected] rootiptables -A INPUT -p tcp --dport 20 -j ACCEPT
[[email protected] rootiptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[[email protected] rootiptables -A OUTPUT -p tcp --sport 20 -j ACCEPT

好,這樣就新增完了,用瀏覽器訪問一下ftp,出現超時。

所以剛才說 ftp 是比較特殊的埠,它還有一些埠是 資料傳輸埠,
例如目錄列表, 上傳 ,下載 檔案都要用到這些埠。

而這些埠是 任意 埠。。。 這個 任意 真的比較特殊。

如果不指定什麼一個埠範圍, iptables 很難對任意埠開放的,
如果iptables允許任意埠訪問, 那和不設定防火牆沒什麼區別,所以不現實的。

那麼的解決辦法就是 指定這個資料傳輸埠的一個範圍。

下面修改一下ftp配置檔案。

這裡使用vsftpd來修改演示,其他ftp不知道哪裡修改,大家可以找找資料。

[[email protected] rootvi /etc/vsftpd.conf

在配置檔案的最下面 加入

pasv_min_port=30001
pasv_max_port=31000

然後儲存退出。

這兩句話的意思告訴vsftpd, 要傳輸資料的埠範圍就在30001到31000 這個範圍內傳送。

這樣們使用 iptables 就好辦多了,們就開啟 30001到31000 這些埠。

[[email protected] rootiptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[[email protected] rootiptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT

[[email protected] rootservice iptables save

最後進行儲存, 然後再用瀏覽器範圍下 ftp。可以正常訪問

用個賬號登陸上去,也沒有問題,上傳一些檔案上去看看。

看到了吧,上傳和下載都正常。。 再檢視下 iptables 的設定

[[email protected] 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 等等要求更加高的驗證