Skip to content

adblock — DNS filtering ​

Blocking ads and trackers at the DNS level, as in AdGuard Home: block lists and allow lists in AdGuard, hosts or plain domain list format, your own rules, per-client settings, statistics and a query log. It works on clients' queries to the built-in DNS (dns.listen or hijack-dns in routing); the proxy's own queries (server names, resolve) are not filtered.

Without the section the module does nothing and costs nothing. The section is applied in place: a reload with a changed adblock rebuilds neither DNS nor its cache and upstreams, and does not touch connections.

yaml
adblock:
  enabled: true
  block_response: null_ip        # null_ip | nxdomain | refused | {ip: [0.0.0.0, "::"]}
  blocked_ttl: 60s
  check_answers: true            # check the CNAME chain and the addresses in the answer
  enforce_on_route: false        # also refuse connections to blocked domains
  lists:                         # block lists
    adguard:
      url: https://adguardteam.github.io/HostlistsRegistry/assets/filter_1.txt
      update: 12h
      via: direct                # the outbound to download through
    oisd: https://big.oisd.nl    # short form: a URL, updated daily, directly
    local:
      path: /etc/ours/block.txt
  allow_lists:                   # allow lists
    fp:
      url: https://example.org/whitelist.txt
      update: 24h
  rules:                         # your own, AdGuard syntax
    - "||mc.yandex.ru^"
    - "@@||icloud.com^"
  clients:
    tv:
      match: {source: 192.168.1.41}
      lists: [adguard, oisd]
    trusted:
      match: {user: alice}
      enabled: false
  stats:
    retention: 7d
    path: /var/lib/ours/adblock
  log:
    size: 10000
    file: false
    anonymize_client: false

Fields ​

FieldDefaultMeaning
enabledtrueFilter. false keeps the lists compiled, so turning it on is instant
block_responsenull_ipThe answer to a blocked query: 0.0.0.0/:: (an empty answer for other types), nxdomain, refused or your own addresses {ip: [...]}
blocked_ttl10sThe TTL of the answer to a blocked query and of $dnsrewrite answers
check_answerstrueCheck the upstream's answer: CNAME targets and A/AAAA addresses. A match blocks the query ("blocked by CNAME")
enforce_on_routefalseRefuse connections whose domain (from sniffing or the destination address) is blocked. The check is after the route rules, only for connections that went to an outbound
lists—Block lists by name
allow_lists—Allow lists: each of their rules is an exception (@@)
rules—Your own rules in AdGuard syntax; in the dashboard, Custom rules
clients—Per-client settings
stats—retention — how long to keep statistics (from 1h to 90d, 24h by default); path — the module's data directory: statistics and compiled lists
log—The query log: size — how many latest queries to keep in memory (1000 by default); file — true (the file querylog.jsonl in stats.path), a path or false; anonymize_client — zero the tail of the client's address (the last byte of IPv4, everything after /48 of IPv6) in the log and statistics

Durations: 30s, 10m, 12h, 7d or a number of seconds.

Lists ​

A list is a url (with update, 24h by default, and via, the outbound or group to download through; without via, directly) or a path (the file is reread when it changes). A string instead of a block is a URL. enabled: false in a list's block turns it off: the list keeps updating and stays compiled but does not filter, so turning it on is instant. In the dashboard this is a switch in the list's row (an edit of the config draft, like the other buttons of the section).

The format is detected by itself, line by line:

  • AdGuard/ABP DNS syntax: ||example.org^, @@, the modifiers below;
  • hosts: 0.0.0.0 example.org blocks the name exactly, without subdomains; another address is given as the answer to A/AAAA queries for that name;
  • a domain list: example.org blocks the name exactly, without subdomains (as AdGuard Home does);
  • a sing-box binary rule-set (.srs): domains, suffixes, AdGuard rules and regular expressions are taken from it, other conditions make no sense for a name and are skipped.

