DOH(DNS-over-HTTPs)服务器搭建

DOH搭建

1)搭建环境以及相关资料

  配置使用服务器或虚拟机都可以,系统镜像推荐使用Debian或Ubuntu。本次搭建使用的是Ubuntu18.04版本。

  • DOH服务器包的下载链接,后面会用到:
    https://pan.baidu.com/s/1au3-AbPOcMo6wqyyVqeZJg密码:fgnl
  • 本文主要是对着一篇英文教程写的,想直接看英文版的可以转到这里:
    https://www.aaflalo.me/2018/10/tutorial-setup-dns-over-https-server
  • DOH的使用方法可以参考:
    https://medium.com/@nykolas.z/troubleshooting-dns-over-https-c1e1009d3eb8
  • Github
    https://github.com/m13253/dns-over-https

2)DOH原理

  DOH全称为DNS-over-HTTPs,顾名思义,其主要目的是使用https协议来进行DNS请求。
  正常的DNS请求过程是通过计算机上的DNS客户端程序来帮助用户发起DNS请求,而不是通过浏览器本身发送,DNS使用的协议是UDP协议,UDP协议不具备很好的安全性,这样发起的DNS请求会遭到DNS劫持,攻击者会将用户想要访问的域名解析到别的IP地址上,因此DOH为了解决这个问题而出现。
  DOH是使用HTTPs协议发送dns请求,请求到达DOH服务器后,由DOH服务器解码HTTPS并发送DNS请求,DNS请求结果返回到DOH服务器上后,再由将其打包成HTTPS返回给客户端,这就保证了客户端发起的dns请求不会被攻击者拿到。
  下图就是本次要搭建DOH的工作流程图,其中我们配置的部分在图中为Nginx、DOH、dnscrypt三个部分。

client Nginx DOH dnscrypt 发送基于HTTPs的DNS请求 解码HTTPs并发DNS-over-HTTP请求 解码并发送DNS请求 DNS响应 发送包含DNS响应的HTTP响应 返回基于HTTPs的DNS响应 client Nginx DOH dnscrypt

3)具体搭建过程

(1)dnscrypt的安装配置

  dnscrypt是位于DOH服务器上的一个客户端,dnscrypt负责安全地转发DNS请求,安装方法很简单,具体命令如下:

sudo add-apt-repository ppa:shevchuk/dnscrypt-proxy
apt-get update
sudo apt install dnscrypt-proxy

  安装成功后/etc路径下会出现dnscrypt-proxy文件夹,dnscrypt-proxy的监听地址为:

127.0.0.53:53

这个监听地址可以通过dig命令来看一下,随便dig一个域名显示的本地服务器应该就是dnscrypt-proxy的监听地址。
  下一步修改/etc/dnscrypt-proxy/dnscrypt-proxy.toml文件的server_names字段,最开始这个字段里面应该是包含很多名字的,修改其内容如下:

server_names = ['cloudflare']

  最后重启dnscrypt-proxy服务:

sudo systemctl restart dnscrypt-proxy

(2)安装DNS-over-HTTPs服务器

  首先下载上面打包好的DOH服务器,这里我使用了上面提供的deb安装包,使用安装命令即可安装好:

sudo dpkg -i doh-server_*_amd64.deb

安装好的DOH服务器的配置文件会在路径/etc/dns-over-https/doh-server.conf,现在对其进行修改,主要修改的部分为,upstream字段,修改为上面提到的dnscrypt-proxy的监听地址,另外listen字段我注释掉了一行,因为这行不注释后面无法启动DOH服务,可以暂时不用注释,如果后面真的遇到了该问题再回来注释一下试试,修改如下:

# HTTP listen port
listen = [
    "127.0.0.1:8053",
    #"[::1]:8053",        //这里我把它注释掉了
]
# TLS certification file
# If left empty, plain-text HTTP will be used.
# You are recommended to leave empty and to use a server load balancer (e.g.
# Caddy, Nginx) and set up TLS there, because this program does not do OCSP
# Stapling, which is necessary for client bootstrapping in a network
# environment with completely no traditional DNS service.
cert = ""
# TLS private key file
key = ""
# HTTP path for resolve application
path = "/dns-query"
# Upstream DNS resolver
# If multiple servers are specified, a random one will be chosen each time.

