修改、拦截 HTTP(S) 请求
中间人攻击模块是 DNS 管家的 HTTP(S) 流量拦截与改写引擎。通过加载 YAML 格式的模块配置,您可以对指定域名的 HTTP 请求和响应进行 URL 重写、Header 修改、Mock 响应和 JavaScript 脚本注入。
DNS 管家通过 VPN 隧道捕获设备流量,对非 DNS 的 TCP 连接进行协议检测:
对于 HTTPS 流量,DNS 管家会在本地生成 CA 证书并动态签发叶证书,实现 TLS 终止。您需要先在系统设置中安装并信任 CA 证书。
模块使用 YAML 格式定义,包含以下字段:
name: "我的模块"
desc: "一个示例模块"
hosts:
- "*.example.com:443"
- "api.test.com:*"
urls:
- name: "拦截广告"
pattern: "^https?://ad\\.example\\.com"
replacement: ""
action: "reject"
- name: "旧地址跳转"
pattern: "^http://old\\.example\\.com/(.*)"
replacement: "https://new.example.com/$1"
action: "302"
headers:
- name: "添加 DNT"
direction: "request"
pattern: "^https://api\\.example\\.com"
action: "add"
header: "DNT"
value: "1"
- name: "删除 Cookie"
direction: "request"
pattern: "^https://api\\.example\\.com"
action: "del"
header: "Cookie"
mocks:
- name: "Mock JSON"
pattern: "^https?://mock\\.test\\.com/json"
type: "text"
data: '{"mocked": true}'
status: 200
headers:
- ["Content-Type", "application/json"]
scripts:
- name: "签名注入"
pattern: "^https?://api\\.test\\.com"
source: |
function onRequest(req) {
req.headers["X-Sign"] = "abc123";
}
function onResponse(req, resp) {
resp.headers["X-Test"] = "yes";
}
hosts 字段控制哪些域名会被 MITM 拦截:
example.com — 不指定端口,匹配该域名所有端口example.com:443 — 只匹配 443 端口example.com:* — 通配端口,匹配所有端口*.example.com — 通配子域名(匹配 api.example.com,不匹配 example.com)通过正则表达式匹配 URL 并执行重写操作。所有 URL 规则均使用正则匹配,replacement 字段支持 $1、$2 等捕获组引用。
{}[]urls:
- name: "广告拦截"
pattern: "^https?://ad\\.example\\.com"
replacement: ""
action: "reject"
- name: "重定向"
pattern: "^http://old\\.example\\.com/(.*)"
replacement: "https://new.example.com/$1"
action: "302"
- name: "透明改写"
pattern: "^https?://example\\.com/transparent(.*)$"
replacement: "https://httpbin.org/get$1"
action: "transparent"
对 HTTP 请求或响应的 Header 进行修改,支持请求阶段和响应阶段分别配置。
search 和 replacement)request(请求阶段)或 response(响应阶段)headers:
- name: "添加请求头"
direction: "request"
pattern: "^https://api\\.example\\.com"
action: "add"
header: "DNT"
value: "1"
- name: "删除 Cookie"
direction: "request"
pattern: "^https://api\\.example\\.com"
action: "del"
header: "Cookie"
- name: "正则替换 UA"
direction: "request"
pattern: "^https://api\\.example\\.com"
action: "replace-regex"
header: "User-Agent"
search: "Safari"
replacement: "Chrome"
- name: "注入响应头"
direction: "response"
pattern: "^https://api\\.example\\.com"
action: "add"
header: "X-Injected"
value: "yes"
根据 URL 正则匹配返回静态响应,无需请求上游服务器。适合调试和 Mock API。
text / base64 / empty)empty 类型可省略)mocks:
- name: "Mock JSON"
pattern: "^https?://mock\\.test\\.com/json"
type: "text"
data: '{"mocked": true}'
status: 200
headers:
- ["Content-Type", "application/json"]
- name: "空响应"
pattern: "^https?://mock\\.test\\.com/empty"
type: "empty"
status: 204
headers: []
使用内置 JS 引擎执行脚本,支持 onRequest 和 onResponse 两个生命周期钩子。脚本可读取和修改请求/响应的 URL、Method、Headers 和 Body。
function onRequest(req) {
// req.url — 请求 URL (可修改)
// req.method — 请求方法 (可修改)
// req.headers — 请求头对象 (可增删改)
// req.body — 请求体 Uint8Array 或 null (可修改)
req.headers["X-Sign"] = "abc123";
// 返回 undefined → 继续 (可携带修改)
// 返回 "continue" → 继续
// 返回 "reject" → 阻断请求 (404)
// 返回 "reject-200" → 阻断请求 (200)
// 返回 "reject-dict" → 阻断请求 (200 + {})
// 返回 "reject-array" → 阻断请求 (200 + [])
// 返回 "reject-img" → 阻断请求 (200 + 1px GIF)
// 返回 { status, headers, body } → 短路响应
}
function onResponse(req, resp) {
// req — 原始请求对象 (只读)
// resp.status — 响应状态码 (可修改)
// resp.headers — 响应头对象 (可增删改)
// resp.body — 响应体 Uint8Array 或 null (可修改)
resp.headers["X-Test"] = "yes";
resp.status = 204;
}
scripts:
- name: "签名注入"
pattern: "^https?://api\\.test\\.com"
source: |
function onRequest(req) {
req.headers["X-Sign"] = "abc123";
// 读取 Body 判断是否拒绝
if (req.body && req.body[0] === 72) {
return "reject";
}
}
- name: "响应替换"
pattern: "^https?://api\\.test\\.com"
source: |
function onResponse(req, resp) {
return {
status: 200,
headers: { "Content-Type": "application/json" },
body: new Uint8Array([123, 125]) // {}
};
}
每个 HTTP 请求按以下 9 个阶段依次处理,短路阶段会直接返回响应:
对 HTTPS 流量进行 MITM 需要 CA 证书。DNS 管家会在首次使用时自动生成 Root CA 证书(10 年有效期),并为每个域名动态签发叶证书(90 天有效期)。
除了导入本地模块外,您还可以通过 URL 订阅远程模块:
+ 按钮,选择「导入本地模块」或「订阅远程模块」.yaml / .yml 格式的模块文件https:// 或 http:// 开头)hosts 列表中匹配的域名才会被拦截,其他域名不受影响如您在使用过程中遇到问题,请通过以下方式与我们联系: