Skip to content

Fix pdbscan picking hal.dll as the kernel base - #2103

Merged
ikelos merged 5 commits into
volatilityfoundation:developfrom
reverseame:fix/pdbscan-kernel-mz-offset
Nov 22, 2026
Merged

ikelos merged 5 commits into
volatilityfoundation:developfrom
reverseame:fix/pdbscan-kernel-mz-offset

Conversation

@ricardojrdez

Copy link
Copy Markdown
Contributor

Resubmission of #2056, which was closed when the fork it came from was deleted. The change is unchanged, rebased onto current develop (77ff0b1), where the bug is still present.

On a Windows 7 x64 VMware snapshot, windows.pslist returns no rows on current develop (it worked before the recent DTB-scan changes).

Cause. KernelPDBScanner.check_kernel_offset scans up to max_pdb_size (4 MB) past an MZ header for a kernel PDB name, but always returns the scanned address as the kernel base. method_low_stub_offset walks pages upward from the 2 MB-aligned LmTarget, so the first MZ it checks is hal.dll, loaded just below ntoskrnl. The scan from there reaches ntkrnlmp.pdb inside ntoskrnl (mz_offset 0xf80001867000), yet the hal.dll base (0xf8000181f000) is used as kernel_virtual_offset. Every kernel symbol ends up 0x48000 off.

This path became reachable when the DTB scan stopped discarding page tables with few valid entries. The boot DTB (0x187000) is now accepted, it matches the Low Stub CR3, and so method_low_stub_offset runs first. Previously a different DTB was chosen, and the kernel base came from the module list.

Fix. Only accept PDB hits whose mz_offset equals the address being checked, so the low stub walk continues to the real ntoskrnl MZ.

Testing

  • New unit test test/test_pdbscan_kernel_offset.py (no image needed); the hal case fails without the fix.
  • Windows 7 x64 VMware snapshot: windows.pslist 0 → 76 rows, with the same PID/PPID/names as before the regression.
  • Unchanged: a Win10 22621 crash dump (220), a Win10 19041 ELF dump (124), and two Windows ELF dumps (57 and 29).

🤖 Generated with Claude Code

check_kernel_offset scans up to max_pdb_size bytes past an MZ header for
a kernel PDB name, but always returned the scanned address as the kernel
base. When that MZ belongs to an image just below ntoskrnl (hal.dll on
Windows 7 x64), the scan reaches ntkrnlmp.pdb inside ntoskrnl and the
hal.dll base is used as kernel_virtual_offset, shifting every kernel
symbol (0x48000 on the test image) and leaving pslist empty.

Only accept PDB hits whose mz_offset is the address being checked, so
method_low_stub_offset keeps walking to the real ntoskrnl MZ.

This became reachable once the DTB scan stopped discarding page tables
with few valid entries: on a Windows 7 x64 VMware snapshot the boot DTB
now matches the Low Stub CR3, so method_low_stub_offset runs first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ikelos

ikelos commented Nov 15, 2026

Copy link
Copy Markdown
Member

If we've already constructed a virtual layer, we probably don't need to scan for the pdb header? We should just be able to read the debug information out of the exe at the MZ header. The reason we used to scan was when this was working on physical layers and so had to guess at the layout of the exe and how it hung together. Is there a reason for still scanning now we're limiting it to only this MZ?

check_kernel_offset scanned up to 4 MB after an MZ header for a kernel PDB
name, a leftover from the physical-layer approach. On a virtual layer the
image is mapped, so read its CodeView record through the PE debug directory
(PE32 and PE32+) instead. A PDB belonging to the next image (e.g. ntoskrnl
after hal.dll) can no longer be attributed to the checked address, so the
mz_offset filter is no longer needed. max_pdb_size was only used here.
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Good point, there isn't one: the scan dated from the physical-layer approach. I've changed check_kernel_offset to read the CodeView record through the image's own debug directory (PE32 and PE32+), so a PDB belonging to the next image can no longer be picked up and the mz_offset == address filter is no longer needed. max_pdb_size was only used there and is gone. The test now builds synthetic PE images (hal.dll below the kernel, the kernel, no image, a PE32 image). Against develop on five Windows images: the Win7 x64 VMware image goes from hal.dll as kernel base and 0 processes to the ntoskrnl base and 76; two Win10 x64 dumps and two Win7 x86 ELF dumps give the same kernel base, symbols, and pslist counts.