upstream = [
    "127.0.0.53:53",   //dnscrypt-proxy的监听地址
]

# Upstream timeout
timeout = 60
# Number of tries if upstream DNS fails
tries = 10
# Only use TCP for DNS query
tcp_only = false
# Enable logging
verbose = false

  最后重启DOH服务:

sudo systemctl restart doh-server

(3)安装并配置Nginx

  接下来需要安装Nginx,Nginx在这里的实际作用是反向代理服务器,将它受到的HTTPS请求解码并发送到doh-server上。
  安装Nginx的步骤也十分简单,安装命令如下:

sudo add-apt-repository ppa:ondrej/nginx
apt-get uptdate
sudo apt install nginx-full

安装后/etc路径下会出现Nginx路径。接下来进行一些配置,首先在/etc/nginx/sites-available/路径下新建一个文件,取名为dns-over-https,将下面内容写入该文件中:

upstream dns-backend {
    server 127.0.0.1:8053;
}
server {
        listen 80;
        server_name dns.example.com;
        root /var/www/html/dns;
        access_log /var/log/nginx/dns.access.log;
         location /dns-query {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_redirect off;
                proxy_set_header        X-Forwarded-Proto $scheme;
                proxy_read_timeout 86400;
                proxy_pass http://dns-backend/dns-query ;
        }
}

其中dns-backend就是你要Nginx转发到的地址,这里就是我们的DOH的监听地址,另外server_name字段是你要给我们这台服务器的域名,没有的话就去注册一个吧,不然没法弄https。然后做一个符号链接,并重启Nginx,命令如下:

sudo ln -s /etc/nginx/sites-available/dns-over-https /etc/nginx/sites-enabled/dns-over-https
sudo nginx -t
sudo systemctl reload nginx

  接下来,在路径/etc/nginx/conf.d/下创建一个文件stapling.conf,让Nginx检查证书是否已过期,并将该信息保存在缓存中,这是为了避免对证书的证书颁发机构(CA)执行过多的请求。配置文件内容如下:

ssl_stapling on;
ssl_stapling_verify on;
resolver 127.0.0.53;

  安装certbot,并申请证书,安装certbot的命令如下:

sudo add-apt-repository ppa:certbot/certbot
sudo apt install python-certbot-nginx

Certbot拥有帮助Nginx配置证书的插件,因此申请证书的过程也非常简单,使用如下命令,这里的域名就是你申请的域名:

sudo certbot --nginx -d dns.example.com

如果申请成功则会出现如下内容:

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

我们选择2选项即可。
  最后我们配置一下/etc/letsencrypt/options-ssl-nginx.conf文件,将里面的内容替换为如下内容:

# This file contains important security parameters. If you modify this file
# manually, Certbot will be unable to automatically provide future security
# updates. Instead, Certbot will print and log an error message with a path to
# the up-to-date file that you will need to refer to when manually updating
# this file.
ssl_session_cache shared:le_nginx_SSL:1m;
ssl_session_timeout 1440m;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
# Enable modern TLS cipher suites
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
# The order of cipher suites matters
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

  重启Nginx:

sudo systemctl stop nginx
sudo systemctl start nginx

4)DOH的使用方法

  完成了上面的步骤后,DOH服务器就搭建好了,地址为:

https://dns.example.com/dns-query

那么这个东西怎么用呢,可以通过curl命令来用,如下:

curl "https://dns.example.com/dns-query?ct=application/dns-json&name=baidu.com&type=A"

你只需要替换其中的域名部分。
  还可以配置浏览器使用,以火狐浏览器为例,配置方法如下:

  1. 首先进入Firefox的about:config页面,在页面搜索框内搜索network.trr。
  2. 找到network.trr.mode,并将其值改为2,2表示使用DOH。
  3. 找到network.trr.uri,将其改为支持DOH的服务器,其默认值为:
https://mozilla.cloudflare-dns.com/dns-query

把它改成你搭建的就可以了。验证的话可以通过抓包来验证。

发布了32 篇原创文章 · 获赞 53 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_39378221/article/details/103245108