iptables浅谈使用(二)
引言
1 概述
防火墙技术是通过有机结合各类用于安全管理与筛选的软件和硬件设备,帮助计算机网络于其内、外网之间构建一道相对隔绝的保护屏障,以保护用户资料与信息安全性的一种技术。
防火墙技术的功能主要在于及时发现并处理计算机网络运行时可能存在的安全风险、数据传输等问题,其中处理措施包括隔离与保护,同时可对计算机网络安全当中的各项操作实施记录与检测,以确保计算机网络运行的安全性,保障用户资料与信息的完整性,为用户提供更好、更安全的计算机网络使用体验。
2 iptables
2.1 关于iptables
iptables是netfilter的一个子项目,iptables本身一个通用的防火墙软件,允许定义规则集。IP 表中的每条规则都由许多分类器(iptables 匹配)和一个连接的操作(iptables 目标)组成。
在与Linux内核集成时,版本对应如下:
• Linux 2.0.X内核对应ipfwadm
• Linux 2.2.X内核对应ipchains
• Linux2.4.X内核对应iptables
iptables的官方地址:https://www.netfilter.org/projects/iptables/index.html
iptables版本更改日志记录:https://git.netfilter.org/iptables/
iptables各个版本下载地址:https://www.netfilter.org/pub/iptables/
2.2 iptables工作原理
iptables是采用数据包过滤机制工作的,所以它会对请求的数据包的包头进行分析,并根据我们预先设定的规则进行匹配来决定是否可以进入主机。
- 防火墙是一层一层过滤的。实际是按照配置规则的顺序从上到下,从前到后进行过滤的;
 - 如果匹配上规则,即明确表明阻止还是通过,此时数据包就不再向下匹配新规则了;
 - 如果所有规则中没有明确表明是阻止还是通过这个数据包,也就是没有匹配上规则,则按照默认策略进行处理;
 - 防火墙的默认规则是对应的链的所有的规则执行完成后才会执行的;
 
2.3 iptables的规则链
iptables的规则链是NetFilter规定的五个规则链,任何一个数据包,只要经过本机,必将经过这五个链中的其中一个链。
- PREROUTING (路由前)
 - INPUT (数据包流入口)
 - FORWARD (转发管卡)
 - OUTPUT(数据包出口)
 - POSTROUTING(路由后)
 

2.4 iptables策略
防火墙策略一般分为两种,一种叫“通”策略,一种叫“堵”策略。
「通策略」
默认门是关着的,必须要定义谁能进。
「堵策略」
大门是洞开的,但是你必须有身份认证,否则不能进。
我们现在用的比较多个功能有3个,
- filter
 
- 定义允许或者不允许的
 - INPUT ,FORWARD ,OUTPUT
 
- nat
 
- 定义地址转换的
 - PREROUTING ,OUTPUT ,POSTROUTING
 
- mangle
 
- 功能管理,修改报文原数据
 - PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
 
3 iptables使用规则
先使用命令iptables -V在机器上面查看是否安装,以及安装的版本。从下面的命令来看,我机器已经安装了iptables,而且安装的版本为v1.4.21。在官网,截止到目前为止,我们看到的最新版本为v1.8.8 。
[root@nto2 ~]# iptables -V
iptables v1.4.21 
  关于使用,可以使用iptables --help来查看具体的使用规则。
[root@nto2 ~]# iptables --help
iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
       iptables -I chain [rulenum] rule-specification [options]
       iptables -R chain rulenum rule-specification [options]
       iptables -D chain rulenum [options]
       iptables -[LS] [chain [rulenum]] [options]
       iptables -[FZ] [chain] [options]
       iptables -[NX] chain
       iptables -E old-chain-name new-chain-name
       iptables -P chain target [options]
       iptables -h (print this help information)
Commands:
Either long or short options are allowed.
  --append  -A chain  Append to chain
  --check   -C chain  Check for the existence of a rule
  --delete  -D chain  Delete matching rule from chain
  --delete  -D chain rulenum
    Delete rule rulenum (1 = first) from chain
  --insert  -I chain [rulenum]
    Insert in chain as rulenum (default 1=first)
  --replace -R chain rulenum
    Replace rule rulenum (1 = first) in chain
  --list    -L [chain [rulenum]]
    List the rules in a chain or all chains
  --list-rules -S [chain [rulenum]]
    Print the rules in a chain or all chains
  --flush   -F [chain]  Delete all rules in  chain or all chains
  --zero    -Z [chain [rulenum]]
    Zero counters in chain or all chains
  --new     -N chain  Create a new user-defined chain
  --delete-chain
            -X [chain]  Delete a user-defined chain
  --policy  -P chain target
    Change policy on chain to target
  --rename-chain
            -E old-chain new-chain
    Change chain name, (moving any references)
Options:
    --ipv4  -4  Nothing (line is ignored by ip6tables-restore)
    --ipv6  -6  Error (line is ignored by iptables-restore)
[!] --protocol  -p proto    protocol: by number or name, eg. `tcp'
[!] --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  numeric output of addresses and ports
[!] --out-interface -o output name[+]
    network interface name ([+] for wildcard)
  --table   -t table    table to manipulate (default: `filter')
  --verbose -v  verbose mode
  --wait    -w [seconds]    maximum wait to acquire xtables lock before give up
  --wait-interval -W [usecs]    wait time to try to acquire xtables lock
    default is 1 second
  --line-numbers  print line numbers when listing
  --exact   -x  expand numbers (display exact values)
[!] --fragment  -f  match second or further fragments only
  --modprobe=<command>  try to insert modules using this command
  --set-counters PKTS BYTES set the counter during insert/append
[!] --version   -V  print package version. 
  - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)