Repository navigation
Conversation
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>
|
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.
|
Good point, there isn't one: the scan dated from the physical-layer approach. I've changed |
|
More data for this, on four builds besides the Win7 x64 image in the description: PR head
I traced 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, So whether develop fails depends on the order of the two images in that boot. |
ikelos
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
|
Thanks for the review. I've pushed a commit that drops the On where this is called from and the DTB: On corrupted images: if any of those reads fails ( For reference, on a Win7 SP1 x64 image |
ikelos
left a comment
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
|
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
|
No problem at all, thanks for taking the time. I've pushed a commit that follows your suggestion: One wrinkle worth knowing about: pefile parses the CodeView record ( Reconstructing the whole image didn't cost anything noticeable: And thanks for clarifying the vlayer point. That makes sense. |
ikelos
left a comment
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
|
Thanks! I've added the |
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.pslistreturns no rows on currentdevelop(it worked before the recent DTB-scan changes).Cause.
KernelPDBScanner.check_kernel_offsetscans up tomax_pdb_size(4 MB) past anMZheader for a kernel PDB name, but always returns the scanned address as the kernel base.method_low_stub_offsetwalks pages upward from the 2 MB-alignedLmTarget, so the firstMZit checks ishal.dll, loaded just below ntoskrnl. The scan from there reachesntkrnlmp.pdbinside ntoskrnl (mz_offset0xf80001867000), yet the hal.dll base (0xf8000181f000) is used askernel_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_offsetruns first. Previously a different DTB was chosen, and the kernel base came from the module list.Fix. Only accept PDB hits whose
mz_offsetequals the address being checked, so the low stub walk continues to the real ntoskrnlMZ.Testing
test/test_pdbscan_kernel_offset.py(no image needed); thehalcase fails without the fix.windows.pslist0 → 76 rows, with the same PID/PPID/names as before the regression.🤖 Generated with Claude Code