-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.py
More file actions
386 lines (321 loc) · 13.3 KB
/
main.py
File metadata and controls
386 lines (321 loc) · 13.3 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GitHub Secret Scanner Pro - 主程序入口
================================================================================
⚠️ 免责声明 ⚠️
================================================================================
本项目仅用于安全研究和授权测试,严禁用于非法扫描。
使用者需自行承担法律责任。
================================================================================
特性:
- Rich TUI 仪表盘实时显示
- 熵值过滤 + 域名黑名单
- AsyncIO + aiohttp 高并发验证 (100 并发)
- aiohttp 异步批量下载文件
- 深度价值评估 (GPT-4 探测、余额检测、RPM 透视)
- Producer-Consumer 架构
"""
import sys
import signal
import queue
import threading
import time
import argparse
import csv
from datetime import datetime
from config import config
from database import Database, KeyStatus
from scanner import start_scanner
from validator import start_validators
from ui import Dashboard
from source_pastebin import start_pastebin_scanner
from source_gist import start_gist_scanner
from source_searchcode import start_searchcode_scanner
from source_gitlab import start_gitlab_scanner
from source_realtime import start_realtime_scanner
class SecretScanner:
"""密钥扫描系统主类"""
def __init__(self, enable_pastebin: bool = False, enable_gist: bool = False,
enable_searchcode: bool = False, enable_gitlab: bool = False,
enable_realtime: bool = False, pastebin_api_key: str = ""):
self.stop_event = threading.Event()
self.result_queue = queue.Queue(maxsize=1000)
self.db = Database(config.db_path)
self.dashboard = Dashboard()
self.scanner_thread = None
self.validator_threads = []
self.pastebin_thread = None
self.gist_thread = None
self.searchcode_thread = None
self.gitlab_thread = None
self.realtime_thread = None
# 扫描源开关
self.enable_pastebin = enable_pastebin
self.enable_gist = enable_gist
self.enable_searchcode = enable_searchcode
self.enable_gitlab = enable_gitlab
self.enable_realtime = enable_realtime
self.pastebin_api_key = pastebin_api_key
# 信号处理
signal.signal(signal.SIGINT, self._signal_handler)
signal.signal(signal.SIGTERM, self._signal_handler)
def _signal_handler(self, signum, frame):
"""信号处理"""
self.stop()
def start(self):
"""启动扫描系统"""
# 初始化仪表盘统计
self.dashboard.update_stats(
total_tokens=len(config.github_tokens),
is_running=True
)
# 启动验证器(Consumer)
self.validator_threads = start_validators(
self.result_queue,
self.db,
self.stop_event,
dashboard=self.dashboard,
num_workers=2
)
# 启动 GitHub 扫描器(Producer)
self.scanner_thread = start_scanner(
self.result_queue,
self.db,
self.stop_event,
dashboard=self.dashboard
)
# 启动 Pastebin 扫描器
if self.enable_pastebin:
self.pastebin_thread = start_pastebin_scanner(
self.result_queue,
self.stop_event,
dashboard=self.dashboard,
api_key=self.pastebin_api_key
)
self.dashboard.add_log("[Pastebin] 扫描源已启用", "INFO")
# 启动 Gist 扫描器
if self.enable_gist:
self.gist_thread = start_gist_scanner(
self.result_queue,
self.stop_event,
dashboard=self.dashboard
)
self.dashboard.add_log("[Gist] 扫描源已启用", "INFO")
# 启动 SearchCode 扫描器
if self.enable_searchcode:
self.searchcode_thread = start_searchcode_scanner(
self.result_queue,
self.stop_event,
dashboard=self.dashboard
)
self.dashboard.add_log("[SearchCode] 扫描源已启用", "INFO")
# 启动 GitLab 扫描器
if self.enable_gitlab:
self.gitlab_thread = start_gitlab_scanner(
self.result_queue,
self.stop_event,
dashboard=self.dashboard
)
self.dashboard.add_log("[GitLab] 扫描源已启用", "INFO")
# 启动实时监控扫描器
if self.enable_realtime:
self.realtime_thread = start_realtime_scanner(
self.result_queue,
self.stop_event,
dashboard=self.dashboard
)
self.dashboard.add_log("[Realtime] 实时监控已启用", "INFO")
# 启动 TUI
with self.dashboard.start():
try:
while not self.stop_event.is_set():
self.dashboard.update_stats(queue_size=self.result_queue.qsize())
self.dashboard.refresh()
time.sleep(0.25)
except KeyboardInterrupt:
pass
finally:
self.stop()
def stop(self):
"""停止系统"""
if self.stop_event.is_set():
return
self.dashboard.stop()
self.stop_event.set()
if self.scanner_thread and self.scanner_thread.is_alive():
self.scanner_thread.join(timeout=3)
if self.pastebin_thread and self.pastebin_thread.is_alive():
self.pastebin_thread.join(timeout=3)
if self.gist_thread and self.gist_thread.is_alive():
self.gist_thread.join(timeout=3)
if self.searchcode_thread and self.searchcode_thread.is_alive():
self.searchcode_thread.join(timeout=3)
if self.gitlab_thread and self.gitlab_thread.is_alive():
self.gitlab_thread.join(timeout=3)
if self.realtime_thread and self.realtime_thread.is_alive():
self.realtime_thread.join(timeout=3)
for thread in self.validator_threads:
if thread.is_alive():
thread.join(timeout=1)
def export_keys(db_path: str, output_file: str, status_filter: str = None):
"""导出 Key"""
from rich.console import Console
from rich.table import Table
console = Console()
db = Database(db_path)
if status_filter:
try:
status = KeyStatus(status_filter)
keys = db.get_keys_by_status(status)
except ValueError:
console.print(f"[red]无效状态: {status_filter}[/]")
return
else:
keys = db.get_valid_keys()
if not keys:
console.print("[yellow]没有符合条件的 Key[/]")
return
# 写入文件
with open(output_file, 'w', encoding='utf-8') as f:
f.write(f"# GitHub Secret Scanner 导出结果\n")
f.write(f"# 时间: {datetime.now().isoformat()}\n")
f.write(f"# 数量: {len(keys)}\n")
f.write("=" * 60 + "\n\n")
for key in keys:
f.write(f"平台: {key.platform}\n")
f.write(f"状态: {key.status}\n")
f.write(f"Key: {key.api_key}\n")
f.write(f"URL: {key.base_url}\n")
f.write(f"信息: {key.balance}\n")
f.write(f"来源: {key.source_url}\n")
f.write("-" * 40 + "\n\n")
console.print(f"[green]✓ 已导出 {len(keys)} 个 Key 到 {output_file}[/]")
def export_keys_csv(db_path: str, output_file: str, status_filter: str = None):
"""导出 Key 到 CSV 文件"""
from rich.console import Console
console = Console()
db = Database(db_path)
if status_filter:
try:
status = KeyStatus(status_filter)
keys = db.get_keys_by_status(status)
except ValueError:
console.print(f"[red]无效状态: {status_filter}[/]")
return
else:
keys = db.get_valid_keys()
if not keys:
console.print("[yellow]没有符合条件的 Key[/]")
return
# 写入 CSV 文件
fieldnames = [
"id",
"platform",
"status",
"api_key",
"base_url",
"balance",
"source_url",
"model_tier",
"rpm",
"is_high_value",
"found_time",
]
with open(output_file, "w", encoding="utf-8", newline="") as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
for key in keys:
writer.writerow({
"id": getattr(key, "id", ""),
"platform": key.platform,
"status": key.status,
"api_key": key.api_key,
"base_url": key.base_url,
"balance": key.balance,
"source_url": key.source_url,
"model_tier": key.model_tier,
"rpm": key.rpm,
"is_high_value": int(bool(getattr(key, "is_high_value", False))),
"found_time": key.found_time.isoformat() if getattr(key, "found_time", None) else "",
})
console.print(f"[green]✓ 已导出 {len(keys)} 个 Key 到 CSV: {output_file}[/]")
def show_stats(db_path: str):
"""显示统计"""
from rich.console import Console
from rich.table import Table
from rich.panel import Panel
from rich import box
console = Console()
db = Database(db_path)
stats = db.get_stats()
# 统计表
table = Table(show_header=False, box=box.ROUNDED)
table.add_column("项目", style="cyan")
table.add_column("数量", justify="right", style="white")
table.add_row("总 Key 数", str(stats['total']))
table.add_row("", "")
statuses = stats.get('statuses', {})
table.add_row("[green]✓ 有效[/]", f"[green]{statuses.get('valid', 0)}[/]")
table.add_row("[yellow]💰 配额耗尽[/]", f"[yellow]{statuses.get('quota_exceeded', 0)}[/]")
table.add_row("[red]✗ 无效[/]", f"[red]{statuses.get('invalid', 0)}[/]")
table.add_row("[magenta]🔌 连接错误[/]", f"[magenta]{statuses.get('connection_error', 0)}[/]")
if stats.get('platforms'):
table.add_row("", "")
table.add_row("[bold]平台分布[/]", "")
for platform, count in stats['platforms'].items():
table.add_row(f" {platform}", str(count))
console.print(Panel(table, title="📊 数据库统计", border_style="cyan"))
def main():
"""主函数"""
parser = argparse.ArgumentParser(
description="GitHub Secret Scanner Pro",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument('--export', type=str, metavar='FILE', help='导出 Key 到文本文件')
parser.add_argument('--export-csv', type=str, metavar='CSV', help='导出 Key 到 CSV 文件')
parser.add_argument('--status', type=str, help='导出状态过滤 (valid/quota_exceeded)')
parser.add_argument('--stats', action='store_true', help='显示统计')
parser.add_argument('--db', type=str, default='leaked_keys.db', help='数据库路径')
parser.add_argument('--proxy', type=str, help='代理地址')
# 扫描源选项
parser.add_argument('--pastebin', action='store_true', help='启用 Pastebin 扫描源')
parser.add_argument('--pastebin-key', type=str, default='', help='Pastebin Pro API Key')
parser.add_argument('--gist', action='store_true', help='启用 GitHub Gist 扫描源')
parser.add_argument('--searchcode', action='store_true', help='启用 SearchCode 扫描源')
parser.add_argument('--gitlab', action='store_true', help='启用 GitLab Snippets 扫描源')
parser.add_argument('--realtime', action='store_true', help='启用实时监控 (GitHub Events)')
parser.add_argument('--all-sources', action='store_true', help='启用所有扫描源')
args = parser.parse_args()
if args.proxy:
config.proxy_url = args.proxy
if args.db:
config.db_path = args.db
# 导出模式
if args.export or args.export_csv:
if args.export:
export_keys(config.db_path, args.export, args.status)
if args.export_csv:
export_keys_csv(config.db_path, args.export_csv, args.status)
return
# 统计模式
if args.stats:
show_stats(config.db_path)
return
# 扫描模式
enable_pastebin = args.pastebin or args.all_sources
enable_gist = args.gist or args.all_sources
enable_searchcode = args.searchcode or args.all_sources
enable_gitlab = args.gitlab or args.all_sources
enable_realtime = args.realtime or args.all_sources
scanner = SecretScanner(
enable_pastebin=enable_pastebin,
enable_gist=enable_gist,
enable_searchcode=enable_searchcode,
enable_gitlab=enable_gitlab,
enable_realtime=enable_realtime,
pastebin_api_key=args.pastebin_key
)
scanner.start()
if __name__ == "__main__":
main()