一、iptables 使用详解
#基本语法
iptables [选项] [链名称] [规则匹配条件] [动作]
[选项]:指定操作,如新增、删除、插入规则等。
[链名称]:指定 iptables 的链,如 INPUT、OUTPUT、FORWARD、PREROUTING 等。
[规则匹配条件]:数据包过滤的条件,如源 IP 地址、目标端口等。
[动作]:匹配到的包执行的操作,如 ACCEPT、DROP、REJECT 等
#链
INPUT:用于控制进入本地系统的数据包。
OUTPUT:用于控制本地系统发送的数据包。
FORWARD:用于控制通过本地系统的数据包(本机不是目的地时)。
PREROUTING:用于在包进入路由之前修改数据包,用于 NAT。
POSTROUTING:用于在包离开路由后修改数据包,用于 NAT。
#选项
#链管理选项
-L:列出指定链中的所有规则
-N:创建新链
-X:删除自定义链
-F:清空指定链中的所有规则。如果不指定链,则清空所有链的规则
-Z:将链的计数器清零
#规则管理选项
-A:在指定链的末尾追加一条规则
-I:在指定链的指定位置插入一条规则(默认在首位)
-D:删除指定链中的规则。可以通过规则编号或直接匹配规则来删除
-R:替换指定链中的某条规则
-P:设置默认策略(即当数据包不匹配任何规则时的默认动作)
#匹配条件选项
-s,--source:指定源 IP 地址
-d,--destination:指定目标 IP 地址
-p,--protocol:指定协议类型(如 tcp、udp、icmp)
--sport:指定外部端口号(仅适用于 tcp 或 udp 协议)
--dport:指定本地端口号
-i,--in-interface:指定进入的网络接口
-o,--out-interface:指定离开的网络接口
-m state --state:使用连接跟踪模块,匹配数据包的连接状态
-m limit:限制数据包匹配速率
#动作:-j,--jump:指定匹配条件满足时跳转到的动作或目标链
ACCEPT:允许数据包通过
DROP:丢弃数据包,不作任何响应
REJECT:拒绝数据包,并返回错误信息
LOG:记录日志,但不阻止数据包的继续处理
msm8953_64:/ # iptables --help
iptables v1.6.1
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.
二、iptables 的“四表五链”及“堵通策略”
-
“四表” 指的是:filter、nat、mangle、raw
filter:控制数据包是否允许进出及转发(INPUT、OUTPUT、FORWARD),可以控制的链路有input, forward, output
-
禁止所有(验证OK)
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
- 删除规则(验证OK)
iptables --flush
iptables -F
- 禁止&使能 ping操作(验证OK)
#禁止
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j DROP
#使能
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
- 保存参数(验证OK)
iptables-save >/sdcard/Music/iptables-default
- 恢复参数(验证失败why?)
iptables-restore < /sdcard/Music/iptables-default
- 禁止TCP协议传输
#禁止:(验证OK)
iptables -A INPUT -p tcp -j REJECT
iptables -A FORWARD -p tcp -j REJECT
iptables -A OUTPUT -p tcp -j REJECT
#使能:(未能打开why?)
iptables -A INPUT -p tcp -j ACCEPT
iptables -A FORWARD -p tcp -j ACCEPT
iptables -A OUTPUT -p tcp -j ACCEPT
8.关闭WireGuard
#禁止:(验证失败,无此协议)
iptables -A INPUT -p wireguard -j REJECT
iptables -A FORWARD -p wireguard -j REJECT
iptables -A OUTPUT -p wireguard -j REJECT
只支持以下协议
const struct xtables_pprot xtables_chain_protos[] = {
{"tcp", IPPROTO_TCP},
{"sctp", IPPROTO_SCTP},
{"udp", IPPROTO_UDP},
{"udplite", IPPROTO_UDPLITE},
{"icmp", IPPROTO_ICMP},
{"icmpv6", IPPROTO_ICMPV6},
{"ipv6-icmp", IPPROTO_ICMPV6},
{"esp", IPPROTO_ESP},
{"ah", IPPROTO_AH},
{"ipv6-mh", IPPROTO_MH},
{"mh", IPPROTO_MH},
{"all", 0},
{NULL},
};
9.停掉WireGuard端口(无法验证)
#禁用本地59381端口
iptables -I INPUT -p tcp,udp --dport 59831 -j DROP
iptables -I OUTPUT -p tcp,udp --dport 59831 -j DROP
iptables -I FORWARD -p tcp,udp --dport 59831 -j DROP
#禁止外部51820端口
iptables -I INPUT -p tcp,udp --sport 51820 -j DROP
iptables -I OUTPUT -p tcp,udp --sport 51820 -j DROP
iptables -I FORWARD -p tcp,udp --sport 51820 -j DROP
- 白名单模式:
1)先清除现有规则
iptables -F
2) 默认禁用所有输入及输出
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
3)允许指定的IP及端口访问
#特定IP及端口的连接
iptables -A INPUT -p tcp --dport 22 -s 123.123.123.123 -j ACCEPT
测试Failed:
iptables -A INPUT -p tcp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -A INPUT -p udp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -A OUTPUT -p udp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -A FORWARD -p tcp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -A FORWARD -p udp --dport 996 -s 218.17.50.142 -j ACCEPT
iptables -I INPUT -p tcp --sport 996 -s 218.17.50.142 -j ACCEPT
iptables -I OUTPUT -p tcp --sport 996 -s 218.17.50.142 -j ACCEPT
iptables -I FORWARD -p tcp --sport 996 -s 218.17.50.142 -j ACCEPT
iptables -I INPUT -p udp --sport 996 -s 218.17.50.142 -j ACCEPT
iptables -I OUTPUT -p udp --sport 996 -s 218.17.50.142 -j ACCEPT
iptables -I FORWARD -p udp --sport 996 -s 218.17.50.142 -j ACCEPT
#验证OK
iptables -A OUTPUT -d 218.17.50.142 -j ACCEPT
#123.123.0/24 IP范围的连接
iptables -A INPUT -p tcp -s 123.123.123.0/24 --dport 22 -j ACCEPT
4)允许指定的URL访问
iptables -A FORWARD -m string --string "www.baidu.com" --algo bm -j ACCEPT
iptables -A FORWARD -m string --string "time.windows.com" --algo bm -j ACCEPT
5)允许本地环回接口 (#验证OK)
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
6)允许已建立连接的数据包
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- 删除规则
查看规则:
iptables --list-rules
将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers
比如要删除INPUT里序号为8的规则,执行:
iptables -D INPUT 8
- 禁用IPV6
echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6
#或者
interfaceSetEnableIPv6
netd:InterfaceController::setEnableIPv6
ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -P OUTPUT DROP
- 使能UID
#uid 更加包名查找 data/system/packages.list CV200使用systemUid=1000
iptables -I fw_OUTPUT -m owner --uid-owner 10161(uid) -j ACCEPT
如下测试:
iptables -F
ip6tables -F
#投屏工具vysor:UID=10094
iptables -I OUTPUT -m owner ! --uid-owner 10094 -j DROP
ip6tables -I OUTPUT -m owner ! --uid-owner 10094 -j DROP
#汽水音乐UID:UID=10095 验证OK
iptables -I OUTPUT -m owner --uid-owner 10095 -j DROP
ip6tables -I OUTPUT -m owner --uid-owner 10095 -j DROP
#百度UID:UID=10097 验证OK
iptables -I OUTPUT -m owner --uid-owner 10097 -j DROP
ip6tables -I OUTPUT -m owner --uid-owner 10097 -j DROP
#今日头条:UID=10096
#新浪:UID=10098
#除了今日头条外其他禁用(投屏设备无法使用没验证)
iptables -A OUTPUT -m owner ! --uid-owner 10096 -j DROP
ip6tables -A OUTPUT -m owner ! --uid-owner 10096 -j DROP
- 使能PID
iptables -A OUTPUT -m owner --pid-owner 78
测试如下:
queclink: PID=6627
iptables -A OUTPUT -m owner ! --pid-owner 6627 -j DROP
- 禁止VPN
iptables -t vpn -P FORWARD DROP
iptables -t vpn -P INPUT DROP
iptables -t vpn -P OUTPUT DROP
2413

被折叠的 条评论
为什么被折叠?



