Advertisement
Guest User

obfuscator.go

a guest
Jun 14th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.09 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io"
  6.     "io/ioutil"
  7.     "os"
  8.     "regexp"
  9.     "strings"
  10. )
  11.  
  12. func main() {
  13.     defer func() {
  14.         if rec := recover(); rec != nil {
  15.             fmt.Fprintf(os.Stderr, "error: %v\n", rec)
  16.             os.Exit(1)
  17.         }
  18.     }()
  19.  
  20.     obfuscate()
  21. }
  22.  
  23. func obfuscate() {
  24.     re := regexp.MustCompile(`\b[_a-zA-Z][_a-zA-Z0-9]*\b`)
  25.     data, err := ioutil.ReadAll(os.Stdin)
  26.     chk(err)
  27.  
  28.     var (
  29.         idxs              = re.FindAllIndex(data, -1)
  30.         subst             = map[string]string{}
  31.         w       io.Writer = os.Stdout
  32.         isStr   bool
  33.         escaped bool
  34.         last    int
  35.         idNum   int
  36.         write   = writeFunc(w, &isStr, &escaped)
  37.     )
  38.     addConst(subst, keywords[:])
  39.     addConst(subst, funcs[:])
  40.     subst["main"] = "main"
  41.  
  42.     for _, idx := range idxs {
  43.         i, j := idx[0], idx[1]
  44.         write(data[last:i])
  45.         last = j
  46.         if isStr {
  47.             write(data[i:j])
  48.             continue
  49.         }
  50.         s := string(data[i:j])
  51.         replS := subst[s]
  52.         if replS == "" {
  53.             idNum++
  54.             replS = strings.Repeat("_", idNum)
  55.             subst[s] = replS
  56.         }
  57.         fmt.Fprint(w, replS)
  58.     }
  59.     write(data[last:len(data)])
  60. }
  61.  
  62. func writeFunc(w io.Writer, isStr *bool, escaped *bool) func([]byte) error {
  63.     return func(slice []byte) error {
  64.         for i := range slice {
  65.             if *escaped {
  66.                 *escaped = false
  67.                 continue
  68.             }
  69.             if slice[i] == '"' {
  70.                 *isStr = !*isStr
  71.             } else if *isStr && slice[i] == '\\' {
  72.                 *escaped = true
  73.             }
  74.         }
  75.         _, err := w.Write(slice)
  76.         return err
  77.     }
  78. }
  79.  
  80. func addConst(subst map[string]string, cs []string) {
  81.     for _, c := range cs {
  82.         subst[c] = c
  83.     }
  84. }
  85.  
  86. var keywords = [...]string{
  87.     "False", "class", "finally", "is", "return",
  88.     "None", "continue", "for", "lambda", "try",
  89.     "True", "def", "from", "nonlocal", "while",
  90.     "and", "del", "global", "not", "with",
  91.     "as", "elif", "if", "or", "yield",
  92.     "assert", "else", "import", "pass",
  93.     "break", "except", "in", "raise",
  94. }
  95.  
  96. // only a minimal set for now because I couldn't find a listing that would be easy to parse into a Go array
  97. var funcs = [...]string{
  98.     "print", "remove", "insert", "xrange", "range", "enumerate", "list",
  99. }
  100.  
  101. func chk(err error) {
  102.     if err != nil {
  103.         panic(err)
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement