Skip to content

Installation on OpenWrt

hugepants edited this page Jan 2, 2023 · 26 revisions

Package installation

The following instructions are for installing from the official OpenWrt repository but note that updates are at the discretion of the package maintainer so the version may not always be up to date.

Using the LuCI web interface

System > Software > Update lists...

Type dnscrypt-proxy2 in the Filter box then Install the desired package

Add 127.0.0.53 to Network > DHCP and DNS > General Settings > DNS forwardings

Save & Apply

Restart dnsmasq in System > Startup

Using the command line

opkg update
opkg install dnscrypt-proxy2
uci add_list dhcp.@dnsmasq[0].server='127.0.0.53'
uci commit dhcp
/etc/init.d/dnsmasq restart

Edit configuration

Use ssh or scp to edit the configuration file at /etc/dnscrypt-proxy2/dnscrypt-proxy.toml e.g:

vi /etc/dnscrypt-proxy2/dnscrypt-proxy.toml
/etc/init.d/dnscrypt-proxy restart

Backups

In a package installation, the configuration files are located in /etc/dnscrypt-proxy2/. To ensure all files here are backed up, in LuCI select System > Backup / Flash Firmware, click the Configuration tab and add the following line:

/etc/dnscrypt-proxy2/

Then click Save. Alternatively, you can add the line to /etc/sysupgrade.conf directly.

Recommended tweaks

Prevent DNS leaks outside of dnscrypt-proxy and disable dnsmasq cache

Warning: Before attempting the following, ensure that you have dnscrypt-proxy running and resolving DNS queries correctly because the following settings will disable the ISP's DNS.

Edit /etc/config/dhcp:

config dnsmasq
    # Ignore ISP's DNS by not reading upstream servers from /etc/resolv.conf
    option noresolv '1'
    # Ensures that /etc/resolv.conf directs local system processes to use dnsmasq and hence dnscrypt-proxy
    option localuse '1'
    # Disable because dnscrypt-proxy's block_undelegated already blocks RFC 1918 private addresses and RFC 6761 top level domains
    option boguspriv '0'
    # Disable dnsmasq cache because we don't want to cache twice and the dnscrypt-proxy cache is superior
    option cachesize '0'

Restart dnsmasq to switch to the new configuration and check for any errors reported:

/etc/init.d/dnsmasq restart
logread -l 100 | grep dnsmasq

Optional: Completely disable ISP's DNS servers

For the perfectionists, add this option to /etc/config/network to prevent the ISP's DNS servers from being used anywhere:

config interface 'wan'    # or 'wan6'
    option peerdns '0'

Prevent DNS queries to other local zones

In order to prevent leakage of queries from these local zones (such as 168.192.in-addr.arpa) to upstream resolvers, this setting should already be enabled by default in dnscrypt-proxy.toml:

block_undelegated = true

Force LAN clients to send DNS queries to dnscrypt-proxy

By default most clients will use the DNS server assigned by DHCP which is the device running dnscrypt-proxy. However there are many ways a client can bypass this with an alternative DNS server. The following firewall rules addresses some common methods.

Add the following rules into /etc/config/firewall:

# Redirect unencrypted DNS queries to dnscrypt-proxy
# This will thwart manual DNS client settings and hardcoded DNS servers like in Google devices
config redirect
    option name 'Divert-DNS, port 53'
    option src 'lan'
    option dest 'lan'
    option src_dport '53'
    option dest_port '53'
    option target 'DNAT'

# Block DNS-over-TLS over port 853
# Assuming you're not actually running a DoT stub resolver
config rule
    option name 'Reject-DoT, port 853'
    option src 'lan'
    option dest 'wan'
    option dest_port '853'
    option proto 'tcp'
    option target 'REJECT'

# Optional: Redirect queries for DNS servers running on non-standard ports. Can repeat for 9953, 1512, 54. Check https://github.com/parrotgeek1/ProxyDNS for examples.
# Warning: can break stuff, don't use this one if you run an mDNS server
config redirect
    option name 'Divert-DNS, port 5353'
    option src 'lan'
    option dest 'lan'
    option src_dport '5353'
    option dest_port '53'
    option target 'DNAT'

And reload Firewall: /etc/init.d/firewall reload

Optional: Block LAN clients from using DoH servers directly

For advanced users, you can block access to IP addresses of known DoH servers with the package banIP. It has a built-in DoH blocklist or you can use the larger list by jpgpi250. Note that you must specify the DoH server(s) that you actually use in its allowlist otherwise dnscrypt-proxy will not be able to use DoH itself.

Optional: Use dnscrypt-proxy for DNS rebinding protection

dnsmasq can do DNS rebinding protection, but it can be useful to perform this in dnscrypt-proxy instead. For example, if you use a filtered DNS service like Cloudflare Security or AdGuard, responses from blocked domains are 0.0.0.0 which causes dnsmasq to fill the system log with possible DNS-rebind attack detected messages. Using dnscrypt-proxy keeps the system log clean and can still optionally log to a separate file (e.g. ip-blocked.log). Steps as follows:

  1. Enable DNS rebinding protection in dnscrypt-proxy then restart dnscrypt-proxy
  2. Disable DNS rebinding protection in dnsmasq by setting option rebind_protection '0' in /etc/config/dhcp then restart dnsmasq

Logging DNS queries with client IPs

dnscrypt-proxy can log queries, but in this OpenWrt configuration it cannot log the requesting client IP address because dnsmasq is forwarding the request to dnscrypt-proxy. It can be helpful to know the requesting client IP in order to identify which device is making a particular DNS query. This can be achieved by enabling query logging in dnsmasq via /etc/config/dhcp:

config dnsmasq
    # equivalent to --log-queries=extra in dnsmasq
    option logqueries '1'
    # where to write log file to
    option logfacility '/tmp/dnsmasq_queries.log'
/etc/init.d/dnsmasq restart

Note: These logs accumulate quickly so it is recommended to write to an external share or storage device as embedded devices have limited flash write cycles and storage space.

Verifying the configuration

Check that you are not using your ISP resolver any more:

openwrt > dnscrypt-proxy -resolve google.com

Resolver IP should not belong to your ISP. You can verify whose network an IP address is on IPtoASN.

This will also detect if the server supports DNSSEC, print its real IP address, and check if it returns fake responses for nonexistent domains.

Check that processes on the router use dnsmasq:

openwrt > cat /etc/resolv.conf
search lan
nameserver 127.0.0.1

The above value for nameserver must be 127.0.0.1, which is the dnsmasq server.

Note: These entries might only show when dhcp option localuse is enabled as instructed above.

3rd party DNS tests

The DNS leak test and DNS randomness test show the actual IP of your DNS server(s).

You should make sure that any reported name or IP is NOT associated with the ISP you are using.

If you see the ISP presence in the test results, something is wrong with the configuration, since it's bypassing your dnscrypt-proxy provider.

The following site can check if the resolver can connect to upstream servers using IPv6 IPv6 and does DNSSEC validation: https://en.internet.nl/connection/

Cloudflare also has a web page to check that you're using their resolvers: https://www.cloudflare.com/ssl/encrypted-sni/

Clone this wiki locally