forked from llccd/RDPWrapOffsetFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdb.py
More file actions
72 lines (53 loc) · 2.09 KB
/
pdb.py
File metadata and controls
72 lines (53 loc) · 2.09 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
from __future__ import annotations
import os
import ssl
import urllib.request
import uuid
from dataclasses import dataclass
from pathlib import Path
import pefile
MS_SYMBOL_SERVER = "https://msdl.microsoft.com/download/symbols"
@dataclass(frozen=True)
class PdbInfo:
pdb_name: str
guid_hex: str
age: int
@property
def guid_age(self) -> str:
return f"{self.guid_hex}{self.age}"
def _u32(b: bytes, off: int) -> int:
return int.from_bytes(b[off:off + 4], "little", signed=False)
def get_pdb_info(pe: pefile.PE) -> PdbInfo:
try:
pe.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_DEBUG"]])
except Exception:
pass
entries = getattr(pe, "DIRECTORY_ENTRY_DEBUG", []) or []
for e in entries:
if int(e.struct.Type) != 2:
continue
data = pe.get_data(int(e.struct.AddressOfRawData), int(e.struct.SizeOfData))
if data[:4] != b"RSDS" or len(data) < 4 + 16 + 4:
continue
guid_bytes = data[4:4 + 16]
age = _u32(data, 4 + 16)
pdb_path = data[4 + 16 + 4:].split(b"\x00", 1)[0].decode(errors="ignore")
pdb_name = os.path.basename(pdb_path)
guid_hex = uuid.UUID(bytes_le=guid_bytes).hex.upper()
return PdbInfo(pdb_name=pdb_name, guid_hex=guid_hex, age=age)
raise RuntimeError("RSDS PDB info not found in PE debug directory")
def ensure_pdb_downloaded(pdb: PdbInfo, cache_root: Path, *, server: str = MS_SYMBOL_SERVER) -> Path:
dst_dir = cache_root / pdb.pdb_name / pdb.guid_age
dst_dir.mkdir(parents=True, exist_ok=True)
dst = dst_dir / pdb.pdb_name
if dst.exists() and dst.stat().st_size > 0:
return dst
url = f"{server}/{pdb.pdb_name}/{pdb.guid_age}/{pdb.pdb_name}"
req = urllib.request.Request(url, headers={"User-Agent": "rdpwrap-offset-finder"})
ssl_ctx = ssl.create_default_context()
ssl_ctx.check_hostname = True
ssl_ctx.verify_mode = ssl.CERT_REQUIRED
with urllib.request.urlopen(req, timeout=60, context=ssl_ctx) as r:
data = r.read()
dst.write_bytes(data)
return dst