-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure_agent.py
More file actions
145 lines (110 loc) · 4.31 KB
/
Copy pathsecure_agent.py
File metadata and controls
145 lines (110 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SecureAgent - 通用安全测试 Agent
================================
用法:
python secure_agent.py --plugin <plugin> --target <target> [options]
示例:
# LLM 安全测试
python secure_agent.py --plugin llm_security --target "openai:gpt-4"
# Web 安全扫描
python secure_agent.py --plugin web_security --target "https://example.com"
# 列出所有插件
python secure_agent.py --list
"""
import argparse
import sys
from pathlib import Path
# 添加项目路径
sys.path.insert(0, str(Path(__file__).parent))
from core import Scanner
def main():
parser = argparse.ArgumentParser(
description="SecureAgent - 通用安全测试 Agent",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument("--list", "-l", action="store_true",
help="列出所有可用插件")
parser.add_argument("--info", "-i", metavar="PLUGIN",
help="显示插件详细信息")
parser.add_argument("--plugin", "-p",
help="指定插件名称")
parser.add_argument("--target", "-t",
help="扫描目标")
parser.add_argument("--config", "-c",
help="配置文件路径")
parser.add_argument("--output", "-o", default="reports",
help="报告输出目录")
parser.add_argument("--format", "-f",
choices=["json", "markdown"],
default="json",
help="报告格式")
parser.add_argument("--rules", nargs="+",
help="指定要执行的规则")
args = parser.parse_args()
# 初始化扫描器
scanner = Scanner(config_path=args.config)
# 列出插件
if args.list:
print("\n" + "="*60)
print("🔌 SecureAgent 可用插件")
print("="*60)
plugins = scanner.list_plugins()
for p in plugins:
print(f"\n📦 {p['name']} v{p['version']}")
print(f" {p['description']}")
print(f" 规则数: {p['rules_count']}")
print(f" 作者: {p['author']}")
print("\n" + "="*60)
return
# 显示插件信息
if args.info:
plugin = scanner.get_plugin(args.info)
if plugin:
print(f"\n📦 {plugin.name} v{plugin.version}")
print(f" {plugin.description}")
print(f" 作者: {plugin.author}")
print(f"\n规则列表:")
for rule in plugin.get_rules()[:10]:
print(f" - [{rule['severity']}] {rule['id']}: {rule['title']}")
if len(plugin.get_rules()) > 10:
print(f" ... 还有 {len(plugin.get_rules()) - 10} 个规则")
else:
print(f"❌ 插件未找到: {args.info}")
return
# 执行扫描
if not args.plugin or not args.target:
parser.print_help()
print("\n⚠️ 请指定 --plugin 和 --target")
return
print("\n" + "="*60)
print(f"🚀 SecureAgent 扫描")
print("="*60)
result = scanner.scan(args.plugin, args.target, args.rules)
# 显示结果
print(f"\n📊 扫描结果")
print(f" 状态: {result.status.value}")
print(f" 耗时: {result.duration_ms:.2f}ms")
if result.findings:
print(f"\n⚠️ 发现 {len(result.findings)} 个问题:")
for f in result.findings[:10]:
print(f" [{f.severity.value.upper()}] {f.title}")
if result.error:
print(f"\n❌ 错误: {result.error}")
# 保存报告
if args.output:
import json
from datetime import datetime
output_dir = Path(args.output)
output_dir.mkdir(exist_ok=True)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"scan_{args.plugin}_{timestamp}.{args.format}"
scanner.export_report(
type('Report', (), {'to_dict': lambda self: result.to_dict(),
'summary': {'total': 1, 'passed': 1 if result.status.value == 'passed' else 0}})(),
str(output_dir / filename),
args.format
)
if __name__ == "__main__":
main()