KanishkNavale/sqlhund
GitHub: KanishkNavale/sqlhund
一个由 Rust 驱动并提供 Python 绑定的高性能 SQL 注入检测库,专为 AI 代理生成或处理 SQL 查询时的安全审计而设计。
Stars: 0 | Forks: 0
sqlhund
由 Rust 驱动的 Python 可审计 SQL 注入检测工具 🐍。专为 AI 代理打造 ✨。
**sqlhund** 检测 SQL 注入模式,并提供 CWE 和 CAPEC 分类。它使用 Rust 编写,并通过 [PyO3](https://pyo3.rs/) 提供 Python 绑定,为生成或中继 SQL 查询的 AI 代理提供安全分析,通过双轴威胁情报(技术 vs 影响)防止数据库被篡改。 ``` >>> import sqlhund >>> sqlhund.is_query_malicious("SELECT * FROM users WHERE id = 1") False >>> sqlhund.is_query_malicious("' OR 1=1 --") True ``` ## 安装 ``` pip install sqlhund # pip poetry add sqlhund # poetry uv add sqlhund # uv ``` 需要 Python 3.10 或更高版本。无运行时依赖。 ## 快速开始 sqlhund 暴露了两个函数——这就是完整的 API: ``` import sqlhund # 简单的布尔检查 sqlhund.is_query_malicious("SELECT * FROM users; DROP TABLE users") # True # 带有安全分类的详细分析 result = sqlhund.analyze_query("SELECT * FROM users; DROP TABLE users") # { # 'is_malicious': True, # 'matches': { # 'general': [ # { # 'technique': ['CWE-89'], # HOW: SQL Injection # 'impact': ['CWE-285', 'CWE-471'], # WHAT: Auth bypass + data tampering # 'capec': [66] # CAPEC-66: SQL Injection # } # ] # } # } # 检测到文件操作攻击 result = sqlhund.analyze_query("SELECT load_extension('evil')") # { # 'is_malicious': True, # 'matches': { # 'sqlite': [ # { # 'technique': ['CWE-89', 'CWE-610', 'CWE-114'], # 'impact': ['CWE-200', 'CWE-285'], # 'capec': [470] # } # ] # } # } # 安全查询干净地通过 sqlhund.analyze_query("SELECT id FROM users WHERE id = 1") # {'is_malicious': False, 'matches': {}} ``` ## 使用示例 ### 验证 AI 生成的 SQL ``` import sqlhund def execute_ai_query(query: str): """Execute AI-generated SQL with injection protection.""" if sqlhund.is_query_malicious(query): raise ValueError("Potential SQL injection detected") # Safe to execute return database.execute(query) ``` ### 详细的威胁分析 ``` result = sqlhund.analyze_query("SELECT * FROM users WHERE id = 1 OR 1=1") if result['is_malicious']: for db_name, patterns in result['matches'].items(): print(f"Database: {db_name}") for pattern in patterns: print(f" Technique: {pattern['technique']}") # CWE-89 print(f" Impact: {pattern['impact']}") # CWE-285 print(f" CAPEC: {pattern['capec']}") # 66 ``` ### 预筛选用户输入 ``` def sanitize_search_query(user_input: str) -> str: """Validate search input before building SQL.""" test_query = f"SELECT * FROM products WHERE name LIKE '%{user_input}%'" if sqlhund.is_query_malicious(test_query): raise ValueError("Invalid search term") return user_input ``` ## 功能 - **快速**:核心检测引擎用 Rust 编写,编译为原生 Python 扩展 - **准确**:在 1000 万+ 查询基准测试中达到 100% 的精确率和召回率(零误报,零漏报) - **多数据库**:检测针对 SQLite、PostgreSQL 和 DuckDB 的注入模式 - **零依赖**:作为独立的原生 wheel 分发 - **AI 代理就绪**:设计为 LLM 生成 SQL 的护栏 - **安全分类**:将检测到的模式映射到 CWE 和 CAPEC 分类体系,用于威胁情报 ## 安全分类 sqlhund 使用行业标准的安全框架对检测到的模式进行分类: ### 双轴 CWE 分析 沿两个独立轴分析每个检测到的模式: - **技术**(HOW):描述注入机制的 CWE 标识符 - CWE-89: SQL Injection - CWE-610: External Resource Reference(文件操作) - CWE-94/95: Code/Eval Injection - CWE-77/78: Command/OS Command Injection - CWE-114: Process Control(加载不受信任的库) - CWE-116/184: Encoding evasion and filter bypass - **影响**(WHAT):描述攻击后果的 CWE 标识符 - CWE-200: Information Disclosure - CWE-285: Authorization Bypass - CWE-269: Privilege Escalation - CWE-471: Data Tampering - CWE-400: Resource Exhaustion(DoS) - CWE-208: Timing Side-Channel(盲注) - CWE-497: System Information Exposure ### CAPEC 攻击模式 匹配项也会映射到 [CAPEC](https://capec.mitre.org/) 攻击模式 ID: - **CAPEC-66**: SQL Injection - **CAPEC-7**: Blind SQL Injection - **CAPEC-54**: Query System for Information - **CAPEC-470**: Expanding Control over the OS from the Database - **CAPEC-664**: Server-Side Request Forgery ### OWASP 对齐 sqlhund 检测来自 [OWASP Top 10 A03:2021 - Injection](https://owasp.org/Top10/A03_2021-Injection/) 的模式,涵盖: - SQL Injection (CWE-89) - Command Injection (CWE-77, CWE-78) - Code Injection (CWE-94, CWE-95) - File/Resource Injection (CWE-610) **资源:** - [OWASP SQL Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html) - [OWASP Query Parameterization Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Query_Parameterization_Cheat_Sheet.html) - [OWASP Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Injection_Prevention_Cheat_Sheet.html) - [CWE-89: SQL Injection](https://cwe.mitre.org/data/definitions/89.html) - [CAPEC-66: SQL Injection](https://capec.mitre.org/data/definitions/66.html) ## 支持的数据库 sqlhund 检测针对以下数据库的特定注入模式: | Database | Detection Patterns | |------------|--------------------| | General | ✓ UNION, comments, tautologies, subqueries, time delays | | SQLite | ✓ load_extension, ATTACH, PRAGMA, virtual tables | | PostgreSQL | ✓ pg_read_file, COPY, DO blocks, dblink, extensions | | DuckDB | ✓ read_csv, ATTACH, httpfs, CREATE SECRET, macros | ## 基准测试 基于 [RbSQLi 数据集](https://data.mendeley.com/datasets/xz4d5zj5yw/3) 进行评估——包含 10,304,026 条已标记的 SQL 查询(2,813,146 条恶意,7,490,880 条良性)。 | | Predicted Malicious | Predicted Benign | |----------------------|--------------------:|-----------------:| | **Actual Malicious** | 2,813,146 | 0 | | **Actual Benign** | 0 | 7,490,880 | Precision: 100% · Recall: 100% · Accuracy: 100% ## 从源码构建 需要 [Rust](https://rustup.rs/)、[Maturin](https://github.com/PyO3/maturin) 和 [uv](https://docs.astral.sh/uv/)。 ``` git clone https://github.com/KanishkNavale/sqlhund cd sqlhund make dev # set up development environment make build # compile debug build make release # compile optimized release build ``` ## 测试 运行单元测试(Rust + Python): ``` make unittest ``` 针对完整的 RbSQLi 数据集运行评估(下载 [该数据集](https://data.mendeley.com/datasets/xz4d5zj5yw/3),并将其放置在 `tests/data/wild.csv`): ``` make wildtest ``` ## 贡献 欢迎贡献。请查看 [open issues](https://github.com/KanishkNavale/sqlhund/issues) 或提交 pull request。 ## 许可证 本项目采用 [MIT License](LICENSE) 许可。标签:AI代理安全, AI安全, API密钥检测, CAPEC, Chat Copilot, DOE合作, PyO3, Python, Rust, SQL注入检测, SQL注入防御, Web安全, 可视化界面, 安全扫描, 无后门, 时序注入, 模式匹配, 网络安全, 网络流量审计, 自动化资产收集, 蓝队分析, 输入验证, 逆向工具, 通知系统, 防注入, 隐私保护