rohansx/vgx
GitHub: rohansx/vgx
一款面向 AI 辅助开发场景的安全扫描工具,能识别 AI 生成代码并检测安全漏洞,在代码提交前进行拦截。
Stars: 10 | Forks: 1
# VGX
**面向 AI 辅助开发的开源安全扫描器。**
[options]
Commands:
detect Detect AI-generated code
scan Security vulnerability scan
version Print version
help Show help
```
### `vgx detect` — AI 代码检测
分析文件中的 AI 生成代码模式。
```
# 扫描目录
vgx detect --path ./src
# 扫描单个文件
vgx detect src/api/handler.ts
# JSON 输出(用于 CI/CD)
vgx detect --path ./src --format json
# 自定义阈值(默认:70)
vgx detect --path ./src --threshold 80
```
**选项:**
| 标志 | 简写 | 默认值 | 描述 |
|------|-------|---------|-------------|
| `--path` | `-p` | `.` | 扫描路径 |
| `--format` | `-f` | `text` | 输出格式:`text`、`json` |
| `--threshold` | | `70` | AI 置信度阈值 (0-100) |
**示例输出:**
```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
VGX AI Code Detection
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Files scanned: 12
AI-generated: 4
Human-written: 8
AI percentage: 33.2%
Max AI confidence: 89%
FILES
🤖 src/api/handlers.ts 89%
🤖 src/utils/fetch.ts 82%
✓ src/config.ts 28%
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
🤖 4 file(s) detected as AI-generated
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
```
### `vgx scan` — 安全扫描
使用 Semgrep 规则扫描漏洞。
```
# 扫描已更改文件(默认)
vgx scan
# 扫描所有文件
vgx scan --changes=false
# 扫描特定文件
vgx scan src/auth.ts src/api/users.ts
```
**选项:**
| 标志 | 默认值 | 描述 |
|------|---------|-------------|
| `--changes` | `true` | 仅扫描 git 变更的文件 |
| `--report` | `true` | 生成 markdown 报告 |
| `--update-context` | `true` | 更新文件上下文缓存 |
**检测内容:**
- 硬编码的密钥和 API Key
- SQL 注入漏洞
- XSS(跨站脚本攻击)
- 不安全的加密方式
- 路径遍历
- 命令注入
- 以及更多通过 [Semgrep rules](https://semgrep.dev/r) 提供的规则
## API
### JSON 输出格式
#### 检测结果
```
{
"files_scanned": 12,
"ai_detected": 4,
"human_written": 8,
"max_ai_confidence": 0.89,
"ai_percentage": 33.2,
"results": [
{
"file_path": "src/api/handler.ts",
"ai_confidence": 0.89,
"confidence_level": "very_high",
"style_score": 0.82,
"pattern_score": 0.94,
"is_ai_generated": true,
"lines_of_code": 145,
"patterns": [
{
"name": "async_await_fetch",
"confidence": 0.10,
"line_start": 23,
"line_end": 35
}
]
}
]
}
```
#### 漏洞结果
```
{
"file": "src/auth.ts",
"description": "Hardcoded API key detected",
"rule": "generic.secrets.security.detected-api-key",
"severity": "high",
"line": 15,
"source": "semgrep",
"recommendation": "Use environment variables for secrets"
}
```
### 退出码
| 代码 | 含义 |
|------|---------|
| `0` | 成功,未发现问题 |
| `1` | 发现问题(AI 代码或漏洞) |
| `2` | 错误(无效路径等) |
## VS Code 扩展
在编辑器中实时进行 AI 代码检测。
### 功能
- **状态栏指示器** — 显示当前文件的 AI 置信度
- **行内高亮** — 高亮显示 AI 生成的代码模式
- **工作区扫描** — 扫描整个工作区的 AI 代码
### 命令
| 命令 | 描述 |
|---------|-------------|
| `VGX: Detect AI Code in File` | 分析当前文件 |
| `VGX: Detect AI Code in Workspace` | 扫描所有文件 |
| `VGX: Security Scan File` | 运行安全扫描 |
### 设置
```
{
"vgx.aiThreshold": 70,
"vgx.highlightAICode": true,
"vgx.showInlineConfidence": true
}
```
### 安装
```
cd vscode-extension
npm install
npm run compile
# 然后通过 VS Code 中的“从 VSIX 安装”进行安装
```
## 工作原理
### AI 检测算法
VGX 结合使用**风格计量学**(代码风格分析)和**模式匹配**来检测 AI 生成的代码。
```
┌─────────────────────────────────────────────────────────────┐
│ VGX DETECTION ENGINE │
├─────────────────────────────────────────────────────────────┤
│ │
│ INPUT: Source code file │
│ │
│ ┌─────────────────────┐ ┌─────────────────────┐ │
│ │ STYLOMETRY (45%) │ │ PATTERNS (55%) │ │
│ ├─────────────────────┤ ├─────────────────────┤ │
│ │ • Naming consistency│ │ • try/catch style │ │
│ │ • Indentation │ │ • async/await fetch │ │
│ │ • Comment density │ │ • React hooks │ │
│ │ • Line length var. │ │ • Go error handling │ │
│ │ • Boilerplate ratio │ │ • Python docstrings │ │
│ │ • Empty line ratio │ │ • JSDoc comments │ │
│ └─────────────────────┘ └─────────────────────┘ │
│ │ │ │
│ └──────────┬───────────────┘ │
│ ▼ │
│ Combined Score (0-100%) │
│ │ │
│ ▼ │
│ ┌─────────────────────────┐ │
│ │ Threshold Check (70%) │ │
│ └─────────────────────────┘ │
│ │ │
│ ┌──────────┴──────────┐ │
│ ▼ ▼ │
│ 🤖 AI-Generated ✓ Human-Written │
│ │
└─────────────────────────────────────────────────────────────┘
```
### 风格计量学特征
| 特征 | AI 信号 | 权重 |
|---------|-----------|--------|
| 命名一致性 | 高一致性 = AI | 20% |
| 缩进 | 完美一致性 = AI | 20% |
| 样板代码比例 | 高样板代码 = AI | 20% |
| 行长度方差 | 低方差 = AI | 15% |
| 注释密度 | ~15% 密度 = AI | 10% |
| 空行比例 | 间距一致 = AI | 10% |
| 平均行长度 | ~45 字符 = AI | 5% |
### 模式检测
VGX 识别 16 种以上 AI 常用的特定语言模式:
**JavaScript/TypeScript:**
- `copilot_try_catch` — 带有 console.error 的标准 try/catch
- `async_await_fetch` — 带有 await fetch 的 async 函数
- `arrow_with_types` — 带类型的箭头函数
- `use_effect_deps` — 带有依赖数组的 React useEffect
- `use_state_destructure` — React useState 解构
**Go:**
- `go_error_check` — `if err != nil { return }` 模式
- `go_defer` — `defer file.Close()` 模式
- `go_struct_init` — 多行结构体初始化
**Python:**
- `python_docstring` — 带有 Args/Returns/Raises 的文档字符串
- `python_type_hints` — 函数类型注解
## Pre-commit 钩子
在提交前拦截 AI 代码或漏洞。
### 设置
```
# 创建钩子
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
vgx scan --changes=true
if [ $? -ne 0 ]; then
echo "VGX: Commit blocked due to security issues"
exit 1
fi
EOF
chmod +x .git/hooks/pre-commit
```
### 配合 pre-commit 框架使用
```
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: vgx-scan
name: VGX Security Scan
entry: vgx scan --changes=true
language: system
pass_filenames: false
```
## 配置
VGX 开箱即用,无需配置。
### 环境变量
| 变量 | 描述 |
|----------|-------------|
| `OPENAI_API_KEY` | 启用 AI 增强扫描(可选) |
| `SEMGREP_RULES` | 自定义 Semgrep 规则(默认:`p/security-audit`) |
### 示例 `.env`
```
# 可选:使用 OpenAI 增强扫描
OPENAI_API_KEY=sk-...
# 可选:自定义 Semgrep 规则
SEMGREP_RULES=p/security-audit,p/secrets
```
## 项目结构
```
vgx/
├── cmd/
│ └── vgx/
│ └── main.go # CLI entrypoint
├── pkg/
│ ├── detection/
│ │ ├── detector.go # Main detector
│ │ ├── stylometry.go # Style analysis
│ │ └── patterns.go # Pattern matching
│ ├── scanner/
│ │ ├── scanner.go # Vulnerability scanner
│ │ ├── semgrep.go # Semgrep integration
│ │ └── openai.go # OpenAI integration
│ ├── context/
│ │ └── manager.go # File context & caching
│ ├── cache/
│ │ └── cache.go # Scan result cache
│ └── types/
│ └── types.go # Shared types
├── vscode-extension/
│ └── src/
│ └── extension.ts # VS Code extension
├── website/
│ └── index.html # Landing page
├── scripts/
│ └── install.sh # Installer script
└── .github/
└── workflows/
└── release.yml # CI/CD
```
## 系统要求
- **Go 1.22+**(用于构建)
- **Semgrep**(可选,用于安全扫描)
```
# 安装 Semgrep(可选)
pip install semgrep
# 或
brew install semgrep
```
## 贡献
欢迎贡献!请阅读我们的贡献指南。
```
# 克隆
git clone https://github.com/rohansx/vgx.git
cd vgx
# 构建
go build -o vgx ./cmd/vgx
# 测试
go test ./...
# 运行
./vgx detect --path ./pkg
```
## 许可证
MIT 许可证 — 详见 [LICENSE](LICENSE)
## 链接
- **网站:** [vgx.sh](https://vgx.sh)
- **GitHub:** [github.com/rohansx/vgx](https://github.com/rohansx/vgx)
- **作者:** [@rohansx](https://twitter.com/rohansx)
安装 • CLI 参考 • API • VS Code • 工作原理
## 问题背景 随着 AI 编程助手(Copilot、Cursor、Claude)的普及,开发者提交的代码并非自己编写,且往往未能完全理解。研究表明: - **42%** 的代码现在有 AI 辅助 ([Sonar 2026](https://www.sonarsource.com/)) - **96%** 的开发者不完全信任 AI 生成的代码 - **45%** 的 AI 生成代码包含安全漏洞 VGX 帮助您**识别 AI 生成的代码**,并在漏洞进入生产环境之前**将其拦截**。 ## 功能特性 | 功能 | 描述 | |---------|-------------| | **AI 代码检测** | 使用风格计量学 + 模式分析识别 AI 生成的代码 | | **安全扫描** | 通过 Semgrep 进行漏洞检测(+ 可选 OpenAI) | | **Pre-commit 钩子** | 在提交前拦截不安全的代码 | | **VS Code 扩展** | 在编辑器中实时进行 AI 检测 | | **离线优先** | 无需 API 密钥。您的代码永远不会离开您的设备。 | | **CI/CD 就绪** | JSON 输出,便于流水线集成 | ## 安装 ### 快速安装 (Linux/macOS) ``` curl -sSL https://vgx.sh/install | bash ``` ### Go 安装 ``` go install github.com/rohansx/vgx@latest ``` ### Docker ``` docker pull ghcr.io/rohansx/vgx:latest docker run -v $(pwd):/code ghcr.io/rohansx/vgx detect --path /code ``` ### 从源码构建 ``` git clone https://github.com/rohansx/vgx.git cd vgx go build -o vgx ./cmd/vgx ``` ## CLI 参考 ### 命令 ``` vgx为负责任地交付 AI 辅助代码的开发者而构建。
标签:AI代码识别, DevSecOps, DLL 劫持, EVTX分析, EVTX分析, Git Pre-commit, Google AI, Go语言, Petitpotam, Semgrep, WordPress安全扫描, 上游代理, 人工智能辅助开发, 代码安全, 大语言模型, 安全扫描, 开源安全工具, 文档安全, 日志审计, 时序注入, 漏洞枚举, 程序破解, 请求拦截, 软件供应链安全, 远程方法调用, 逆向工程平台, 错误基检测, 静态代码分析, 风格分析