0

【笔记】Iptables学习笔记

 2 years ago
source link: https://feiju12138.github.io/2022/04/25/Iptables%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Iptables学习笔记
Linux的包过滤规则(包过滤防火墙)学习笔记

Iptables的所有功能/表

  • mangle
  • filter

Iptables中链所包含的规则属性

  • pkts:处理的数据包个数
  • byte:处理的数据包字节数
  • target:规则处理方式
    • ACCEPT:允许
    • REJECT:拒绝
  • prot:协议
    • all:所有协议
    • tcp:TCP协议
    • udp:UDP协议
    • icmp:ICMP协议
  • in:入站网卡
  • out:出站网卡
  • source:数据包源地址网段
  • destination:数据包目标地址网段

filter包过滤规则(包过滤防火墙)

filter表中所包含的链

  • INPUT:入站链
  • FORWARD:转发链
  • OUTPUT:出站链

查看filter表的所有链

-t <raw/mangle/nat/filter>:指定查看的表。如果不写,缺省值是filter
-L:列出所有规则的条目
-n:以数字形式显示地址、端口号等信息
-v:显示详细信息
--line-numbers:显示行号

iptables -t filter -nvL

1秒钟刷新1次

watch -n1 iptables -t filter -nvL

通用匹配(必选参数)

-A <INPUT/FORWARD/OUTPUT>:在链尾添加规则
-I <INPUT/FORWARD/OUTPUT>:在链首添加规则

-I <INPUT/FORWARD/OUTPUT> <num>:在链首开始的指定行添加规则

-j <ACCEPT/REJECT/DROP/LOG>:允许/拒绝/丢弃/仅记录日志
-p <tcp/udp/icmp>:定义协议
-i eth0:入站网卡
-o eth0:出站网卡
-s 0.0.0.0:源地址
-d 0.0.0.0:目标地址

隐含匹配(可选参数)

-sport <port>:源端口号
-dport <port>:目标端口号
--tcp-flags:TCP标记匹配
--icmp-type <8/0/3>:ICMP类型,请求/回显/不可达

显式匹配(特殊功能)

-m multiport --sport:源端口号列表
-m multiport --dport:目标端口号列表
-m iprange --src-range:IP范围
-m mac --mac-source:MAC地址
-m state --state <state>:状态匹配

iptables -t filter -I <INPUT/FORWARD/OUTPUT> -p <tcp/udp/icmp> -dport <port> -j <ACCEPT/REJECT>

删除指定行号的规则

<num>:行号

iptables -t filter -D <INPUT/FORWARD/OUTPUT> <num>
iptables -t filter -F <INPUT/FORWARD/OUTPUT>

修改默认规则

iptables -t filter -P <INPUT/FORWARD/OUTPUT> <ACCEPT/DROP>

nat网络地址转换

查看nat表的所有链

-t <raw/mangle/nat/filter>:指定查看的表
-L:列出所有规则的条目
-n:以数字形式显示地址、端口号等信息
-v:显示详细信息
--line-numbers:显示行号

iptables -t nat -nvL

1秒钟刷新1次

watch -n1 iptables -t nat -nvL

nat表中所包含的链

  • PREROUTING:路由前(内网)位置链
  • POSTROUTING:路由后(外网)位置链
  • OUTPUT:输出链

通用匹配(必选参数)

-A <PREROUTING/POSTROUTING/OUTPUT>:在链尾添加规则
-I <PREROUTING/POSTROUTING/OUTPUT>:在链首添加规则

-I <PREROUTING/POSTROUTING/OUTPUT> <num>:在链首开始的指定行添加规则

-j <SNAT/MASQUERADE/DNAT>:源地址转换/自动转换/目标地址转换
-p <tcp/udp/icmp>:定义协议
-i eth0:入站网卡
-o eth0:出站网卡
-s 0.0.0.0:源地址
-d 0.0.0.0:目标地址
--to-source 10.0.0.1:将源地址转换为网关地址

iptables -t nat -A POSTROUTING -p tcp -o eth1 -s 192.168.1.0/24 -j SNAT --to-source 10.0.0.1

备份/还原iptables

<bak>:备份文件

备份iptables

iptables-save > <bak>

还原iptables

iptables-restore < <bak>

将当前iptables保存为默认

service iptables save

哔哩哔哩——千锋教育网络安全学院


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK