165 lines
6.9 KiB
Python
165 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
# Offline host verification for Qualcomm ELF32 MCFG (.mbn) parser,
|
||
|
|
# profile store, PLMN auto-matching, and duress blocking in bananarchy-imsctl.
|
||
|
|
|
||
|
|
import hashlib
|
||
|
|
import os
|
||
|
|
import struct
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
BASE_DIR = Path(__file__).resolve().parents[1]
|
||
|
|
IMSCTL_SRC = BASE_DIR / "src/bananarchy-daemons/bananarchy-imsctl.c"
|
||
|
|
INC_DIR = BASE_DIR / "src/bananarchy-daemons"
|
||
|
|
|
||
|
|
|
||
|
|
def build_synthetic_mbn(config_type=1, desc="Telia_SE-Commercial", version=(5, 1, 1, 1),
|
||
|
|
plmns=((240, 1), (240, 5)), include_ims_nv=True):
|
||
|
|
# Build MCFG_TRL TLVs
|
||
|
|
trl_body = bytearray()
|
||
|
|
trl_body += struct.pack("<BH", 0x00, 2) + struct.pack("<H", 3)
|
||
|
|
desc_b = desc.encode("ascii") + b"\x00"
|
||
|
|
trl_body += struct.pack("<BH", 0x01, len(desc_b)) + desc_b
|
||
|
|
ver_u32 = ((version[0] & 0xFF) << 24) | ((version[1] & 0xFF) << 16) | ((version[2] & 0xFF) << 8) | (version[3] & 0xFF)
|
||
|
|
trl_body += struct.pack("<BH", 0x02, 4) + struct.pack("<I", ver_u32)
|
||
|
|
plmn_b = bytearray([0x00, len(plmns)])
|
||
|
|
for mcc, mnc in plmns:
|
||
|
|
plmn_b += struct.pack("<HH", mcc, mnc)
|
||
|
|
trl_body += struct.pack("<BH", 0x03, len(plmn_b)) + plmn_b
|
||
|
|
trl = b"MCFG_TRL" + struct.pack("<H", len(trl_body)) + trl_body
|
||
|
|
|
||
|
|
items = bytearray()
|
||
|
|
num_items = 1
|
||
|
|
if include_ims_nv:
|
||
|
|
nv_payload = struct.pack("<IB", 67218, 1)
|
||
|
|
items += struct.pack("<IBBH", 8 + len(nv_payload), 1, 0, 0) + nv_payload
|
||
|
|
efs_path = b"/nv/item_files/ims/qp_ims_reg_config\x00\x01"
|
||
|
|
items += struct.pack("<IBBH", 8 + len(efs_path), 2, 0, 0) + efs_path
|
||
|
|
num_items += 2
|
||
|
|
items += struct.pack("<IBBH", 8 + len(trl), 10, 0, 0) + trl
|
||
|
|
|
||
|
|
mcfg = b"MCFG" + struct.pack("<HHII", 3, config_type, num_items, 0) + items
|
||
|
|
|
||
|
|
# Minimal ELF32 LE container with 1 PT_LOAD header
|
||
|
|
ehdr_sz = 52
|
||
|
|
phdr_sz = 32
|
||
|
|
seg_off = ehdr_sz + phdr_sz
|
||
|
|
ehdr = (
|
||
|
|
b"\x7fELF\x01\x01\x01" + b"\x00" * 9 +
|
||
|
|
struct.pack("<HHIIIIIHHHHHH", 2, 40, 1, 0, ehdr_sz, 0, 0, ehdr_sz, phdr_sz, 1, 0, 0, 0)
|
||
|
|
)
|
||
|
|
phdr = struct.pack("<IIIIIIII", 1, seg_off, 0, 0, len(mcfg), len(mcfg), 4, 4)
|
||
|
|
return ehdr + phdr + mcfg
|
||
|
|
|
||
|
|
|
||
|
|
def fail(msg):
|
||
|
|
print(f"[FAIL] {msg}", file=sys.stderr)
|
||
|
|
return 1
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
with tempfile.TemporaryDirectory(prefix="bananarchy-mbn-test-") as td:
|
||
|
|
tmp = Path(td)
|
||
|
|
bin_path = tmp / "bananarchy-imsctl"
|
||
|
|
subprocess.run(
|
||
|
|
["gcc", "-std=c11", "-O2", "-Wall", "-Wextra", "-Werror",
|
||
|
|
f"-I{INC_DIR}", str(IMSCTL_SRC), "-o", str(bin_path)],
|
||
|
|
check=True
|
||
|
|
)
|
||
|
|
|
||
|
|
conf_path = tmp / "ims.conf"
|
||
|
|
state_dir = tmp / "state"
|
||
|
|
lib_dir = tmp / "lib"
|
||
|
|
prof_dir = lib_dir / "profiles"
|
||
|
|
duress_flag = tmp / "duress_mode"
|
||
|
|
|
||
|
|
env = os.environ.copy()
|
||
|
|
env.update({
|
||
|
|
"BANANARCHY_IMS_CONF_PATH": str(conf_path),
|
||
|
|
"BANANARCHY_IMS_STATE_DIR": str(state_dir),
|
||
|
|
"BANANARCHY_IMS_LIB_DIR": str(lib_dir),
|
||
|
|
"BANANARCHY_IMS_PROFILES_DIR": str(prof_dir),
|
||
|
|
"BANANARCHY_DURESS_FLAG_PATH": str(duress_flag),
|
||
|
|
})
|
||
|
|
|
||
|
|
# 1. Inspect valid SW MCFG
|
||
|
|
sw_bytes = build_synthetic_mbn(config_type=1, desc="Telia_SE-VoLTE", plmns=((240, 1), (240, 5)))
|
||
|
|
sw_path = tmp / "telia_se_sw.mbn"
|
||
|
|
sw_path.write_bytes(sw_bytes)
|
||
|
|
expected_id = "ba_" + hashlib.sha1(sw_bytes).hexdigest()[:16]
|
||
|
|
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "inspect", str(sw_path)],
|
||
|
|
env=env, capture_output=True, text=True, check=True)
|
||
|
|
if f"ID={expected_id}" not in r.stdout or "CONFIG_TYPE=SW" not in r.stdout or \
|
||
|
|
"IMS_CAPABLE=1" not in r.stdout or "PLMNS=240-01,240-05" not in r.stdout:
|
||
|
|
return fail(f"unexpected inspect output:\n{r.stdout}")
|
||
|
|
|
||
|
|
# 2. Reject HW MCFG on install
|
||
|
|
hw_path = tmp / "bad_hw.mbn"
|
||
|
|
hw_path.write_bytes(build_synthetic_mbn(config_type=0, desc="HW_Config"))
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "install", str(hw_path)],
|
||
|
|
env=env, capture_output=True, text=True)
|
||
|
|
if r.returncode == 0:
|
||
|
|
return fail("HW MCFG (config_type=0) must be rejected by profile install")
|
||
|
|
|
||
|
|
# 3. Reject corrupted / truncated ELF
|
||
|
|
bad_path = tmp / "corrupt.mbn"
|
||
|
|
bad_path.write_bytes(b"\x7fELF\x01\x01\x01" + b"\x00" * 100)
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "install", str(bad_path)],
|
||
|
|
env=env, capture_output=True, text=True)
|
||
|
|
if r.returncode == 0:
|
||
|
|
return fail("corrupted ELF without MCFG segment must be rejected")
|
||
|
|
|
||
|
|
# 4. Install + activate SW MCFG and verify list --tsv & config
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "install", str(sw_path), "--activate"],
|
||
|
|
env=env, capture_output=True, text=True, check=True)
|
||
|
|
if f"INSTALLED={expected_id}" not in r.stdout or "ACTIVATED=1" not in r.stdout:
|
||
|
|
return fail(f"install output mismatch: {r.stdout}")
|
||
|
|
if not (prof_dir / f"{expected_id}.mbn").is_file():
|
||
|
|
return fail("installed .mbn file missing in store")
|
||
|
|
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "list", "--tsv"],
|
||
|
|
env=env, capture_output=True, text=True, check=True)
|
||
|
|
if f"{expected_id}\t1\tSW\t1\t5.1.1.1\t240-01,240-05\tTelia_SE-VoLTE" not in r.stdout:
|
||
|
|
return fail(f"profile list --tsv mismatch:\n{r.stdout}")
|
||
|
|
|
||
|
|
# 5. PLMN auto-match (clear active_mbn first, then auto-match 240-05)
|
||
|
|
subprocess.run([str(bin_path), "profile", "reset-default"], env=env, check=True)
|
||
|
|
match_env = env.copy()
|
||
|
|
match_env["BANANARCHY_MOCK_MCC"] = "240"
|
||
|
|
match_env["BANANARCHY_MOCK_MNC"] = "05"
|
||
|
|
r = subprocess.run([str(bin_path), "auto-match"], env=match_env, capture_output=True, text=True, check=True)
|
||
|
|
if f"MATCHED={expected_id}" not in r.stdout:
|
||
|
|
return fail(f"auto-match failed for PLMN 240-05: {r.stdout}")
|
||
|
|
|
||
|
|
# 6. Settings validation & duress blocking
|
||
|
|
subprocess.run([str(bin_path), "set", "volte_mode", "auto"], env=env, check=True)
|
||
|
|
r = subprocess.run([str(bin_path), "get", "volte_mode"], env=env, capture_output=True, text=True, check=True)
|
||
|
|
if r.stdout.strip() != "auto":
|
||
|
|
return fail(f"expected volte_mode=auto, got {r.stdout.strip()}")
|
||
|
|
|
||
|
|
r = subprocess.run([str(bin_path), "set", "volte_mode", "bogus"], env=env, capture_output=True, text=True)
|
||
|
|
if r.returncode == 0:
|
||
|
|
return fail("invalid volte_mode must fail")
|
||
|
|
|
||
|
|
duress_flag.write_text("1\n")
|
||
|
|
r = subprocess.run([str(bin_path), "set", "volte_mode", "on"], env=env, capture_output=True, text=True)
|
||
|
|
if r.returncode == 0:
|
||
|
|
return fail("mutating imsctl set must be blocked in duress mode")
|
||
|
|
r = subprocess.run([str(bin_path), "profile", "remove", expected_id], env=env, capture_output=True, text=True)
|
||
|
|
if r.returncode == 0:
|
||
|
|
return fail("mutating imsctl profile remove must be blocked in duress mode")
|
||
|
|
duress_flag.unlink()
|
||
|
|
|
||
|
|
subprocess.run([str(bin_path), "profile", "remove", expected_id], env=env, check=True)
|
||
|
|
if (prof_dir / f"{expected_id}.mbn").exists():
|
||
|
|
return fail("removed .mbn still exists")
|
||
|
|
|
||
|
|
print("[ok] test-ims-mbn: ELF32 MCFG parser, HW/corrupt rejection, store, PLMN match, and duress guards")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|