广告漏网之鱼的背后

一. 前言

img

在浏览知乎(ios17, 知乎app)的时候, 打开了一个搜狐页面, 诡异的一画出现, 搜狐这个页面居然充斥着广告. 这是难以理解的, 个人的网络一直处于相对可控的状态, 理论上这种明晃晃的广告是不可能出现在我的设备上. 特别是最近才刚刚梳理了一下本地的网络设备, 补充了一些广告拦截的内容, 搜狐这个广告也在整理的范围, 这就诡异了, 广告从何而来?

1.1 iStoreOS

先介绍一下环境, 软路由使用的是istoreos, 部署在hyperv虚拟机上, 相关介绍见: Hyper虚拟机折腾记录 | Lian

iStoreOS易有云网络科技开发的路由存储系统

iStoreOS开源免费的路由➕存储系统

轻松管理网络与存储, 享受一致操作体验. 代码开源, 欢迎探索与贡献.

1.2 Dnsmsaq

img

用于管理本地dns相关的工具是dnsmsaq, 这也是本次设置的核心组件.

Dnsmasq - network services for small networks.

Dnsmasq provides network infrastructure for small networks: DNS, DHCP, router advertisement and network boot. It is designed to be lightweight and have a small footprint, suitable for resource constrained routers and firewalls. It has also been widely used for tethering on smartphones and portable hotspots, and to support virtual networking in virtualisation frameworks. Supported platforms include Linux (with glibc and uclibc), Android, *BSD, and Mac OS X. Dnsmasq is included in most Linux distributions and the ports systems of FreeBSD, OpenBSD and NetBSD. Dnsmasq provides full IPv6 support.

dnsmasq详解&手册 - sunsky303 - 博客园

类似的工具还有:

使用dnsmasq主要用途主要是拦截广告.

二. 问题排查

事实上, 对于广告在手机端(ios)和电脑端(浏览器-模拟移动设别)上一直存在差异, 我一直以为这个问题, 主要出在部分网页加载的广告可能不会和真实的移动端完全一致.

img

如这种, 拦截了主要的广告内容, 但是广告位置依然还是出现.

这个问题一直存在, 但是由于基本就是拦截广告的主体部分, 不影响体验, 所以一直没有细究这种差异. 但这次知乎这个广告出现的实在是太诡异了, 属于完全没有拦截.

由于之前曾手滑, 重置过istoreos, 是不是这个原因导致了异常?

删掉iPhone上的WiFi, 清空了safari的缓存, 关掉广告过滤app, 重新连接测试, 结果还是出现广告.

同样的使用其他设备连接这个网络, 广告拦截是完全启用的, 就是这台iPhone存在异常...那问题何在?

img

设置dns重定向也无效果(该设置相当于强制客户端的dns解析需要经过dnsmsaq, 就算客户端单独设置了dns)

2.1 httpDNS

首先注意到的是httpdns

计算机网络 | 图解 DNS & HTTPDNS 原理-阿里云开发者社区

关于这个东西为什么出现可以部分见: DNS污染? SNI阻断? 全新网络协议保护隐私安全_哔哩哔哩_bilibili

至于httpdnsdns之间的差异, 这里不叙述, 简单来说, 就是和普通的web服务一样通过http/https协议返回查询的域名ip

img

这种模式, 对于依赖于本地 dns 解析来拦截广告的方式是个致命打击.

知乎app已经启用httpdns(应该说这是所有有点规模的app都在采用的模式, httpdns 进一步强化了这些大型企业对于app的绝对控制力)

img

103.41.167.237这个 ip 发出请求, 会返回一系列知乎相关的域名ip地址.

所以, 当开始排查的时候, 第一个怀疑的就是httpdns, 会不会知乎 app 的外链请求也是采用这种方式.

但很快这个想法被排除, 抓包并没有看到相关的内容, 同时在safari上, 搜狐的页面全部出现广告.

在safari上因为启用了广告拦截器, 难道是因为广告拦截器的问题?

下载了chrome, 同样问题依旧.

这, 难办了, 问题何在?

2.2 检索

既然就这台iPhone(ios17)出现问题, 那么很大可能和这台设备有关, 以此为关键词检索.

img

但令人遗憾的是, 中文互联网检索, 完全没有相关信息提及这个问题(考虑到玩软路由 + ios设备的群体, 这个问题居然没人提及...)

