BrowserGather Part 2: Fileless Chrome Cookie Extraction with PowerShell

I am pleased to announce that the BrowserGather project now supports the extraction of Chrome cookie data. In the first part of the BrowserGather project, I used binary regular expressions in PowerShell to extract Chrome credentials in a novel, fileless manner. In this blog post, I will discuss how I was able to apply this technique to extract Chrome cookie data, and the issues that arose during development. Check out my GitHub for the updated code.

The cookie SQLite database in Chrome is significantly more complicated than the one used for storing credentials. There are several distinct fields present, including flags for the cookie (such as HTTPOnly), as well as properties like hostname, name, and path. In addition, it is not unusual for the cookie database to contain several hundred entries within it. While I was able to develop regular expressions that cover all scenarios I encountered during testing, the size of the database created an issue that could not be resolved.

First, let’s discuss the regular expressions used to extract the encrypted blob. The encrypted blob contains the actual value of the cookie, similar to how the password is stored in the credential database. It is encrypted using the Windows Data Protection API. The following regex was used to extract the blob from the database file:
(\x01\x00\x00\x00\xD0\x8C\x9D\xDF\x01\x15\xD1\x11\x8C\x7A\x00\xC0\x4F\xC2\x97\xEB\x01\x00\x00\x00)[\s\S]*?(?=[\s\S]{2}\x97[\s\S]{8}\x00[\s\S]{2}\x0D|\x0D[\s\S]{2}\x00[\s\S]{3}\x00\x02|\x00{20}|\Z)
The lookbehind portion was simple, and is consistent with all DPAPI-encrypted blobs. The lookaheads were considerably more complex, and encompass four different situations I found during testing. In order:

  1. Lookahead for the next cookie entry inside the database. This is very similar to the lookbehind used in the cookie properties extraction.
  2. Unique lookahead that occurred in a small minority of cases. This one is most at risk of causing a regex mismatch.
  3. A series of 20 null bytes in a row, indicating the end of the blob.
  4. An End of File (EOF) check.

An interesting note about scenario 3, this regex created a situation where a trailing null byte could be removed from the encrypted blob itself, leading to decryption failure. The function will automatically append a null byte to any decryption failures before proceeding, in order to correct for this situation.

Now let’s look at the regex used to extract the properties of each cookie. In its current form, only the Name, Hostname, and Path values are extracted. This should be sufficient for whatever cookie forging you are attempting, but please let me know if it is not. The regex is as follows:

(?<=\x97[\s\S]{8}\x00[\s\S]{2}\x0D[\s\S]{11,12})[\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x2d\x21\x20\x22\x20\x23\x20\x24\x20\x25\x20\x26\x20\x27\x20\x28\x20\x29\x20\x2a\x20\x2b\x2d\x20\x2e\x20\x2f\x3a\x3c\x20\x3d\x20\x3e\x20\x3f\x20\x40\x5b\x20\x5c\x20\x5d\x20\x5e\x20\x5f\x20\x60\x7b\x20\x7c\x20\x7d\x20\x7e\x2c]{3,}?(?=[\x00\x01\x02\x03])

The lookbehind used was quite tricky, and went through many updates before I found one that worked in all test cases. I had to restrict the characters returned by possible ASCII values, by using the Netscape cookie allowed character list as a baseline, along with other characters found during testing. Finally, the lookahead simply looks for the first terminating character encountered after the data is collected.

The returned properties string looks something like this:

.stackoverflow.comprov/

The blue text is the hostname. It always ends with the Top Level Domain (TLD). The red text is the name of the cookie. The green text is the path, which always begins with the backslash character. While it would technically be possible to write a regex to separate these values, you would need to account for every TLD in existence, as well as future ones. I believe it to be easier to simply separate them by hand.

Now, let’s talk about the database size issue. Once the SQLite cookie database grows to a certain size (around 400 cookies in my testing), the encrypted blobs are stored non-contiguously. The entry remains at the same place, but the data gets moved around to a different location, usually within a different encrypted blob. This means that both the original entry, as well as the entry where the data gets moved to, gets corrupted. Luckily this only affects about 1% of cookies in large databases, with the other 99% of cookies being unaffected. When this error occurs, the encrypted blob will be replaced with the string “Unable to decrypt blob”, allowing for easy recognition. Hopefully the cookie you need is not one of the affected ones, or you don’t run into this situation at all!

Finally, I have implemented a simple regex mismatch check to help with troubleshooting. If the number of cookie property matches does not match the number of encrypted blob matches, then a non-terminating error is presented with the following text:
"The number of cookies is different than the number of encrypted blobs! This is most likely due to a regex mismatch."
Depending on the severity of the mismatch, this could lead to cookie properties being matched with the wrong decrypted blob contents, or even entirely missing. If you see this error, please let me know so that I can try to adjust the regular expressions. I have also included this check in the Get-ChromeCreds function.

An explanation of how to use the data extracted from this tool is a bit out scope for this writeup. However, I do highly recommend Empire’s Get-BrowserData function. It is able to retrieve the history of Chrome, in order to determine which cookies are recent, and therefore useful.

That’s it for the BrowserGather project for now, I may extend the current functionality to different browsers once I’m no longer sick of looking at regular expressions. Thanks for reading!

Acknowledgements:

  • wald0, tifkin_, and harmj0y for requesting this project.
  • mattifestation for the many reasons listed above.
  • et0x for his existing work on PowerShell-based Chrome credential extraction, located here.
  • xorrior for his previous work on Empire and providing guidance.
  • Coalfire/Veris Group, for allowing me to develop this project.

1 thought on “BrowserGather Part 2: Fileless Chrome Cookie Extraction with PowerShell”

Leave a Reply

Your email address will not be published. Required fields are marked *