歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux基礎知識 >> Linux下iptables 禁止端口和開放端口

Linux下iptables 禁止端口和開放端口

日期:2017/3/2 17:15:32   编辑:Linux基礎知識

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 一下域名也是不通
[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 等等要求更加高的驗證


Copyright © Linux教程網 All Rights Reserved