Sintaxe das regras de filtragem de DNS
Aqui, mostramos como escrever regras de filtragem de DNS personalizadas para uso nos produtos AdGuard
Links rápidos: Baixe o bloqueador de anúncios do AdGuard, Adquira o AdGuard Home, Experimente o AdGuard DNS
Introdução
Pode utilizar a sintaxe das regras de filtragem do AdGuard DNS para tornar as regras mais flexíveis, para que possam bloquear o conteúdo conforme as suas preferências. A sintaxe das regras de filtragem do AdGuard DNS pode ser utilizada em diferentes produtos AdGuard, tais como AdGuard Home, AdGuard DNS, AdGuard para Windows/Mac/Android.
Existem três abordagens diferentes para escrever listas de bloqueio de hosts:
Adblock-style syntax: the modern approach to writing filtering rules based on using a subset of the Adblock-style rule syntax. Dessa forma, as listas de bloqueio são compatíveis com os bloqueadores de anúncios do navegador.
/etc/hostssintaxe: a abordagem antiga e comprovada que usa a mesma sintaxe que os sistemas operacionais usam para seus arquivos de hosts.Sintaxe somente de domínios: uma lista simples de nomes de domínio.
If you are creating a blocklist, we recommend using the Adblock-style syntax. Tem algumas vantagens importantes sobre a sintaxe de estilo antigo:
Tamanho das listas de bloqueio. O uso de correspondência de padrões permite que você tenha uma única regra em vez de centenas de
/etc/hostsentradas.Compatibilidade. Sua lista de bloqueio será compatível com bloqueadores de anúncios do navegador e será mais fácil compartilhar regras com uma lista de filtros do navegador.
Extensibilidade. Na última década, a sintaxe do estilo Adblock evoluiu muito e não vemos razão para não estendê-la ainda mais e oferecer recursos adicionais para bloqueadores no nível da rede.
If you’re maintaining either a /etc/hosts-style blocklist or multiple filtering lists (regardless of type), we provide a tool for blocklist compilation. We named it Hostlist compiler and we use it ourselves to create AdGuard DNS filter.
Exemplos básicos
||exemplo.org^: bloqueia o acesso ao domínioexemplo.orge a todos os seus subdomínios, comowww.exemplo.org.@@||exemplo.org^: desbloqueia o acesso ao domínioexemplo.orge a todos os seus subdomínios.1.2.3.4 exemplo.org: (atenção,/etc/hosts- estilo antigo de sintaxe) em AdGuard Home, responder com1.2.3.4a consultas para o domínioexemplo.orgmas não os seus subdomínios. No AdGuard DNS Privado, bloqueia o acesso aexemplo.org.www.exemplo.orgpermanece permitido.No AdGuard Home, usar o endereço IP não especificado (
0.0.0.0) ou um endereço local (127.0.0.1e similares) para um host é basicamente o mesmo que bloquear esse host.# Retorna o endereço IP 1.2.3.4 para exemplo.org.
1.2.3.4 exemplo.org
# Bloqueia exemplo.org respondendo com 0.0.0.0.
0.0.0.0 exemplo.orgexemplo.org: uma regra de domínio simples. Bloqueia o domínioexemplo.org, mas não os seus subdomínios.www.exemplo.orgpermanece permitido.! Aqui vai um comentárioe# Também um comentário: comentários./REGEX/: bloqueia acesso aos domínios que correspondem à expressão regular especificada.
Adblock-style syntax
This is a subset of the traditional Adblock-style syntax which is used by browser ad blockers.
regra = ["@@"] padrão ["$" modificadores ]
modificadores = [modificador0, modificador1[, ...[, modificadorN]]]
pattern: a máscara do hostname. Cada hostname é comparado com esta máscara. O padrão também pode conter caracteres especiais, que são descritos abaixo.@@: o marcador que é usado nas regras de exceção. Inicie sua regra com este marcador se desejar desativar a filtragem dos nomes de host correspondentes.modifiers: parâmetros que esclarecem a regra. Eles podem limitar o escopo da regra ou até mesmo mudar completamente a forma como ela funciona.
Special characters
*: o caractere curinga. É usado para representar qualquer conjunto de caracteres. Isso também pode ser uma string vazia ou uma string de qualquer tamanho.||: corresponde ao início de um nome de host, incluindo qualquer subdomínio. Por exemplo,||exemplo.orgcorresponde aexemplo.orgeteste.exemplo.org, mas nãotestexample.org.^: o caractere separador. Unlike browser ad blocking, there’s nothing to separate in a hostname, so the only purpose of this character is to mark the end of the hostname.|: um ponteiro para o início ou o fim do nome do host. O valor depende do posicionamento do caractere na máscara. Por exemplo, a regraample.org|corresponde aexemplo.orgmas não aexemplo.org.com.|exemplocorresponde aexemplo.orgmas não ateste.exemplo.
Regular expressions
If you want even more flexibility in making rules, you can use regular expressions instead of the default simplified matching syntax. Se você quiser usar uma expressão regular, o padrão deve ficar assim:
padrão = "/" regexp "/"
Exemplos:
/example.*/bloqueará os hosts correspondentes aoexample.*regexp.@@/example.*/$importantirá desbloquear os hosts correspondentes aoexemplo.*regexp. Observe que esta regra também implica o modificadorimportant.
Comments
Qualquer linha que comece com um ponto de exclamação ou hash é um comentário e será ignorado pelo mecanismo de filtragem. Os comentários geralmente são colocados acima das regras e usados para descrever o que uma regra faz.
Exemplo:
! Isso é um comentário.
# Isso também é um comentário.
Rule modifiers
Você pode alterar o comportamento de uma regra adicionando modificadores. Os modificadores devem estar localizados no final da regra após o caractere $ e ser separados por vírgulas.
Exemplos:
||example.org^é o padrão correspondente.$é o delimitador, que sinaliza que o restante da regra são modificadores.importanté o modificador.You may want to use multiple modifiers in a rule. Nesse caso, separe-os por vírgulas:
||example.org^$client=127.0.0.1,dnstype=A||example.org^é o padrão correspondente.$é o delimitador, que sinaliza que o restante da regra são modificadores.client=127.0.0.1is theclientmodifier with its value,127.0.0.1.,is the delimiter between modifiers. And finally,dnstype=Ais thednstypemodifier with its value,A.
If a rule contains a modifier not listed in this document, the whole rule must be ignored. This way we avoid false-positives when people are trying to use unmodified browser ad blockers’ filter lists like EasyList or EasyPrivacy.
client
The client modifier allows specifying clients this rule is applied to. There are two main ways to identify a client:
Por seu endereço IP ou prefixo CIDR. Esta forma funciona para todos os tipos de clientes.
Pelo seu nome. Essa forma funciona apenas para clientes persistentes (no AdGuard Home) e dispositivos (no DNS privado do AdGuard), que você adicionou manualmente.
noteIn AdGuard Home, ClientIDs are not currently supported, only names are. If you have added a client with the name “My Client” and ClientID
my-clientspell your modifier as$client='My Client'as opposed to$client=my-client.
The syntax is:
$client=valor1|valor2|...
You can also exclude clients by adding a ~ character before the value. In this case, the rule is not be applied to this client’s DNS requests.
$client=~valor1
Client names usually contain spaces or other special characters, which is why you should enclose the name in quotes. Both single and double ASCII quotes are supported. Use the backslash (\) to escape quotes (" and '), commas (,), and pipes (|).
When excluding a client, you must place ~ outside the quotes.
Exemplos:
@@||*^$client=127.0.0.1: desbloqueie tudo para localhost.||example.org^$client='Laptop do Frank': blocoexample.orgpara o cliente chamadoLaptop do Frankapenas. Observe que as aspas (') no nome devem ter escape.||example.org^$client=~'Laptops de Mary, John e Boris: bloqueieexample.orgpara todos, exceto para o cliente chamadoLaptop de Mary, John e Boris. Observe que a vírgula (,) também deve ser escapada.||example.org^$client=~Mãe|~Pai|Crianças: bloqueieexemplo.orgparaCrianças, mas não paraMãeePai. Este exemplo demonstra como especificar vários clientes em uma regra.||example.org^$client=192.168.0.0/24: bloqueieexample.orgpara todos os clientes com endereços IP no intervalo de192.168.0.0a192.168.0.255.
denyallow
You can use the denyallow modifier to exclude domains from the blocking rule. To add multiple domains to one rule, use the | character as a separator.
The syntax is:
$denyallow=domínio1|domínio2|...
This modifier allows avoiding creating unnecessary exception rules when our blocking rule covers too many domains. You may want to block everything except for a couple of TLD domains. You could use the standard approach, i.e. rules like this:
! Bloquear tudo.
/.*/
! Desbloqueie alguns TLDs.
@@||com^
@@||net^
The problem with this approach is that this way you will also unblock tracking domains that are located on those TLDs (i.e. google-analytics.com). Here’s how to solve this with denyallow:
*$denyallow=com|net
Exemplos:
*$denyallow=com|net: bloqueie tudo exceto*.come*.net.@@*$denyallow=com|net: desbloqueie tudo exceto*.come*.net.||example.org^$denyallow=sub.example.org: blockexample.organd*.example.org, but notsub.example.org.
dnstype
The dnstype modifier allows specifying DNS request or response type on which this rule will be triggered.
The syntax is:
$dnstype=valor1|valor2|...
$dnstype=~valor1|~valor2|~...
The names of the types are case-insensitive, but are validated against a set of actual DNS resource record (RR) types.
Do not combine exclusion rules with inclusion ones. This:
$dnstype=~valor1|valor2
is equivalent to this:
$dnstype=valor2
Exemplos:
||example.org^$dnstype=AAAA: bloqueia consultas DNS para os endereços IPv6 deexample.org.||example.org^$dnstype=~A|~CNAME: permita apenasAeCNAMEconsultas DNS paraexample.org, bloqueie o resto.
Before version v0.108.0, AdGuard Home would use the type of the request to filter the response records, as opposed to the type of the response record itself. That caused issues, since that meant that you could not write rules that would allow certain CNAME records in responses in A and AAAA requests. In v0.108.0 that behaviour was changed, so now this:
||canon.example.com^$dnstype=~CNAME
allows you to avoid filtering of the following response:
RESPOSTAS:
-> exemplo.com
nome canônico = canon.exemplo.com.
ttl = 60
-> canon.example.com
internet address = 1.2.3.4
ttl = 60
dnsrewrite
The dnsrewrite response modifier allows replacing the content of the response to the DNS request for the matching hosts. Note that this modifier in AdGuard Home works in all rules, but in Private AdGuard DNS — only in custom ones.
Rules with the dnsrewrite response modifier have higher priority than other rules in AdGuard Home and AdGuard DNS.
Responses to all requests for a host matching a dnsrewrite rule will be replaced. The answer section of the replacement response will only contain RRs that match the request’s query type and, possibly, CNAME RRs. Note that this means that responses to some requests may become empty (NODATA) if the host matches a dnsrewrite rule.
The shorthand syntax is:
$dnsrewrite=1.2.3.4
$dnsrewrite=abcd::1234
$dnsrewrite=example.net
$dnsrewrite=REFUSED
The keywords MUST be in all caps (e.g. NOERROR). Keyword rewrites take precedence over the other and will result in an empty response with an appropriate response code.
The full syntax is of the form RCODE;RRTYPE;VALUE:
$dnsrewrite=NOERROR;A;1.2.3.4
$dnsrewrite=NOERROR;AAAA;abcd::1234
$dnsrewrite=NOERROR;CNAME;example.net
$dnsrewrite=REFUSED;;
The $dnsrewrite modifier with the NOERROR response code may also has empty RRTYPE and VALUE fields.
The CNAME one is special because AdGuard Home will resolve the host and add its info to the response. That is, if example.net has IP 1.2.3.4, and the user has this in their filter rules:
||example.com^$dnsrewrite=example.net
! Ou:
||example.com^$dnsrewrite=NOERROR;CNAME;example.net
then the response will be something like:
nslookup example.com my.adguard.local
Server: my.adguard.local
Address: 127.0.0.1#53
Non-authoritative answer:
example.com canonical name = example.net.
Name: example.net
Address: 1.2.3.4
Next, the CNAME rewrite. After that, all other records’ values are summed as one response, so this:
||example.com^$dnsrewrite=NOERROR;A;1.2.3.4
||example.com^$dnsrewrite=NOERROR;A;1.2.3.5
will result in a response with two A records.
Currently supported RR types with examples:
||4.3.2.1.in-addr.arpa^$dnsrewrite=NOERROR;PTR;example.net.adds aPTRrecord for reverse DNS. Solicitações DNS reversas para1.2.3.4para o servidor DNS resultarão emexample.net.noteThe IP MUST be in reverse order. See RFC 1035.
||exemplo.com^$dnsrewrite=NOERROR;A;1.2.3.4adiciona um registroAcom o valor1.2.3.4.||example.com^$dnsrewrite=NOERROR;AAAA;abcd::1234adiciona um registroAAAAcom o valorabcd::1234.||example.com^$dnsrewrite=NOERROR;CNAME;example.orgadiciona o registroCNAME. Veja a explicação acima.||example.com^$dnsrewrite=NOERROR;HTTPS;32 example.com alpn=h3adiciona um registroHTTPS. Apenas um subconjunto de valores de parâmetro é suportado: os valores devem sercontíguose, onde uma lista devaloreséesperada, apenas um valor é atualmente compatível:ipv4hint=127.0.0.1 // Compatível.
ipv4hint="127.0.0.1" // Não suportado.
ipv4hint=127.0.0.1,127.0.0.2 // Não suportado.
ipv4hint="127.0.0.1,127.0.0.2" // Não suportado.Isso será alterado no futuro.
||example.com^$dnsrewrite=NOERROR;MX;32 example.mailadiciona um registroMXcom valor de precedência32e valor de trocaexample.mail.||example.com^$dnsrewrite=NOERROR;SVCB;32 example.com alpn=h3adiciona um valorSVCB. Veja o exemploHTTPSacima.||example.com^$dnsrewrite=NOERROR;TXT;hello_worldadiciona um registroTXTcom o valorhello_world.||_svctype._tcp.example.com^$dnsrewrite=NOERROR;SRV;10 60 8080 example.comadiciona um registoSRVcom valor de prioridade10, valor de peso60, porta8080e valor de destinoexample.com.||exemplo.com^$dnsrewrite=NXDOMAIN;;responde com um códigoNXDOMAIN.$dnstype=AAAA,denyallow=example.org,dnsrewrite=NOERROR;;responde com umNOERRORvazio responde a todos os pedidosAAAAexceto os pedidos paraexample.org.
Exception rules unblock one or all rules:
@@||example.com^$dnsrewriteunblocks all DNS rewrite rules.@@||example.com^$dnsrewrite=1.2.3.4unblocks the DNS rewrite rule that adds anArecord with the value1.2.3.4.
If you are maintaining a blocklist that is included in AdGuard DNS and AdGuard Home (i.e. included into HostlistsRegistry), $dnsrewrite rules will be automatically filtered out. If these rules are required for your blocklist, please request permission by opening a new issue in the HostlistsRegistry repo.
important
The important modifier applied to a rule increases its priority over any other rule without the modifier. Even over basic exception rules.
Exemplos:
Neste exemplo:
||example.org^$important
@@||example.org^||example.org^$importantbloqueará todas as solicitações para*.example.orgapesar da regra de exceção.Neste exemplo:
||example.org^$important
@@||example.org^$importanta regra de exceção também tem o modificador
important, então funcionará.
badfilter
The rules with the badfilter modifier disable other basic rules to which they refer. It means that the text of the disabled rule should match the text of the badfilter rule (without the badfilter modifier).
Exemplos:
||exemplo.com$badfilterdesativa||exemplo.com.@@||example.org^$badfilterdesativa@@||example.org^.noteThe
badfiltermodifier currently doesn’t work with/etc/hosts-style rules.127.0.0.1 example.org$badfilterwill not disable the original127.0.0.1 example.orgrule.
ctag
The ctag modifier can only be used in AdGuard Home.
It allows to block domains only for specific types of DNS client tags. You can assign tags to clients in the AdGuard Home UI. In the future, we plan to assign tags automatically by analyzing the behavior of each client.
The syntax is:
$ctag=valor1|valor2|...
If one of client’s tags matches the ctag values, this rule applies to the client. The syntax for exclusion is:
$ctag=~valor1|~valor2|...
If one of client’s tags matches the exclusion ctag values, this rule doesn’t apply to the client.
Exemplos:
||example.org^$ctag=device_pc|device_phone: blocoexample.orgpara clientes marcados comodevice_pcoudevice_phone.||example.org^$ctag=~device_phone: blocoexample.orgpara todos os clientes, exceto aqueles marcados comodevice_phone.
The list of allowed tags:
Por tipo de dispositivo:
device_audio: dispositivos de áudio.device_camera: câmeras.device_gameconsole: consoles de jogos.device_laptop: notebooks.device_nas: NAS (Network-attached Storages).device_pc: PCs.device_phone: celulares.device_printer: impressoras.device_securityalarm: alarmes de segurança.device_tablet: tablets.device_tv: TVs.device_other: outros dispositivos.
Por sistema operacional:
os_android: Android.os_ios: iOS.os_linux: Linux.os_macos: macOS.os_windows: Windows.os_other: outros sistemas operacionais.
Por grupo de usuários:
user_admin: administradores.user_regular: usuários comuns.user_child: filhos.
/etc/hosts-style syntax
For each host a single line should be present with the following information:
IP_address canonical_hostname [aliases...]
Fields of the entries are separated by any number of space or tab characters. Text from the # character until the end of the line is a comment and is ignored.
Hostnames may contain only alphanumeric characters, hyphen-minus signs (-), and periods (.). They must begin with an alphabetic character and end with an alphanumeric character. Optional aliases provide for name changes, alternate spellings, shorter hostnames, or generic hostnames (for example, localhost).
Exemplo:
# Este é um comentário
127.0.0.1 exemplo.org exemplo.info
127.0.0.1 exemplo.com
127.0.0.1 exemplo.net # isso também é um comentário
In AdGuard Home, the IP addresses are used to respond to DNS queries for these domains. In Private AdGuard DNS, these addresses are simply blocked.
Domains-only syntax
A simple list of domain names, one name per line.
Exemplo:
# Isto é um comentário
exemplo.com
exemplo.org
exemplo.net # isto também é um comentário
If a string is not a valid domain (e.g. *.example.org), AdGuard Home will consider it to be an Adblock-style syntax rule.
Hostlist compiler
If you are maintaining a blocklist and use different sources in it, Hostlist compiler may be useful to you. It is a simple tool that makes it easier to compile a hosts blocklist compatible with AdGuard Home, Private AdGuard DNS or any other AdGuard product with DNS filtering.
What it’s capable of:
Compilar uma única lista de bloqueios a partir de várias fontes.
Exclude the rules you don’t need.
Limpe a lista resultante: remova a duplicação, remova regras inválidas e compacte a lista.