-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
133 lines (110 loc) · 4.47 KB
/
run.py
File metadata and controls
133 lines (110 loc) · 4.47 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
"""
Python-C Cross-Language Vulnerability Analyzer - Quick Run Script
运行方式:
python run.py # 默认模式:多Agent分析(包含Fuzz测试和运行时插桩)
python run.py --mode single # 单Agent模式
python run.py --help # 显示帮助信息
"""
import sys
import os
import argparse
# 添加源码路径到Python路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
# 加载 .env 文件
from dotenv import load_dotenv
load_dotenv()
def run_analysis(mode=None, max_workers=None, request_interval=None,
dataset_path=None, output_path=None):
"""运行漏洞分析"""
from tqdm import tqdm
from pyc_bug_analyzer import Config
from pyc_bug_analyzer.analyzers import SingleAgentAnalyzer, MultiAgentAnalyzer
# 创建Config获取默认值(从环境变量读取)
config = Config()
# 使用命令行参数或Config默认值,默认使用多Agent模式
actual_mode = mode if mode is not None else "multi"
actual_max_workers = max_workers if max_workers is not None else config.max_workers
actual_request_interval = request_interval if request_interval is not None else config.request_interval
actual_dataset_path = dataset_path if dataset_path is not None else os.path.join(os.path.dirname(__file__), "datasets", "PyCBench")
actual_output_path = output_path if output_path is not None else os.path.join(os.path.dirname(__file__), "output")
# 更新config
config.max_workers = actual_max_workers
print(f"Python-C Cross-Language Vulnerability Analyzer")
print(f"Mode: {actual_mode}")
print(f"Workers: {actual_max_workers}")
print(f"Request Interval: {actual_request_interval}s")
print(f"Dataset: {actual_dataset_path}")
print(f"Output: {actual_output_path}\n")
if actual_mode == "single":
analyzer = SingleAgentAnalyzer(config)
print("Using Single Agent Mode (StaticAnalysisAgent)")
else:
analyzer = MultiAgentAnalyzer(config)
print("Using Multi Agent Mode (4Agent + Runtime Instrumentation)")
analyzer.output_base_dir = actual_output_path
os.makedirs(actual_output_path, exist_ok=True)
analyzer.request_interval = actual_request_interval
case_paths = analyzer.get_all_cases(actual_dataset_path)
total_cases = len(case_paths)
print(f"Total cases to analyze: {total_cases}")
if total_cases == 0:
print("\nNo cases found! Please check your DATASET_PATH.")
return
summary_file = os.path.join(actual_output_path, "all_agent_responses.json")
with tqdm(total=total_cases, desc="Analyzing cases", unit="case") as pbar:
analyzer.analyze_cases_parallel(
case_paths,
progress_callback=lambda: pbar.update(1),
summary_file=summary_file
)
def main():
parser = argparse.ArgumentParser(
description="Python-C Cross-Language Vulnerability Analyzer",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python run.py # 多Agent模式分析(默认,包含Fuzz测试和运行时插桩)
python run.py --mode single # 单Agent模式分析
python run.py --dataset /path/to/dataset # 指定数据集路径
python run.py --output /path/to/output # 指定输出目录
python run.py --workers 8 # 设置并行线程数
"""
)
parser.add_argument(
"--mode",
choices=["single", "multi"],
default=None,
help="分析模式:single(单Agent)或 multi(多Agent),默认multi(包含Fuzz测试和运行时插桩)"
)
parser.add_argument(
"--dataset",
type=str,
help="数据集路径"
)
parser.add_argument(
"--output",
type=str,
help="输出目录路径"
)
parser.add_argument(
"--workers",
type=int,
default=None,
help=f"并行线程数(默认从Config读取,通常为{16})"
)
parser.add_argument(
"--interval",
type=float,
default=None,
help=f"请求间隔(秒,默认从Config读取,通常为{1.0})"
)
args = parser.parse_args()
run_analysis(
mode=args.mode,
max_workers=args.workers,
request_interval=args.interval,
dataset_path=args.dataset,
output_path=args.output
)
if __name__ == "__main__":
main()