iptables禁止端口和开放端口
1、關(guān)閉所有的 INPUT FORWARD OUTPUT 只對(duì)某些端口開放。
下面是命令實(shí)現(xiàn):
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
再用命令 iptables -L -n 查看 是否設(shè)置好, 好看到全部 DROP 了
這樣的設(shè)置好了,我們只是臨時(shí)的, 重啟服務(wù)器還是會(huì)恢復(fù)原來沒有設(shè)置的狀態(tài)
還要使用 service iptables save 進(jìn)行保存
看到信息 firewall rules 防火墻的規(guī)則 其實(shí)就是保存在 /etc/sysconfig/iptables
可以打開文件查看 vi /etc/sysconfig/iptables
2、
下面我只打開22端口,看我是如何操作的,就是下面2個(gè)語(yǔ)句
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
現(xiàn)在Linux服務(wù)器只打開了22端口,用putty.exe測(cè)試一下是否可以鏈接上去。
可以鏈接上去了,說明沒有問題。
最后別忘記了保存 對(duì)防火墻的設(shè)置
通過命令:service iptables save 進(jìn)行保存
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
針對(duì)這2條命令進(jìn)行一些講解吧
-A 參數(shù)就看成是添加一條 INPUT 的規(guī)則
-p 指定是什么協(xié)議 我們常用的tcp 協(xié)議,當(dāng)然也有udp 例如53端口的DNS
到時(shí)我們要配置DNS用到53端口 大家就會(huì)發(fā)現(xiàn)使用udp協(xié)議的
而 --dport 就是目標(biāo)端口 當(dāng)數(shù)據(jù)從外部進(jìn)入服務(wù)器為目標(biāo)端口
反之 數(shù)據(jù)從服務(wù)器出去 則為數(shù)據(jù)源端口 使用 --sport
-j 就是指定是 ACCEPT 接收 或者 DROP 不接收
3、禁止某個(gè)IP訪問
1臺(tái)Linux服務(wù)器,2臺(tái)windows xp 操作系統(tǒng)進(jìn)行訪問
Linux服務(wù)器ip 192.168.1.99
xp1 ip: 192.168.1.2
xp2 ip: 192.168.1.8
下面看看我2臺(tái)xp 都可以訪問的
192.168.1.2 這是 xp1 可以訪問的,
192.168.1.8 xp2 也是可以正常訪問的。
那么現(xiàn)在我要禁止 192.168.1.2 xp1 訪問, xp2 正常訪問,?
下面看看演示
通過命令 iptables -A INPUT -p tcp -s 192.168.1.2 -j DROP
這里意思就是 -A 就是添加新的規(guī)則, 怎樣的規(guī)則呢? 由于我們?cè)L問網(wǎng)站使用tcp的,
我們就用 -p tcp , 如果是 udp 就寫udp,這里就用tcp了, -s就是 來源的意思,
ip來源于 192.168.1.2 ,-j 怎么做 我們拒絕它 這里應(yīng)該是 DROP
好,看看效果。好添加成功。下面進(jìn)行驗(yàn)證 一下是否生效
一直出現(xiàn)等待狀態(tài) 最后 該頁(yè)無(wú)法顯示 ,這是 192.168.1.2 xp1 的訪問被拒絕了。
再看看另外一臺(tái) xp 是否可以訪問, 是可以正常訪問的 192.168.1.8 是可以正常訪問的
4、如何刪除規(guī)則
首先我們要知道 這條規(guī)則的編號(hào),每條規(guī)則都有一個(gè)編號(hào)
通過 iptables -L -n --line-number 可以顯示規(guī)則和相對(duì)應(yīng)的編號(hào)
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 這一列, 這樣我們就可以 看到剛才的規(guī)則對(duì)應(yīng)的是 編號(hào)2
那么我們就可以進(jìn)行刪除了
iptables -D INPUT 2
刪除INPUT鏈編號(hào)為2的規(guī)則。
再 iptables -L -n 查看一下 已經(jīng)被清除了。
5、過濾無(wú)效的數(shù)據(jù)包
假設(shè)有人進(jìn)入了服務(wù)器,或者有病毒木馬程序,它可以通過22,80端口像服務(wù)器外傳送數(shù)據(jù)。
它的這種方式就和我們正常訪問22,80端口區(qū)別。它發(fā)向外發(fā)的數(shù)據(jù)不是我們通過訪問網(wǎng)頁(yè)請(qǐng)求
而回應(yīng)的數(shù)據(jù)包。
下面我們要禁止這些沒有通過請(qǐng)求回應(yīng)的數(shù)據(jù)包,統(tǒng)統(tǒng)把它們堵住掉。
iptables 提供了一個(gè)參數(shù) 是檢查狀態(tài)的,下面我們來配置下 22 和 80 端口,防止無(wú)效的數(shù)據(jù)包。
iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
可以看到和我們以前使用的:
iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT
多了一個(gè)狀態(tài)判斷。
同樣80端口也一樣, 現(xiàn)在刪掉原來的2條規(guī)則,
iptables -L -n --line-number??? 這個(gè)是查看規(guī)則而且?guī)暇幪?hào)。我們看到編號(hào)就可以
刪除對(duì)應(yīng)的規(guī)則了。
iptables -D OUTPUT 1???? 這里的1表示第一條規(guī)則。
當(dāng)你刪除了前面的規(guī)則, 編號(hào)也會(huì)隨之改變??吹搅税伞?/p>
好,我們刪除了前面2個(gè)規(guī)則,22端口還可以正常使用,說明沒問題了
下面進(jìn)行保存,別忘記了,不然的話重啟就會(huì)還原到原來的樣子。
service iptables save??? 進(jìn)行保存。
Saving firewall rules to /etc/sysconfig/iptables:????????? [ OK ]
其實(shí)就是把剛才設(shè)置的規(guī)則寫入到 /etc/sysconfig/iptables 文件中。
6、DNS端口53設(shè)置
下面我們來看看如何設(shè)置iptables來打開DNS端口,DNS端口對(duì)應(yīng)的是53
大家看到我現(xiàn)在的情況了吧,只開放22和80端口, 我現(xiàn)在看看能不能解析域名。
host?www.google.com??? 輸入這個(gè)命令后,一直等待,說明DNS不通
出現(xiàn)下面提示 :
;; connection timed out; no servers could be reached
ping 一下域名也是不通
[root@localhost ~ping?www.google.com
ping: unknown host?www.google.com
我這里的原因就是 iptables 限制了53端口。
有些服務(wù)器,特別是Web服務(wù)器減慢,DNS其實(shí)也有關(guān)系的,無(wú)法發(fā)送包到DNS服務(wù)器導(dǎo)致的。
下面演示下如何使用 iptables 來設(shè)置DNS 53這個(gè)端口,如果你不知道 域名服務(wù)端口號(hào),你
可以用命令 : 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 協(xié)議。
好了, 開始設(shè)置。。。
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT?
這是我們 ping 一個(gè)域名,數(shù)據(jù)就是從本機(jī)出去,所以我們先設(shè)置 OUTPUT,
我們按照ping這個(gè)流程來設(shè)置。
然后 DNS 服務(wù)器收到我們發(fā)出去的包,就回應(yīng)一個(gè)回來
iptables -A INPUT -p udp --sport 53 -j ACCEPT
同時(shí)還要設(shè)置?
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
好了, 下面開始測(cè)試下, 可以用 iptables -L -n 查看設(shè)置情況,確定沒有問題就可以測(cè)試了
[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
可以測(cè)試一下 是否 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 方面可能還要設(shè)置些東西。
用 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
說明本機(jī)DNS正常, iptables 允許53這個(gè)端口的訪問。
7、iptables對(duì)ftp的設(shè)置
現(xiàn)在我開始對(duì)ftp端口的設(shè)置,按照我們以前的視頻,添加需要開放的端口
ftp連接端口有2個(gè) 21 和 20 端口,我現(xiàn)在添加對(duì)應(yīng)的規(guī)則。
[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,出現(xiàn)超時(shí)。
所以我剛才說 ftp 是比較特殊的端口,它還有一些端口是 數(shù)據(jù)傳輸端口,
例如目錄列表, 上傳 ,下載 文件都要用到這些端口。
而這些端口是 任意 端口。。。 這個(gè) 任意 真的比較特殊。
如果不指定什么一個(gè)端口范圍, iptables 很難對(duì)任意端口開放的,
如果iptables允許任意端口訪問, 那和不設(shè)置防火墻沒什么區(qū)別,所以不現(xiàn)實(shí)的。
那么我們的解決辦法就是 指定這個(gè)數(shù)據(jù)傳輸端口的一個(gè)范圍。
下面我們修改一下ftp配置文件。
我這里使用vsftpd來修改演示,其他ftp我不知道哪里修改,大家可以找找資料。
[root@localhost rootvi /etc/vsftpd.conf
在配置文件的最下面 加入
pasv_min_port=30001
pasv_max_port=31000
然后保存退出。
這兩句話的意思告訴vsftpd, 要傳輸數(shù)據(jù)的端口范圍就在30001到31000 這個(gè)范圍內(nèi)傳送。
這樣我們使用 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
最后進(jìn)行保存, 然后我們?cè)儆脼g覽器范圍下 ftp??梢哉TL問
用個(gè)賬號(hào)登陸上去,也沒有問題,上傳一些文件上去看看。
看到了吧,上傳和下載都正常。。 再查看下 iptables 的設(shè)置
[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特殊端口做的簡(jiǎn)單規(guī)則,大家可以添加一些對(duì)數(shù)據(jù)包的驗(yàn)證
例如 -m state --state ESTABLISHED,RELATED 等等要求更加高的驗(yàn)證
?
iptables?防火墻?只允許某IP訪問某端口、訪問特定網(wǎng)站 ? 需要開80端口,指定IP和局域網(wǎng)?
下面三行的意思:
先關(guān)閉所有的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
以上是臨時(shí)設(shè)置。
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轉(zhuǎn)發(fā)記得配合以下才能生效
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規(guī)則如下: 只能收發(fā)郵件,別的都關(guān)閉
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服務(wù)器的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
只允許訪問指定網(wǎng)址
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
開放一個(gè)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
多個(gè)端口
iptables -A Filter -p tcp -m multiport --destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT
連續(xù)端口
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
指定時(shí)間上網(wǎng)
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
禁止多個(gè)端口服務(wù)
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服務(wù)器要轉(zhuǎn)的端口
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,別的服務(wù)都禁止
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,只能收發(fā)郵件,其它都拒絕
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 其它公網(wǎng)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
禁止某個(gè)MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:20:18:8F:72:F8 -j DROP
禁止某個(gè)IP地址的PING:
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP
禁止某個(gè)IP地址服務(wù):
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
只允許某些服務(wù),其他都拒絕(2條規(guī)則)
iptables -A Filter -p tcp -s 192.168.0.1 --dport 1000 -j ACCEPT
iptables -A Filter -j DROP
禁止某個(gè)IP地址的某個(gè)端口服務(wù)
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
禁止某個(gè)MAC地址的某個(gè)端口服務(wù)
iptables -I Filter -p tcp -m mac --mac-source 00:20:18:8F:72:F8 --dport 80 -j DROP
禁止某個(gè)MAC地址訪問internet:
iptables -I Filter -m mac --mac-source 00:11:22:33:44:55 -j DROP
iptables –A Filter –p icmp –s 192.168.0.1 –j DROP
l
come from:http://blog.csdn.net/cssmhyl/article/details/7966789
- 上一篇iOS設(shè)備中WiFi、藍(lán)牙和飛行模式的開啟與關(guān)閉
轉(zhuǎn)載于:https://www.cnblogs.com/seasonzone/p/5067229.html
總結(jié)
以上是生活随笔為你收集整理的iptables禁止端口和开放端口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ1683 Puzzlestan —
- 下一篇: AppScan报错--发生内部错误