跳到正文
Skillpedia
安全6.5k · #13971

sql-injection

它能做什么

这项技能是一份排查源代码中 SQL 注入漏洞的操作指南,包括原始查询和 ORM 的绕过入口。适合检查自己有权审查的代码的开发人员和审查人员。

适用场景

  • ✓当开发人员要求智能体检查,在已获授权的代码库中用户输入是否会进入数据库查询时。
  • ✓当智能体审查原始查询辅助函数、动态表名或搜索过滤条件是否存在不安全的字符串拼接时。
  • ✓当团队需要一张列出其编程语言中安全与不安全查询方法的对照表时。

技能链接

https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/analyst/sql-injection/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/BitterSecurity/Decepticon/blob/main/packages/decepticon/decepticon/skills/standard/analyst/sql-injection/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我检查我们已获授权的 Python 代码,看看是否有根据用户输入拼接 SQL 的数据库查询?”

每周 5 个新技能,1 分钟读完。

工作原理

智能体找到查询调用,把用户输入从请求一路追踪到这些调用,并列出使用字符串拼接而非参数化的位置。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

SQL Injection Hunting Playbook

Classic, still the king. Modern ORMs reduce the surface area but every codebase has escape hatches (raw queries, dynamic table names, LIKE fragments) where taint tracking pays off.

1. Sources

Anything accepting untrusted input: query params, form bodies, headers, cookies, JWT claims, file contents parsed as config, message-queue payloads.

2. Sinks by language/framework

Language Safe API Dangerous API
Python cursor.execute(sql, params) cursor.execute(f"... {user}"), %-format
Python SQLAlchemy text().bindparams text(f"... {user}"), raw_sql
Python Django ORM filter kwargs Model.objects.raw(...), .extra(where=[...])
Node pg.query(text, params) Template literals with user data
Node Prisma $queryRaw\...`` $queryRawUnsafe(userString)
Java PreparedStatement Statement.executeQuery(string + user)
Java JPA criteria entityManager.createNativeQuery(user + sql)
Go db.Query(text, args...) fmt.Sprintf → db.Query
Ruby Rails where("col = ?", x) where("col = '#{x}'"), find_by_sql(interp)
PHP PDO prepared mysqli_query($conn, $user . $sql)

3. Taint audit steps

# Semgrep covers 80% of obvious cases
semgrep --config p/sql-injection /workspace/src --sarif -o /workspace/sem-sqli.sarif
kg_ingest_sarif("/workspace/sem-sqli.sarif", "semgrep")

# Grep for the high-signal patterns semgrep misses (dynamic order/group by)
grep -rE 'ORDER BY.*\$|LIMIT.*\$|GROUP BY.*\$' /workspace/src
grep -rE 'execute\s*\(.*f["\']|execute\s*\(.*%.*%' /workspace/src

# Django raw + extra (scanner blind spot)
grep -rE '\.raw\(|\.extra\(' /workspace/src

# Second-order: anything that stores user input then queries by it unescaped
grep -rE 'INSERT INTO.*VALUES.*\?|UPDATE.*SET' /workspace/src

4. Dynamic table/column sinks — the interesting subclass

ORMs parameterize values but NOT identifiers. If the code does:

cursor.execute(f"SELECT * FROM {tenant}_users WHERE id=%s", (uid,))

the table name is user-controlled. Check whether tenant is validated against an allowlist. If not, this is trivial RCE on Postgres (; DROP ... ; COPY ... TO PROGRAM 'bash -c ...' --).

5. NoSQL injection

DB Dangerous Safe
MongoDB db.users.find({name: req.body.name}) find({name: String(req.body.name)})
Firebase db.ref(req.query.path) path allowlist
Dynamo Filter expressions built from strings ExpressionAttributeValues

Classic Mongo bypass: {"username": "admin", "password": {"$ne": null}}.

6. Second-order injection

Most dangerous variant. The sink query reads from a previously stored value that was user-controlled. Missed by 90% of scanners because the taint breaks at the DB boundary.

Audit approach: any column that stores user input and is later concatenated into another query is a candidate. Grep for the column name across the repo.

7. PoC + validation contract

# Blind time-based
curl "https://target.com/search?q=1' AND SLEEP(5)-- -"
# Success: response time > 5s

# Error-based
curl "https://target.com/search?q=1' AND (SELECT 1 FROM (SELECT COUNT(*),CONCAT(version(),FLOOR(RAND(0)*2))x FROM information_schema.tables GROUP BY x)a)-- -"
# Success: mysql error with version string reflected

validate_finding contract:

  • success_patterns: version\(\), mysql, postgres, sleep.*5, syntax error near, ORA-01756
  • negative_command: same URL with q=1 (no payload)
  • negative_patterns: same error string (if present without payload, it's unrelated noise — demote)

8. Default CVSS

Variant Score Notes
Authenticated read-only SQLi
常见问题

什么是 SQL 注入?

就是用户输入改变了数据库查询的含义,从而可能泄露或修改数据。

ORM 能防止它吗?

该技能指出,ORM 能降低风险,但原始查询辅助函数和动态部分仍可能不安全。

更安全的做法是什么?

该技能推荐参数化查询,即把用户的值与 SQL 文本分开传递。

详情

作者的原始描述

Hunt SQL injection (CWE-89) via source-level taint tracking. Covers string concat, format-string, ORM raw queries, second-order injection, and NoSQL injection in MongoDB/DynamoDB.

repo: BitterSecurity/Decepticon

热度

GitHub 星标
6.5k
排名
#13971
许可证
Apache-2.0
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能