V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
suspended
V2EX  ›  DNS

写了一个 CDN 友好+反污染的 DNS 服务器

  •  3
     
  •   suspended · 2020-03-03 09:03:02 +08:00 · 11356 次点击
    这是一个创建于 1477 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Purified DNS

    https://github.com/arloan/prdns

    欢迎尝试。

    第 1 条附言  ·  2020-03-03 11:13:39 +08:00

    演示:

    bogon: ~/projects/prdns ]
    suspended$ dig @localhost facebook.com
    
    ; <<>> DiG 9.8.3-P1 <<>> @localhost facebook.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8442
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
    
    ;; QUESTION SECTION:
    ;facebook.com.			IN	A
    
    ;; ANSWER SECTION:
    facebook.com.		56	IN	A	157.240.11.35
    
    ;; Query time: 180 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Tue Mar  3 11:10:25 2020
    ;; MSG SIZE  rcvd: 46
    
    bogon: ~/projects/prdns ]
    suspended$ dig @localhost taobao.com
    
    ; <<>> DiG 9.8.3-P1 <<>> @localhost taobao.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19749
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0
    
    ;; QUESTION SECTION:
    ;taobao.com.			IN	A
    
    ;; ANSWER SECTION:
    taobao.com.		300	IN	A	140.205.220.96
    taobao.com.		300	IN	A	140.205.94.189
    
    ;; Query time: 46 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Tue Mar  3 11:10:29 2020
    ;; MSG SIZE  rcvd: 60
    
    bogon: ~/projects/prdns ]
    suspended$ 
    
    第 2 条附言  ·  2020-03-04 09:56:57 +08:00
    再贴个附言吧。
    1. Purified DNS 不依赖 GeoIP 库;
    2. 原理 13 楼有小伙伴大致解释了一下(细节有出入),详细解释原理请阅读代码(代码真的很短,Ruby 比一般语言更接近自然语言一些,应该很好懂)。
    3. 目前发现了一些缺陷如下:
    3.1) cname 到一个污染域名的话无法探测到被污染
    3.2) 只污染特定主机名而非污染整个域的情况无法探测,类似的,只污染特定子域同理。
    54 条回复    2020-03-06 12:22:36 +08:00
    scys
        1
    scys  
       2020-03-03 10:06:23 +08:00
    看起来听不多,不过 Ruby 暂时就观看态度
    aoerboquartz
        2
    aoerboquartz  
       2020-03-03 10:09:09 +08:00
    你这个有点意思,如果有演示就更好了。
    scys
        3
    scys  
       2020-03-03 10:12:11 +08:00
    @scys #1 听来不错,暂时观看模式
    suspended
        4
    suspended  
    OP
       2020-03-03 11:11:44 +08:00
    @scys
    @aoerboquartz

    演示来喽~

    bogon: ~/projects/prdns ]
    suspended$ dig @localhost facebook.com

    ; <<>> DiG 9.8.3-P1 <<>> @localhost facebook.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8442
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

    ;; QUESTION SECTION:
    ;facebook.com. IN A

    ;; ANSWER SECTION:
    facebook.com. 56 IN A 157.240.11.35

    ;; Query time: 180 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Tue Mar 3 11:10:25 2020
    ;; MSG SIZE rcvd: 46

    suspended: ~/projects/prdns ]
    stone$ dig @localhost taobao.com

    ; <<>> DiG 9.8.3-P1 <<>> @localhost taobao.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 19749
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

    ;; QUESTION SECTION:
    ;taobao.com. IN A

    ;; ANSWER SECTION:
    taobao.com. 300 IN A 140.205.220.96
    taobao.com. 300 IN A 140.205.94.189

    ;; Query time: 46 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Tue Mar 3 11:10:29 2020
    ;; MSG SIZE rcvd: 60

    bogon: ~/projects/prdns ]
    stone$
    xishijt
        5
    xishijt  
       2020-03-03 11:18:34 +08:00
    一脸期待的进来,一脸懵逼的出去,不知道怎么用
    reechangs
        6
    reechangs  
       2020-03-03 11:20:25 +08:00
    @xishijt #5 mac 下比较好用,别的平台麻烦一点
    suspended
        7
    suspended  
    OP
       2020-03-03 11:38:07 +08:00
    @reechangs Windows 可能麻烦点,因为没有自带 ruby。linux 自带 ruby 的还好吧?只需要 gem install prdns 就安装好了啊。
    Archeb
        8
    Archeb  
       2020-03-03 11:56:21 +08:00
    之前好像有人做过类似的

    https://github.com/ExcitedCodes/SmartChinaDNS
    suspended
        9
    suspended  
    OP
       2020-03-03 12:41:42 +08:00
    @Archeb 所有市面上现有的反污染机制都不如我的这个噢,我的这个不需要什么 GFWList,GEOIP 啥的数据库。
    demonzoo
        10
    demonzoo  
       2020-03-03 13:21:57 +08:00
    貌似有前途,先马
    moxnet
        11
    moxnet  
       2020-03-03 13:28:49 +08:00
    如果继续走 53 的 udp,很容易被识别,流量多了也是难免的。

    要不被卡,还得用 DoT 或 DoH 啊。
    richard1122
        12
    richard1122  
       2020-03-03 14:18:32 +08:00
    简单看了下代码不太理解为什么这样就是污染的域名?。。
    XiaoxiaoPu
        13
    XiaoxiaoPu  
       2020-03-03 14:28:39 +08:00   ❤️ 3
    @richard1122 构造一个不存在的子域名 ne-{timestamp}.example.com ,正常情况下(域名未被污染)查询结果应该不存在解析,如果被污染了,会被 GFW 抢答返回包含 A 记录的回应,查询结果就是有解析
    richard1122
        14
    richard1122  
       2020-03-03 14:30:20 +08:00
    @XiaoxiaoPu #13 明白了,竟然这么酷
    suspended
        15
    suspended  
    OP
       2020-03-03 14:35:54 +08:00
    @XiaoxiaoPu 嘻嘻,花了我一个小时研究出来的机制,可惜不能申请专利。:D
    tpsxiong
        16
    tpsxiong  
       2020-03-03 14:46:14 +08:00
    @XiaoxiaoPu 如果只污染部分子域名的,怎么处理?
    XiaoxiaoPu
        17
    XiaoxiaoPu  
       2020-03-03 14:50:55 +08:00
    @tpsxiong 目前来看 dns 污染应该是按后缀匹配的,不管前面的前缀。你说的只污染部分子域名,有具体的例子吗?
    Archeb
        18
    Archeb  
       2020-03-03 14:52:23 +08:00
    @suspended 我发的那个也不需要,各种 LIST 似乎只有加速作用。
    suspended
        19
    suspended  
    OP
       2020-03-03 15:03:35 +08:00
    @Archeb 我没有去看代码,但是如果是从 ChinaDNS 派生的,应该都是同时查国内 /国外的 DNS,先返回的如果是国内 IP 则取信,否则取国外 DNS 的结果,因此是需要判定是否为国内 IP——这个就需要数据库。当然,在 ipv4 地址因为差不多耗尽的现状下,ip 数据库已经变动很少的话,也是可以用的。

    我的机制则完全不需要 ip 库,不需要维护,可靠程度极高,甚至可以说不存在判定错误。
    Archeb
        20
    Archeb  
       2020-03-03 15:05:09 +08:00
    @suspended 我看了代码,思路和 chinadns 不是完全相同,不过确实依赖国内 IP 数据库。
    你的实现方法确实不错的。
    tpsxiong
        21
    tpsxiong  
       2020-03-03 15:24:29 +08:00
    reechangs
        22
    reechangs  
       2020-03-03 15:29:29 +08:00
    @suspended #19 方法的确是好方法,但是这样查询速度会不会更慢?
    gainsurier
        23
    gainsurier  
       2020-03-03 15:31:17 +08:00 via iPhone
    楼主注意保护好自己的隐私,有 wj,dns server 非常敏感。
    suspended
        24
    suspended  
    OP
       2020-03-03 15:35:13 +08:00
    @reechangs 不会。对每个顶级域名,只有第一次会多查一次 DNS 记录,后续就已经知道这个域名是否是污染的,不会再去检测是否污染,会直接查国内 DNS/可信 DNS。
    suspended
        25
    suspended  
    OP
       2020-03-03 15:36:23 +08:00
    @gainsurier 多谢提醒。不过没关系,最多也就是让我关掉这个 repo 而已。:D
    yulihao
        26
    yulihao  
       2020-03-03 16:01:52 +08:00
    是不是还要安装 net-dns ?
    C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
    : cannot load such file -- async/dns (LoadError)
    yulihao
        27
    yulihao  
       2020-03-03 16:05:19 +08:00
    @yulihao 装了也不行,我不懂 ruby 额
    hhhsuan
        28
    hhhsuan  
       2020-03-03 16:11:13 +08:00
    所以具体是什么机制? 能讲一下吗?
    suspended
        29
    suspended  
    OP
       2020-03-03 17:28:13 +08:00
    @yulihao 倒是没有在 Windows 上测试过,不过 async/dns 在 prdns 的依赖项里,按说 gem install prdns 会自动把 async/dns 装上才对。试试再装一下 async/dns 吧:`gem install async-dns`
    suspended
        30
    suspended  
    OP
       2020-03-03 17:29:39 +08:00
    @hhhsuan 13 楼有小伙伴解释了大致原理,更详细的原理可以直接看代码。
    bonny23
        31
    bonny23  
       2020-03-03 17:49:01 +08:00
    lz 的方案的确挺创新的,赞一个
    hhhsuan
        32
    hhhsuan  
       2020-03-03 18:24:16 +08:00
    那些有泛解析的域名怎么办? 所有的二级域名都能解析到一个地址。
    suspended
        33
    suspended  
    OP
       2020-03-03 18:26:14 +08:00
    @hhhsuan 泛解析也是支持的:
    ```shell
    suspended$ dig @localhost anything-is-resolved.bing.com

    ; <<>> DiG 9.8.3-P1 <<>> @localhost anything-is-resolved.bing.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 39738
    ;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 0

    ;; QUESTION SECTION:
    ;anything-is-resolved.bing.com. IN A

    ;; ANSWER SECTION:
    anything-is-resolved.bing.com. 3600 IN CNAME star-bing-com.a-0001.a-msedge.net.
    star-bing-com.a-0001.a-msedge.net. 60 IN CNAME dual-a-0001.a-msedge.net.
    dual-a-0001.a-msedge.net. 49 IN A 13.107.21.200
    dual-a-0001.a-msedge.net. 49 IN A 204.79.197.200

    ;; Query time: 43 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Tue Mar 3 18:25:13 2020
    ;; MSG SIZE rcvd: 152

    ```
    suspended
        34
    suspended  
    OP
       2020-03-03 18:26:52 +08:00
    @suspended 晕,v2ex 回复不支持 markdown 的吗?也不能修改……
    Yourshell
        35
    Yourshell  
       2020-03-03 18:28:45 +08:00
    污染不了就会被墙掉
    hhhsuan
        36
    hhhsuan  
       2020-03-03 18:33:09 +08:00
    @suspended #33 那怎么知道是正常的解析还是被墙了?
    yulihao
        37
    yulihao  
       2020-03-03 20:40:39 +08:00
    @hhhsuan 我不知道我的理解对不对,我的理解是 GFW 会针对如*.google.com ,只要是 google.com 他都返回一个值,因此随机修改*的字符串看看 GFW 有没有返回结果,若有则为污染
    ZeroSimple
        38
    ZeroSimple  
       2020-03-03 20:44:19 +08:00 via Android
    支持一下。
    不过小心搭,搭 DNS 似乎是违法的,会被查水表。之前有 V 友被查了。不过如果你已经肉身 fq 了当我没说
    yulihao
        39
    yulihao  
       2020-03-03 20:46:03 +08:00
    我来提交新 BUG ( windows 下的)
    若输入的域名不存在或者查询超时,这边就会发生错误
    | Errno::ECONNRESET: An existing connection was forcibly closed
    by the remote host. - recvmsg(2)
    | → C:/Ruby27-x64/lib/ruby/2.7.0/socket.rb 444
    | C:/Ruby27-x64/lib/ruby/2.7.0/socket.rb 444
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-io-1.27.3/lib/
    async/io/generic.rb 216
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-io-1.27.3/lib/
    async/io/generic.rb 62
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-dns-1.2.5/lib/
    async/dns/handler.rb 73
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-dns-1.2.5/lib/
    async/dns/server.rb 114
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-io-1.27.3/lib/
    async/io/socket.rb 169
    | C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/async-1.24.2/lib/asy
    nc/task.rb 258
    5m28s warn: <> UDP response failed: #<IOError: closed stream>! [pid=13688]
    [2020-03-03 20:39:44 +0800]

    一会儿同时提交到你的 repo 那里,感谢有这样一种工具,希望能有效解决移动 dns 劫持
    crella
        40
    crella  
       2020-03-03 20:49:22 +08:00
    虽然楼主的本意是好,但是为啥要把这个项目放上 rubygems 上面去?万一哪一天 gems.ruby-china.com 被封了怎么办
    bitdepth
        41
    bitdepth  
       2020-03-03 20:49:50 +08:00 via iPad
    判斷 root server 進行查詢?然後全場才有 dnssec ?
    有空我看看這邊機制,如果方案好可以寫論文
    suspended
        42
    suspended  
    OP
       2020-03-03 21:02:13 +08:00
    @bitdepth 那我得是第二作者?:D
    suspended
        43
    suspended  
    OP
       2020-03-03 21:04:23 +08:00
    @yulihao 我的 macOS 上无法复现,回头找个 Windows 看看。
    suspended
        44
    suspended  
    OP
       2020-03-03 21:04:51 +08:00
    @ZeroSimple 怕怕。。。不过我不提供 DNS 服务啊。
    yankebupt
        45
    yankebupt  
       2020-03-03 21:05:23 +08:00   ❤️ 1
    @XiaoxiaoPu
    有的域名走 cdn 會出百度加速驗證碼...其實這個還好
    有的域名會自己主動把 CDN 解析部分用 cloudflare 屏蔽掉,比如 kknews.cc

    所以最後是不是還是得保留一個哪怕很小的規則文件呢.
    bitdepth
        46
    bitdepth  
       2020-03-03 23:01:02 +08:00 via iPad
    @suspended 我是建議您可以如此考慮,例如 tor 的 obfuscation 的幾個方案都有論文
    要是我去寫論文,基本上是會拿來批判用的,因為不是自己作品
    p0h5
        47
    p0h5  
       2020-03-04 00:48:58 +08:00 via iPhone
    期待 Mac 做个 brew 一键安装
    szpunk
        48
    szpunk  
       2020-03-04 02:28:23 +08:00
    这个我记得 @XiaoxiaoPu 早利用这个做过了,电信可以,移动无效。

    https://github.com/puxxustc/sans
    sola97
        49
    sola97  
       2020-03-04 02:45:58 +08:00
    直接装的是 ruby2.3.3。。还要升级 2.4,真麻烦。。
    pythonee
        50
    pythonee  
       2020-03-04 09:16:20 +08:00
    原理是什么呢?
    hhhsuan
        51
    hhhsuan  
       2020-03-04 10:37:25 +08:00
    @yulihao #37 这个我知道,但我的疑问是怎么处理那些加入了泛解析的域名呢?就是不管你的二级域名写什么都会有记录返回的域名。这些域名你怎么分辨它是正常的还是被污染了?
    suspended
        52
    suspended  
    OP
       2020-03-04 10:47:02 +08:00
    @hhhsuan 我测试了好些个泛域名,通常都会返回 CNAME,所以可以判定。当然如果泛域名只返回个 A 记录那就会判错。
    hgg12580
        53
    hgg12580  
       2020-03-04 15:58:37 +08:00
    @XiaoxiaoPu 如果域名有泛解析呢?
    marquina
        54
    marquina  
       2020-03-06 12:22:36 +08:00
    @suspended @XiaoxiaoPu 感谢提供思路和原理解惑。自己撸了一个 GO 版本的,支持简单的自定义规则、域名污染结果缓存(支持 redis )。
    地址: https://github.com/wolf-joe/dns-splitter,欢迎尝试
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5955 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 02:27 · PVG 10:27 · LAX 19:27 · JFK 22:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.