Performing Dns Tunneling Detection

通过计算 DNS 查询名称的香农熵(Shannon Entropy)、分析查询长度分布、检测 TXT 记录载荷以及 识别高子域名基数,检测 DNS 隧道(DNS Tunneling)攻击。使用 scapy 进行数据包捕获分析, 结合统计方法区分合法 DNS 流量和隐蔽信道。适用于数据泄露猎威场景。

killvxk 814cad3 4 files · 20.3 KB Updated

File contents

DNS 隧道检测

使用说明

使用熵分析和统计方法对查询名称特征进行分析,检测 DNS 流量中的 DNS 隧道指标。

import math
from collections import Counter

def shannon_entropy(data):
    if not data:
        return 0
    counter = Counter(data)
    length = len(data)
    return -sum((c/length) * math.log2(c/length) for c in counter.values())

# 合法域名:低熵(~3.0-3.5)
print(shannon_entropy("www.google.com"))
# DNS 隧道:高熵(~4.0-5.0)
print(shannon_entropy("aGVsbG8gd29ybGQ.tunnel.example.com"))

关键检测指标:

  1. 查询名称香农熵过高(子域名标签 > 3.5)
  2. 查询名称异常长(> 50 字符)
  3. 单个域名的 TXT 记录请求量过高
  4. 每个父域名下的唯一子域名数量过多
  5. 标签中字符分布异常

示例

from scapy.all import rdpcap, DNS, DNSQR
packets = rdpcap("dns_traffic.pcap")
for pkt in packets:
    if pkt.haslayer(DNSQR):
        query = pkt[DNSQR].qname.decode()
        entropy = shannon_entropy(query)
        if entropy > 4.0:
            print(f"可疑:{query}(entropy={entropy:.2f})")

killvxk/cybersecurity-skills-zh/tree/main/skills/performing-dns-tunneling-detection commit 814cad3494

Frequently asked questions

npx skillmds@latest add killvxk/performing-dns-tunneling-detection