@ricardojrdez

Copy link
Copy Markdown
Contributor Author

More data for this, on four builds besides the Win7 x64 image in the description: PR head e32c8f3a against develop 77ff0b19.

Image develop: kernel base / pslist This PR: kernel base / pslist MemProcFS 5.19 at develop's / this PR's base
Server 2012 R2, 6.3.9600.17031 (VirtualBox core) 0xf802e1607000 / 0 0xf802e1677000 / 37 hal.dll / ntoskrnl.exe
Same VM, another boot 0xf80066005000 / 38 0xf80066005000 / 38 ntoskrnl.exe
Windows 8.1 x64 (VMware snapshot) 0xf8018f81b000 / 0 0xf8018f88b000 / 29 HAL.dll / ntoskrnl.exe (PE headers read through MemProcFS, its module list is empty on this image)
Windows Vista x64 (VMware snapshot) 0xf80001810000 / 0 0xf80001856000 / 33 hal.dll / ntoskrnl.exe
Windows 7 x64 (VMware snapshot, another image than yours) 0xf80002a0f000 / 0 0xf80002a57000 / 29 hal.dll / ntoskrnl.exe

I traced KernelPDBScanner on develop (wrapping check_kernel_offset and the methods, no code change). On all five images LmTarget points into hal.dll, and the walk starts at the 2 MB boundary below it. For example, on the first Server 2012 R2 boot:

low stub at 0xb000: LmTarget 0xf802e1622840, walk starts at 0xf802e1600000
check_kernel_offset MZ at 0xf802e1607000: ntkrnlmp.pdb signature 0xf802e1691e40 mz_offset 0xf802e1677000
method_low_stub_offset: 0xf802e1607000

The other boot of the same VM shows why develop gets some images right: there ntoskrnl is loaded below hal.dll (hal.dll starts where ntoskrnl ends, 0xf80066005000 + 0x789000), so the 4 MB scan from the hal.dll header finds no kernel PDB, the walk moves down in 2 MB steps and reaches the ntoskrnl MZ:

low stub at 0xb000: LmTarget 0xf800667a9840, walk starts at 0xf80066600000
check_kernel_offset MZ at 0xf8006678e000 (HAL.dll): none
check_kernel_offset MZ at 0xf80066005000: ntkrnlmp.pdb signature 0xf8006601fe40 mz_offset 0xf80066005000

So whether develop fails depends on the order of the two images in that boot.

@ikelos ikelos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making these changes. I'm quite cautious about hard coding in specific offsets to read structures (even if they're very old and stable) given the volatility object system was designed to cope with changing structures and offsets.

This is slightly more specific because it's before Volatility has bootstrapped all the information it needs about the image, but I'd still feel better using the volatility parsing framework than direct struct calls to achieve this (since it will set a bad precedent for the rest of the codebase).

I also need to trace back where this is called from and how we ended up being certain enough in our DTB to ensure that hunting down the kernel this way can be done in the virtual layer, and that nothing is lost when applying this to corrupted images.

optional_header = nt_header + 0x18
(magic,) = struct.unpack("<H", vlayer.read(optional_header, 2))
# Offset of NumberOfRvaAndSizes in IMAGE_OPTIONAL_HEADER32/64
rva_count_offset = {0x10B: 0x5C, 0x20B: 0x6C}.get(magic)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not keen on having arbitrary hex bytes embedded in the code. It would be much better to try instantiating an IMAGE_OPTIONAL_HEADER32 at hte chosen address and read it using volatility structures (which can change between versions) than hard coding in specific values, even if the likelihood of them changing is low.

Same with the debug_rva and debug_size parsing below. There should already be code to do this in volatility and as long as it doesn't carry too many dependencies, we should try to do it that way where possible

