Linux之Iptables防火墙相关概念和基本操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22067469/article/details/83745843

iptables概念

一、 iptables的前身叫ipfirewall(内核1.x时代),这是一个作者从freeBSD上移植过来的,能够工作在内核当中的,对数据包进行检测的一款简易访问控制工具。但是ipfirewall工作功能极其有限(它需要将所有的规则都放进内核当中,这样规则才能够运行起来,而放进内核,这个做法一般是极其困难的)。当内核发展到2.x系列的时候,软件更名为ipchains,它可以定义多条规则,将他们串起来,共同发挥作用,而现在,它叫做iptables,可以将规则组成一个列表,实现绝对详细的访问控制功能。

二、他们都是工作在用户空间中,定义规则的工具,本身并不算是防火墙。它们定义的规则,可以让在内核空间当中的netfilter来读取,并且实现让防火墙工作。而放入内核的地方必须要是特定的位置,必须是tcp/ip的协议栈经过的地方。而这个tcp/ip协议栈必须经过的地方,可以实现读取规则的地方就叫做 netfilter.(网络过滤器)

三、内核空间的5个位置

  1. 内核空间中:从一个网络接口进来,到另一个网络接口去的
  2. 数据包从内核流入用户空间的
  3. 数据包从用户空间流出的
  4. 进入/离开本机的外网接口
  5. 进入/离开本机的内网接口

linux的防火墙iptables

netfilter(iptables)

  • netfilter --工作在内核软件,实现数据包的过滤。
  • iptables --工作应用层一个软件,用来控制netfilter。
    1.netfilter/iptables包过滤防火墙(tcp/ip四层)
    1)应用层 --通过软件为用户提供接口
    2)传输层 --提供可靠或不可靠的数据传输(TCP/UDP)使用端口来标示服务类型 sport dport
    3)网络层 --提供路由和选址(icmp) sip dip
    4)数据链路层 --传输数据帧(MAC) s _mac arp写 在局域网内泛洪 来找到我们对应的MAC
    5)物理层 --传输透明比特流 eth0 eth1
    过滤的依据: s_mac/sip/dip/sport/dport/状态(三次握手/四次断开)SYN DDOS攻击怎么防御我们DDOS 小流量
    netfilter的逻辑架构: nat 10
    image

netfilter防火墙的元素及关系:
netfilter==>表==>链==>规则

三张表:

filter 防火墙表,允许和拒绝都在这里实现
nat 地址转换
mangle 数据包整形

五条链:

INPUT 本机进站的数据流 (路由前)
OUTPUT 本机出站的数据流 (数据包流入口)
FORWARD 路由的数据流 (转发管卡)
POSTROUTING 路由后的数据流(数据包出口)
PREROUTING 路由前的数据流 (路由后)
注意:这是NetFilter规定的五个规则链,任何一个数据包,只要经过本机,必将经过这五个链中的其中一个链。

表跟链的对应关系:

  • filter: INPUT——OUTPUT——FORWARD
  • nat: OUTPUT——PREROUTING——POSTROUTING
  • mangle: INPUT——OUTPUT——FORWARD——PREROUTING——POSTROUTING

四种数据流:

本机进站的数据流:packet–>ethX–>PREROUTING–>INPUT–>本机
本机出站的数据流:packet–>OUTPUT–>POSTROUTING–>ethX–>destination
路由的数据流:
出去: packet–>eth0–>PREROUTING–>FORWARD–>POSTROUTING–>eth1–destination
回来: packet–>eth1–>PREROUTING–>FORWARD–>POSTROUTING–>eth0–destination
本机访问本机: 本机–>packet–>lo–>PREROUTING–>INPUT–>本机

  • 表的匹配顺序:mangle–>nat–>filter
  • 防火墙规则匹配顺序:
  1. 按顺序匹配,如果第一条匹配到了就直接执行这条规则的动作,不往下匹配其它规则。
  2. 如果第一条匹配不到,第二条也匹配不到,继续往下匹配直到找到匹配的规则,如果找不到匹配默认规则。

