歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> iptables訪問策略

iptables訪問策略

日期:2017/2/28 15:34:45   编辑:Linux教程

iptables由3個表filter,nat,mangle組成,主要實驗了filter表,這個表是用來過濾數據包的,有三個鏈INPUT,OUTPUT,FORWARD。

配置防火牆策略有固定的格式


Iptables 表名 鏈名 匹配條件 動作

-t 表名 (默認為filter)

-A 鏈名(在該鏈末尾append追加策略)

-I 鏈名 數字

-I (insert)插入鏈,如果不加數字,默認是將寫的策略添加到表中所有策略的前面,但是我們要指定插入到相應的行,我們可以這樣

Iptables –t filter –I INPUT 2 …… 這裡就是插到第二個

匹配條件:


-i 網卡 數據包進入的網卡

-o 網卡 出去的

-s ip 源ip

-d ip 目的ip

-p 協議

--dport 端口號 目的端口號

--sport 端口號 源端口號

動作:


ACCEPT:對滿足策略的數據包允許通過

DROP:丟棄數據包,且不返回任何信息

REJECT:丟棄數據包,但是會返回拒絕的信息

LOG:把通過的數據包寫到日志中(相當於一個門衛對進去的人進行登記)

iptables -t filter -A INPUT –s 192.168.0.0/24 -p tcp --dport 80 –j REJECT

上面這句的意思是:對filter表的INPUT鏈,追加一條策略,策略是,源地址在192.168.0.0/24網段內,使用tcp協議,目標端口為80的所有輸入包都執行REJECT動作(拒絕)
實驗室完成了filter表的INPUT鏈的基本操作和增加刪除一條鏈,過程如下:

[root@mail ~]# iptables -L #查看當前內存中iptables策略,默認是filter表
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@mail ~]# iptables -t filter -vnL #加vn參數,有更多選項
Chain INPUT (policy ACCEPT 31471 packets, 4322K bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 37490 packets, 3056K bytes)
pkts bytes target prot opt in out source destination

#策略格式:iptables 表名 鏈名 匹配條件 動作
#下面這句話的意思是,對於filter表的INPUT鏈,源地址為192.169.1.0/24網段內的使用tcp
#協議80端口的輸入包,都執行REJECT(拒絕)動作
[root@mail ~]# iptables -t filter -A INPUT -s 192.169.1.0/24 -p tcp --dport 80 -j REJECT
[root@mail ~]# iptables -L #需要注意的是,該策略目前只在內存中,/etc/sysconfig/iptables配置文件中是沒有的
Chain INPUT (policy ACCEPT)
target prot opt source destination



REJECT tcp -- 192.169.1.0/24 anywhere tcp dpt:http reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@mail ~]# iptables -vnL #看的更詳細點
Chain INPUT (policy ACCEPT 88 packets, 8188 bytes)
pkts bytes target prot opt in out source destination
4 240 REJECT tcp -- * * 192.169.1.0/24 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination

Chain OUTPUT (policy ACCEPT 131 packets, 11625 bytes)
pkts bytes target prot opt in out source destination
[root@mail ~]# vim /etc/sysconfig/iptables #iptables中沒有


# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT


#執行save後會將這條策略寫入/etc/sysconfig/iptables,在保存的時候,是執行覆蓋式的保存
#內存中有的保存下來,內存中沒有的,這個文件中有的將會被刪除。
[root@mail ~]# service iptables save
iptables:將防火牆規則保存到 /etc/sysconfig/iptables: [確定]
[root@mail ~]# vim /etc/sysconfig/iptables


# Generated by iptables-save v1.4.7 on Wed Aug 15 17:28:53 2012
*filter
:INPUT ACCEPT [4:352]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:298]
-A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Wed Aug 15 17:28:53 2012


#表的每條鏈後面都有一個默認動作,Chain INPUT (policy ACCEPT),默認動作意思是
#沒有匹配所以策略的匹配條件時(按序匹配),就執行的動作,可以修改鏈的默認動作
[root@mail ~]# iptables -t filter -P INPUT DROP #修改filter表的INPUT鏈的默認動作
[root@mail ~]# iptables -L
Chain INPUT (policy DROP)
target prot opt source destination
REJECT tcp -- 192.169.1.0/24 anywhere tcp dpt:http reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

[root@mail ~]# iptables -t filter -P INPUT ACCEPT #暫時改回來

#可以刪除一條策略,策略是有序的,從1開始,要刪除一條策略,需要知道它的序號
[root@mail ~]# iptables -L --line-numbers #查看策略的序號
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 REJECT tcp -- 192.169.1.0/24 anywhere tcp dpt:http reject-with icmp-port-unreachable

Chain FORWARD (policy ACCEPT)
num target prot opt source destination

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@mail ~]# iptables -D INPUT 1 #刪除INPUT鏈的序號為1的策略
[root@mail ~]# vim /etc/sysconfig/iptables #和前面一樣,這只是刪除內存中的,/etc/sysconfig/iptables中仍然存在

# Generated by iptables-save v1.4.7 on Wed Aug 15 17:28:53 2012
*filter
:INPUT ACCEPT [4:352]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [4:298]
-A INPUT -s 192.169.1.0/24 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Wed Aug 15 17:28:53 2012


[root@mail ~]# service iptables save #執行保存,配置文件中也被刪除了
iptables:將防火牆規則保存到 /etc/sysconfig/iptables: [確定]
[root@mail ~]# vim /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Wed Aug 15 17:45:03 2012
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT
# Completed on Wed Aug 15 17:45:03 2012


#除了INPUT,FORWARD,OUTPUT鏈,可以定義自己的鏈
[root@mail ~]# iptables -N chen #定義一個chen鏈,相當於多了一扇門

#拒絕通過chen鏈,地址為192.169.1.99,協議為tcp端口為80的數據包
[root@mail ~]# iptables -t filter -A chen -s 192.169.1.99 -p tcp --dport 80 -j REJECT
[root@mail ~]# iptables -t filter -A INPUT -j chen #把經過INPUT鏈的數據引入到chen這個鏈上
[root@mail ~]# iptables -L --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 chen all -- anywhere anywhere #INPUT鏈的target變為chen了

Chain FORWARD (policy ACCEPT)
num target prot opt source destination

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination

Chain chen (1 references) #可以看到多了一個鏈,下面的策略也存在
num target prot opt source destination

1 REJECT tcp -- 192.169.1.99 anywhere tcp dpt:http reject-with icmp-port-unreachable
[root@mail ~]#
[root@mail ~]# service iptables save
iptables:將防火牆規則保存到 /etc/sysconfig/iptables: [確定]
[root@mail ~]# vim /etc/sysconfig/iptables


# Generated by iptables-save v1.4.7 on Wed Aug 15 18:11:28 2012
*filter
:INPUT ACCEPT [1:125]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1:71]
:chen - [0:0]
-A INPUT -j chen
-A chen -s 192.169.1.99/32 -p tcp -m tcp --dport 80 -j REJECT --reject-with icmp-port-unreachable
COMMIT
# Completed on Wed Aug 15 18:11:28 2012


[root@mail ~]# iptables -X chen #刪除chen這條鏈
iptables: Too many links.
[root@mail ~]# iptables -F #刪除前需要清空策略,否則刪除不掉
[root@mail ~]# iptables -X chen
[root@mail ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@mail ~]#

Copyright © Linux教程網 All Rights Reserved