Skip to content

Installation on OpenWrt

translucentfocus edited this page Sep 7, 2021 · 26 revisions

Which method to use?

The basic opkg package installation is for those looking for a simple no-frills installation, where updates are at the discretion of the package maintainer. Success is dependent upon the updated state of the package, it may not work if it goes stale.

The advanced manual installation offers more flexibility, and is the preferred method for the more competent users.

Whichever you choose, the additional tweaks are highly recommended.

Package installation

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

Note: If you're using an OpenWrt version built before 23 February 2019, you need to update dnsmasq, since its option localuse was added on 23 Feb 2019:

opkg update; opkg upgrade 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, ensure that you're running version 2.0.36 or later with this line present (it is now 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 outlines some common workarounds but it is not possible to thwart every approach, like local DoH on port 443 or servers running on non-standard ports.

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 proto 'tcp udp'
    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 proto 'tcp udp'
    option dest_port '853'
    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 proto 'tcp udp'
    option src_dport '5353'
    option dest_port '53'
    option target 'DNAT'

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

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.

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.

DNSSEC resolver test determines whether your DNS resolver validates DNSSEC signatures.

You can also use this site to test both your IPV4 & IPV6 & DNSSEC settings: https://en.internet.nl/connection/

Clone this wiki locally