PS4/PS5 Exploitation; Part II: kASLR defeat, kernel module extraction, Exotic!
KASLR, kernel dumping, writing an exploitation framework for… PS4…?
At the end of this article, I talk about payloads. That’s right. This article follows the release of Exotic, my remote exploitation framework, currently supporting PS4, with features like kASLR defeat, kernel ELF dumping, heap, syscall dumping and more. The link should be above. Take a look at it and tell me what you think.
When reverse-engineering Orbis, I wanted to look into the Crypto Processor (CCP) kernel module to document how it interacts with the SAMU HSM. To do so, I thought about using the kdfind FreeBSD syscall. I unfortunately found that syscall range (304-309) all returning ENOSYS (0x4e/not implemented), probably for defense-in-depth security reasons:
| Name | ID on FreeBSD | Description |
|---|---|---|
kldload |
304 | Load a kernel module (KLD) |
kldunload |
305 | Unload a kernel module (KLD) |
kldfind |
306 | Find a loaded KLD by name and returns fileid |
kldnext |
307 | Get the next fileid in the KLD list |
kldstat |
308 | Get status info for a fileid |
kldfirstmod |
309 | Get the first modid inside a KLD |
Since these syscalls are stubbed, will we still be able to dump kernel modules? Limitation breeds innovation, of course we can; read on for my full methodology!
I had two options; either:
-
reimplement these syscalls as PIC shellcode based off their FreeBSD implementation. This probably can work but would require reversing the linker too, maybe the modules are SELF with obscure flags?
-
See below.
Methodology
With these syscalls stubbed, it took me slightly longer to figure out how to extract the kernel.
We are on x86 (x64), so syscalls are interpreted, from usermode to hardware, like this, according to the System V x86_64 ABI (example exit syscall because it only has one parameter):
flowchart LR
classDef user fill:#e3f2fd,stroke:#1565c0,color:#0d47a1
classDef boot fill:#fff8e1,stroke:#f9a825,color:#e65100
classDef hw fill:#f3e5f5,stroke:#6a1b9a,color:#4a148c
classDef kernel fill:#fff3e0,stroke:#ef6c00,color:#e65100
classDef op fill:#e8f5e9,stroke:#2e7d32,color:#1b5e20
subgraph U["User mode"]
P["program"] --> L["libc!exit"]
L --> A["<code>mov rdi, 0 </code> ; exit code<br/><code>mov rax, 1</code> ; exit syscall<br/><code>syscall</code>"]
end
subgraph B["Boot time"]
W["OS writes MSR at boot<br/><code>wrmsr(0xC0000082, Xfast_syscall)</code>"]
end
subgraph H["CPU / MSR"]
M["IA32_LSTAR<br/>MSR <code>0xC0000082</code>"]
S["<code>syscall</code> instruction executes<br/>loads RIP from IA32_LSTAR<br/>JMPs to that address"]
end
subgraph K["Kernel"]
X["Xfast_syscall<br/>fast syscall entry"]
T["kernel syscall table"]
SW["switch statement<br/>identify syscall by int in <code>rax</code>"]
O["perform the operation"]
end
A --> S
W -.->|"written at boot"| M
M -->|"provides jump target"| S
S --> X --> T --> SW --> O
class P,L,A user
class W boot
class M,S hw
class X,T,SW kernel
class O op
High quality image of the graph
If you can’t see the graph:
program -> libc!exit, which is, in asm,:
mov rdi, 0 ; exit_code_supplied, assumed to be 0 in this example
mov rax, 1 ; https://cgit.freebsd.org/src/tree/sys/sys/syscall.h
syscall ; clobbers rcx, r11
-> kernel syscall table -> switch statement, identify our syscall by the int in rax -> perform the operation.
syscall loads RIP from IA32_LSTAR (ModelSpecific Register 0xC0000082) and JMPs to it (basically jmp to lstar), that address is written by the OS at boot with wrmsr on 0xC0000082 as such:
wrmsr(0xC0000082, Xfast_syscall) // xfast_syscall is fast syscall entry on FreeBSD/Orbis
After that, every syscall instruction jumps there. Because the kernel image was relocated by KASLR before that point, the value in LSTAR is a runtime kernel address, not a link-time address. Reading it gives us one known point inside the kernel’s executable segment:
LSTAR = 0xFFFFFFFF9D4341C0
From here on, the rest is arithmetic!
From LSTAR to the image base
There are three ways to get from that pointer to the start of the kernel image:
- If the syscall entry lies in the first page of the image, the base is just the address rounded down to the page boundary. I find this sloppy because there are no guarantees that assertion is correct. On Orbis, the page size is 16 KiB:
uint64_t base = lstar & ~0x3FFFULL;For the tested firmware (13.50) that is exact: LSTAR
0xFFFFFFFF9D4341C0LSTAR & ~3FFF0xFFFFFFFF9D434000<- image base - The general method is to scan backwards from LSTAR on 16 KiB boundaries for an ELF header, then validate it:
for (uint64_t addr = lstar & ~0x3FFFULL; addr; addr -= 0x4000) { if (*(uint32_t *)addr != 0x464C457F) /* "\x7fELF" */ continue; if (!valid_kernel_elf(addr, lstar)) continue; return addr; }You should check at least:
EI_CLASS == ELFCLASS64 && EI_DATA == ELFDATA2LSBe_machine == EM_X86_64e_type == ET_EXEC || ET_DYNe_phentsize == sizeof(Elf64_Phdr)and a sanee_phnum
Most importantly;
e_entryis a canonical kernel address and falls inside aPT_LOADLSTARfalls inside aPT_LOAD
You should check for all of those. For instance, a 13.50 dump contains seven \x7fELF signatures. Three of them are structurally plausible headers with kernel-looking PT_LOAD addresses but they are not 16 KiB-aligned and their e_entry values are low user-space addresses (0x400080, 0x414700)
This method dereferences kernel pages while scanning. If your primitive can fault, find a better one, prefer the mask or:
- Hardcode a firmware-specific value. For a given firmware build, the syscall entry is always at the same offset from the image base. You can, as Al Azif’s SDK does, simply subtract that offset:
image_base = LSTAR - deltaFor 13.50,delta = 0x1C0. Read on to understand how to obtain that value.
Computing the kASLR slide
Do not confuse that firmware-specific delta with the slide! The slide is image_base - link_base.
If the unslid kernel ELF links at 0xFFFFFFFF82200000, then:
runtime_base 0xFFFFFFFF9D434000
link_base 0xFFFFFFFF82200000
slide 0x1B234000
slide = runtime_base - link_base;
The runtime base is what defeats KASLR. The slide is a derived value you can print for diagnostics. My toolkit’s static offsets (PS_SYSENT_OFFSET, KERNEL_MAP_OFFSET, etc.) are relative to the runtime base, so they work even if the link-base assumption is wrong.
On the 13.50 dump, the runtime image starts at 0xFFFFFFFF9D434000 and the link-time base still appears in kernel metadata as 0xFFFFFFFF82200000, which corroborates the slide.
Consequently: that switch table exists within the kernel’s ELF. So, if we can read that MSR register’s address, we can have the address a region inside the kernel, subject to KASLR. Then, we could scan backwards for the first ELF header we find to find the image’s start and, from headers, identify the image’s size…?
The delta exists because the syscall entry is not at a universal offset. Kernel code layout changes between firmware builds, so the distance from the image base to Xfast_syscall changes too. The delta is stable for a given firmware and independent of the KASLR slide. Deriving it from one boot The simplest way to find the delta for a new firmware is to use the generic scan once:
- Boot that firmware with a kernel payload.
- read IA32_LSTAR with rdmsr (restricted instruction but don’t forget, we’re in kernel-mode!);
mov ecx, 0xC0000082 rdmsr ; result in edx:eax - Recover the image base with one of the three methods above
- Compute:
delta = LSTAR - image_base - Reboot and repeat. LSTAR changes with the slide, image_base changes with the slide, but your delta should stay the same!
Now you can find joy in having found a build-specific delta.
Deriving it offline
You can also derive it from a decrypted kernel image for that firmware:
- Take the unslid kernel ELF or a runtime dump where file offset 0 is the image base.
- Find the syscall entry stub. On 13.50 the signature I found is:
swapgs; 0F 01 F8 mov %rsp,%gs:0x2a8 ; 65 48 89 24 25 A8 02 00 00 ; 0F 01 F8 65 48 89 24 25 A8 02 00 00` - Subtract the image base:
stub_vaddr 0xFFFFFFFF9D4341C0 image_base 0xFFFFFFFF9D434000 delta 0x1C0The signature is unique in the 13.50 dump, but do not assume it is identical in every build. If it changes, identify the entry by its behavior and its position in the executable segment, not only by those bytes.
TLDR:
- Read MSR
0xC0000082(IA32_LSTAR). It holds the runtime address of the syscall entry. - Recover the image base: LSTAR &
~0x3FFF, a validated backward ELF scan, or LSTAR - firmware_delta. - Validate the candidate: ELF64, x86-64, sane program headers, e_entry canonical and inside a PT_LOAD.
- Compute the image size from the program-header span.
Phew. Now, this out of the way.
Dumping the kernel
Once KASLR is defeated and KERNEL_IMAGE_SIZE is known, the kernel dump is a straight copy out of kernel memory. For each chunk:
- address = KERNEL_ADDRESS_IMAGE_BASE + offset + done
kernel_copyout(address, buf, 0x8000)(kernel_copyout is effectively a syscall)- send DATA frame to host/write to disk
- done += chunk
On Exotic, chunks are 32kb.
What does a full kernel dump look like, with my framework?
shell> kdump
wrote kernel-20261201-150034.bin 42158888 bytes
As easy as that. And there goes your ELF. Load into your favorite RE framework at KERNEL_ADDRESS_IMAGE_BASE (the very first bytes of the file).
It contains:
- the ELF header and program headers,
- the executable and read-only segments,
- the writable data segment and BSS,
- runtime addresses with the KASLR slide applied.
$ readelf -h kernel-20261201-150034.bin Class: ELF64 OS/ABI: UNIX - FreeBSD Machine: Advanced Micro Devices X86-64 Entry point address: 0xffffffff9d49e410 ; runtime address Number of program headers: 6That dump may contain sensitive material such as keys; assume worst-case!
Dumping loaded kernel modules
There are two different things people call “kernel modules” on this system:
- Loaded SPRX modules, which appear in a process’s dynlib list. libkernel_sys.sprx, libSceNet.sprx, and libSceSysmodule.sprx are examples.
- Statically linked internal modules, which are compiled into the kernel image and do not appear in any dynlib list, I discuss them later.
Loaded SPRX modules: dynlib list
The kernel keeps a per-process list of loaded objects. kernel_get_proc(pid) finds the process by walking allproc and matching p_pid. On 13.50:
proc + 0xB0// p_pid
proc + 0x340 // dynlib list head
// each list entry is a dynlib_obj_t with the fields we need:
typedef struct dynlib_obj {
uintptr_t next;
uintptr_t path;
...
uint64_t handle;
uintptr_t mapbase;
uintptr_t mapsize;
uintptr_t textsize;
uintptr_t database;
size_t datasize;
...
uintptr_t vaddrbase;
uintptr_t entry;
...
} dynlib_obj_t;
dynlibs [pid] walks that list and prints, for every object:
handle, base, size, text, data, datasize, vaddr, entry, path
dynlibs 0 asks for the kernel process and shows the kernel-loaded modules.
Why not use kldstat/kldnext? Well did you even read the intro?
To dump, use my payload (module_dump <pid> <basename> [cap]):
- Resolves the process with kernel_get_proc(pid).
- Finds the module by basename with kernel_dynlib_handle.
- Copies the dynlib_obj_t with kernel_dynlib_obj.
- Streams from obj.mapbase for min(obj.mapsize, cap) bytes using kernel_copyout.
- Sends a module_meta text line before the data.
This writes three files to your host:
<name>.bin mapped image, mapbase .. mapbase + mapsize (or cap)
<name>.bin.json pid, name, handle, base, size, text, data,
datasize, vaddr, entry
<name>.bin.elf synthetic ELF64 for IDA/Ghidra
The synthetic ELF has one PT_LOAD at the runtime base, e_entry = entry, file offset 0x1000, and alignment 0x4000. Its PF_R|PF_W|PF_X flags are a disassembler convenience rather than live kernel permission.
Typical usage:
shell> dynlibs 0
shell> module_dump 0 libSceNet.sprx
Then load libSceNet.sprx.bin.elf in IDA/Ghidra. Use the JSON to rebase, and the raw .bin when you need a byte-exact mapped image.
For protected system SELFs under /system/sys and /system/vsh, do not load them again just to dump them. A protected SELF could panic the kernel. Use GoldHEN’s FTP with DECRYPT, or work from the kernel dump.
What about the crown jewel, the thing we came for originally, SAMU’s CCP, which wasn’t visible as a loaded module (anywhere)? Well…
Statically linked internal modules
These are the most important modules of the OS and never appear in dynlibs. Among a few, you can find: Bluetooth & WiFi drivers, hardware drivers, audio drivers… They are inside the stripped kernel image. So how to find them? Debug strings in production builds, like:
W:\Build\J02696091\sys\internal\modules\<module>\<unit>\<file>.c
found in the core of every function for a given module. You can therefore identify functions related to a specific module by relying on this string being present. My static extraction pipeline uses those strings to identify individual modules:
- Dump the kernel with kdump.
- Open the dump in IDA.
- Load in IDA: tools/kmodules.py [OUT] kernel.elf.
- Inspect your dump dir.
It should have this format:
OUT/
index.md
names.py
<module>/
names.py
<unit>/
functions.csv address, size, proposed name, source path
strings.txt string address + source path
module.json structured functions and strings
Apply names.py inside IDA to label the attributed functions. Of course, this heuristic might miss some modules or functions but I believe it is the best possible given our technical limitations.
Last notes
Remember the ENOSYS stub at the beginning? Now that we have the kernel binary, we can look at the syscall handler at slot 308, to see the stub in practice from the kernel:
Bytes at the handler:
55 48 89 e5 b8 4e 00 00 00 5d c3 90 90 90 90 90
Disassembly:
55 push rbp
48 89 e5 mov rbp, rsp
b8 4e 00 00 00 mov eax, 0x4e
5d pop rbp
c3 ret
90 nop
Or, if you use Exotic:
shell> sysent 304 309
304 narg=1 handler=0xFFFFFFFF9D6A6E50 off=0x272E50 code=554889E5B84E0000 ; ENOSYS
305 narg=1 handler=0xFFFFFFFF9D6A6E70 off=0x272E70 code=554889E5B84E0000 ; ENOSYS
306 narg=1 handler=0xFFFFFFFF9D6A6E90 off=0x272E90 code=554889E5B84E0000 ; ENOSYS
307 narg=1 handler=0xFFFFFFFF9D6A6EA0 off=0x272EA0 code=554889E5B84E0000 ; ENOSYS
308 narg=2 handler=0xFFFFFFFF9D6A6EB0 off=0x272EB0 code=554889E5B84E0000 ; ENOSYS
309 narg=1 handler=0xFFFFFFFF9D6A6ED0 off=0x272ED0 code=554889E5B84E0000 ; ENOSYS
:)
And so I conclude the most technically demanding piece I’ve written so far, with a heartfelt thank you to the infosec and vulnerability research community for taking such like in my work, I think there are no words to express how grateful I am, it means a lot!
Oh, and, remember I wanted to look at CCP and SAMU? More on that on another episode!
Thank you for reading! If you liked this post, don’t hesitate to follow my Twitter/X and/or email me your thoughts!
- pwn2nice