Total Pageviews

Thursday 12 April 2012

利用本地机器上所安装的DNSMASQ解决dns污染问题

 from http://bullshitlie.blogspot.com/2012/03/dnsmasq-dns.html

至于DNS在瓷器国被污染的这回事,不用多解释了,正确的DNS解析对于使用squid透明代理进行翻墙极为重要,因为在squid在收到http请求前,域名已经在客户端先解析了

以下说明如何获得正确的dns解析。

首先,你必须要有一台境外服务器,笔者用的是一台境外VPS。

在这台VPS上,安装bind。 (当然dnsmasq也可以。)

配置 /etc/named.conf options部分
options {
        listen-on port 3722 { any; };  // 要点,必须绑定非标准端口(non 53 UDP)
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { any; };
        recursion yes;

        forward only;
        forwarders { 208.67.222.222; 208.67.220.220; }; // 这里选取VPS访问最快的上游DNS服务器,笔者选的是OpenDNS,毫秒级。
        max-cache-size 32M;
        cleaning-interval 480;
        lame-ttl 1800;
        max-ncache-ttl 166000;
        max-cache-ttl 86400;
        edns-udp-size 512;
        max-udp-size 512;
};


在网关上或本地机器上,安装dnsmasq,配置/usr/local/etc/dnsmasq.conf
--server=VPS的IP#3722  // 配置访问非标准端口3722!
--bogus-priv
--domain-needed

或者不调用配置文件,直接加参数运行亦可:
sudo dnsmasq --listen-address=127.0.0.1 --port=53 --server=vps-ip#bind-port-number-on-vps
好了,现在你的dnsmasq应该能解析出正确的dns了。

GFW对DNS只做了UDP 53端口的干扰。

使用例子:
sudo wg-quick up wg0

sudo networksetup -setdnsservers "Wi-Fi" 127.0.0.1

sudo dnsmasq --listen-address=127.0.0.1 --port=53 --server=vps-ip#bind-port-number-on-vps

如果你的本地机器是mac,不必往下看了。

(最后,以彼之道,施彼之身。利用iptables劫持局域网用户的DNS访问,以保证解析到正确的IP。

iptables -t nat -A PREROUTING -p udp -m udp --dport 53 -j REDIRECT --to-ports 53 )

用dig 实验:

第一次,用google dns解析,得出被污染的解析记录

第二次,用DNSMASQ解析,得出正确的解析记录



相关帖子:
http://briteming.blogspot.com/2012/02/dnsmasq.html
http://briteming.blogspot.com/2012/09/dnsmasquedns.html
http://briteming.blogspot.com/2014/03/dnsmasq-china-list.html
http://briteming.blogspot.com/2016/03/dnsmasq.html

https://wiki.archlinux.org/index.php/Dnsmasq_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 
------------------------------

利用dnsmasq搭建DNS服务器简易教程


Assume that you use Ubuntu. First download&install dnsmasq:
apt-get install dnsmasq

Then modify the config file: /etc/dnsmasq.conf, add the following lines (Assume that you use port 53 for DNS service):   
port=53
server=208.67.222.222#5353
...


Add firewall rule if necessary (Assume that you use UFW):
 ufw allow 53

Finally, enable&run:   
service dnsmasq start
--------------------------

用dnsmasq做dns代理缓存服务器

一般说到dns,都会想到bind,bind虽然强大,但太复杂了,配置起来很头疼。用牛刀杀鸡倒不是鸡不死,而是怕那刀砍伤脚。dnsmasq这个小程序,使用上比dns始祖bind要简便得多,可以做正向dns代理,也可以直接做内部纯dns服务器用。 

在debian/ubuntu下安装 
apt-get install dnsmasq

配置 
vi /etc/dnsmasq.conf 
默认配置下,dnsmasq使用系统的/etc/resolv.conf并读取/etc/hosts,在配置里可以更改或者关闭,现在是修改了这两个, 
其它的按默认: 
resolv-file=/etc/dnsmasq.resolv.conf 
addn-hosts=/etc/dnsmasq.hosts 

(dnsmasq还支持dhcp服务,但一般不用搭理) 

dnsmasq可以用hosts文件来设置域名:
例:test.sudone.com是不存在的域名,我在dnsmasq中指向到一个ip里:
echo "64.233.189.99 test.sudone.com" > /etc/dnsmasq.hosts 
改完要重启 
/etc/init.d/dnsmasq restart 


做完之后,只要把本机dns指到dnsmasq的机器,就可以ping到test.sudone.com这个域名.