myph.git


myph

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

commit dcca5bed26b7f770b9d7ddd580adcb2fe9b7bdda
parent f05174f6008ada5fa61db7226d4fb67a031422b4
Author: winds0r <[email protected]>
Date: Tue, 18 Jul 2023 19:27:29 +0900

fix(tools): aes implementation

Diffstat:
Mcli/parser.go | 10+++++++---
Mtools/crypting.go | 69++++++++++++++++++++++++++++++++++-----------------------------------
Mtools/encodings.go | 13+++++++------

3 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/cli/parser.go b/cli/parser.go
@@ -24,22 +24,26 @@ func GetParser(opts *Options) *cobra.Command {
 				os.Exit(1)
 			}
 
+            /* later, we will call "go build" on a golang project, so we need to set up the project tree */
             err := tools.CreateTmpProjectRoot(opts.Outfile)
             if err != nil {
                 fmt.Printf("[!] Error generating project root: %s", err)
                 os.Exit(1)
             }
 
+            /* reading the shellcode as a series of bytes */
             shellcode, err := tools.ReadFile(opts.ShellcodePath)
             if err != nil {
                 fmt.Printf("[!] Error reading shellcode file: %s", err)
                 os.Exit(1)
             }
 
+            /* i got 99 problems but generating a random key aint one */
             if opts.Key == "" {
-                opts.Key = tools.RandomString(64)
+                opts.Key = tools.RandomString(32)
             }
 
+            /* encoding defines the way the series of bytes will be written into the template */
             encType := tools.SelectRandomEncodingType()
 
             /*
@@ -59,7 +63,7 @@ func GetParser(opts *Options) *cobra.Command {
                     encrypted, err = tools.EncryptAES(shellcode, []byte(opts.Key))
                     if err != nil {
                         fmt.Println("[!] Could not encrypt with AES")
-                        os.Exit(1)
+                        panic(err)
                     }
                     template = tools.GetAESTemplate()
 
@@ -68,7 +72,7 @@ func GetParser(opts *Options) *cobra.Command {
                     encrypted, err = tools.EncryptXOR(shellcode, []byte(opts.Key))
                     if err != nil {
                         fmt.Println("[!] Could not encrypt with XOR")
-                        os.Exit(1)
+                        panic(err)
                     }
                     template = tools.GetXORTemplate()
             }
diff --git a/tools/crypting.go b/tools/crypting.go
@@ -4,11 +4,19 @@ import (
 	"crypto/aes"
 	"crypto/cipher"
 	"crypto/rand"
+	"errors"
 	"fmt"
+	"io"
 
 	"golang.org/x/crypto/blowfish"
 )
 
+/*
+   TODO:
+
+   add support for CHACHA20
+*/
+
 func GetBlowfishTemplate() string {
     return fmt.Sprintf(`
     package main
@@ -42,31 +50,28 @@ func GetAESTemplate() string {
     import (
         "crypto/aes"
         "crypto/cipher"
+        "errors"
         "fmt"
     )
 
     func Decrypt(encrypted []byte, key []byte) ([]byte, error) {
-        block, err := aes.NewCipher(key)
+        c, err := aes.NewCipher(key)
         if err != nil {
             return nil, err
         }
 
-        gcm, err := cipher.NewGCM(block)
+        gcm, err := cipher.NewGCM(c)
         if err != nil {
             return nil, err
         }
 
         nonceSize := gcm.NonceSize()
         if len(encrypted) < nonceSize {
-            return nil, fmt.Errorf("invalid encrypted data")
-        }
-        nonce := encrypted[:nonceSize]
-        encrypted = encrypted[nonceSize:]
-        decrypted, err := gcm.Open(nil, nonce, encrypted, nil)
-        if err != nil {
-            return nil, err
+            return nil, errors.New("ciphertext too short")
         }
-        return decrypted, nil
+
+        nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
+        return gcm.Open(nil, nonce, ciphertext, nil)
     }
     `)
 }
@@ -136,46 +141,40 @@ func DecryptXOR(toDecrypt []byte, key []byte) ([]byte, error) {
 }
 
 func EncryptAES(toEncrypt []byte, key []byte) ([]byte, error) {
-    block, err := aes.NewCipher(key)
+    c, err := aes.NewCipher(key)
 	if err != nil {
 		return nil, err
 	}
 
-	gcm, err := cipher.NewGCM(block)
+	gcm, err := cipher.NewGCM(c)
 	if err != nil {
 		return nil, err
 	}
 
 	nonce := make([]byte, gcm.NonceSize())
-	if _, err := rand.Read(nonce); err != nil {
+	if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
 		return nil, err
 	}
 
-	encrypted := gcm.Seal(nil, nonce, toEncrypt, nil)
-	encrypted = append(nonce, encrypted...)
-	return encrypted, nil
+	return gcm.Seal(nonce, nonce, toEncrypt, nil), nil
 }
 
 func DecryptAES(encrypted []byte, key []byte) ([]byte, error) {
-    block, err := aes.NewCipher(key)
-	if err != nil {
-		return nil, err
-	}
+    c, err := aes.NewCipher(key)
+    if err != nil {
+        return nil, err
+    }
 
-	gcm, err := cipher.NewGCM(block)
-	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, fmt.Errorf("invalid encrypted data")
-	}
-	nonce := encrypted[:nonceSize]
-	encrypted = encrypted[nonceSize:]
-	decrypted, err := gcm.Open(nil, nonce, encrypted, nil)
-	if err != nil {
-		return nil, err
-	}
-	return decrypted, nil
+    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)
 }
diff --git a/tools/encodings.go b/tools/encodings.go
@@ -1,12 +1,13 @@
 package tools
 
 import (
+	b32 "encoding/base32"
+	b64 "encoding/base64"
+	hex "encoding/hex"
 	"errors"
+	"fmt"
 	"math/rand"
 	"time"
-    b64 "encoding/base64"
-    b32 "encoding/base32"
-    hex "encoding/hex"
 )
 
 /// Enum type that defines which bytes encoding to use
@@ -58,13 +59,13 @@ func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
     switch method {
 
     case EncodingBase64:
-        return b64.StdEncoding.EncodeToString(toEncode)
+        return fmt.Sprintf("\"%s\"", b64.StdEncoding.EncodeToString(toEncode))
 
     case EncodingBase32:
-        return b32.StdEncoding.EncodeToString(toEncode)
+        return fmt.Sprintf("\"%s\"", b32.StdEncoding.EncodeToString(toEncode))
 
     case EncodingHex:
-        return hex.EncodeToString(toEncode)
+        return fmt.Sprintf("\"%s\"", hex.EncodeToString(toEncode))
 
     default:
         return ""