Corrobora Development Series Part 3: Building the Prefetch Parser(and a Bug I Didn't See Coming)
First, a quick housekeeping note. A couple of days after Part 2 went up, I renamed the project from VeriTrace to Corrobora. I wanted a name that said what the tool actually does, which is check whether different Windows artifacts back each other up. Same project, same goals, new name. If you see VeriTrace in the earlier posts, that's this.
Okay, on to Prefetch.
After EVTX and the Registry, Prefetch was the obvious next parser. When I'm looking at a Windows system, one of the first things I want to know is whether a program actually ran, and Prefetch is usually where I look first. Windows keeps these files so programs launch faster, and as a side effect they keep a record of what ran. Each one is named after the program with a hash on the end, something like POWERSHELL.EXE-CA1AE517.pf, and inside you get the run count, the last few times it ran (up to eight on Windows 8 and later), the DLLs and files it loaded, and which volume it ran from.
It's not perfect. Windows only keeps the last eight run times, and Prefetch is turned off by default on Windows Server, so a missing Prefetch file doesn't mean something never ran. I kept running into that idea while building this, and it ended up shaping how Corrobora reports things.
How the parser works:
I didn't write the binary format handling myself. Prefetch on Windows 10 and 11 is compressed, and the libscca library already handles all of that well, so the parser sits on top of it. My part was everything around it. Before parsing anything, it checks the file exists, ends in .pf, and actually has a Prefetch signature. Then it pulls the fields out one at a time. If one piece is unreadable, say the volume information, it logs that and keeps the rest instead of throwing out the whole file. I did the same thing in the EVTX and Registry parsers, and it's saved me more than once on messy data.
It also does one small check that I like: it compares the hash in the filename with the hash stored inside the file. Windows sets both, so if they don't match, the file doesn't look like something Windows created as-is. Maybe it was renamed, maybe something else happened. Corrobora flags it for a closer look, but it doesn't treat it as proof of anything.
You can run it on its own too:
corrobora-prefetch C:\Windows\Prefetch
The Bug:
This is the part I actually wanted to write about.
Each Prefetch file has eight slots for run times. If a program has only run three or four times, the rest of the slots are empty. I assumed the library would hand me nothing for those. It doesn't. It hands back January 1, 1601, which is where Windows timestamps start counting from. My parser was only looking for "nothing," so it happily reported those empty slots as runs from 1601.
And those weren't just cosmetic. They went straight into the correlation as execution evidence. Most programs on a normal PC have run fewer than eight times, so most Prefetch files were affected.
The annoying part is that I had already tested on real data. I ran it against my own Windows 11 machine, 388 Prefetch files, zero errors. I was testing with PowerShell, which I'd launched enough times to fill all eight slots, so the bug never showed up.
I only caught it weeks later while building a fake Prefetch file by hand for a test case I wanted anyone to be able to reproduce. I gave it exactly four runs, parsed it, and four extra runs from 1601 popped out right away. The fix took a few minutes: stop at the first empty timestamp, since Windows fills the slots newest first. I added tests for partly empty, fully empty, and full files so it can't quietly come back.
What stuck with me is that the parser never crashed. No errors on 388 real files, and it was still wrong. Real data only tells you about the cases you happen to have.
Where Prefetch fits in Corrobora:
On its own, Prefetch tells you a program ran. Corrobora's whole point is comparing it with other artifacts. Right now that means checking each Prefetch run against Security event 4688 or Sysmon process creation events and against the BAM registry key, which records when a program last closed. If they line up, Corrobora calls it corroborated. If they don't, it says what's missing and leaves the call to the examiner, because there are plenty of boring reasons an artifact isn't there, like auditing being off.
One thing surprised me while testing that comparison. On my machine, Prefetch recorded the run about 30 milliseconds after the process started. I'd seen people mention a delay of around ten seconds. That's just one machine, so I'm not calling it a rule, but it changed how I compare these timestamps. I'll get into that, and what BAM turned out to be recording, in Part 4.
The code is all on GitHub if you want to poke at it, and if you have a different Windows build and want to try it, I'd love to hear what you get.

Comments
Post a Comment