netfilter 五個表五個鏈介紹,iptables案例

linux防火牆 netfilter

  • selinux 臨時關閉 setenforce 0
[root@bogon ~]# getenforce
Enforcing                  開啓狀態
[root@bogon ~]# setenforce 0
[root@bogon ~]# getenforce
Permissive                 暫停狀態,重啓後恢復
  • selinux 永久關閉 vi /etc/selinux/config(默認直接關閉selinux,否則會對後期的服務產生限制)

將SELINUX=enforcing改成SELINUX=disabled,保存後退出 (重啓後纔會生效)html

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

在centOS 7以前還有個防火牆是netfilter ,contos7之後改用 firewalldlinux

關閉firewalld開機自啓 : systemctl disable firewalldvim

關閉firewalld服務: systemctl stop firewalldcentos

[root@aminglinux-01 network-scripts]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@aminglinux-01 network-scripts]#

開啓netfilterbash

yum install -y iptables-services   下載安裝netfilter
[root@aminglinux-01 network-scripts]# systemctl enable iptables          設置開機自啓
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@aminglinux-01 network-scripts]# systemctl start iptables             啓動服務
[root@aminglinux-01 network-scripts]# iptables -nvL                       查看filter表
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    8   576 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    6   468 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 8 packets, 928 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-01 network-scripts]#

netfilter的5個表5個連接

  • 五個表

filter 主要用於過濾包,是系統預設的表,該表內建3個鏈:INPUT,OUTPUT,FORWARD。INPUT鏈做用於進入本機的包,OUTPUT鏈做用於本機送出去的包,FORWARD鏈做用於那些跟本機無關的包。網絡

nat表 主要用於網絡地址轉換,它也有三個鏈。PREROUTING鏈的做用是在包剛剛到達防火牆時改變它的目的地址(若是須要的話),OUTPUT鏈的做用是改變本地產生的包的目的地址,POSTROUTING鏈的做用是在包即將離開防火牆時改變其源地址。tcp

mangle表主要用於給數據包作標記,而後根據標記去操做相應的包。這個表幾乎不怎麼用,除非像稱爲一個高級網絡工程師。測試

raw表 能夠實現不追蹤某些數據包,默認系統的數據包都會被追蹤,但追蹤勢必消耗必定的資源,因此能夠用raw表來指定某些端口的包不被追蹤。this

security表,在centos6中是沒有的,他用於強制訪問控制(MAC)的網絡規則。命令行

  • netfilter的5個鏈

PREROUTING: 數據包進入路由表以前。

INPUT:經過路由表後目的地爲本機。

FORWARDING: 經過路由表後,目的地不爲本機。

OUTPUT: 由本機產生,向外轉發。

POSTROUTONG: 發送到網卡接口以前。


iptables 語法

iptables規則的儲存位置/etc/sysconfig/iptables

[root@aminglinux-01 network-scripts]# cat /etc/sysconfig/iptables
# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@aminglinux-01 network-scripts]#

iptables -nvL 查看錶,

  • 默認查看的是filter表
[root@bogon ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    5   356 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 400 bytes)
 pkts bytes target     prot opt in     out     source               destination
  • -t 指定查看的表好比查看nat表,iptables -nvL -t nat
[root@bogon ~]# iptables -nvL  -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain OUTPUT (policy ACCEPT 1 packets, 76 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 1 packets, 76 bytes)
 pkts bytes target     prot opt in     out     source               destination

清空iptables全部規則iptables -F ,清空以後規則儲存文件裏面仍是有配置的。

[root@aminglinux-01 ~]# iptables -F
[root@aminglinux-01 ~]# ^C
[root@aminglinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 13 packets, 948 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 12 packets, 1208 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-01 ~]#

保存規則:service iptables save以後配置文件會有改變.

 

清除包及流量計數器置零:iptables -Z

  • 流量計數器是指iptables -nvL 輸出內容的最前面有pkts,bytes這兩列. iptables -Z清零
pkts      bytes
有多少包   數據量

iptables -nvL --line-number 顯示規則編號

清除包及流量計數器置零:iptables -Z 。

保存規則:service iptables save

-A/-D:表示增長/刪除一條規則 (-A 加的規則默認寫到前面已經有的規則的後面)

-I: 表示插入一條規則,其實跟-A同樣也是增長 (不過-I加的規則會寫到已有規則的前面,優先執行)

-p:表示指定協議,能夠是tcp,udp,或者icmp

--dport: 跟-p 一塊兒使用,表示指定目標端口。 (用以前前面必須-p 指定協議,否則會報錯)

--sport: 跟-p 一塊兒使用,表示指定來源的端口。 (用以前前面必須-p 指定協議,否則會報錯)

-s:表示指定來源IP .(能夠是一個IP段)。

-d: 指定目標IP.

