-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1562 lines (1368 loc) · 62.9 KB
/
Copy pathmain.py
File metadata and controls
1562 lines (1368 loc) · 62.9 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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys, os, re, asyncio, subprocess, json, socket, ipaddress, csv
import hashlib
import time
from datetime import datetime
from PySide6.QtCore import Qt, Slot, QTimer
from PySide6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QTextEdit, QLineEdit, QPushButton,
QLabel, QTableWidget, QTableWidgetItem, QSplitter,
QHeaderView, QGroupBox, QCheckBox, QFileDialog,
QInputDialog, QMessageBox, QSpinBox, QDialog,
QGridLayout, QProgressBar)
from PySide6.QtGui import QTextCursor, QColor
from asset_common import (
FAIL_SAMPLE_FILE,
LOW_RISK_UI_LIMIT,
PROJECT_COPY_FILES,
RE_AST,
RE_DOM,
RE_IP,
RESULT_FIELDS,
SCAN_PROGRESS_FILE,
WORKSPACE_DIR,
collect_unique_hosts,
collect_unique_ips,
derive_site_label,
get_base_config,
install_global_crash_handlers,
normalize_task_record,
write_global_log,
)
from asset_dialogs import (
CustomBindDialog,
IPFissionDialog,
RealIPHunterDialog,
ReverseIPDialog,
SubdomainDictDialog,
)
from asset_engine import CollTask
from asset_tasks import ToolTask
from asset_project import (
append_csv_rows,
append_lines,
default_project_root,
fail_sample_path,
is_subpath,
open_local_path,
overwrite_csv_rows,
read_progress_state,
save_settings,
save_text_file,
write_progress_state,
)
CONFIDENCE_SORT_WEIGHT = {
"极危": 0,
"高危": 1,
"中危": 2,
"低危": 3,
}
LOW_RISK_UI_RENDER_LIMIT = 80
MEDIUM_RISK_UI_LIMIT = 300
TOTAL_UI_SOFT_LIMIT = 1200
MOJIBAKE_CONFIDENCE_MAP = {
"6942樺嵄": "高危",
"楂樺嵄": "高危",
"✅ 高危": "高危",
"9241": "高危",
"6935": "中危",
"涓嵄": "中危",
"⚠️ 中危": "中危",
"⚠ 中危": "中危",
"93cb佸嵄": "极危",
"鏋佸嵄": "极危",
"🔥 极危": "极危",
"6d63庡嵄": "低危",
"浣庡嵄": "低危",
"ℹ️ 低危": "低危",
"ℹ 低危": "低危",
}
def normalize_confidence_label(value):
raw = str(value or "").strip()
if not raw:
return ""
if "极危" in raw or "🔥" in raw:
return "极危"
if "高危" in raw or "✅" in raw:
return "高危"
if "中危" in raw or "⚠" in raw:
return "中危"
if "低危" in raw or "ℹ" in raw or "🛈" in raw:
return "低危"
for pattern, normalized in MOJIBAKE_CONFIDENCE_MAP.items():
if pattern in raw:
return normalized
cleaned = (
raw.replace("✅", "")
.replace("⚠️", "")
.replace("⚠", "")
.replace("🔥", "")
.replace("ℹ️", "")
.replace("ℹ", "")
.strip()
)
return cleaned
def normalize_result_row(row_data):
normalized = {field: row_data.get(field, "") for field in RESULT_FIELDS}
raw_conf = str(normalized.get("conf", "") or "").strip()
normalized_conf = normalize_confidence_label(raw_conf)
normalized["conf"] = normalized_conf
changed = normalized_conf != raw_conf
return normalized, changed
class SortableTableWidgetItem(QTableWidgetItem):
def __lt__(self, other):
if isinstance(other, QTableWidgetItem):
self_key = self.data(Qt.UserRole)
other_key = other.data(Qt.UserRole)
if self_key is not None and other_key is not None:
return self_key < other_key
return super().__lt__(other)
class AssetCommander(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("AssetCommander V3.5 - 智能资产过滤版")
self.resize(1450, 950)
self.config = get_base_config()
if not os.path.exists(WORKSPACE_DIR): os.makedirs(WORKSPACE_DIR)
self.proj = None
self.ips, self.doms = set(), set()
self.init_ui()
self.last_scan_stats = {}
self.csv_buffer = []
self.scanned_buffer = []
self.scanned_set = set()
self.scanned_count = 0
self._pending_scanned_keys = set()
self.scan_signature = ""
self.scan_started_at = 0.0
self.scan_resume_base = 0
self.ui_conf_counts = {"极危": 0, "高危": 0, "中危": 0, "低危": 0}
self._risk_cap_notified = set()
self.csv_timer = QTimer(self)
self.csv_timer.timeout.connect(self.flush_csv_buffer)
self.csv_timer.start(2000)
self._update_artifact_buttons()
def _set_pool_text(self, widget, items):
unique_items = list(dict.fromkeys(item for item in items if item))
widget.setPlainText("\n".join(unique_items) + ("\n" if unique_items else ""))
def _current_ip_pool(self):
return collect_unique_ips(self.i_pl.toPlainText().splitlines())
def _current_domain_pool(self):
return collect_unique_hosts(self.d_pl.toPlainText().splitlines())
def _sanitize_ip_pool(self):
self._set_pool_text(self.i_pl, self._current_ip_pool())
def _sanitize_domain_pool(self):
domains = []
for host in self._current_domain_pool():
if not re.fullmatch(RE_IP, host):
domains.append(host)
self._set_pool_text(self.d_pl, domains)
def _set_result_header_tooltips(self):
tips = {
0: "实际发包目标,通常是 IP 或 IP:端口。",
1: "更适合人工识别的站点身份,通常带协议。",
2: "本次碰撞注入的 Host 头。",
3: "Host 碰撞请求返回的状态码。",
4: "Host 碰撞请求响应长度。",
5: "Host 碰撞请求页面标题。",
6: "引擎给出的风险置信度。",
7: "差异判断和命中原因说明。",
}
for column, tip in tips.items():
header_item = self.tb.horizontalHeaderItem(column)
if header_item is not None:
header_item.setToolTip(tip)
def _format_eta(self, seconds):
seconds = max(int(seconds), 0)
mins, secs = divmod(seconds, 60)
hours, mins = divmod(mins, 60)
if hours > 0:
return f"{hours}h {mins}m {secs}s"
if mins > 0:
return f"{mins}m {secs}s"
return f"{secs}s"
def _refresh_main_progress_display(self, current, total, stopped=False):
current = max(0, int(current))
total = max(1, int(total))
elapsed = max(time.time() - self.scan_started_at, 0.0) if self.scan_started_at else 0.0
eta_text = "ETA: 计算中"
if current > self.scan_resume_base and elapsed > 0:
delta_done = current - self.scan_resume_base
remaining = max(total - current, 0)
eta_seconds = int((elapsed / max(delta_done, 1)) * remaining)
eta_text = f"ETA: {self._format_eta(eta_seconds)}"
elif current >= total:
eta_text = "ETA: 0s"
percent = int((current / total) * 100) if total else 0
status = "已停止" if stopped else ("已完成" if current >= total else "对撞进度")
self.pg_bar.setFormat(f"{status} {current} / {total} [{percent}%] {eta_text}")
if hasattr(self, "lbl_scan_eta"):
self.lbl_scan_eta.setText(
f"进度: {current} / {total} | 已耗时: {self._format_eta(elapsed)} | {eta_text}"
)
def _apply_result_item_style(self, item, field, conf):
base_bg = None
base_fg = QColor(201, 209, 217)
if field == "url":
base_bg = QColor(31, 111, 235, 28)
base_fg = QColor(88, 166, 255)
elif field == "site":
base_bg = QColor(35, 134, 54, 28)
base_fg = QColor(63, 185, 80)
elif field == "host":
base_bg = QColor(210, 153, 34, 28)
base_fg = QColor(227, 179, 65)
elif field in {"code", "len"}:
base_fg = QColor(139, 148, 158)
if base_bg is not None:
item.setBackground(base_bg)
item.setForeground(base_fg)
if "极危" in conf:
if field in {"conf", "remark"}:
item.setBackground(QColor(248, 81, 73, 45))
item.setForeground(QColor(255, 123, 114))
elif "高危" in conf:
if field in {"conf", "remark"}:
item.setBackground(QColor(35, 134, 54, 40))
item.setForeground(QColor(88, 166, 255))
elif "中危" in conf:
if field in {"conf", "remark"}:
item.setBackground(QColor(210, 153, 34, 28))
item.setForeground(QColor(210, 153, 34))
elif field not in {"url", "site", "host", "code", "len"}:
item.setForeground(QColor(139, 148, 158))
def select_dict(self):
path, _ = QFileDialog.getOpenFileName(self, "选择无差别碰撞字典", "", "Text Files (*.txt);;All Files (*)")
if path:
self.dict_path = path
try:
size_mb = os.path.getsize(path) / (1024 * 1024)
self.lbl_dict_info.setText(f"已挂载 {os.path.basename(path)} ({size_mb:.2f} MB)")
self.sv_sett()
except Exception as e:
QMessageBox.warning(self, "错误", f"读取字典失败: {e}")
def inject_internal_hosts(self):
internal_hosts = [
"kubernetes.default", "kubernetes.default.svc",
"kubernetes.default.svc.cluster.local",
"host.docker.internal",
"gateway.docker.internal",
"localhost",
]
current_doms = self._current_domain_pool()
self._set_pool_text(self.d_pl, current_doms + internal_hosts)
self.log("SUCCESS", f"成功注入 {len(internal_hosts)} 个高价值内网/云原生 Host,已准备好继续测试。")
def inject_internal_ips(self):
internal_ips = [
"127.0.0.1",
"169.254.169.254",
"100.100.100.200",
"172.17.0.1",
"192.168.1.1",
"10.0.0.1",
]
current_ips = self._current_ip_pool()
self._set_pool_text(self.i_pl, current_ips + internal_ips)
self.log("SUCCESS", f"成功注入 {len(internal_ips)} 个高价值内网 IP(网关 / 元数据 / 回环)。")
def deduplicate_pools(self):
self._sanitize_ip_pool()
self._sanitize_domain_pool()
self.log("SUCCESS", "资产清理完毕,IP 池与域名池已完成一键去重。")
def open_custom_bind(self):
if not self.proj: return QMessageBox.warning(self, "提示", "请先选择或新建一个工程。")
dlg = CustomBindDialog(self, self.proj)
if dlg.exec() == QDialog.Accepted:
all_ips = self._current_ip_pool() + dlg.ips
all_doms = self._current_domain_pool() + dlg.domains
self._set_pool_text(self.i_pl, all_ips)
self._set_pool_text(self.d_pl, all_doms)
self.log("SUCCESS", f"自定义绑定成功,已注入并去重 {len(dlg.ips)} 个 IP 和 {len(dlg.domains)} 个域名。")
def open_ip_fission(self):
self.fission_dialog = IPFissionDialog(self, ip_pool_widget=self.i_pl, proj_dir=self.proj)
self.fission_dialog.show()
def open_ip_hunter(self):
self.hunter_dialog = RealIPHunterDialog(self, ip_pool_widget=self.i_pl, proj_dir=self.proj)
self.hunter_dialog.show()
def open_ip_reverse(self):
ips = self._current_ip_pool()
self.rev_dialog = ReverseIPDialog(self, dom_pool_widget=self.d_pl, ips=ips, proj_dir=self.proj)
self.rev_dialog.show()
def open_dict_ui(self):
if not self.proj: return QMessageBox.warning(self, "提示", "请先打开或新建一个工程。")
dlg = SubdomainDictDialog(self, self.proj)
dlg.exec()
def init_ui(self):
self.setStyleSheet("""
QMainWindow { background-color: #0d1117; }
QWidget { color: #c9d1d9; font-family: 'Consolas', 'Microsoft YaHei'; }
QLineEdit, QTextEdit { background-color: #161b22; border: 1px solid #30363d; border-radius:3px; padding:5px; }
QPushButton { background-color: #238636; color: white; font-weight: bold; border-radius:3px; padding:7px; }
QPushButton:hover { background-color: #2ea043; }
QTableWidget { background-color: #161b22; border: 1px solid #30363d; }
QHeaderView::section { background-color: #21262d; border: 1px solid #30363d; font-weight:bold; }
QGroupBox { border: 1px solid #30363d; margin-top: 10px; font-weight: bold; }
""")
cw = QWidget(); self.setCentralWidget(cw); ml = QVBoxLayout(cw)
pl = QHBoxLayout(); self.lbl_p = QLabel("当前工程: [未载入]"); self.lbl_p.setStyleSheet("color:#e3b341; font-size:14px;")
bn = QPushButton("新建工程"); bn.clicked.connect(self.new_p); bo = QPushButton("打开工程"); bo.clicked.connect(self.open_p)
bc = QPushButton("关闭工程")
bc.setStyleSheet("background-color: #da3633;")
bc.clicked.connect(self.close_project)
pl.addWidget(self.lbl_p); pl.addStretch(1); pl.addWidget(bn); pl.addWidget(bo); pl.addWidget(bc); ml.addLayout(pl)
tl = QHBoxLayout(); self.t_ed = QLineEdit(); self.t_ed.setPlaceholderText("Target Domain or IP Segment")
bf = QPushButton("FScan 收割"); bf.clicked.connect(self.run_f); bo_ofa = QPushButton("OFA 收割"); bo_ofa.clicked.connect(self.run_o)
tl.addWidget(QLabel("目标:")); tl.addWidget(self.t_ed); tl.addWidget(bf); tl.addWidget(bo_ofa); ml.addLayout(tl)
self.t_ed.textChanged.connect(self.sv_sett)
sp = QSplitter(Qt.Horizontal); lb = QWidget(); ll = QVBoxLayout(lb)
self.i_pl = QTextEdit(); self.d_pl = QTextEdit()
self.i_pl.setLineWrapMode(QTextEdit.NoWrap)
self.d_pl.setLineWrapMode(QTextEdit.NoWrap)
self.i_pl.textChanged.connect(lambda: self.sv_f("ips.txt", self.i_pl))
self.d_pl.textChanged.connect(lambda: self.sv_f("domains.txt", self.d_pl))
# ==========================================
# 左侧:IP 池与辅助工具
# ==========================================
i_header = QHBoxLayout()
i_header.addWidget(QLabel("IP 池 (ips.txt) [TCP连接目标]:"))
i_header.addStretch(1)
ll.addLayout(i_header)
i_tools = QGridLayout()
i_tools.setContentsMargins(0, 0, 0, 5)
i_tools.setSpacing(5)
btn_fission = QPushButton("C段裂变与提纯")
btn_fission.setStyleSheet("background-color: #8957e5; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_fission.clicked.connect(self.open_ip_fission)
btn_dedup = QPushButton("一键去重")
btn_dedup.setStyleSheet("background-color: #d29922; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_dedup.clicked.connect(self.deduplicate_pools)
btn_internal_ip = QPushButton("注入内网 IP")
btn_internal_ip.setStyleSheet("background-color: #2ea043; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_internal_ip.clicked.connect(self.inject_internal_ips)
i_tools.addWidget(btn_fission, 0, 0)
i_tools.addWidget(btn_dedup, 0, 1)
i_tools.addWidget(btn_internal_ip, 1, 0, 1, 2)
ll.addLayout(i_tools)
ll.addWidget(self.i_pl)
# ==========================================
# 右侧:域名池与辅助工具
# ==========================================
d_header = QHBoxLayout()
d_header.addWidget(QLabel("域名池 (domains.txt) [伪造 Host 身份]:"))
d_header.addStretch(1)
ll.addLayout(d_header)
d_tools = QGridLayout()
d_tools.setContentsMargins(0, 0, 0, 5)
d_tools.setSpacing(5)
btn_hunter = QPushButton("真实 IP 猎手")
btn_hunter.setStyleSheet("background-color: #238636; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_hunter.clicked.connect(self.open_ip_hunter)
btn_reverse = QPushButton("IP 反查域名")
btn_reverse.setStyleSheet("background-color: #8957e5; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_reverse.clicked.connect(self.open_ip_reverse)
btn_bind = QPushButton("强制绑定")
btn_bind.setStyleSheet("background-color: #1f6feb; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_bind.clicked.connect(self.open_custom_bind)
btn_internal_host = QPushButton("注入内网 Host")
btn_internal_host.setStyleSheet("background-color: #d29922; padding: 5px; font-size: 12px; border-radius: 4px;")
btn_internal_host.clicked.connect(self.inject_internal_hosts)
d_tools.addWidget(btn_hunter, 0, 0)
d_tools.addWidget(btn_reverse, 0, 1)
d_tools.addWidget(btn_bind, 1, 0)
d_tools.addWidget(btn_internal_host, 1, 1)
ll.addLayout(d_tools)
ll.addWidget(self.d_pl)
pg = QGroupBox("碰撞策略与高级绕过"); pgl = QVBoxLayout(pg)
hl1 = QHBoxLayout()
self.c_k = QCheckBox("保留原端口")
self.c_8 = QCheckBox("补齐:80")
self.c_4 = QCheckBox("补齐:443")
self.c_n = QCheckBox("无端口")
for c in [self.c_k, self.c_8, self.c_4, self.c_n]:
c.setChecked(True)
c.stateChanged.connect(self.sv_sett)
hl1.addWidget(c)
hl2 = QHBoxLayout()
self.c_abs = QCheckBox("绝对路径")
self.c_waf = QCheckBox("注入 WAF 绕过头")
self.c_sni = QCheckBox("强同步 SNI")
for c in [self.c_abs, self.c_waf, self.c_sni]:
c.stateChanged.connect(self.sv_sett)
hl2.addWidget(c)
hl2.addStretch(1)
pgl.addLayout(hl1)
pgl.addLayout(hl2)
self.btn_adv_dict = QPushButton("高级字典配置(防 OOM 模式)")
self.btn_adv_dict.setStyleSheet("background-color: #d29922; color: white; padding: 6px; border-radius: 4px;")
self.btn_adv_dict.clicked.connect(self.open_dict_ui)
ll.addWidget(self.btn_adv_dict)
hl_ctrl = QHBoxLayout()
hl_ctrl.addWidget(QLabel("并发:"))
self.spin_threads = QSpinBox()
self.spin_threads.setRange(10, 3000)
self.spin_threads.setValue(150)
self.spin_threads.setStyleSheet("background-color: #161b22; color: #c9d1d9;")
hl_ctrl.addWidget(self.spin_threads)
self.btn_c = QPushButton("启动对撞")
self.btn_c.clicked.connect(self.run_c)
hl_ctrl.addWidget(self.btn_c)
self.btn_stop = QPushButton("停止")
self.btn_stop.setEnabled(False)
self.btn_stop.clicked.connect(self.stop_c)
hl_ctrl.addWidget(self.btn_stop)
self.btn_export = QPushButton("导出新工程")
self.btn_export.setStyleSheet("background-color: #1f6feb;")
self.btn_export.clicked.connect(self.export_project)
hl_ctrl.addWidget(self.btn_export)
self.btn_fail_samples = QPushButton("失败样本")
self.btn_fail_samples.setStyleSheet("background-color: #d29922;")
self.btn_fail_samples.clicked.connect(self.show_fail_samples)
self.btn_fail_samples.setEnabled(False)
hl_ctrl.addWidget(self.btn_fail_samples)
# 3. 进度条(占满剩余空间)
# self.pg_bar = QProgressBar()
# self.pg_bar.setTextVisible(True)
# self.pg_bar.setFormat("%v / %m [%p%]")
#self.pg_bar.setStyleSheet("""
# QProgressBar { border: 1px solid #30363d; background-color: #161b22; color: white; border-radius: 4px; text-align: center; font-weight: bold; }
# QProgressBar::chunk { background-color: #2ea043; border-radius: 3px; }
#""")
#hl_ctrl.addWidget(self.pg_bar, stretch=1)
# 允许左侧面板在窗口缩小时继续压缩
lb.setMinimumWidth(200)
# 将策略和并发控制加入左侧布局
ll.addWidget(pg)
ll.addLayout(hl_ctrl)
sp.addWidget(lb)
# --- 右侧:结果表格区域 ---
self.tb = QTableWidget(0, 8)
self.tb.setHorizontalHeaderLabels(["Target(IP/URL)", "站点身份", "Host 头", "Code", "Len", "Title", "置信度", "智能诊断"])
self.tb.horizontalHeader().setSectionResizeMode(QHeaderView.Interactive)
self.tb.horizontalHeader().setStretchLastSection(True)
self.tb.setColumnWidth(0, 220)
self.tb.setColumnWidth(1, 240)
self.tb.setColumnWidth(2, 220)
self.tb.setColumnWidth(7, 320)
self._table_sort_active = False
self._table_sort_column = -1
self._table_sort_order = Qt.AscendingOrder
header = self.tb.horizontalHeader()
header.setSortIndicatorShown(True)
header.sectionClicked.connect(self.handle_table_sort_click)
self.tb.setSortingEnabled(False)
self._set_result_header_tooltips()
sp.addWidget(self.tb); sp.setStretchFactor(1, 2); ml.addWidget(sp)
self.pg_bar = QProgressBar()
self.pg_bar.setTextVisible(True)
self.pg_bar.setFormat("引擎火力全开:对撞进度 %v / %m [%p%]")
self.pg_bar.setFixedHeight(22)
self.pg_bar.setStyleSheet("""
QProgressBar { border: 1px solid #30363d; background-color: #0d1117; color: #58a6ff; border-radius: 4px; text-align: center; font-weight: bold; font-family: 'Microsoft YaHei'; }
QProgressBar::chunk { background-color: #238636; border-radius: 3px; }
""")
ml.addWidget(self.pg_bar)
self.lbl_scan_eta = QLabel("进度: 0 / 1 | 已耗时: 0s | ETA: 计算中")
self.lbl_scan_eta.setStyleSheet("color: #8b949e;")
ml.addWidget(self.lbl_scan_eta)
self.lv = QTextEdit()
self.lv.setReadOnly(True)
self.lv.setFixedHeight(150)
ml.addWidget(QLabel("战地实时审计日志:"))
ml.addWidget(self.lv)
self.lv.document().setMaximumBlockCount(2000)
self.log_buffer = []
self.log_timer = QTimer(self)
self.log_timer.timeout.connect(self.flush_log_buffer)
self.log_timer.start(200)
# =============== 鏍稿績閫昏緫鍑芥暟 ===============
def sv_f(self, f, w):
if not self.proj:
return
try:
save_text_file(self.proj, f, w.toPlainText())
except Exception:
pass
@Slot(str, str)
def log(self, lvl, m):
self.log_buffer.append((lvl, m))
@Slot(int)
def handle_table_sort_click(self, column):
if self._table_sort_active and self._table_sort_column == column:
self._table_sort_order = (
Qt.DescendingOrder
if self._table_sort_order == Qt.AscendingOrder
else Qt.AscendingOrder
)
else:
self._table_sort_active = True
self._table_sort_column = column
self._table_sort_order = Qt.AscendingOrder
self.apply_table_sort()
def apply_table_sort(self):
if not getattr(self, "_table_sort_active", False):
return
if self._table_sort_column < 0:
return
self.tb.sortItems(self._table_sort_column, self._table_sort_order)
self.tb.horizontalHeader().setSortIndicator(
self._table_sort_column,
self._table_sort_order,
)
def flush_log_buffer(self):
if not hasattr(self, 'pending_file_log'):
self.pending_file_log = ""
# 1. 先刷新界面日志
if hasattr(self, 'log_buffer') and self.log_buffer:
tm = datetime.now().strftime("%H:%M:%S")
cs = {"INFO":"#58a6ff", "SUCCESS":"#3fb950", "ERROR":"#f85149", "DEBUG":"#8b949e", "FATAL":"#ff0000"}
gui_html = []
for lvl, m in self.log_buffer:
gui_html.append(f'<span style="color:#8b949e">[{tm}]</span> <span style="color:{cs.get(lvl,"#fff")}">[{lvl}]</span> {m}')
self.pending_file_log += f"[{tm}] [{lvl}] {m}\n"
self.lv.append("<br>".join(gui_html))
self.lv.moveCursor(QTextCursor.End)
self.log_buffer.clear()
# 2. 再同步写入磁盘日志
if self.proj and self.pending_file_log:
try:
with open(os.path.join(self.proj, "runtime.log"), 'a', encoding='utf-8') as f:
f.write(self.pending_file_log)
# 只有真正落盘成功,才清空待写缓存
self.pending_file_log = ""
except Exception as e:
# 写入失败时保留缓存,等待下一个周期继续重试
pass
@Slot(str)
def cln(self, t):
if not self.proj: return
self.i_pl.blockSignals(True); self.d_pl.blockSignals(True)
ip_c, dom_c = False, False
for ip in collect_unique_ips(re.findall(RE_AST, t)):
if ip not in self.ips:
self.ips.add(ip)
self.i_pl.append(ip)
ip_c = True
ex = [".html", ".js", ".css", ".py", ".exe", "fscan", "version"]
candidates = collect_unique_hosts(re.findall(RE_DOM, t))
for host in candidates:
if "." in host and host not in self.doms and not any(x in host.lower() for x in ex):
self.doms.add(host)
self.d_pl.append(host)
dom_c = True
self.i_pl.blockSignals(False); self.d_pl.blockSignals(False)
if ip_c: self.sv_f("ips.txt", self.i_pl)
if dom_c: self.sv_f("domains.txt", self.d_pl)
def run_f(self):
if not self.proj: return QMessageBox.warning(self, "提示", "请先选择工程")
if hasattr(self, 'th') and self.th.isRunning(): return self.log("ERROR", "外部工具仍在运行,请等待结束。")
t = self.t_ed.text().strip(); st = t
try:
if not re.match(RE_IP, t) and "/" not in t:
clean_t = t.replace("http://", "").replace("https://", "").split(':')[0].split('/')[0]
ip = socket.gethostbyname(clean_t)
st = str(ipaddress.ip_network(f"{ip}/24", False).network_address)+"/24"
except: pass
cmd = self.config.get("fscan_cmd", "").replace("{target}", st)
self.th = ToolTask("FScan", cmd); self.th.log_sig.connect(self.log); self.th.ast_sig.connect(self.cln); self.th.start()
def run_o(self):
if not self.proj: return QMessageBox.warning(self, "提示", "请先选择工程")
if hasattr(self, 'th') and self.th.isRunning(): return self.log("ERROR", "外部工具仍在运行,请等待结束。")
raw_target = self.t_ed.text().strip()
ip_target = collect_unique_ips([raw_target])
if ip_target:
target = ip_target[0]
else:
host_target = collect_unique_hosts([raw_target])
target = host_target[0] if host_target else ""
if not target:
return self.log("ERROR", "OFA 目标为空。")
cmd_tpl = self.config.get("oneforall_cmd", "").strip()
if not cmd_tpl:
return self.log("ERROR", "未配置 OneForAll 命令。")
cmd = cmd_tpl.replace("{target}", target)
self.th = ToolTask("OFA", cmd); self.th.log_sig.connect(self.log); self.th.ast_sig.connect(self.cln); self.th.start()
def stop_c(self):
if hasattr(self, 'cth') and self.cth.isRunning():
self.log("INFO", "正在发送强制截断指令...")
self.btn_stop.setEnabled(False); self.btn_stop.setText("截断中...")
self.pg_bar.setFormat(f"正在停止 {self.pg_bar.value()} / {self.pg_bar.maximum()} [请等待]")
self.cth.stop()
def force_save(self):
if not self.proj: return
try:
rows = []
for row in range(self.tb.rowCount()):
row_data = {
"url": self.tb.item(row, 0).text() if self.tb.item(row, 0) else "",
"site": self.tb.item(row, 1).text() if self.tb.item(row, 1) else "",
"host": self.tb.item(row, 2).text() if self.tb.item(row, 2) else "",
"code": self.tb.item(row, 3).text() if self.tb.item(row, 3) else "",
"len": self.tb.item(row, 4).text() if self.tb.item(row, 4) else "",
"title": self.tb.item(row, 5).text() if self.tb.item(row, 5) else "",
"conf": self.tb.item(row, 6).text() if self.tb.item(row, 6) else "",
"remark": self.tb.item(row, 7).text() if self.tb.item(row, 7) else "",
}
if row_data["url"]:
rows.append(row_data)
overwrite_csv_rows(os.path.join(self.proj, "results.csv"), RESULT_FIELDS, rows)
self.log("SUCCESS", "战果已强制覆盖保存,并已转换为标准 CSV 格式。")
QMessageBox.information(self, "成功", "当前表格数据已完整保存为 CSV,可以直接用 Excel 打开。")
except Exception as e:
self.log("ERROR", f"保存失败: {str(e)}")
def _default_project_root(self):
return default_project_root(self.proj, WORKSPACE_DIR)
def _fail_sample_path(self):
return fail_sample_path(self.proj, FAIL_SAMPLE_FILE)
def _is_subpath(self, candidate, parent):
return is_subpath(candidate, parent)
def _open_local_path(self, path):
try:
open_local_path(path)
return True
except Exception as e:
QMessageBox.warning(self, "错误", f"打开失败: {e}")
return False
def _update_artifact_buttons(self):
if hasattr(self, 'btn_fail_samples'):
fail_path = self._fail_sample_path()
self.btn_fail_samples.setEnabled(bool(fail_path and os.path.exists(fail_path)))
def _progress_state_path(self, proj_dir=None):
base_dir = proj_dir or self.proj
if not base_dir:
return ""
return os.path.join(base_dir, SCAN_PROGRESS_FILE)
def _read_progress_state(self, proj_dir=None):
try:
return read_progress_state(proj_dir or self.proj, SCAN_PROGRESS_FILE)
except Exception as e:
self.log("ERROR", f"读取扫描进度失败: {e}")
return {}
def _load_dict_config(self, proj_dir=None, quiet=True):
proj_dir = proj_dir or self.proj
if not proj_dir:
return {}
path = os.path.join(proj_dir, "dict_config.json")
if not os.path.exists(path):
return {}
try:
with open(path, 'r', encoding='utf-8') as f:
return json.load(f) or {}
except Exception as e:
if not quiet:
self.log("ERROR", f"读取字典配置失败: {e}")
return {}
def _build_scan_signature(self, ip_list, domain_list, policies, dict_config):
dict_config = dict(dict_config or {})
dict_path = os.path.abspath(dict_config.get("path", "")) if dict_config.get("path") else ""
dict_meta = {"path": dict_path}
if dict_path and os.path.exists(dict_path):
try:
dict_meta["size"] = os.path.getsize(dict_path)
dict_meta["mtime"] = int(os.path.getmtime(dict_path))
except OSError:
pass
payload = {
"ips": sorted(dict.fromkeys(str(item).strip() for item in ip_list if str(item).strip())),
"domains": sorted(dict.fromkeys(str(item).strip() for item in domain_list if str(item).strip())),
"policies": {key: bool(value) for key, value in sorted((policies or {}).items())},
"dict": {
"enabled": bool(dict_config.get("enabled")),
"prefixes": [item.strip() for item in str(dict_config.get("prefixes", "")).splitlines() if item.strip()],
"suffixes": [item.strip() for item in str(dict_config.get("suffixes", "")).splitlines() if item.strip()],
"meta": dict_meta,
},
}
raw = json.dumps(payload, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
return hashlib.blake2b(raw.encode("utf-8"), digest_size=16).hexdigest()
def _estimate_scan_total(self, ip_list, domain_list, policies, dict_config):
url_variants_per_ip = 0
if policies.get("k", True):
url_variants_per_ip += 2
if policies.get("80", True):
url_variants_per_ip += 1
if policies.get("443", True):
url_variants_per_ip += 1
if policies.get("n", True):
url_variants_per_ip += 2
if url_variants_per_ip == 0:
url_variants_per_ip = 4
dom_variants = len(domain_list)
dict_path = str((dict_config or {}).get("path", "") or "").strip()
if (
dict_config
and dict_config.get("enabled")
and dict_path
and os.path.exists(dict_path)
):
try:
with open(dict_path, "rb") as handle:
dict_lines = sum(1 for _ in handle)
except OSError:
dict_lines = 0
prefix_count = max(
1,
len([item for item in str(dict_config.get("prefixes", "")).splitlines() if item.strip()]),
)
suffix_count = max(
1,
len([item for item in str(dict_config.get("suffixes", "")).splitlines() if item.strip()]),
)
dom_variants += dict_lines * prefix_count * suffix_count
return len(ip_list) * url_variants_per_ip * dom_variants
def _scanned_log_path(self, proj_dir=None, signature=None):
proj_dir = proj_dir or self.proj
if not proj_dir:
return ""
if signature:
return os.path.join(proj_dir, f"scanned.{signature[:8]}.log")
return os.path.join(proj_dir, "scanned.log")
def _migrate_legacy_scanned_log(self, proj_dir=None, signature=None):
proj_dir = proj_dir or self.proj
signature = signature or self.scan_signature
if not proj_dir or not signature:
return
legacy_path = self._scanned_log_path(proj_dir=proj_dir, signature=None)
signature_path = self._scanned_log_path(proj_dir=proj_dir, signature=signature)
if not os.path.exists(legacy_path) or os.path.exists(signature_path):
return
try:
os.replace(legacy_path, signature_path)
except OSError:
return
def _set_scan_config_enabled(self, enabled):
widgets = [
getattr(self, "t_ed", None),
getattr(self, "i_pl", None),
getattr(self, "d_pl", None),
getattr(self, "c_k", None),
getattr(self, "c_8", None),
getattr(self, "c_4", None),
getattr(self, "c_n", None),
getattr(self, "c_abs", None),
getattr(self, "c_waf", None),
getattr(self, "c_sni", None),
getattr(self, "spin_threads", None),
getattr(self, "btn_adv_dict", None),
]
for widget in widgets:
if widget is not None:
widget.setEnabled(bool(enabled))
def _load_scanned_index(self, proj_dir=None, quiet=False, signature=None):
proj_dir = proj_dir or self.proj
self.scanned_set = set()
self.scanned_count = 0
self._pending_scanned_keys.clear()
if not proj_dir:
return
scanned_path = self._scanned_log_path(proj_dir=proj_dir, signature=signature)
if not os.path.exists(scanned_path):
return
try:
loaded = set()
with open(scanned_path, 'r', encoding='utf-8', errors='ignore') as f:
for line in f:
task_key = normalize_task_record(line)
if task_key:
loaded.add(task_key)
self.scanned_set = loaded
self.scanned_count = len(loaded)
except Exception as e:
self.scanned_set = set()
self.scanned_count = 0
self._pending_scanned_keys.clear()
if not quiet:
self.log("ERROR", f"读取 scanned.log 失败: {e}")
def _write_progress_state(self, current=None, total=None, active=None, stats=None):
if not self.proj:
return
scanned_current = self.scanned_count
try:
write_progress_state(
self.proj,
SCAN_PROGRESS_FILE,
scanned_current=scanned_current,
current=current,
total=total,
bar_total=self.pg_bar.maximum() if hasattr(self, 'pg_bar') else 0,
active=active,
stats=stats,
scan_signature=self.scan_signature,
)
except Exception as e:
self.log("ERROR", f"保存扫描进度失败: {e}")
def sv_sett(self):
if not self.proj:
return
try:
save_settings(self.proj, {
'target': self.t_ed.text(),
'k': self.c_k.isChecked(),
'80': self.c_8.isChecked(),
'443': self.c_4.isChecked(),
'n': self.c_n.isChecked(),
'abs': self.c_abs.isChecked(),
'waf': self.c_waf.isChecked(),
'sni': self.c_sni.isChecked(),
'threads': self.spin_threads.value(),
})
except Exception as e:
self.log("ERROR", f"保存设置失败: {e}")
def persist_project_state(self):
if not self.proj:
return
try:
self.sv_f("ips.txt", self.i_pl)
self.sv_f("domains.txt", self.d_pl)
self.sv_sett()
except Exception:
pass
self.flush_log_buffer()
self.flush_csv_buffer()
self._write_progress_state(
current=self.scanned_count,
total=max(self.pg_bar.maximum(), self.scanned_count, 1),
active=bool(hasattr(self, 'cth') and self.cth.isRunning()),
)
def new_p(self):
root = QFileDialog.getExistingDirectory(self, "选择工程根目录", self._default_project_root())
if not root:
return
name, ok = QInputDialog.getText(self, "新建工程", "输入工程名")
if not ok:
return
name = name.strip().strip("\\/")
if not name:
return
proj_dir = os.path.abspath(os.path.join(root, name))
try:
os.makedirs(proj_dir, exist_ok=True)
except Exception as e:
return QMessageBox.warning(self, "错误", f"创建工程失败: {e}")
self.load_p(proj_dir)
def open_p(self):
proj_dir = QFileDialog.getExistingDirectory(self, "选择工程", self._default_project_root())
if proj_dir:
self.load_p(proj_dir)
def close_project(self):
if hasattr(self, 'cth') and self.cth.isRunning():
return QMessageBox.warning(self, "提示", "请先停止当前扫描任务,再关闭工程。")
self.persist_project_state()
self.proj = None
self.ips.clear()
self.doms.clear()
self.scanned_set.clear()
self.scanned_count = 0
self.scan_signature = ""
self.csv_buffer.clear()
self.scanned_buffer.clear()
self._pending_scanned_keys.clear()
self.i_pl.blockSignals(True)
self.d_pl.blockSignals(True)
self.t_ed.blockSignals(True)
self.i_pl.clear()
self.d_pl.clear()
self.t_ed.clear()
self.i_pl.blockSignals(False)
self.d_pl.blockSignals(False)
self.t_ed.blockSignals(False)
self.tb.setRowCount(0)
self.pg_bar.setMaximum(1)
self.pg_bar.setValue(0)