- Replace the hard-coded struct offsets in _codeview_record with the windows/pe
  ISF types (_IMAGE_DOS_HEADER.get_nt_header, OptionalHeader.DataDirectory,
  _IMAGE_DEBUG_DIRECTORY), loaded once per scanner and context
- Name the bounds on debug directory count and CodeView record size
- Set FileHeader.Machine in the test PE images, which get_nt_header uses to
  choose between the PE32 and PE32+ headers
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I've pushed a commit that drops the struct calls and hard-coded offsets: _codeview_record now instantiates _IMAGE_DOS_HEADER from the windows/pe ISF, uses get_nt_header() to pick the PE32/PE32+ header, and reads OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] and an array of _IMAGE_DEBUG_DIRECTORY through the object system. The pe table doesn't depend on the kernel symbols, so it can be used at this stage; it is loaded once per scanner (check_kernel_offset can be called a few thousand times from the low stub method). The only literals left are the PE spec enum values (IMAGE_DIRECTORY_ENTRY_DEBUG = 6, IMAGE_DEBUG_TYPE_CODEVIEW = 2) and two bounds on how much is read from a candidate.

On where this is called from and the DTB: check_kernel_offset is only reached from method_low_stub_offset (x64 only: the PROCESSOR_START_BLOCK whose CR3 matches the DTB the layer was built with, LmTarget as the hint, then stepping back by pages) and from _method_offset (method_kdbg_offset / method_module_offset: a pointer next to a KDBG / \SystemRoot\system32\nt hit in the physical layer). In both cases vlayer is the Intel layer the Windows stacker already built from the DTB before pdbscan runs, and the previous code already read from that same virtual layer (pdbname_scan over vlayer.name from address to address + max_pdb_size). So this change doesn't add any new reliance on the DTB; it narrows what is read from a 4 MB window to the candidate image's own headers, debug directory and a CodeView record of at most 1 KB.

On corrupted images: if any of those reads fails (InvalidAddressException), the header is malformed (get_nt_header raising ValueError), there is no CodeView entry, or the RSDS record isn't at the address the debug directory points to, that candidate is rejected and the loop moves on to the next candidate and then the next method. method_fixed_mapping and method_slow_scan are unchanged and still find the kernel by scanning for RSDS records. The one case that behaves differently is a candidate whose own debug record is unreadable but which has a kernel PDB record somewhere in the following 4 MB. The old code accepted that address, but with whichever PDB it found first, which is exactly how hal.dll ended up paired with ntoskrnl's GUID. Now that candidate is skipped and the later methods handle it.

For reference, on a Win7 SP1 x64 image develop picks 0xf8000181f000 (hal.dll) with the ntkrnlmp GUID, while this branch picks 0xf80001867000; two Win10 x64 images resolve to the same base on both.

@ikelos ikelos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'm much happier with this, but rather it turns out when we do this in other plugins, we tend to dive off from volatility after we get the dos_header, and instead put it through pefile (which is a core dependency and then ensures we're not reimplementing PE reading code, and all of it's centralized in one place).

We also try to avoid putting constants in file at the top, because if one part of volatility uses them, they all should, so ideally those would live in volatility3.constants.windows if we can't eliminate them complete (I think a couple of them become strings when interacting with pefile).

ValidKernelType = Tuple[str, int, Dict[str, Optional[Union[bytes, str, int]]]]
KernelsType = Iterable[Dict[str, Any]]

IMAGE_DIRECTORY_ENTRY_DEBUG = 6

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other plugins (skeleton_key_check, iat, verinfo and pe_symbols) we'd use pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_DEBUG"] rather than needing to keep the constants in here. Since pefile is a required dependency of volatility3, it should be fine to use here.

I'm happy enough using pefile to parse the data rather than volatility given we use it elsewhere and it would save reimplementing the same code in multiple places, but it depends how easy it is to get the pedata read out from the image.

