Instrumenting Windows APIs with Frida
Last updated
Last updated
Frida is dynamic instrumentation toolkit for developers, reverse-engineers, and security researchers.
We can ask frida to spawn a new process for us to instrument:
We can ask frida to attach to an existing process:
The below code in hooking.js
will find address of the Windows API WriteFile
(lives in kernel32.dll/kernelbase.dll) and hexdump the contents of the 1st argument passed to it:
Let's spawn a new notepad.exe
through Frida and supply it with the above hooking.js
code, so that we can start instrumenting the WriteFile
API and inspect the contents of the buffer that is being written to disk:
Notice that we can update the hooking.js
code and the instrumentation happens instantly - it does not require us to re-spawn the notepad or re-attaching Frida to it. In the above GIF, this can be seen at the end when we request the console to spit out the process.id
(the frida is attached to) and the notepad process ID gets printed out to the screen instantly.
If we want to see if certain API calls are invoked by some specific process, say WriteFile
, we can use frida-trace
tool like so:
Below shows how we can combine the above knowledge for something a bit more interesting.
Can we intercept the plaintext credentials from the credentials prompt the user gets when they want to execute a program as another user?
The answer is of course yes, so let's see how this could be done using Frida tools.
Let's use frida-trace
to see if explorer.exe ever calls any functions named *Cred*
when we invoke the credentials popup:
Below, we can see that indeed, there is a call to CredUIPromptForWindowsCredentialsW
made when the prompt is first invoked:
Entering some fake credentials shows the following interesting Cred*
API calls are made (in red):
...and the CredUnPackAuthenticationBufferW
(in lime) is of special interest, because per MSDN:
The CredUnPackAuthenticationBuffer function converts an authentication buffer returned by a call to the CredUIPromptForWindowsCredentials function into a string user name and password.
We can now instrument CredUnPackAuthenticationBufferW
in a frida javascript like so:
We can now hook the explorer.exe by providing frida with our instrumentation script like so:
With CredUnPackAuthenticationBufferW
instrumented, entering credentials in the prompt launched by explorer.exe, gives us the expected result - the credentials are seen in plaintext: