中间人攻击

修改、拦截 HTTP(S) 请求

最后更新:2026年8月20日

中间人攻击模块是 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 拦截:

四、URL Rewrite

通过正则表达式匹配 URL 并执行重写操作。所有 URL 规则均使用正则匹配,replacement 字段支持 $1$2 等捕获组引用。

支持的 Action

示例

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"

五、Header Rewrite

对 HTTP 请求或响应的 Header 进行修改,支持请求阶段和响应阶段分别配置。

支持的 Action

字段说明

示例

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"

六、Mock 响应

根据 URL 正则匹配返回静态响应,无需请求上游服务器。适合调试和 Mock API。

支持的类型

字段说明

示例

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: []

七、JavaScript 脚本

使用内置 JS 引擎执行脚本,支持 onRequestonResponse 两个生命周期钩子。脚本可读取和修改请求/响应的 URL、Method、Headers 和 Body。

脚本 API

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;
}

返回值约定

Body 处理策略

示例

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 个阶段依次处理,短路阶段会直接返回响应:

九、CA 证书

对 HTTPS 流量进行 MITM 需要 CA 证书。DNS 管家会在首次使用时自动生成 Root CA 证书(10 年有效期),并为每个域名动态签发叶证书(90 天有效期)。

十、模块订阅

除了导入本地模块外,您还可以通过 URL 订阅远程模块:

十一、注意事项

十二、联系我们

如您在使用过程中遇到问题,请通过以下方式与我们联系: