79 lines
3.1 KiB
Python
79 lines
3.1 KiB
Python
"""Deterministic TEB + SEH-chain modeling for fs:0 prolog/epilog funclets.
|
|
|
|
MSVC x86 SEH prologs read and rewrite the thread exception-handler chain head
|
|
at ``fs:[0]`` and read the security cookie; the TEB they reach lives at the FS
|
|
segment base. Unicorn 2.1.4 ignores a direct ``UC_X86_REG_FS_BASE`` write, so a
|
|
flat GDT is installed and the FS selector loaded to point at a mapped,
|
|
deterministically seeded TEB. Every field is a fixture input, never host state,
|
|
so the emulated chain walk is reproducible and an injected bug is observable.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import struct
|
|
from dataclasses import dataclass
|
|
from typing import Final
|
|
|
|
from unicorn import Uc
|
|
from unicorn.unicorn_const import UC_PROT_ALL
|
|
from unicorn.x86_const import (
|
|
UC_X86_REG_DS, UC_X86_REG_ES, UC_X86_REG_FS, UC_X86_REG_GDTR,
|
|
UC_X86_REG_GS, UC_X86_REG_SS,
|
|
)
|
|
|
|
TEB_BASE: Final = 0x7EFDD000
|
|
TEB_SIZE: Final = 0x2000
|
|
GDT_BASE: Final = 0x00003000
|
|
GDT_LIMIT: Final = 0x1000
|
|
_FLAT_INDEX: Final = 16
|
|
_FS_INDEX: Final = 17
|
|
SEH_CHAIN_END: Final = 0xFFFFFFFF
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class TebError(Exception):
|
|
detail: str
|
|
|
|
def __str__(self) -> str:
|
|
return f"TEB error: {self.detail}"
|
|
|
|
|
|
def _descriptor(base: int, limit: int, access: int, flags: int) -> bytes:
|
|
"""Pack one 8-byte GDT segment descriptor (4KB-granular flat data segment)."""
|
|
if limit > 0xFFFFF:
|
|
limit >>= 12
|
|
flags |= 0x8
|
|
value = limit & 0xFFFF
|
|
value |= (base & 0xFFFFFF) << 16
|
|
value |= (access & 0xFF) << 40
|
|
value |= ((limit >> 16) & 0xF) << 48
|
|
value |= (flags & 0xF) << 52
|
|
value |= ((base >> 24) & 0xFF) << 56
|
|
return struct.pack("<Q", value)
|
|
|
|
|
|
def install_teb(uc: Uc, *, seh_head: int = SEH_CHAIN_END) -> int:
|
|
"""Map a deterministic TEB at the FS base; seed the SEH chain head at fs:[0].
|
|
|
|
Returns the TEB linear base. ``seh_head`` is the ``ExceptionList`` pointer a
|
|
prolog reads from ``fs:[0]`` and saves before installing its own record; a
|
|
different value here changes what the prolog pushes, which is the non-vacuity
|
|
lever for SEH tests.
|
|
"""
|
|
if not 0 <= seh_head <= 0xFFFFFFFF:
|
|
raise TebError(detail="SEH chain head must be a uint32")
|
|
uc.mem_map(GDT_BASE, GDT_LIMIT, UC_PROT_ALL)
|
|
uc.mem_map(TEB_BASE, TEB_SIZE, UC_PROT_ALL)
|
|
# Loading GDTR re-resolves every data selector, so SS/DS/ES/GS need a flat
|
|
# 4GB descriptor or the stack access wraps to a 16-bit offset. FS alone maps
|
|
# to the TEB base so fs:[0] reaches the SEH chain head.
|
|
uc.mem_write(GDT_BASE + _FLAT_INDEX * 8, _descriptor(0, 0xFFFFFFFF, 0x92, 0xC))
|
|
uc.mem_write(GDT_BASE + _FS_INDEX * 8, _descriptor(TEB_BASE, 0xFFFFFFFF, 0x92, 0xC))
|
|
uc.reg_write(UC_X86_REG_GDTR, (0, GDT_BASE, GDT_LIMIT - 1, 0))
|
|
for segment in (UC_X86_REG_SS, UC_X86_REG_DS, UC_X86_REG_ES, UC_X86_REG_GS):
|
|
uc.reg_write(segment, (_FLAT_INDEX << 3))
|
|
uc.reg_write(UC_X86_REG_FS, (_FS_INDEX << 3))
|
|
# TEB.NtTib.ExceptionList (fs:[0]) and the self pointer (fs:[0x18]).
|
|
uc.mem_write(TEB_BASE + 0x00, struct.pack("<I", seh_head))
|
|
uc.mem_write(TEB_BASE + 0x18, struct.pack("<I", TEB_BASE))
|
|
return TEB_BASE
|