传输层:协议(tcp/udp)
端口(sport/dport)
网络层:IP地址(sip/dip/icmp) ping
数据链路层: mac地址(–mac-source)
物理层: 从哪个网卡进来 -i eth0 eth1 服务器2个地址 外网地址 一个内网地址

iptables操作命令

#iptables --help
Usage: iptables -[AD] chain rule-specification [options]
       iptables -[RI] chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LFZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)

  --append  -A chain            追加规则
  --delete  -D chain            删除规则
  --delete  -D chain rulenum    删除指定序号的规则
                                
  --insert  -I chain [rulenum]  插入一条规则(default 1=first) 插入会插入表的第一行
  --replace -R chain rulenum    替换一条规则
                                
  --list    -L [chain]          显示出链或者链中的规则
  --flush   -F [chain]          在一个链或者所有链中清空规则
  --zero    -Z [chain]          清空计数
  --new     -N chain            创建用户自定义链
  --delete-chain
            -X [chain]          删除用户自定义链
  --policy  -P chain target     指定默认规则
                                Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
                                     重命名用户自定义链
Options:
  --proto       -p [!] proto    指定协议,!代表取反
  --source      -s [!] address[/mask]   --指定源地址
                                source specification
  --destination -d [!] address[/mask]   --指定目标地址
                                destination specification
  --in-interface -i [!] input name[+]   --指定数据从哪个网口进来
                                network interface name ([+] for wildcard)
  --jump        -j target                       --匹配动作
                                target for rule (may load target extension)
  --goto      -g chain
                              jump to chain with no return
  --match       -m match                        --扩展匹配
                                extended match (may load extension)
  --numeric     -n              --端口和IP以数值方式显示,不作反解
  --out-interface -o [!] output name[+] --指定数据从哪个网口出去
                                network interface name ([+] for wildcard)
  --table       -t table        --指定使用哪个表 (default: `filter')
  --verbose     -v              --显示详细信息
  --line-numbers                --显示规则的序号
  --exact       -x              expand numbers (display exact values)
  • 查看iptables命令

  1. iptables -t filter -L -n -v
[root@localhost ~]# iptables -t filter -L -n -v
Chain INPUT (policy ACCEPT 378 packets, 24964 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 199 packets, 22012 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]#

[root@localhost ~]# iptables -t nat -L 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

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

  1. iptables -t filter -L INPUT -v
[root@localhost ~]# iptables -t filter -L INPUT -v
Chain INPUT (policy ACCEPT 928 packets, 61464 bytes)
pkts bytes target     prot opt in     out     source               destination     
[root@localhost ~]#

3.watch -n 0.1 iptables -L INPUT

Every 0.1s: iptables -L INPUT -v                                                   Sun Jun 18 09:54:03 2017

Chain INPUT (policy ACCEPT 1140 packets, 71280 bytes)
pkts bytes target     prot opt in     out     source               destination
  • 插入iptables规则命令

1.iptables -t filter -I INPUT -i eth0 -j ACCEPT

[root@localhost ~]# iptables -t filter -I INPUT -i eth0 -j ACCEPT
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

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

注意只有一条数据的时间不能插入第三条
[root@localhost ~]# iptables -t filter -I INPUT 3 -i eth1 -j ACCEPT
iptables: Index of insertion too big.

  • 替换iptables规则命令

  1. iptables -t filter -R INPUT 1 -i eth1 -j ACCEPT
[root@localhost ~]# iptables -t filter -R INPUT 1 -i eth1 -j ACCEPT
[root@localhost ~]# iptables -L -v
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  eth1   any     anywhere             anywhere            
  562 37564 ACCEPT     all  --  eth0   any     anywhere             anywhere            
    0     0 ACCEPT     all  --  eth1   any     anywhere             anywhere            

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

Chain OUTPUT (policy ACCEPT 17 packets, 1580 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]#

  • 删除iptables规则命令

1.iptables -t filter -D INPUT 2

[root@localhost ~]# iptables -t filter -D INPUT 2 
[root@localhost ~]# iptables -L -v
Chain INPUT (policy ACCEPT 30 packets, 1980 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  eth1   any     anywhere             anywhere            
    0     0 ACCEPT     all  --  eth1   any     anywhere             anywhere            

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

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

  • 练习
  1. iptables -t filter -A INPUT -i lo -j ACCEPT
  2. iptables -t filter -A OUTPUT -o lo -j ACCEPT
  3. iptables -t filter -A INPUT -i eth0 -s 192.168.0.0/24 -d 192.168.0.250 -j ACCEPT
  4. iptables -t filter -A INPUT -i eth1 -j ACCEPT
  5. iptables -t filer -I INPUT 2 -i eth2 -j ACCEPT 插入默认第二条
  6. iptables -t filter -I INPUT -i eth3 -j ACCEPT 插入默认第一条
  7. iptables -t filter -R INPUT 1 -i eth4 -j ACCEPT 替换默认第一条
  8. iptables -t filter -D INPUT 1 删除默认第一条
  9. iptables -t filter -F INPUT 清空filter所有INPUT链
  • 空规则
  1. 清空一张表
    iptables -t filter -F
  2. 清空一条链中的规则
    iptables -t filter -F INPUT FORWARD OUTPUT
  • 新建/删除用户自定义的链:
  1. iptables -t filter -N uplooking 新建
[root@localhost ~]# iptables -t filter -N UPLOOKING
[root@localhost ~]# iptables -L -v
Chain INPUT (policy ACCEPT 30 packets, 1980 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         
    0     0 ACCEPT     all  --  eth5   any     192.168.1.0/24       192.168.0.250       

Chain OUTPUT (policy ACCEPT 15 packets, 1396 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     all  --  any    lo      anywhere             anywhere            

Chain UPLOOKING (0 references)
 pkts bytes target     prot opt in     out     source               destination         
[root@localhost ~]#

  1. iptables -t filter -X uplooking 删除一条自定义的链
  2. iptables -t filter -X 清空filter表中所有用户自定义链
  • 练习
  1. iptables -t filter -N TCP
  2. iptables -t filter -N UDP
  3. iptables -t filter -A INPUT -i lo -j ACCEPT
  4. iptables -t filter -A INPUT -p tcp -j TCP
  5. iptables -t filter -P INPUT ACCEPT 允许
  6. iptables -t filter -P INPUT DROP 数据包丢弃
  • tcpdump抓包命令

从哪个网卡出去 -o

传输层:协议(tcp/udp)
端口(sport /dport)
网络层:
IP地址(sip /dip /icmp) ping
数据链路层:
mac地址(–mac-source)
物理层:
从哪个网卡进来 -i eth0 eth1 服务器2个地址 外网地址 一个内网地址
从哪个网卡出去 -o

tcpdump 抓包工具

1)tcpdump(传输/网络层)

tcpdump -i eth0

tcpdump -i eth0 -vnn

-v:显示包含有TTL,TOS值等等更详细的信息

-n:不要做IP解析为主机名

-nn:不做名字解析和端口解析

更有针对性的抓包:

针对IP,网段,端口,协议

tcpdump -n -i eth0 -vnn host 192.168.0.154 --主机地址里边只要包含地址有:192.168.0.154

tcpdump -i eth0 -vnn net 192.168.0.0 /24 --抓取一个网段数据包

tcpdump -i eth0 -vnn port 22

tcpdump -i eth0 -vnn udp

tcpdump -i eth0 -vnn icmp ping

tcpdump -i eth0 -vnn arp

x = y x = y x = y x = y # tcpdump -i eth0 -vnn ip

tcpdump -n -i eth0 -vnn src host 192.168.0.154

tcpdump -i eth0 -vnn dst host 192.168.0.154

tcpdump -i eth0 -vnn src port 22

tcpdump -i eth0 -vnn src host 192.168.0.253 and dst port 22

tcpdump -i eth0 -vnn src host 192.168.0.154 or port 22

tcpdump -i eth0 -vnn src host 192.168.0.154 and not port 22

抓包最主要的功能在于调试,比如在调试某个服务的时候 明明服务起来了 但是不知道为什么链接不上 可以通过抓包工具来测试看看请求包有没有过来服务器,来判断是传输过程中失败了 还是被服务器拒绝了

猜你喜欢

转载自blog.csdn.net/qq_22067469/article/details/83745843
今日推荐