-j:後面跟動做,其中ACCEPT表示容許包,DROP表示丟掉包,REJECT 表示拒絕包。

-i:指定網卡(不經常使用:可是偶爾能用到)。

DROP: 丟掉數據包. 拒絕訪問.

[root@aminglinux-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@aminglinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  440 36272 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
   21  1472 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   71  7269 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 15 packets, 1288 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-01 ~]#

按照編號刪除規則,刪除第六條測試

[root@aminglinux-01 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      531 42528 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3       21  1472 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       73  7737 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
6        0     0 DROP       tcp  --  *      *       192.168.188.1        192.168.188.128      tcp spt:1234 dpt:80

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 69 packets, 7276 bytes)
num   pkts bytes target     prot opt in     out     source               destination
[root@aminglinux-01 ~]# iptables -D INPUT 6
[root@aminglinux-01 ~]# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      585 46272 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3       21  1472 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        2   104 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       73  7737 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 480 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
[root@aminglinux-01 ~]#

iptables 小案例

  • 需求。須要把80.22.21放行,22端口須要指定一個ip段,只有這個ip段的ip訪問的時候才能訪問到。

須要用到一個腳本

首先vi /usr/local/sbin/iptables.sh

[root@aminglinux-01 ~]# vim /usr/local/sbin/iptables.sh

#!/bin/bash
ipt="/usr/sbin/iptables"               #定義一個變量。要寫絕對路徑
$ipt -F                                #首先清空以前的規則
$ipt -P INPUT DROP                     #沒有寫-t 說明操做的是filter表。而後定義一下默認策略。 #定義INPUT鏈的默認策略爲所有丟掉。
$ipt -P OUTPUT ACCEPT                #定義了OUTPUT鏈的默認策略爲所有放行
$ipt -P FORWARD ACCEPT               #定義了FORWARD鏈的默認策略爲所有放行  
$ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT     #目的是讓相關的數據包RELATED,ESTABLISHED這兩個狀態放行
$ipt -A INPUT -s 192.168.133.0/24 -p tcp --dport 22 -j ACCEPT    #把這個網段的數據22端口放行
$ipt -A INPUT -p tcp --dport 80 -j ACCEPT                        #把80端口數據包放行
$ipt -A INPUT -p tcp --dport 21 -j ACCEPT                        #把21端口數據包放行

~                                                                                                                                                   
~                                                                                                                                                   
~

nat表應用

  • A機器兩塊網卡ens33(192.168.245.128),ens37(192.168.100.1),ens33能夠上外網,ens37僅僅是內部網絡,B機器只有ens37(192.168.100.100),和A機器ens37能夠通訊互聯。

首先在虛擬機A上添加一塊網卡,在B上也添加一起網卡,新加的兩塊網卡都改爲LAN區段模式。而後配好ip。

  • 需求1:可讓B機器鏈接外網。

A機器上打開路由轉發 echo"1">/proc/sys/net/ipv4/ip_forward

默認linux內核是沒有開啓轉發的,/proc/sys/net/ipv4/ip_forward這個文件是0
[root@aminglinux-01 ~]# cat /proc/sys/net/ipv4/ip_forward
0
改爲1,就開起了內核轉發。

命令行設置ipifconfig ens37 192.168.100.1/24 (重啓後失效,改配置文件纔會永久生效)

A上執行iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

[root@aminglinux-01 ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE

[root@aminglinux-01 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 8 packets, 2525 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

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

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      ens33   192.168.100.0/24     0.0.0.0/0           
[root@aminglinux-01 ~]#

B上設置網關爲192.168.100.1

route -n 查看網關

[root@aminglinux-01 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.245.2   0.0.0.0         UG    100    0        0 ens33
192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 ens37
192.168.245.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@aminglinux-01 ~]#

添加網關 route add default gw 192.168.100.1

這時候ping A機器的1網卡,若是通訊了說明B電腦已經能夠訪問公網了。

能夠設置dns來進行驗證。vi /etc/resolv.conf

  • 需求2:C機器只能和A通訊,讓C機器能夠直接鏈接通B機器的22端口。

A上打開路由轉發 echo"1">/ proc/sys/net/ipv4/ip_forward

A上執行iptabls -t nat -A PREROUTING -d 192.168.245.128 -p tcp --dport 1122 -j DNAT --to 192.168.100.100:22

A上執行iptables -t nat -A POSTROUTING -s 192.168.100.100 -j SNAT --to 192.168.245.128

B上設置網關爲192.168.100.1


擴展(selinux瞭解便可)

selinux教程 http://os.51cto.com/art/201209/355490.htm

selinux pdf電子書 http://pan.baidu.com/s/1jGGdExK

iptables應用在一個網段 http://www.aminglinux.com/bbs/thread-177-1-1.html

sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html

iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html

http://jamyy.us.to/blog/2006/03/206.html

相關文章
相關標籤/搜索