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

一、關閉全部的 INPUT FORWARD OUTPUT 只對某些端口開放。
下面是命令實現:windows

iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

再用命令瀏覽器

 iptables -L -n

查看 是否設置好, 好看到所有 DROP 了bash

這樣的設置好了,咱們只是臨時的, 重啓服務器仍是會恢復原來沒有設置的狀態
還要使用 service iptables save 進行保存服務器

service iptables save

看到信息 firewall rules 防火牆的規則 其實就是保存在 /etc/sysconfig/iptablesdom

能夠打開文件查看 vi /etc/sysconfig/iptablestcp

 

二、下面我只打開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測試一下是否能夠連接上去。
能夠連接上去了,說明沒有問題。google

最後別忘記了保存 對防火牆的設置
經過命令:service iptables save 進行保存spa

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 不接收

三、禁止某個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 是能夠正常訪問的


四、如何刪除規則
首先咱們要知道 這條規則的編號,每條規則都有一個編號

經過 iptables -L -n --line-number 能夠顯示規則和相對應的編號

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 查看一下 已經被清除了。


五、過濾無效的數據包
假設有人進入了服務器,或者有病毒木馬程序,它能夠經過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 文件中。


六、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這個端口的訪問。

七、iptables對ftp的設置
如今我開始對ftp端口的設置,按照咱們之前的視頻,添加須要開放的端口
ftp鏈接端口有2個 21 和 20 端口,我如今添加對應的規則。

[root@localhost root]#iptables -A INPUT -p tcp --dport 21 -j ACCEPT
[root@localhost root]#iptables -A INPUT -p tcp --dport 20 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 21 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 20 -j ACCEPT

 

好,這樣就添加完了,咱們用瀏覽器訪問一下ftp,出現超時。
因此我剛纔說 ftp 是比較特殊的端口,它還有一些端口是 數據傳輸端口,
例如目錄列表, 上傳 ,下載 文件都要用到這些端口。
而這些端口是 任意 端口。。。 這個 任意 真的比較特殊。
若是不指定什麼一個端口範圍, iptables 很難對任意端口開放的,
若是iptables容許任意端口訪問, 那和不設置防火牆沒什麼區別,因此不現實的。
那麼咱們的解決辦法就是 指定這個數據傳輸端口的一個範圍。
下面咱們修改一下ftp配置文件。
我這裏使用vsftpd來修改演示,其餘ftp我不知道哪裏修改,你們能夠找找資料。

[root@localhost root]#vi /etc/vsftpd.conf

 

在配置文件的最下面 加入

pasv_min_port=30001
pasv_max_port=31000

 

而後保存退出。

這兩句話的意思告訴vsftpd, 要傳輸數據的端口範圍就在30001到31000 這個範圍內傳送。

這樣咱們使用 iptables 就好辦多了,咱們就打開 30001到31000 這些端口。

[root@localhost root]#iptables -A INPUT -p tcp --dport 30001:31000 -j ACCEPT
[root@localhost root]#iptables -A OUTPUT -p tcp --sport 30001:31000 -j ACCEPT
[root@localhost root]#service iptables save

最後進行保存, 而後咱們再用瀏覽器範圍下 ftp。能夠正常訪問

用個帳號登錄上去,也沒有問題,上傳一些文件上去看看。

上傳和下載都正常。 再查看下 iptables 的設置

[root@localhost root]#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: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 等等要求更加高的驗證

相關文章
相關標籤/搜索