Repository navigation
Expand file tree
/
Copy pathphp2shell_offsets.py
More file actions
248 lines (220 loc) · 9.21 KB
/
Copy pathphp2shell_offsets.py
File metadata and controls
248 lines (220 loc) · 9.21 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
php2shell_offsets.py — extract libphp symbol offsets from PHP Docker images.
This fills the offset inventory for builds that have not been heap-calibrated.
It does not run the image: it creates a container, copies
/usr/lib/apache2/modules/libphp.so out, parses the ELF symbol table and writes
profiles/offsets/<version>-<arch>.json plus docs/offset-inventory.md.
Offsets alone do not give RCE. They are the per-build constants needed once a
heap profile exists; the dispatcher only runs verified profiles.
Usage:
python3 tools/php2shell_offsets.py
python3 tools/php2shell_offsets.py --images php:7.4-apache php:8.1-apache
"""
from __future__ import annotations
import argparse
import glob
import hashlib
import json
import os
import platform
import re
import shutil
import struct
import subprocess
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
OUT = os.path.join(ROOT, "profiles", "offsets")
DOC = os.path.join(ROOT, "docs", "offset-inventory.md")
DEFAULT_IMAGES = [
"php:7.4-apache",
"php:8.0-apache",
"php:8.1-apache",
"php:8.2.34-apache",
"php:8.3.35-apache",
"php:8.4.26-apache",
"php:8.5.11-apache",
]
LIB_PATHS = [
"/usr/lib/apache2/modules/libphp.so",
"/usr/lib/apache2/modules/libphp7.so",
"/usr/lib/apache2/modules/libphp5.so",
]
SHT_SYMTAB = 2
SHT_DYNSYM = 11
PT_LOAD = 1
def docker(*args, timeout=900, text=True):
return subprocess.run(["docker", *args], capture_output=True, text=text, timeout=timeout)
def parse_elf(path: str):
data = open(path, "rb").read()
if data[:4] != b"\x7fELF" or data[4] != 2 or data[5] != 1:
raise ValueError("not a little-endian ELF64 file")
e_phoff, = struct.unpack_from("<Q", data, 0x20)
e_shoff, = struct.unpack_from("<Q", data, 0x28)
e_phentsize, e_phnum = struct.unpack_from("<HH", data, 0x36)
e_shentsize, e_shnum = struct.unpack_from("<HH", data, 0x3A)
loads = []
for i in range(e_phnum):
off = e_phoff + i * e_phentsize
p_type, = struct.unpack_from("<I", data, off)
p_vaddr, = struct.unpack_from("<Q", data, off + 16)
p_memsz, = struct.unpack_from("<Q", data, off + 40)
if p_type == PT_LOAD and p_memsz:
loads.append(p_vaddr)
bias = min(loads) if loads else 0
sections = []
for i in range(e_shnum):
off = e_shoff + i * e_shentsize
name_off, sh_type = struct.unpack_from("<II", data, off)
sh_offset, sh_size = struct.unpack_from("<QQ", data, off + 24)
sh_link, = struct.unpack_from("<I", data, off + 40)
sh_entsize, = struct.unpack_from("<Q", data, off + 56)
sections.append({"type": sh_type, "offset": sh_offset, "size": sh_size,
"link": sh_link, "entsize": sh_entsize})
wanted = {"zif_system": None, "zval_ptr_dtor": None, "ps_globals": None}
for sec in sections:
if sec["type"] not in (SHT_SYMTAB, SHT_DYNSYM):
continue
if sec["link"] >= len(sections):
continue
strtab = sections[sec["link"]]
entsize = sec["entsize"] or 24
for i in range(sec["size"] // entsize):
off = sec["offset"] + i * entsize
st_name, = struct.unpack_from("<I", data, off)
st_value, = struct.unpack_from("<Q", data, off + 8)
if not st_name:
continue
name_off = strtab["offset"] + st_name
end = data.find(b"\x00", name_off)
if end < 0:
continue
name = data[name_off:end].decode("ascii", "replace")
if name in wanted and wanted[name] is None:
wanted[name] = st_value
return bias, wanted
def image_env(image: str) -> dict:
out = docker("inspect", "--format",
"{{range .Config.Env}}{{println .}}{{end}}", image)
env = {}
for line in out.stdout.splitlines():
if "=" in line:
k, v = line.split("=", 1)
env[k.strip()] = v.strip()
return env
def image_arch(image: str) -> str:
# Some imported/emulated images report an empty Architecture, so ask the
# image itself. uname inside the container is authoritative.
out = docker("run", "--rm", "--entrypoint", "uname", image, "-m", timeout=120)
machine = out.stdout.strip().lower()
if machine:
mapped = {"aarch64": "aarch64", "arm64": "aarch64",
"x86_64": "amd64", "amd64": "amd64"}.get(machine)
if mapped:
return mapped
out = docker("inspect", "--format", "{{.Architecture}}", image)
arch = out.stdout.strip()
if not arch or arch == "unknown":
machine = platform.machine().lower()
arch = {"aarch64": "aarch64", "arm64": "aarch64", "x86_64": "amd64"}.get(machine, machine)
return arch
def extract(image: str) -> dict:
name = "p2s-off-" + image.replace(":", "-").replace("/", "-")
docker("rm", "-f", name)
print(f"[*] {image}: creating container", flush=True)
created = docker("create", "--name", name, image, timeout=900)
if created.returncode != 0:
return {"image": image, "status": "pull/create failed",
"error": created.stderr.strip()[:200]}
try:
lib = None
for candidate in LIB_PATHS:
target = f"/tmp/{name}.so"
cp = docker("cp", f"{name}:{candidate}", target)
if cp.returncode == 0:
lib = target
break
if not lib:
return {"image": image, "status": "libphp not found"}
bias, symbols = parse_elf(lib)
sha = hashlib.sha256(open(lib, "rb").read()).hexdigest()
env = image_env(image)
version = env.get("PHP_VERSION", "")
if not version:
tag = re.search(r"php:(\d+\.\d+\.\d+)", image)
version = tag.group(1) if tag else ""
arch = image_arch(image)
result = {
"image": image,
"php_version": version,
"branch": ".".join(version.split(".")[:2]) if version else "",
"arch": arch,
"libphp": {k: (hex(v) if v is not None else None) for k, v in symbols.items()},
"libphp_sha256": sha,
"status": "offsets extracted; heap not calibrated",
}
os.makedirs(OUT, exist_ok=True)
path = os.path.join(OUT, f"{version or name}-{arch}.json")
with open(path, "w", encoding="utf-8") as fh:
json.dump(result, fh, indent=2)
result["file"] = os.path.relpath(path, ROOT)
print(f" PHP {version} {arch}: zif={result['libphp']['zif_system']} "
f"dtor={result['libphp']['zval_ptr_dtor']}", flush=True)
return result
finally:
docker("rm", "-f", name)
def write_doc(results) -> None:
entries = {}
for path in glob.glob(os.path.join(OUT, "*.json")):
try:
with open(path, "r", encoding="utf-8") as fh:
item = json.load(fh)
except (OSError, ValueError):
continue
entries[(item.get("php_version"), item.get("arch"), item.get("image"))] = item
for item in results:
if item.get("libphp"):
entries[(item.get("php_version"), item.get("arch"), item.get("image"))] = item
ordered = sorted(entries.values(),
key=lambda r: tuple(int(x) if x.isdigit() else 0
for x in (r.get("php_version") or "0").split(".")))
lines = [
"# libphp offset inventory",
"",
"Generated by `tools/php2shell_offsets.py`. These are symbol offsets only:",
"a build still needs heap calibration before the chain can run.",
"",
"| Image | PHP | Arch | `zif_system` | `zval_ptr_dtor` | libphp sha256 | Status |",
"| --- | --- | --- | --- | --- | --- | --- |",
]
for r in ordered:
lib = r.get("libphp") or {}
lines.append("| {image} | {php} | {arch} | {zif} | {dtor} | {sha} | {status} |".format(
image=r.get("image", ""), php=r.get("php_version", "?"),
arch=r.get("arch", "?"), zif=lib.get("zif_system") or "-",
dtor=lib.get("zval_ptr_dtor") or "-",
sha=(r.get("libphp_sha256") or "-")[:12], status=r.get("status", "")))
lines += ["", "## Notes", "",
"- Offsets are relative to the `libphp.so` load base; ASLR changes the base, not the offset.",
"- The same PHP version built by another distribution/arch has different offsets.",
"- Heap parameters (`stage1_n`, forge length, `htdata`, pads) are not included here;",
" run `tools/php2shell_calibrate.py` against a replica of the build for those.",
""]
os.makedirs(os.path.dirname(DOC), exist_ok=True)
with open(DOC, "w", encoding="utf-8") as fh:
fh.write("\n".join(lines))
def main() -> int:
ap = argparse.ArgumentParser(description="Extract libphp offsets from PHP images.")
ap.add_argument("--images", nargs="*", default=DEFAULT_IMAGES)
args = ap.parse_args()
if not shutil.which("docker"):
print("docker not found", file=sys.stderr)
return 4
results = [extract(image) for image in args.images]
write_doc(results)
print(f"[*] wrote {DOC}")
return 0
if __name__ == "__main__":
sys.exit(main())