gftrace:一款针对Golang源码的Windows API跟踪工具

admin 2024年7月20日12:47:05评论9 views字数 5228阅读17分25秒阅读模式
关于gftrace
gftrace是一款针对Golang源码的Windows API跟踪工具,可以帮助广大研究人员对Golang二进制文件进行安全分析。
gftrace:一款针对Golang源码的Windows API跟踪工具
该工具本质上是一个命令行工具,当前正处于积极开发中,欢迎随时提供反馈。
运行机制
尽管 Golang程序在构建方式和运行时行为方面有很多细微差别,但它们仍然需要与操作系统层交互,这意味着在某些时候它们需要从 Windows API 调用函数。
Go运行时包包含一个名为asmstdcall的函数,该函数是一种用于与Windows API交互的「网关」。由于该函数需要调用Windows API函数,我们可以假设它需要访问函数地址及其参数等信息,而这正是gftrace可以切入的地方。
asmstdcall函数可以接收一个参数,该参数是指向类似于以下结构的指针:
struct LIBCALL {DWORD_PTR Addr;DWORD Argc;DWORD_PTR Argv;DWORD_PTR ReturnValue;[...]}
其中一些字段是在调用API函数后填充的,例如返回值,其他字段由asmstdcall接收,例如函数地址、参数数量和参数列表。无论何时设置这些字段,很明显asmstdcall函数操纵了大量有关Golang中编译的程序执行的有趣信息。
gftrace利用asmstdcall及其工作方式来监控上述结构的特定字段并将其记录给用户。该工具能够记录函数名称、其参数以及Golang应用程序调用的每个Windows函数的返回值。所有这些都不需要设置单个API函数钩子或为其提供签名。
该工具还会尝试忽略 Go 运行时初始化产生的所有噪音,并且仅记录其后调用的函数(即来自主包的函数)。
工具安装
广大研究人员可以直接访问该项目的【https://github.com/leandrofroes/gftrace/releases/tag/v3.3】下载最新版本的预编译gftrace。
除此之外,我们也可以使用下列命令将该项目源码克隆至本地:
git clone https://github.com/leandrofroes/gftrace.git
然后将项目源码导入到Visual Studio中,自行完成项目代码构建。
工具使用
1、确保gftrace.exe、gftrace.dll和gftrace.cfg位于同一目录中;
2、在gftrace.cfg文件中指定要跟踪的API函数(如果不应用API过滤器,该工具将无法工作);
3、运行gftrace.exe,并将目标Golang程序路径作为参数传递;
命令执行样例如下:
gftrace.exe <filepath> <params>
工具配置
我们唯一需要做的,就是在gftrace.cfg文件中指定想要跟踪的函数,并用逗号分隔(不带空格):
CreateFileW,ReadFile,CreateProcessW
工具使用样例
跟踪一个Golang文件(调用os.ReadFile两次)中的CreateFileW()和ReadFile()调用:
- CreateFileW("C:UsersuserDesktopdoc.txt", 0x80000000, 0x3, 0x0, 0x3, 0x1, 0x0) = 0x168 (360)- ReadFile(0x168, 0xc000108000, 0x200, 0xc000075d64, 0x0) = 0x1 (1)- CreateFileW("C:UsersuserDesktopdoc2.txt", 0x80000000, 0x3, 0x0, 0x3, 0x1, 0x0) = 0x168 (360)- ReadFile(0x168, 0xc000108200, 0x200, 0xc000075d64, 0x0) = 0x1 (1)
跟踪TunnelFish恶意软件中的CreateProcessW():
- CreateProcessW("C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe", "powershell /c "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-Recipient | Select Name -ExpandProperty EmailAddresses -first 1 | Select SmtpAddress | ft -hidetableheaders"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000ace98, 0xc0000acd68) = 0x1 (1)- CreateProcessW("C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe", "powershell /c "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-Recipient | Select Name -ExpandProperty EmailAddresses -first 1 | Select SmtpAddress | ft -hidetableheaders"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000c4ec8, 0xc0000c4d98) = 0x1 (1)- CreateProcessW("C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe", "powershell /c "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-Recipient | Select Name -ExpandProperty EmailAddresses -first 1 | Select SmtpAddress | ft -hidetableheaders"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc00005eec8, 0xc00005ed98) = 0x1 (1)- CreateProcessW("C:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe", "powershell /c "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn; Get-Recipient | Select Name -ExpandProperty EmailAddresses -first 1 | Select SmtpAddress | ft -hidetableheaders"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000bce98, 0xc0000bcd68) = 0x1 (1)- CreateProcessW("C:WINDOWSsystem32cmd.exe", "cmd /c "wmic computersystem get domain"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000c4ef0, 0xc0000c4dc0) = 0x1 (1)- CreateProcessW("C:WINDOWSsystem32cmd.exe", "cmd /c "wmic computersystem get domain"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000acec0, 0xc0000acd90) = 0x1 (1)- CreateProcessW("C:WINDOWSsystem32cmd.exe", "cmd /c "wmic computersystem get domain"", 0x0, 0x0, 0x1, 0x80400, "=C:=C:UsersuserDesktop", 0x0, 0xc0000bcec0, 0xc0000bcd90) = 0x1 (1)[...]
跟踪Sunshuttle恶意软件中的多个函数:
- CreateFileW("config.dat.tmp", 0x80000000, 0x3, 0x0, 0x3, 0x1, 0x0) = 0xffffffffffffffff (-1)- CreateFileW("config.dat.tmp", 0xc0000000, 0x3, 0x0, 0x2, 0x80, 0x0) = 0x198 (408)- CreateFileW("config.dat.tmp", 0xc0000000, 0x3, 0x0, 0x3, 0x80, 0x0) = 0x1a4 (420)- WriteFile(0x1a4, 0xc000112780, 0xeb, 0xc0000c79d4, 0x0) = 0x1 (1)- GetAddrInfoW("reyweb.com", 0x0, 0xc000031f18, 0xc000031e88) = 0x0 (0)- WSASocketW(0x2, 0x1, 0x0, 0x0, 0x0, 0x81) = 0x1f0 (496)- WSASend(0x1f0, 0xc00004f038, 0x1, 0xc00004f020, 0x0, 0xc00004eff0, 0x0) = 0x0 (0)- WSARecv(0x1f0, 0xc00004ef60, 0x1, 0xc00004ef48, 0xc00004efd0, 0xc00004ef18, 0x0) = 0xffffffff (-1)- GetAddrInfoW("reyweb.com", 0x0, 0xc000031f18, 0xc000031e88) = 0x0 (0)- WSASocketW(0x2, 0x1, 0x0, 0x0, 0x0, 0x81) = 0x200 (512)- WSASend(0x200, 0xc00004f2b8, 0x1, 0xc00004f2a0, 0x0, 0xc00004f270, 0x0) = 0x0 (0)- WSARecv(0x200, 0xc00004f1e0, 0x1, 0xc00004f1c8, 0xc00004f250, 0xc00004f198, 0x0) = 0xffffffff (-1)[...]
跟踪DeimosC2框架代理中的多个函数:
- WSASocketW(0x2, 0x1, 0x0, 0x0, 0x0, 0x81) = 0x130 (304)- setsockopt(0x130, 0xffff, 0x20, 0xc0000b7838, 0x4) = 0xffffffff (-1)- socket(0x2, 0x1, 0x6) = 0x138 (312)- WSAIoctl(0x138, 0xc8000006, 0xaf0870, 0x10, 0xb38730, 0x8, 0xc0000b746c, 0x0, 0x0) = 0x0 (0)- GetModuleFileNameW(0x0, "C:UsersuserDesktopsamplesdeimos.exe", 0x400) = 0x2f (47)- GetUserProfileDirectoryW(0x140, "C:Usersuser", 0xc0000b7a08) = 0x1 (1)- LookupAccountSidw(0x0, 0xc00000e250, "user", 0xc0000b796c, "DESKTOP-TEST", 0xc0000b7970, 0xc0000b79f0) = 0x1 (1)- NetUserGetInfo("DESKTOP-TEST", "user", 0xa, 0xc0000b7930) = 0x0 (0)- GetComputerNameExW(0x5, "DESKTOP-TEST", 0xc0000b7b78) = 0x1 (1)- GetAdaptersAddresses(0x0, 0x10, 0x0, 0xc000120000, 0xc0000b79d0) = 0x0 (0)- CreateToolhelp32Snapshot(0x2, 0x0) = 0x1b8 (440)- GetCurrentProcessId() = 0x2584 (9604)- GetCurrentDirectoryW(0x12c, "C:UsersuserAppDataLocalProgramsretoolkitbin") = 0x39 (57)[...]
许可证协议
本项目的开发与发布遵循GPL-3.0开源许可协议。
项目地址
gftrace:
https://github.com/leandrofroes/gftrace

原文始发于微信公众号(FreeBuf):gftrace:一款针对Golang源码的Windows API跟踪工具

 

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2024年7月20日12:47:05
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   gftrace:一款针对Golang源码的Windows API跟踪工具https://cn-sec.com/archives/2978242.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息