用 iptables 禁用 AAAA 查询

2015-12-11 12:37:33 +08:00
 datocp
OpenWrt VPN 按域名路由
https://blog.sorz.org/p/openwrt-outwall/

看到上面的文档,应该是很简单的能实现路由分流,可是花了一星期不得要领。昨天才注意到 vps 是 ipv4+ipv6 ,所以 nslookup 的结果包含了 AAAA 查询纪录,那么 youtube 的 ipset 结果都是基于 ipv4 结果而实际访问的是 ipv6 网站,当然怎么也无法按照文档实现。

Linux: Block DNS queries for specific zone with IPTables
https://www.perturb.org/display/1186_Linux_Block_DNS_queries_for_specific_zone_with_IPTables.htm

终于搜到一篇最简单的用 iptables 实现的禁止 AAAA 查询,这样就可以简单的用日本 vps vpn 分流 美国 vps 上的 youtube 资源,让访客判断 ip 为美国而不是日本。

就这么简单

iptables -A OUTPUT -o eth0 -p udp --dport 53 -m string --hex-string "|07|youtube|03|com|00001c|" --algo bm -j DROP

iptables -A OUTPUT -o eth0 -p udp --dport 53 -m string --hex-string "|05|ip138|03|com|00001c|" --algo bm -j DROP

Advanced:

Technically the query looks like X domain Y TLD 0 where the zero indicates that there are no more parts of the domain. This is immediately followed by two bytes indicating the type of query.

This allows you to get fancy and only block specific types of queries for a domain while allowing others:

Type Code
Any 00ff
A 0011
CNAME 0005
MX 000f
AAAA 001c
NS 0002
SOA 0006
7822 次点击
所在节点    DNS
11 条回复
Vicer
2015-12-12 11:21:43 +08:00
问一下,怎么确定 openconnect 怎么推送了路由表, iptables 该怎么用?
datocp
2015-12-12 12:20:45 +08:00
啊。这篇文档主要介绍的是如何用 iptables 屏蔽特定域名的 AAAA 查询,防止查询到 ipv6 地址,导致因为服务器不支持 ip6tables nat 只能用 ipv4 而导致的失败。想实现的效果是就近使用日本 vps ,又可以通过在 vps 实现 vpn 地址欺骗 youtbe 网站来访问有美国 ip 限制的资源。

iptables -I OUTPUT -o eth0 -p udp --dport 53 -m string --hex-string "|07|youtube|03|com|00001c|" --algo bm -j DROP

关于 ocserv 你可以参考下面的文档,不过路由表分流功能没研究过,一直习惯全局翻。 openconnect 不会玩在 windows 下面好像没效果。。。可以试试 anyconnect 客户端 http://dl.xxshe.com/cisco_anyconnect/,但是即便指定了 no route ,下面的规则在 windows 下面可以正常访问 lan ,在 android 下面使用 openconnect 确不可以访问 lan 。
no-route = 192.168.0.0/255.255.0.0
no-route = 10.0.0.0/255.0.0.0
no-route = 169.254.0.0/255.255.0.0
no-route = 172.16.0.0/255.240.0.0

做个笔记, Debian 上面装 ocserv ( AnyConnect 服务器端)
http://www.dreamsafari.info/2015/02/ocserv_on_debian/

Linux 策略路由与多 VPN 并存最佳实践
https://oogami.name/1496/
datocp
2015-12-12 12:38:50 +08:00
一般做法是

>nslookup www.ip138.com
服务器: UnKnown
Address: fc00:101:102::1

非权威应答:
名称: yd.ecoma.ourwebpic.com
Addresses: 218.92.221.155
218.92.221.153
218.92.221.152
218.92.221.157
Aliases: www.ip138.com
www.ip138.com.lxdns.com

然后修改 ocserv.conf ,通过浏览 www.ip138.com 看显示的 ip 添加之后的变化
no-route = 218.92.221.0/255.255.255.0
Vicer
2015-12-12 12:44:33 +08:00
Ok,谢谢了,我试一下
datocp
2015-12-12 13:13:08 +08:00
汗,刚刚验证了一下真正的地址是 http://1111.ip138.com/ic.asp

而且这个地址要在 vps 上验证才有效,这就造成结果可能有严重的差别,所以有时候出于安全原因还是建议使用全局路由。

# nslookup 1111.ip138.com
Server: 192.168.40.253
Address: 192.168.40.253#53

Non-authoritative answer:
Name: 1111.ip138.com
Address: 183.238.101.232

Vicer
2015-12-12 14:00:48 +08:00
我 VPS 上查询到的是跟你一样的地址,添加路由表后,连上 VPN ,还是显示的是我 VPS 的地址
datocp
2015-12-12 16:32:03 +08:00
天啊,这个问题验证得头都要大了,这篇文档提供了默认的 defaults 全局路由和 routed 白名单(route)。按照它建了个 norouted 黑名单 (no-route),情况是 windows anyconnect 支持 route 或者 no-route 语法, android 下的 openconnect anyconnect 只支持 route ,通过 android ip route 命令可以查询到路由配置项。没有 iphone 设备未知。仅 200 条的配置项想照顾各种情况还是比较困难的。

做个笔记, Debian 上面装 ocserv ( AnyConnect 服务器端)
http://www.dreamsafari.info/2015/02/ocserv_on_debian/

关于 no-route 解决 ip138.com
服务器完整的配置文件
no-route = 122.13.195.57/255.255.255.255
no-route = 183.238.101.232/255.255.255.255
no-route = 198.105.0.0/255.255.0.0
no-route = 219.128.0.0/255.255.0.0
no-route = 203.130.55.0/255.255.255.0
no-route = 219.128.78.0/255.255.255.0

dnsmasq.conf
address=/ip138.com/outwall

ipset -L #显然通过 nslookup 查询有很大的出入。
122.13.195.57
219.128.78.43
183.238.101.232
219.128.78.41
219.128.78.39
219.128.78.38
Vicer
2015-12-12 16:37:33 +08:00
超级感谢。只有铜币了
LGA1150
2016-01-10 12:21:53 +08:00
我发现了另一个方法:直接用 ip6tables 屏蔽对应的 IPv6 地址段:
ip6tables -I FORWARD -d 2607:f8b0::/32 -j REJECT --reject-with icmp6-adm-prohibited
然后客户端自动回落到 IPv4 ,同样可解决这个问题。
huaxie1988
2018-12-20 13:19:24 +08:00
在 unbound 的配置文件中添加 private-address: ::/0 即可屏蔽 AAAA 解析
jelly0925
2020-12-08 17:32:26 +08:00
学习到了,感谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/242793

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX