dnsmasq 是处理 dns 请求的工具,实现域名请求解析到目标 IP 地址的过程。可以方便的管理本机或局域网设备的域名解析服务,使用教程参考:https://blog.niekun.net/archives/1869.html

iptables 是网络防火墙规则管理/修改工具,管理网络数据包的处理和转发。使用教程参考:https://blog.niekun.net/archives/1863.html

iptables 管理某个源地址或目标地址的流量时,只能识别 IP 地址,比如:

# -s 匹配源地址流量,接收来自 192.168.1.230 发往本机的流量:
iptables -t filter -A INPUT -s 192.168.1.230 -j ACCEPT

# -d 匹配目标地址的流量,丢弃发往 192.168.1.123 的流量:
iptables -t filter -A OUTPUT -d 192.168.1.123 -j DROP

如果要使用 iptables 管理某域名的流量,可以使用 dnsmasq-full 里的 ipset 工具标记并保存域名包含的 IP 地址池,然后在 iptables 中调取。

使用命令查看当前安装版本的 dnsmasq 是否支持 ipset:

查询版本:

dnsmasq -v

信息里 Compile time options 可以看到当前安装版本支持的选项功能 ,如:ipset

root@OpenWrt:/etc# dnsmasq -v
Dnsmasq version 2.80  Copyright (c) 2000-2018 Simon Kelley
Compile time options: IPv6 GNU-getopt no-DBus no-i18n no-IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC no-ID loop-detect inotify dumpfile
...

如果没有 ipset 就需要手动安装 dnsmasq-full 版本了,升级方法参考:https://blog.niekun.net/archives/1869.html

域名解析标记

首先建立我们自定义的 ipset 表来存储解析的 IP 地址:

ipset create block hash:ip

可以查询当前这个列表是空的:

root@OpenWrt:~# ipset list block
Name: block
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 88
References: 0
Number of entries: 0
Members:

使用 dnsmasq 的 ipset 命令做域名解析结果标记和存储,在 dnsmasq 的配置文件,如:/etc/dnsmasq.conf 中加入如下语句:

ipset=/360.com/block

重启 ndsmasq 服务:

systemctl restart dnsmasq

使用 nslookup 命令对 360.com 域名进行 dns 查询:

root@OpenWrt:/etc# nslookup 360.com
Server:        127.0.0.1
Address:    127.0.0.1#53

Name:      360.com
Address 1: 180.163.251.93
Address 2: 36.110.213.45

这时候重新查看建立的 ipset 表内容:

root@OpenWrt:/etc# ipset list block
Name: block
Type: hash:ip
Revision: 4
Header: family inet hashsize 1024 maxelem 65536
Size in memory: 184
References: 0
Number of entries: 2
Members:
36.110.213.45
180.163.251.93

可以看到域名对应的 IP 地址已经记录到了 block 表内。

流量处理

iptables 可以调用 ipset 表来对流量进行处理,使用 -I 参数将规则添加到链表最上方以确保顺利匹配,使用 -m set --match-set 列表名 dst 匹配目标地址,或 -m set --match-set 列表名 src 匹配原地址,下面举例说明:

# 屏蔽 block 表中的地址对本机的访问
iptables -t filter -I INPUT -m set --match-set block src -j DROP

# 允许本机对 allow 表中的地址的访问
iptables -t filter -I OUTPUT -m set --match-set allow dst -j RETURN

iptables 设置完成后就可以测试对应域名是否受防火墙规则控制了。

查询路由表加入的规则:

iptables -t filter -L -n

如果要删除 INPUT 路由链第一条规则,使用命令:

iptables -t filter -D INPUT 1

或者直接删除指定规则:

iptables -t filter -D INPUT -m set --match-set block src -j DROP

删除一个 ipset:

ipset -X mylist

和 iptables 一样,ipset 的配置会在系统重启后失效。

以上就是使用 dnsmasq 的 ipset 功能实现 iptables 对域名流量的控制。

标签:无

1 条评论

  1. [...]iptables 的操作教程参考:https://blog.niekun.net/archives/1872.html需要 sftp 连接系统的话需要安装支持包:opkg install openssh-sftp-server[...]

你的评论