Comments (!, #) and cosmetic rules of browser lists (##, #@#, …) are skipped silently. A rule with a modifier that is not in the list below is skipped whole with a warning: the name of the list, the line number, the reason (visible in the dashboard and in the log at the debug level).

A downloaded list is compiled in the background and replaces the previous one when ready; until then the previous version filters. A compiled list is kept in memory by source and content: two lists with one URL are compiled once, turning enabled off and on again recompiles nothing, and a list unchanged on update is not compiled again. With stats.path compiled lists are kept on disk (lists/): after a restart the process does not download them again until their update time comes.

False matches ​

Plain rules (||name^, |name^, hosts lines and bare domains) are stored as 56-bit hashes of the names; the rest match exactly. So a name no list holds can, extremely rarely, match by hash. Such a false match sticks: the name is blocked on every query until the list changes. An exception (@@) can match by chance the same way.

How to tell. The query log and "Check a domain" in the dashboard show the rule and its list, for example ||example.com^ · AdGuard DNS filter. For a plain rule that text is rebuilt from the name asked for, so a false match shows a rule that looks real. Look for that line in the list itself (open its URL or file): if it is not there, the match is false.

What to do. Add an exception for the name: "Unblock" in the query log adds @@||name^ to adblock.rules through the config draft. Clients that already got the blocked answer keep it for blocked_ttl; flushing the DNS cache of the browser or system makes it work at once. The exception stays in the config and would also cancel a real rule for that name if a list gets one later: remove it once the list has changed. Turning the list off works too, but drops all its rules.

Rule syntax ​

EntryMeaning
||example.org^The name and all subdomains
|example.org^, example.orgOnly this name
@@…An exception
*Any characters: ||ads.*.example.org^
| at the start or end, ^ at the endThe start or end of the name; without them the pattern can match in the middle of a name (ads. matches x.ads.example)
/regex/A regular expression on the name, case-insensitive
$importantStronger than exceptions without $important
$client=…Only for clients: an address, a network 192.168.1.0/24, a name from clients in quotes; ~ excludes; several separated by |
$dnstype=A|AAAA, $dnstype=~CNAMEOnly for these query types, or all but them. When checking an answer, the type is the record's type (CNAME for CNAME targets)
$dnsrewrite=…Answer by ourselves: 1.2.3.4, ::1, example.net (CNAME), NXDOMAIN, REFUSED, NOERROR;A;1.2.3.4. The exception @@…$dnsrewrite removes the rewrite
$badfilterDisables the rule with the same text in all lists

The order of decision, as in AdGuard Home: an $important exception, an $important block, an exception, $dnsrewrite, a block. Of several matching rules of one kind, the log shows the most specific one.

Clients ​

clients.<name>.match is source (an address or a network, a list is allowed), inbound, user (a user of an inbound); all the given conditions must hold, and the first matching client is chosen. lists is which block lists apply to it (allow lists and rules always apply); enabled: false means its queries are not filtered. The client's name can be used in $client='tv'.

Where adblock does not apply ​

skip_adblock: true is a parameter that the adblock module adds to upstreams and DNS rules:

yaml
dns:
  upstreams:
    local:
      udp: 192.168.1.1
      skip_adblock: true         # queries to this upstream are not filtered
    remote:
      https: 1.1.1.1
      via: proxy
  rules:
    - match:
        domain_suffix: [lan, local]
      to:
        upstream: local
        skip_adblock: true       # the same for one rule
  final:
    upstream: remote

Such queries appear in the log as "not checked" and are counted in the statistics. Without the adblock module in the build the field is an error.

Order of processing a query ​

query → dns.rules choose the upstream → adblock checks the name (a block is answered right away, the upstream is not asked) → cache or upstream → adblock checks the answer (CNAME and addresses, if check_answers) → to the client.

The DNS cache keeps the upstreams' answers as they are, and the filter is applied on top: a new rule, turning off and pausing take effect on the very next query without clearing the cache. If a DNS rule answers by itself (block, an answer without an upstream), adblock does not check it; if the answer is obtained through evaluate/respond, the name and the answer are checked after the answer is received.

Control without changing the config ​

The Admin API (and the dashboard, the Adblock section): pause for N minutes, stop until restart, update lists, check a domain, clear statistics. Turning it off in the config and blocking or unblocking a domain from the log are edits of the config draft (adblock.enabled, adblock.rules) by the same path as in the Config section: check, apply, save to the file. More in Dashboard.

Statistics and log ​

Statistics: total queries, blocked, allowed by an allow list, average response time, the share of answers from the cache, hourly series, top blocked and queried names and clients (with the share of blocks), hits per list, queries and time per upstream. The tops are counted per day and limited to a thousand names per day (rare ones are evicted first). Statistics are written to disk (stats.path/stats.json) no more often than every 10 minutes and on stop; the log file (log.file) is appended to in the same portions and starts over at 8 MiB, with the previous one kept with the suffix .1.

GPL-3.0-or-later