Set up the DNS firewall
Your role needs permission to manage DNS firewall profiles and to assign them to service policies.
The DNS firewall controls which domain names your devices can resolve. You write rules that allow or deny domain names, and the emnify DNS resolver applies them when a device looks up a name. Use it to keep devices away from domains they don’t need, so a compromised device can’t reach an unknown server by name.
The DNS firewall filters DNS lookups only. It doesn’t block traffic to an IP address that a device reaches without a DNS lookup. To filter traffic by IP address, protocol, and port, set up the IP firewall.
Prerequisites
- The DNS firewall is a subscription for your organization. To request access, contact emnify support.
- Your devices must use the emnify DNS servers. You can’t enable the DNS firewall on a service policy that has a custom DNS server.
How the DNS firewall works
You configure the DNS firewall with two objects:
- A DNS firewall profile is a named list of rules. You can reuse one profile on several service policies.
- A service policy applies DNS firewall profiles to its devices and sets the mode. You can assign up to 10 profiles to one service policy, and every device with that service policy uses them.
The DNS firewall applies to a device only when its service policy has at least one DNS firewall profile assigned and the DNS firewall is enabled on that service policy. Otherwise, the device’s DNS lookups aren’t filtered.
Choose a mode
The mode decides what happens to a domain name that no rule matches:
In allowlist mode, your devices can resolve only the names that your rules allow. Before you enable the DNS firewall, make sure your rules cover every domain your devices need, such as your backend, time servers, and firmware update servers.
How rules are evaluated
Within a profile, the most specific rule that matches a name decides whether it’s allowed or denied:
- A rule for the exact name wins over a wildcard rule.
- Among wildcard rules, the one for the longest domain wins.
For example,
*.api.example.comwins over*.example.comforeu.api.example.com.
The order of the rules in the file doesn’t matter.
When a service policy has several DNS firewall profiles, the DNS resolver checks each profile on its own:
- If any profile denies the name, it’s denied, even if another profile allows it.
- If no profile denies the name and at least one allows it, it’s allowed, in both modes.
- If no rule in any profile matches, the mode decides.
When a name is denied, the DNS resolver answers that the domain doesn’t exist (NXDOMAIN).
The device gets no IP address for the name, so it can’t connect to it by name.
Write the rules
A DNS firewall profile is a plain text file with one rule per line. Each rule has this format:
Keywords and domain names aren’t case-sensitive, and empty lines are ignored. A profile can have up to 100 rules, an organization can have up to 100 DNS firewall profiles, and a service policy can have up to 10 of them assigned.
A wildcard matches every subdomain of a domain, but not the domain itself.
For example, *.example.com matches api.example.com and eu.api.example.com, but not example.com.
To match the domain and its subdomains, add a rule for each.
This example is for allowlist mode. It allows your backend and its subdomains, an MQTT broker, and a time server, and denies everything else:
This example is for blocklist mode. It denies one domain with all its subdomains, and one single name, and allows everything else:
When you write rules, keep in mind:
- A deny in any profile is final.
A
DENYrule in one profile blocks the name for the whole service policy, even if another profile allows it. - Two rules for the same domain can’t have different actions. The Portal rejects the profile, because neither rule is more specific than the other. A second rule with the same domain and action is redundant, and the Portal shows a warning.
- Allow every name that your devices look up. Many services use more than one domain name, for example for regional endpoints or a content delivery network. In allowlist mode, a device can’t resolve a name you didn’t allow, even if it belongs to the same service.
- Expect cached answers. Devices keep DNS answers for a while. After you deny a name, a device can use an answer it already has until that answer expires. To apply the change sooner, reset the connectivity of the devices.
Create a DNS firewall profile
In Device Policies, find DNS Firewall profiles in the Security Policies section.
Click Actions > Download example profile. The file explains the rule format and contains example rules as comments.
Every line in the example profile is a comment, so the file has no rules. If you upload it unchanged to a service policy in allowlist mode, the DNS firewall denies every name after you enable it.
Change a DNS firewall profile
Changes to a profile apply to every service policy it’s assigned to.
Assign profiles and enable the DNS firewall
You can’t turn on the DNS firewall on a service policy without an assigned profile, or on a service policy that has a custom DNS server. While the DNS firewall is on, you can’t remove the last assigned profile.
In Service Policies, choose the policy you want to update and expand the policy details.
In Security, click the edit icon in the Enable DNS Firewall row. If no profile is assigned yet, you can also click Attach profiles. The DNS firewall profiles dialog opens.
Disable the DNS firewall
In Service Policies, choose the policy you want to update and expand the policy details.
Track configuration changes
Assigning or removing a DNS firewall profile, changing the mode, and enabling or disabling the DNS firewall each create a Policy management event. Use these events to audit when the configuration changed.
Manage the DNS firewall with the emnify REST API
You can also manage DNS firewall profiles, their assignment, and the mode with the emnify REST API.
Use the dry_run parameter to validate rules without saving them.
For details, see Create a DNS firewall profile and Replace DNS firewall profiles on a service profile in the API reference.