myph.git


myph

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

encodings.go (1656B)

      1 package tools
      2 
      3 import (
      4 	b32 "encoding/base32"
      5 	b64 "encoding/base64"
      6 	"encoding/hex"
      7 	"errors"
      8 	"fmt"
      9 	"math/rand"
     10 	"time"
     11 )
     12 
     13 // / Enum type that defines which bytes encoding to use
     14 // / in template source-code
     15 type BytesEncodingType string
     16 
     17 const (
     18 	EncodingBase64 BytesEncodingType = "base64"
     19 	EncodingBase32 BytesEncodingType = "base32"
     20 	EncodingHex    BytesEncodingType = "hex"
     21 )
     22 
     23 // / Available encodings type
     24 var Encodings = [3]string{"base64", "base32", "hex"}
     25 
     26 // String is used both by fmt.Print and by Cobra in help text
     27 func (e *BytesEncodingType) String() string {
     28 	return string(*e)
     29 }
     30 
     31 // / Set must have pointer receiver so it doesn't change the value of a copy
     32 func (e *BytesEncodingType) Set(v string) error {
     33 	switch v {
     34 	case "base64", "base32", "hex":
     35 		*e = BytesEncodingType(v)
     36 		return nil
     37 	default:
     38 		return errors.New(`unknown encoding type`)
     39 	}
     40 }
     41 
     42 // / Type is only used in help text
     43 func (e *BytesEncodingType) Type() string {
     44 	return "BytesEncodingType"
     45 }
     46 
     47 // / Select a random encoding type
     48 func SelectRandomEncodingType() BytesEncodingType {
     49 	rand.Seed(time.Now().Unix())
     50 	n := rand.Int() % len(Encodings)
     51 
     52 	return BytesEncodingType(Encodings[n])
     53 }
     54 
     55 // / Encode a series of bytes so that it can be interpolated into a template
     56 func EncodeForInterpolation(method BytesEncodingType, toEncode []byte) string {
     57 
     58 	switch method {
     59 
     60 	case EncodingBase64:
     61 		return fmt.Sprintf("\"%s\"", b64.StdEncoding.EncodeToString(toEncode))
     62 
     63 	case EncodingBase32:
     64 		return fmt.Sprintf("\"%s\"", b32.StdEncoding.EncodeToString(toEncode))
     65 
     66 	case EncodingHex:
     67 		return fmt.Sprintf("\"%s\"", hex.EncodeToString(toEncode))
     68 
     69 	default:
     70 		return ""
     71 	}
     72 }