日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iptables禁止端口和开放端口

發布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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端口, 我現在看看能不能解析域名。

host?www.google.com??? 輸入這個命令后,一直等待,說明DNS不通

出現下面提示 :
;; connection timed out; no servers could be reached

ping 一下域名也是不通
[root@localhost ~ping?www.google.com
ping: unknown host?www.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 ~host?www.google.com
www.google.com?is an alias for?www.l.google.com.
www.l.google.com?is 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.com?canonical 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 等等要求更加高的驗證



?

iptables?防火墻?只允許某IP訪問某端口、訪問特定網站 ? 需要開80端口,指定IP和局域網

?

下面三行的意思:

先關閉所有的80端口

開啟ip段192.168.1.0/24端的80口

開啟ip段211.123.16.123/24端ip段的80口

# iptables -I INPUT -p tcp --dport 80 -j DROP?
# iptables -I INPUT -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT
# iptables -I INPUT -s 211.123.16.123/24 -p tcp --dport 80 -j ACCEPT

以上是臨時設置。

1.先備份iptables

# cp /etc/sysconfig/iptables /var/tmp

2.然后保存iptables

# service iptables save

3.重啟防火墻

#service iptables restart

以下是端口,先全部封再開某些的IP

iptables -I INPUT -p tcp --dport 9889 -j DROP?
iptables -I INPUT -s 192.168.1.0/24?-p tcp --dport 9889 -j ACCEPT

如果用了NAT轉發記得配合以下才能生效

iptables -I FORWARD -p tcp --dport 80 -j DROP?
iptables -I FORWARD -s 192.168.1.0/24 -p tcp --dport 80 -j ACCEPT

?

?

常用的IPTABLES規則如下:

只能收發郵件,別的都關閉
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -j DROP
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p udp --dport 53 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 25 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0F:EA:25:51:37 -p tcp --dport 110 -j ACCEPT


IPSEC NAT 策略
iptables -I PFWanPriv -d 192.168.100.2 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 80 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:80

iptables -t nat -A PREROUTING -p tcp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

iptables -t nat -A PREROUTING -p udp --dport 1723 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:1723

iptables -t nat -A PREROUTING -p udp --dport 500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:500

iptables -t nat -A PREROUTING -p udp --dport 4500 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.2:4500


FTP服務器的NAT

iptables -I PFWanPriv -p tcp --dport 21 -d 192.168.100.200 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 21 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.200:21


只允許訪問指定網址
iptables -A Filter -p udp --dport 53 -j ACCEPT
iptables -A Filter -p tcp --dport 53 -j ACCEPT
iptables -A Filter -d www.3322.org -j ACCEPT
iptables -A Filter -d img.cn99.com -j ACCEPT
iptables -A Filter -j DROP


開放一個IP的一些端口,其它都封閉
iptables -A Filter -p tcp --dport 80 -s 192.168.100.200 -d www.pconline.com.cn -j ACCEPT
iptables -A Filter -p tcp --dport 25 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 109 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 110 -s 192.168.100.200 -j ACCEPT
iptables -A Filter -p tcp --dport 53 -j ACCEPT
iptables -A Filter -p udp --dport 53 -j ACCEPT
iptables -A Filter -j DROP


多個端口
iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT


連續端口
iptables -A Filter -p tcp -m multiport --source-port 22,53,80,110 -s 192.168.20.3 -j REJECT iptables -A Filter -p tcp --source-port 2:80 -s 192.168.20.3 -j REJECT


指定時間上網
iptables -A Filter -s 10.10.10.253 -m time --timestart 6:00 --timestop 11:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
iptables -A Filter -m time --timestart 12:00 --timestop 13:00 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
iptables -A Filter -m time --timestart 17:30 --timestop 8:30 --days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

禁止多個端口服務
iptables -A Filter -m multiport -p tcp --dport 21,23,80 -j ACCEPT


將WAN 口NAT到PC
iptables -t nat -A PREROUTING -i $INTERNET_IF -d $INTERNET_ADDR -j DNAT --to-destination 192.168.0.1


將WAN口8000端口NAT到192。168。100。200的80端口
iptables -t nat -A PREROUTING -p tcp --dport 8000 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.200:80


MAIL服務器要轉的端口
iptables -t nat -A PREROUTING -p tcp --dport 110 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.200:110
iptables -t nat -A PREROUTING -p tcp --dport 25 -d $INTERNET_ADDR -j DNAT --to-destination 192.168.100.200:25


只允許PING 202。96。134。133,別的服務都禁止
iptables -A Filter -p icmp -s 192.168.100.200 -d 202.96.134.133 -j ACCEPT
iptables -A Filter -j DROP

禁用BT配置
iptables –A Filter –p tcp –dport 6000:20000 –j DROP

禁用QQ防火墻配置
iptables -A Filter -p udp --dport ! 53 -j DROP
iptables -A Filter -d 218.17.209.0/24 -j DROP
iptables -A Filter -d 218.18.95.0/24 -j DROP
iptables -A Filter -d 219.133.40.177 -j DROP

基于MAC,只能收發郵件,其它都拒絕
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -j DROP
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 25 -j ACCEPT
iptables -I Filter -m mac --mac-source 00:0A:EB:97:79:A1 -p tcp --dport 110 -j ACCEPT

禁用MSN配置
iptables -A Filter -p udp --dport 9 -j DROP
iptables -A Filter -p tcp --dport 1863 -j DROP
iptables -A Filter -p tcp --dport 80 -d 207.68.178.238 -j DROP
iptables -A Filter -p tcp --dport 80 -d 207.46.110.0/24 -j DROP

只允許PING 202。96。134。133 其它公網IP都不許PING
iptables -A Filter -p icmp -s 192.168.100.200 -d 202.96.134.133 -j ACCEPT
iptables -A Filter -p icmp -j DROP

禁止某個MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:20:18:8F:72:F8 -j DROP

禁止某個IP地址的PING:
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

禁止某個IP地址服務:
iptables –A Filter -p tcp -s 192.168.0.1 --dport 80 -j DROP
iptables –A Filter -p udp -s 192.168.0.1 --dport 53 -j DROP

只允許某些服務,其他都拒絕(2條規則)
iptables -A Filter -p tcp -s 192.168.0.1 --dport 1000 -j ACCEPT
iptables -A Filter -j DROP

禁止某個IP地址的某個端口服務
iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j ACCEPT
iptables -A Filter -p tcp -s 10.10.10.253 --dport 80 -j DROP

禁止某個MAC地址的某個端口服務

iptables -I Filter -p tcp -m mac --mac-source 00:20:18:8F:72:F8 --dport 80 -j DROP

禁止某個MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:11:22:33:44:55 -j DROP

禁止某個IP地址的PING:
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP

l

come from:http://blog.csdn.net/cssmhyl/article/details/7966789

轉載于:https://www.cnblogs.com/seasonzone/p/5067229.html

總結

以上是生活随笔為你收集整理的iptables禁止端口和开放端口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。