Essentially I believe it'd be something like:

                try:
                    for offset, data in dos_header.reconstruct():
                        pe_data.seek(offset)
                        pe_data.write(data)
                except (exceptions.InvalidAddressException, ValueError) as excp:
                    vollog.warning(
                        f"Exception triggered when reconstructing PE file for process {proc.UniqueProcessId} at address {peb.ImageBaseAddress:#x} due to {excp}. Output file may be corrupt a
nd/or truncated."
                    )

                try:
                    pe_obj = pefile.PE(data=pe_data.getvalue(), fast_load=True)
                except pefile.PEFormatError as excp:
                    vollog.debug(
                        f"Exception triggered when creating PE file object for process {proc.UniqueProcessId} at address {peb.ImageBaseAddress:#x} due to {excp}. Unable to extract file."
                    )
                    continue

                pe_obj.parse_data_directories(
                    [pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_DEBUG"]]
                )

And you should be able to do the samething for CODEVIEW entry.

Thanks for putting up with all the little tweaks I'm asking for, but this is quite a core component of volatility and it will get used by lots and lots of people so I want to make sure we tweak it all now, rather than have to tinker with it down the line...

@ikelos

ikelos commented Nov 21, 2026

Copy link
Copy Markdown
Member

Yeah, I didn't think this added any reliance on a vlayer, I just thought it used to work on physical layers and I couldn't remember when we started to guarantee we handed it a working vlayer.

- Reconstruct the candidate image from its _IMAGE_DOS_HEADER and parse the
  debug directory with pefile, as other Windows plugins do, instead of walking
  the PE headers with the pe ISF types
- Use pefile.DIRECTORY_ENTRY and pefile.DEBUG_TYPE instead of module constants,
  and drop the read bounds that reconstruct() and pefile now cover
- Read the CodeView record from AddressOfRawData: pefile parses it from
  PointerToRawData, a file offset that does not hold the record in images
  mapped in memory when file and section alignments differ
- Make the test images loadable by pefile and point their PointerToRawData
  away from the record
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

No problem at all, thanks for taking the time. I've pushed a commit that follows your suggestion: _codeview_record now reconstructs the candidate from its _IMAGE_DOS_HEADER, loads it with pefile.PE(data=..., fast_load=True) and parses pefile.DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_DEBUG"], matching the entry against pefile.DEBUG_TYPE["IMAGE_DEBUG_TYPE_CODEVIEW"]. The module-level constants are gone, and so are the two read bounds, since reconstruct() already caps the image size.

One wrinkle worth knowing about: pefile parses the CodeView record (debug_entry.entry) from the debug directory's PointerToRawData, which is a file offset. reconstruct() rewrites the section headers, but not the debug directory, so in a mapped image that offset only holds the record when file and section alignment happen to match. On a Win7 SP1 x64 kernel (AddressOfRawData 0x12740, PointerToRawData 0x11d40) and on one of my Win10 x64 kernels (0x40530 vs 0x3fd30), entry comes back as None. So the record is read with pe_obj.get_data(AddressOfRawData, SizeOfData) and parsed with the existing PdbSignatureScanner, which keeps the GUID formatting consistent with the rest of pdbscan. The test images now have a PointerToRawData that doesn't point at the record, to cover that case.

Reconstructing the whole image didn't cost anything noticeable: windows.info takes about the same time as with the previous version on the three images I have, and reconstructing ntoskrnl itself takes 30–70 ms.

And thanks for clarifying the vlayer point. That makes sense.

@ikelos ikelos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is looking really good. One of our tests spotted that there is now a dependency on PdbSignatureScanner which needs to be declared, but otherwise this should be ready for commit! Thanks very much for spotting and taking the time to fix it... 5:)

@classmethod
def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
return [
requirements.VersionRequirement(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly PdbSignatureScanner is a separate dependency, so needs to be listed up here to ensure any changes/breaks in that scanner get spotted quickly against the KernelPdbScanner.

- check_kernel_offset parses the CodeView record with PdbSignatureScanner,
  so list it in KernelPDBScanner.get_requirements() as the code analysis test
  requires
@ricardojrdez

Copy link
Copy Markdown
Contributor Author

Thanks! I've added the PdbSignatureScanner VersionRequirement to KernelPDBScanner.get_requirements(); test/volatility3_code_analysis.py passes locally now. Thanks a lot for the thorough review.

@ikelos
ikelos merged commit 46d73b6 into volatilityfoundation:develop Nov 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants