【笔记】openwrt - iptables 命令、例子、日志( 二 )


在转发包离开本机之前 , 它会经过chain 。对于本机生成的包 , 这里还有一 点不同:它会先经过chain , 然后再经过chain 。
/UTING
(路由判断)
raw
(连接跟踪)
nat (DNAT)

【笔记】openwrt - iptables 命令、例子、日志

文章插图
(路由判断)
nat (SNAT)
上面的表格展示了 table 和 chain 的关系 。横向是 table ,  纵向是 chain , Y 表示 这个 table 里面有这个 chain 。例如 , 第二行表示 raw table 有和两 个 chain 。具体到每列 , 从上倒下的顺序就是hook 触发的时候 , (对应 table 的)chain 被调用的顺序 。
有几点需要说明一下 。在下面的图中 , nat table 被细分成了 DNAT (修改目的地址) 和 SNAT(修改源地址) , 以更方便地展示他们的优先级 。另外 , 我们添加了路由决策点 和连接跟踪点 , 以使得整个过程更完整全面
结合最开始的包流向图 , 我们可以得到如下不同场景下包的游走流程:
再综合前面讨论的 table 顺序问题 , 我们可以看到对于一个收到的、目的是本机的包:首先依次经过chain 上面的 raw、、nat table;然后依次经 过 INPUT chain 的 、、、nat table , 然后才会到达本机 的某个。
基本命令
# Abstract structure of an iptables instruction:iptables [-t table] [-command chain] [match pattern] [-j action]-t table 表名 , 默认filter-comman chain 管理选项 , 如:添加A、插入I、删除D、查看Lmatch pattern 数据包特征 , 如:ip、端口等-j action 控制类型(数据包的处理方式) , 如:ACCEPT 允许、REJECT 拒绝、DROP 丢弃、LOG 记录日志
表的几个重要命令
.04的网络配置(静态IP和动态IP) -
# 例子:禁止自己被ping
自己可以ping别人 , 别人不能ping自己
$ sudo iptables -t filter -I INPUT -p icmp --icmp-type 8 -j DROP$ sudo iptables -t filter -LChain INPUT (policy ACCEPT)targetprot opt sourcedestinationDROPicmp --anywhereanywhereChain FORWARD (policy ACCEPT)targetprot opt sourcedestinationChain OUTPUT (policy ACCEPT)targetprot opt sourcedestination
别人ping我 , INPUT , icmp-type=8 , 被丢弃
使用ipv6的话 , 命令要改改
ip6tables -t filter -A input_wan_rule -p ipv6-icmp --icmpv6-type 128 -j DROP
nat表的几个重要命令
默认的 table 是table , 因此我们接下来每次都要指定 -t nat
最重要的几个命令:
# In the following "chain" represents# one of the chains PREROUTING, OUTPUT and POSTROUTING# add a rule:$> iptables -t nat -A chain [...]# list rules:$> iptables -t nat -L# remove user-defined chain with index 'myindex':$> iptables -t nat -D chain myindex# Remove all rules in chain 'chain':$> iptables -t nat -F chain
完整命令可以查看 man page:
# manual pages of iptables$> man iptables
按 q 退出帮助 。
# 选择匹配模式()
要对特定包进行处理需要指定匹配模式 。如下是几个重要的例子 , 完整模式信息请参阅man page 。
# actions to be taken on matched packets# will be abbreviated by '[...]'.# Depending on the match pattern the appropriate chain is selected.# TCP packets from 192.168.1.2:$> iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.2 [...]# s source# UDP packets to 192.168.1.2:$> iptables -t nat -A POSTROUTING -p udp -d 192.168.1.2 [...]# d destination# all packets from 192.168.x.x arriving at eth0:$> iptables -t nat -A PREROUTING -s 192.168.0.0/16 -i eth0 [...]# i interface# all packets except TCP packets and except packets from 192.168.1.2:$> iptables -t nat -A PREROUTING -p ! tcp -s ! 192.168.1.2 [...]# packets leaving at eth1:$> iptables -t nat -A POSTROUTING -o eth1 [...]# TCP packets from 192.168.1.2, port 12345 to 12356# to 123.123.123.123, Port 22# (a backslash indicates contination at the next line)$> iptables -t nat -A POSTROUTING -p tcp -s 192.168.1.2 \--sport 12345:12356 -d 123.123.123.123 --dport 22 [...]