img

img

不管是喜欢抢东西的csdn还是外网搬运工的B站, 皆无相关信息.

2.3 问题的根源

img

相关问题, 多的是...可见中文互联网凋敝到什么程度了

img

[iOS描述文件] 自动配置阿里巴巴DNS over HTTPS加密查询服务器 – 蓝点网

苹果设备访问 V2EX 等网站的奇怪的问题 - V2EX

ios14 无法访问本地dns的自定义dns解析 type65 HTTPS记录 SVCB HTTPSSVC_dns type65-CSDN博客

iOS 屏蔽 type65 请求解决偶发性打不开部分网站 - IrineSistiana/mosdns - Discussion #294

DoH, DoT, DoQ 的区别 - DNS-WIKI

iOS14原生加密DNS方案_移动解析HTTPDNS_云解析DNS(DNS)-阿里云帮助中心

QType 65 in DNS refers to the HTTPS resource record type, a relatively new standard designed to convey information about HTTP protocol versions and security features supported by a server. This record type has been implemented by operating systems like Apple's iOS 14 and macOS 11.

Regarding how dnsmasq handles QType 65:

  • Lack of Native Support: dnsmasq does not have built-in, specific logic to interpret or generate QType 65 (HTTPS) records. It was not designed with this newer record type in mind.
  • Forwarding or Failure: When dnsmasq encounters a QType 65 query, it typically forwards it to its configured upstream DNS servers, as it cannot process it directly. In some cases, it might respond with an "Unknown" query type or a SERVFAIL status if it cannot resolve or handle the request.
  • No Direct Blocking Option: There are no specific configuration options within dnsmasq to directly block or manage QType 65 queries. Blocking these queries at the firewall level (e.g., using iptables or nftables) is a separate mechanism and not a feature of dnsmasq itself.
  • Potential Impact of Blocking: Blocking QType 65 queries is generally not recommended, as it can disrupt the functionality of modern operating systems and applications that rely on these records for service discovery and configuration.

In summary, dnsmasq treats QType 65 queries as unrecognized and typically passes them on to upstream DNS servers, as it lacks the functionality to process them directly.

2.4 小结

问题起源于, ios自版本14开始启用实验性的QType 65 请求, 这导致部分域名的 dns 解析并不会经过dnsmsaq, 所以, 以此为基础的广告过滤自然也就失效,

三. 解决

[Resolving querytype=65] to local address for iOS clients in dnsmasq - Installing and Using OpenWrt / Network and Wireless Configuration - OpenWrt Forum

iptables全面详解(图文并茂含命令指南)_iptables详解-CSDN博客

img

istoreos的防火墙中加入如下规则:

iptables -t raw -I OUTPUT -p udp --dport 53 -m comment --comment DNS_Type_65 -m string --hex-string "|0000410001|" --algo bm --to 65535 --icase -j DROP

iptables -t raw -I OUTPUT -p tcp --dport 53 -m comment --comment DNS_Type_65 -m string --hex-string "|0000410001|" --algo bm --to 65535 --icase -j DROP

防火墙将对QType 65请求作drop的处理.

但可能会导致部分异常:

QType 65 (HTTPS) 的封锁导致苹果设备 App 和网页访问异常缓慢 - V2EX

实际上, 就算是阻止了上述的请求, 但是在 ios 端上的网络依然还是存在一些问题, 例如

iptables -I FORWARD -d 103.41.167.237 -j DROP

# iptables -L FORWARD --line-numbers 查看是否成功添加

假如将iPhone 的代理设置到主机上, 使用fiddler抓包, 那么就会导致知乎app出现大面积异常, 无法连接网络, 但是直接使用就不会出现这个问题.

四. 总结

在互联网厂商日益"关爱"用户的当下, 留给用户折腾的空间也将越来越小, 不管是 Google 称广告过滤扩展(规则太多)导致chrome性能无法释放, 还是各种app都在换装httpdns, 为了更好更快地服务用户, 亦或者曾经风靡一时的Android root/刷第三方os已经被全面封锁, 厂商们都更希望用户呆在"安全可控"的环境下使用它们提供的产品服务.

相对于解析http/https流量拦截广告, dns解析层拦截, 不仅速度快, 效果好, 副作用小.