-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeshbreaker.py
More file actions
542 lines (458 loc) · 21.2 KB
/
Copy pathmeshbreaker.py
File metadata and controls
542 lines (458 loc) · 21.2 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
#!/usr/bin/env python3
import asyncio
import json
import sys
from pathlib import Path
from typing import Optional
import click
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich import box
sys.path.insert(0, str(Path(__file__).parent))
from src.core.session import SessionState
from src.core.plugin_registry import PluginRegistry
from src.core.scanner import BLEScanner, ClassicScanner, BTDevice
from src.core.enumerator import GATTEnumerator, SDPEnumerator
from src.modules.gatt_fuzzer import GATTFuzzer
from src.modules.sdp_enum import SDPProbe
from src.modules.l2cap_fuzzer import L2CAPFuzzer
from src.modules.passive_capture import PassiveCapture
from src.modules.protocol_identifier import ProtocolIdentifier
from src.modules.mesh_frame_parser import MeshFrameParser
from src.modules.mesh_fuzzer import MeshFuzzer
from src.modules.cve_checker import CVEChecker
from src.modules.report_generator import ReportGenerator
from src.firmware_analysis.firmware_analyzer import FirmwareAnalyzer
from src.utils import logger
console = Console()
BANNER = """\
███╗ ███╗███████╗███████╗██╗ ██╗██████╗ ██████╗ ███████╗ █████╗ ██╗ ██╗███████╗██████╗
████╗ ████║██╔════╝██╔════╝██║ ██║██╔══██╗██╔══██╗██╔════╝██╔══██╗██║ ██╔╝██╔════╝██╔══██╗
██╔████╔██║█████╗ ███████╗███████║██████╔╝██████╔╝█████╗ ███████║█████╔╝ █████╗ ██████╔╝
██║╚██╔╝██║██╔══╝ ╚════██║██╔══██║██╔══██╗██╔══██╗██╔══╝ ██╔══██║██╔═██╗ ██╔══╝ ██╔══██╗
██║ ╚═╝ ██║███████╗███████║██║ ██║██████╔╝██║ ██║███████╗██║ ██║██║ ██╗███████╗██║ ██║
╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝"""
def _load_session(output_dir: str) -> SessionState:
Path(output_dir).mkdir(parents=True, exist_ok=True)
sf = Path(output_dir) / "session.json"
if sf.exists():
try:
data = json.loads(sf.read_text())
s = SessionState(
target_mac=data.get("target_mac"),
adapter=data.get("adapter", "hci0"),
scan_time=data.get("scan_time", 10.0),
output_dir=output_dir,
)
s.results = data.get("results", {})
s.mesh_protocol = data.get("mesh_protocol")
s.firmware_path = data.get("firmware_path")
if hasattr(s, "target_ip"):
s.target_ip = data.get("target_ip")
return s
except Exception:
pass
return SessionState(adapter="hci0", scan_time=10.0, output_dir=output_dir)
def _save_session(session: SessionState, output_dir: str):
sf = Path(output_dir) / "session.json"
data = {
"target_mac": session.target_mac,
"adapter": session.adapter,
"scan_time": session.scan_time,
"mesh_protocol": session.mesh_protocol,
"firmware_path": session.firmware_path,
"results": session.results,
}
if hasattr(session, "target_ip"):
data["target_ip"] = session.target_ip
sf.write_text(json.dumps(data, indent=2, default=str))
def _banner():
console.print(f"[bold cyan]{BANNER}[/]\n", highlight=False)
def _print_devices(devices: list, title: str = "Devices"):
if not devices:
logger.warning("No devices found")
return
t = Table(title=title, box=box.ROUNDED, border_style="cyan")
t.add_column("#", style="dim", width=3)
t.add_column("MAC", style="bold magenta", width=18)
t.add_column("Name", style="bold white", width=24)
t.add_column("RSSI", style="cyan", width=6)
t.add_column("Type", style="dim", width=8)
t.add_column("UUIDs", style="dim", width=40)
for i, d in enumerate(devices, 1):
color = "green" if d.rssi > -60 else "yellow" if d.rssi > -80 else "red"
t.add_row(str(i), d.mac, d.name[:24],
f"[{color}]{d.rssi}[/]",
"BLE" if d.is_ble else "Classic",
", ".join(d.uuids[:3])[:40])
console.print(t)
# CLI
@click.group()
@click.option("--output", "-o", default="./output", show_default=True,
help="Output directory", envvar="MB_OUTPUT")
@click.option("--adapter", "-a", default="hci0", show_default=True,
help="Bluetooth adapter", envvar="MB_ADAPTER")
@click.pass_context
def cli(ctx, output, adapter):
"""MeshBreaker v3.0 — BLE Mesh security research tool."""
ctx.ensure_object(dict)
ctx.obj["output"] = output
ctx.obj["adapter"] = adapter
# commands
@cli.command()
@click.option("--time", "-t", "scan_time", default=10.0, show_default=True,
help="Scan duration (seconds)")
@click.option("--classic", is_flag=True, help="Also run Bluetooth Classic scan")
@click.option("--target", default=None, metavar="MAC",
help="Pre-set target MAC (saves to session)")
@click.pass_context
def recon(ctx, scan_time, classic, target):
"""BLE scan + protocol fingerprint."""
_banner()
output = ctx.obj["output"]
adapter = ctx.obj["adapter"]
session = _load_session(output)
if target:
session.set_target(target)
async def _run():
scanner = BLEScanner(adapter=adapter, timeout=scan_time)
devices = await scanner.scan()
_print_devices(devices, "BLE Devices")
session.devices = [{"mac": d.mac, "name": d.name, "rssi": d.rssi,
"uuids": d.uuids, "manufacturer_data": {}} for d in devices]
if classic:
cs = ClassicScanner(timeout=scan_time)
cdevs = cs.scan()
_print_devices(cdevs, "Classic Devices")
session.devices += [{"mac": d.mac, "name": d.name, "rssi": d.rssi,
"uuids": d.uuids, "manufacturer_data": {}} for d in cdevs]
if session.devices:
ident = ProtocolIdentifier()
matches = ident.from_devices(session.devices)
session.store("protocol_id", [
{"protocol_id": m.protocol_id, "name": m.name,
"confidence": m.confidence, "evidence": m.evidence,
"cve_tags": m.cve_tags} for m in matches
])
if matches:
session.mesh_protocol = matches[0].protocol_id
logger.info(f"Protocol: {session.mesh_protocol}")
if devices and not session.target_mac:
console.print("\n[dim]Hint: set target with[/] meshbreaker set-target <MAC>")
asyncio.run(_run())
_save_session(session, output)
@cli.command()
@click.option("--duration", "-d", default=30.0, show_default=True,
help="Capture duration (seconds)")
@click.option("--no-parse", is_flag=True, help="Skip topology decode after capture")
@click.pass_context
def capture(ctx, duration, no_parse):
"""Passive BLE capture + mesh beacon decode."""
_banner()
output = ctx.obj["output"]
adapter = ctx.obj["adapter"]
session = _load_session(output)
async def _run():
cap = PassiveCapture(adapter=adapter, output_dir=output)
try:
s = await cap.capture(duration=duration, save_json=True)
except Exception as e:
if "No Bluetooth" in str(e) or "adapter" in str(e).lower():
logger.error("No Bluetooth adapter found. Plug in a USB dongle.")
return
raise
session.store("capture", {"file": s.pcap_file, "beacons": s.device_count,
"duration": s.duration})
if s.pcap_file:
session.results["capture_file"] = s.pcap_file
if s.pcap_file and not no_parse:
topo = MeshFrameParser().parse_file(s.pcap_file)
session.store("topology", {"nodes": len(topo.nodes), "protocol": topo.protocol,
"relays": topo.relay_candidates,
"proxies": topo.proxy_candidates})
logger.info(f"Topology: {len(topo.nodes)} nodes, protocol={topo.protocol}")
asyncio.run(_run())
_save_session(session, output)
@cli.command()
@click.argument("path", type=click.Path(exists=True))
@click.option("--no-protocol-id", is_flag=True, help="Skip protocol fingerprint from strings")
@click.pass_context
def firmware(ctx, path, no_protocol_id):
"""Analyze a firmware binary.
PATH — path to the firmware binary (ELF, raw, or squashfs image).
"""
_banner()
output = ctx.obj["output"]
session = _load_session(output)
session.firmware_path = path
analyzer = FirmwareAnalyzer(path)
info = analyzer.analyze()
analyzer.print_report()
analyzer.export(output)
session.store("firmware", {
"path": str(info.path), "size": info.size, "arch": info.arch,
"is_encrypted": info.is_encrypted, "is_compressed": info.is_compressed,
"urls": info.urls, "credentials": info.credentials,
"aes_keys": len(info.crypto_keys.get("aes_keys", [])),
"ble_mesh_keys": len(info.crypto_keys.get("ble_mesh_keys", [])),
})
if not no_protocol_id:
ident = ProtocolIdentifier()
matches = ident.from_firmware(info)
existing = session.get("protocol_id", [])
for m in matches:
existing.append({"protocol_id": m.protocol_id, "name": m.name,
"confidence": m.confidence, "evidence": m.evidence,
"cve_tags": m.cve_tags, "source": "firmware"})
session.store("protocol_id", existing)
_save_session(session, output)
@cli.command()
@click.option("--target", "-t", default=None, metavar="MAC",
help="Target BT MAC address")
@click.option("--no-sdp", is_flag=True, help="Skip Bluetooth Classic SDP browse")
@click.pass_context
def enumerate(ctx, target, no_sdp):
"""GATT service enumeration + SDP browse."""
_banner()
output = ctx.obj["output"]
adapter = ctx.obj["adapter"]
session = _load_session(output)
if target:
session.set_target(target)
if not session.target_mac:
logger.error("No target MAC. Use: meshbreaker set-target AA:BB:CC:DD:EE:FF")
sys.exit(1)
async def _run():
logger.info(f"GATT enumeration → {session.target_mac}")
enum_g = GATTEnumerator(target=session.target_mac, adapter=adapter)
try:
gatt = await enum_g.enumerate()
except Exception as e:
if any(k in str(e).lower() for k in ("no bluetooth", "adapter", "connect")):
logger.error(f"BLE error: {e}")
return
raise
session.store("gatt", gatt)
for item in gatt.get("attack_surface", []):
logger.warning(f" WRITE [{item['handle']:#06x}] {item['uuid']}")
if not no_sdp:
sdp = SDPProbe(target=session.target_mac)
services = sdp.browse()
session.store("sdp", [s.__dict__ for s in services])
asyncio.run(_run())
_save_session(session, output)
@cli.command()
@click.option("--target", "-t", default=None, metavar="MAC",
help="Target BT MAC address")
@click.option("--methods", "-m", default="mesh",
help="Comma-separated: mesh,gatt,l2cap,sdp,all", show_default=True)
@click.option("--strategy", "-s", default="all",
help="Mesh fuzz strategy: all|net_pdu|proxy_cfg|oversized", show_default=True)
@click.pass_context
def fuzz(ctx, target, methods, strategy):
"""Protocol-aware fuzzing (mesh/GATT/L2CAP/SDP)."""
_banner()
output = ctx.obj["output"]
adapter = ctx.obj["adapter"]
session = _load_session(output)
if target:
session.set_target(target)
if not session.target_mac:
logger.error("No target MAC. Use: meshbreaker set-target AA:BB:CC:DD:EE:FF")
sys.exit(1)
method_set = {m.strip() for m in methods.split(",")}
if "all" in method_set:
method_set = {"mesh", "gatt", "l2cap", "sdp"}
logger.warning(f"Fuzzing {session.target_mac} — {', '.join(method_set)}")
async def _run():
if "mesh" in method_set:
fuzzer = MeshFuzzer(target=session.target_mac, adapter=adapter)
results = await fuzzer.fuzz_gatt_proxy(strategy=strategy)
session.store("mesh_fuzz", [{"strategy": r.strategy, "payload": r.payload.hex(),
"crashed": r.crashed, "note": r.note} for r in results])
if "gatt" in method_set:
gf = GATTFuzzer(target=session.target_mac, adapter=adapter)
results = await gf.run()
session.store("gatt_fuzz", [{"uuid": r.uuid, "len": len(r.payload),
"crashed": r.crashed} for r in results])
if "l2cap" in method_set:
lf = L2CAPFuzzer(target=session.target_mac)
results = lf.run()
session.store("l2cap_fuzz", [{"psm": r.psm, "crashed": r.crashed,
"note": r.note} for r in results])
if "sdp" in method_set:
probe = SDPProbe(target=session.target_mac)
crashed = probe.probe_sdp_bof()
session.store("sdp_bof", {"crashed": crashed})
asyncio.run(_run())
_save_session(session, output)
@cli.command()
@click.option("--target", "-t", default=None, metavar="MAC",
help="Target BT MAC address")
@click.option("--method", "-m", default="a2mp",
type=click.Choice(["a2mp", "sdp-bof", "plugin"], case_sensitive=False),
show_default=True)
@click.option("--plugin-name", default=None,
help="Plugin name when using --method plugin")
@click.pass_context
def exploit(ctx, target, method, plugin_name):
"""Exploit probes: BleedingTooth A2MP check, SDP BOF, or custom plugin."""
_banner()
output = ctx.obj["output"]
adapter = ctx.obj["adapter"]
session = _load_session(output)
if target:
session.set_target(target)
if not session.target_mac:
logger.error("No target MAC. Use: meshbreaker set-target AA:BB:CC:DD:EE:FF")
sys.exit(1)
registry = PluginRegistry()
registry.load_directory(Path(__file__).parent / "src" / "plugins")
if method == "a2mp":
probe = SDPProbe(target=session.target_mac)
reachable = probe.probe_a2mp()
session.store("a2mp", {"reachable": reachable})
if reachable:
logger.warning("A2MP CID 3 reachable — BleedingTooth surface (CVE-2020-12351)")
else:
logger.info("A2MP not reachable")
elif method == "sdp-bof":
probe = SDPProbe(target=session.target_mac)
crashed = probe.probe_sdp_bof()
session.store("sdp_bof", {"crashed": crashed})
elif method == "plugin":
plugins = registry.by_category("exploit")
if not plugins:
logger.error("No exploit plugins in src/plugins/")
sys.exit(1)
if not plugin_name:
logger.info("Available: " + ", ".join(plugins.keys()))
logger.error("Specify with --plugin-name NAME")
sys.exit(1)
if plugin_name not in plugins:
logger.error(f"Unknown plugin: {plugin_name} (available: {', '.join(plugins.keys())})")
sys.exit(1)
p = plugins[plugin_name](target=session.target_mac, adapter=adapter, session=session)
session.store(f"exploit_{plugin_name}", p.run())
_save_session(session, output)
@cli.command("cve-check")
@click.option("--kernel", default=None, help="Kernel version, e.g. 5.4.47")
@click.option("--bluez", default=None, help="BlueZ version, e.g. 5.72")
@click.option("--runc", default=None, help="runc version")
@click.option("--tags", default=None, help="Extra tags, comma-separated (wirepas,kura,mqtt…)")
@click.pass_context
def cve_check(ctx, kernel, bluez, runc, tags):
"""Match CVE database against stack versions and protocol tags."""
_banner()
output = ctx.obj["output"]
session = _load_session(output)
checker = CVEChecker()
ctx_data: dict = {}
all_tags: list = []
for p in session.get("protocol_id", []):
cvt = p.get("cve_tags", []) if isinstance(p, dict) else p.cve_tags
all_tags.extend(cvt)
ctx_data["protocol_tags"] = all_tags
# auto-pull versions from firmware analysis if available in session
fw = session.get("firmware", {})
auto_kernel = fw.get("kernel_version")
auto_bluez = fw.get("bluez_version")
if auto_kernel and not kernel:
logger.info(f"Kernel version from firmware analysis: {auto_kernel}")
ctx_data["kernel_version"] = auto_kernel
if auto_bluez and not bluez:
logger.info(f"BlueZ version from firmware analysis: {auto_bluez}")
ctx_data["bluez_version"] = auto_bluez
if kernel: ctx_data["kernel_version"] = kernel
if bluez: ctx_data["bluez_version"] = bluez
if runc: ctx_data["runc_version"] = runc
if tags:
ctx_data["custom_tags"] = [t.strip() for t in tags.split(",") if t.strip()]
matches = checker.check_all(ctx_data)
session.store("cve_check", [
{"cve_id": m.cve_id, "name": m.name, "severity": m.severity,
"cvss": m.cvss, "description": m.description,
"matched_on": m.matched_on, "attack_vector": m.attack_vector}
for m in matches
])
_save_session(session, output)
@cli.command()
@click.option("--format", "-f", "fmt",
type=click.Choice(["md", "html", "json", "all"], case_sensitive=False),
default="all", show_default=True)
@click.pass_context
def report(ctx, fmt):
"""Generate Markdown, HTML, or JSON report from session data."""
_banner()
output = ctx.obj["output"]
session = _load_session(output)
gen = ReportGenerator(session)
paths = gen.generate_all() if fmt == "all" else {fmt: gen.generate(fmt)}
console.print("\n[bold green]Reports written:[/]")
for f, p in paths.items():
console.print(f" [dim]{f.upper():8}[/] {p}")
@cli.command("parse-capture")
@click.argument("path", type=click.Path(exists=True))
@click.pass_context
def parse_capture(ctx, path):
"""Load an existing capture JSON file and decode topology."""
_banner()
output = ctx.obj["output"]
session = _load_session(output)
topo = MeshFrameParser().parse_file(path)
session.store("topology", {"nodes": len(topo.nodes), "protocol": topo.protocol,
"relays": topo.relay_candidates,
"proxies": topo.proxy_candidates})
if topo.protocol and not session.mesh_protocol:
session.mesh_protocol = topo.protocol
matches = ProtocolIdentifier().from_capture_file(path)
if matches:
session.mesh_protocol = session.mesh_protocol or matches[0].protocol_id
logger.info(f"Protocol: {session.mesh_protocol}")
_save_session(session, output)
@cli.command("set-target")
@click.argument("mac")
@click.pass_context
def set_target(ctx, mac):
"""Save a target MAC to the session (persists across commands)."""
output = ctx.obj["output"]
session = _load_session(output)
session.set_target(mac)
_save_session(session, output)
logger.success(f"Target → {session.target_mac}")
@cli.command("set-ip")
@click.argument("ip")
@click.pass_context
def set_ip(ctx, ip):
"""Save a target IP to the session (persists across commands)."""
output = ctx.obj["output"]
session = _load_session(output)
session.target_ip = ip.strip()
_save_session(session, output)
logger.success(f"IP → {ip}")
@cli.command("session")
@click.option("--reset", is_flag=True, help="Clear all session state")
@click.pass_context
def show_session(ctx, reset):
"""Show current session state (target, protocol, results)."""
output = ctx.obj["output"]
sf = Path(output) / "session.json"
if reset:
if sf.exists():
sf.unlink()
logger.info("Session cleared")
return
s = _load_session(output)
console.print(Panel(
f"Target MAC : [bold magenta]{s.target_mac or 'not set'}[/]\n"
f"Target IP : [bold]{getattr(s, 'target_ip', None) or 'not set'}[/]\n"
f"Protocol : [yellow]{s.mesh_protocol or 'unknown'}[/]\n"
f"Firmware : [dim]{s.firmware_path or 'not set'}[/]\n"
f"Results : {', '.join(s.results.keys()) if s.results else 'none'}",
title="[bold cyan]Session[/]",
border_style="cyan",
expand=False,
))
if __name__ == "__main__":
cli()