Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
289 lines (263 loc) · 10.6 KB
/
Copy pathmain.py
File metadata and controls
289 lines (263 loc) · 10.6 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
"""
EtherFix: Universal Multi-Chain EtherHiding & ClickFix Threat Extraction Platform.
CLI Orchestrator and Incident Response Intelligence Generator.
"""
import sys
import argparse
from typing import List
from core.bootstrap import init_environment
init_environment()
from core.models import SOCReport, CryptographicMaterial
from engines.crypto_engine import calculate_shannon_entropy
from engines.evm_inspector import EVMInspector
from engines.explorers import MultiChainExplorerClient
from engines.artifact_parser import ArtifactParser
from engines.stage2_prober import Stage2Prober
from engines.enricher import ThreatIntelligenceEnricher
from core.tor_controller import resolve_proxy_environment
from core.logger import get_logger, configure_logging
from core.pipeline_helpers import (
parse_targets,
phase1_discover_attack_surface,
phase2_dump_onchain_payloads,
phase3_deobfuscate_and_parse,
phase4_probe_stage2,
phase5_enrich_threat_intelligence,
phase6_export_and_register
)
from reporting.console import print_banner
logger = get_logger(__name__)
def build_arg_parser() -> argparse.ArgumentParser:
"""Builds CLI arguments parser."""
parser = argparse.ArgumentParser(
description="EtherFix: Universal Multi-Chain EtherHiding & ClickFix Threat Extractor",
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
"target",
nargs="+",
help="Target compromised website URL (e.g. https://example.com) OR Smart Contract Address(es) (0x...)"
)
parser.add_argument(
"--report",
dest="report_dir",
default=None,
help="Directory name under 'reports/' to export the complete incident bundle (report.json & obfuscated_artifacts.json)"
)
parser.add_argument(
"--json",
dest="json_output",
default=None,
help="[DEPRECATO] Utilizzare '--report <cartella>'. Esporta il bundle di report in formato compatibile."
)
parser.add_argument(
"--probe",
dest="probe",
action="store_true",
default=False,
help="Enable active HTTP probing of discovered C2 staging URLs to extract Stage 2 secondary droppers"
)
parser.add_argument(
"--enrich",
dest="enrich",
action="store_true",
default=False,
help="Perform deep OSINT Threat Intelligence (RDAP Registrar, Domain Age, ASN, and Firewall Blocking guide)"
)
parser.add_argument(
"--proxy",
dest="proxy",
default=None,
help="Route network traffic through a custom HTTP or SOCKS5 proxy (e.g. http://127.0.0.1:8080 or socks5://127.0.0.1:1080)"
)
parser.add_argument(
"--tor",
dest="tor",
nargs="?",
const=True,
default=None,
help="Route traffic through local Tor daemon (127.0.0.1:9050/9150). Optional country code for exit node (e.g. --tor CH, --tor US, --tor DE)"
)
parser.add_argument(
"--rpc",
dest="custom_rpc",
default=None,
help="Specify a custom RPC endpoint URL to override automatic chain resolution"
)
parser.add_argument(
"--headed",
dest="headed",
action="store_true",
help="Run Chromium in visible/headed mode (useful for debugging)"
)
parser.add_argument(
"--verbose", "-v",
dest="verbose",
action="store_true",
default=False,
help="Enable detailed debug logging output"
)
parser.add_argument(
"--quiet", "-q",
dest="quiet",
action="store_true",
default=False,
help="Suppress informational logs and only show warnings/errors"
)
return parser
def main() -> None:
print_banner()
parser = build_arg_parser()
args = parser.parse_args()
configure_logging(verbose=args.verbose, quiet=args.quiet)
# 1. Target & Environment Initialization
try:
target_input, is_direct_contracts, raw_targets = parse_targets(args.target)
except ValueError as e:
print(f"[-] {e}")
sys.exit(1)
proxy_url, geo_profile, proxy_msg = resolve_proxy_environment(args.proxy, args.tor)
if proxy_msg:
print(f"\n{proxy_msg}")
# Engine Instances
evm_inspector = EVMInspector()
explorer_client = MultiChainExplorerClient()
artifact_parser = ArtifactParser()
stage2_prober = Stage2Prober(proxy=proxy_url, geo_profile=geo_profile)
enricher = ThreatIntelligenceEnricher()
# -------------------------------------------------------------
# FASE 1: TARGET DISCOVERY & ATTACK SURFACE MAPPING
# -------------------------------------------------------------
(
contract_profiles,
raw_corpus_items,
all_obfuscated_artifacts,
candidate_keys,
captured_responses
) = phase1_discover_attack_surface(
target_input=target_input,
is_direct_contracts=is_direct_contracts,
raw_targets=raw_targets,
custom_rpc=args.custom_rpc,
headed=args.headed,
proxy_url=proxy_url,
geo_profile=geo_profile,
evm_inspector=evm_inspector
)
if not contract_profiles and not raw_corpus_items and not captured_responses:
print("\n[-] Nessuna interazione Web3, script o comando ClickFix individuato sul target.")
sys.exit(0)
if contract_profiles:
print(f"\n[+] Profili Web3 identificati ({len(contract_profiles)} contratti):")
# -------------------------------------------------------------
# FASE 2: EVM STATE DUMP & HISTORICAL EXPLORATION
# -------------------------------------------------------------
phase2_dump_onchain_payloads(
contract_profiles=contract_profiles,
raw_corpus_items=raw_corpus_items,
all_obfuscated_artifacts=all_obfuscated_artifacts,
candidate_keys=candidate_keys,
captured_responses=captured_responses,
evm_inspector=evm_inspector,
explorer_client=explorer_client,
custom_rpc=args.custom_rpc
)
# -------------------------------------------------------------
# FASE 3: DEEP RECURSIVE DEOBFUSCATION & ARTIFACT EXTRACTION
# -------------------------------------------------------------
stage1_artifacts, unparsed_artifacts_list = phase3_deobfuscate_and_parse(
raw_corpus_items=raw_corpus_items,
artifact_parser=artifact_parser
)
# -------------------------------------------------------------
# FASE 4: STAGE 2 ACTIVE PROBING
# -------------------------------------------------------------
final_artifacts, stage2_results = phase4_probe_stage2(
stage1_artifacts=stage1_artifacts,
stage2_prober=stage2_prober,
enable_probe=args.probe,
is_direct_contracts=is_direct_contracts,
target_input=target_input
)
# -------------------------------------------------------------
# FASE 5: THREAT INTELLIGENCE & INFRASTRUCTURE ENRICHMENT
# -------------------------------------------------------------
(
domain_enrichments,
clean_domains,
clean_urls,
all_abused_rpcs
) = phase5_enrich_threat_intelligence(
contract_profiles=contract_profiles,
final_artifacts=final_artifacts,
enricher=enricher,
enable_enrich=args.enrich
)
# -------------------------------------------------------------
# FASE 6: REPORTING & WATCHER REGISTRY EXPORT
# -------------------------------------------------------------
for ck in final_artifacts.crypto_keys:
if ck.hex_value:
candidate_keys.add(ck.hex_value)
# Construct verified cryptographic materials from confirmed on-chain decryptions
verified_crypto_materials: List[CryptographicMaterial] = []
for c_prof in contract_profiles.values():
if c_prof.encryption_details:
enc = c_prof.encryption_details
algo = enc.get("Algoritmo", "AES-GCM")
passphrase = enc.get("Passphrase Rilevata", "")
iv_val = enc.get("IV (12 bytes)", "")
clean_pass = passphrase.split(" ")[0] if passphrase else ""
if clean_pass:
verified_crypto_materials.append(CryptographicMaterial(
hex_value=clean_pass,
key_type=f"Chiave di Decrittografia On-Chain Verificata ({algo})",
entropy=calculate_shannon_entropy(clean_pass),
source_type="Smart Contract EtherHiding Decryption Key",
source_detail=f"Passphrase verificata per smart contract {c_prof.address} ({algo})"
))
if iv_val:
clean_iv = iv_val.split(" ")[0] if iv_val else ""
if clean_iv:
verified_crypto_materials.append(CryptographicMaterial(
hex_value=clean_iv,
key_type="Initialization Vector (IV 12-byte)",
entropy=calculate_shannon_entropy(clean_iv),
source_type="Smart Contract EtherHiding Decryption IV",
source_detail=f"IV derivato dall'indirizzo del contratto {c_prof.address}"
))
# If no on-chain encryption details exist, fall back to high-entropy keys from static analysis
if not verified_crypto_materials:
verified_crypto_materials = final_artifacts.crypto_keys
report = SOCReport(
target=target_input,
contracts=list(contract_profiles.values()),
malicious_domains=clean_domains,
malicious_urls=clean_urls,
dga_candidate_domains=final_artifacts.dga_candidate_domains,
dga_candidate_urls=final_artifacts.dga_candidate_urls,
clickfix_commands=final_artifacts.commands,
stage2_probes=stage2_results,
abused_rpcs=all_abused_rpcs,
detected_techniques=final_artifacts.detected_techniques,
unknown_artifacts=final_artifacts.unknown_artifacts,
raw_artifacts=unparsed_artifacts_list,
enrichments=domain_enrichments,
cryptographic_materials=verified_crypto_materials
)
if args.json_output and not args.report_dir:
logger.warning("Il parametro CLI '--json' è deprecato. Utilizzare '--report <nome_cartella>' per esportare il bundle incident response completo.")
report_target = args.report_dir or args.json_output
phase6_export_and_register(
report=report,
report_target=report_target,
all_obfuscated_artifacts=all_obfuscated_artifacts,
contract_profiles=contract_profiles,
candidate_keys=candidate_keys,
clean_domains=clean_domains,
clean_urls=clean_urls,
commands=final_artifacts.commands,
target_input=target_input
)
if __name__ == "__main__":
main()