forked from unixpickle/gobfuscate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
110 lines (95 loc) · 2.6 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"crypto/rand"
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
func main() {
var encKey string
var outputGopath bool
var keepTests bool
flag.StringVar(&encKey, "enckey", "", "rename encryption key")
flag.BoolVar(&outputGopath, "outdir", false, "output a full GOPATH")
flag.BoolVar(&keepTests, "keeptests", false, "keep _test.go files")
flag.Parse()
if len(flag.Args()) != 2 {
fmt.Fprintln(os.Stderr, "Usage: gobfuscate [flags] pkg_name out_path")
flag.PrintDefaults()
os.Exit(1)
}
pkgName := flag.Args()[0]
outPath := flag.Args()[1]
if encKey == "" {
buf := make([]byte, 32)
rand.Read(buf)
encKey = string(buf)
}
if !obfuscate(keepTests, outputGopath, encKey, pkgName, outPath) {
os.Exit(1)
}
}
func obfuscate(keepTests, outGopath bool, encKey, pkgName, outPath string) bool {
var newGopath string
if outGopath {
newGopath = outPath
if err := os.Mkdir(newGopath, 0755); err != nil {
fmt.Fprintln(os.Stderr, "Failed to create destination:", err)
return false
}
} else {
var err error
newGopath, err = ioutil.TempDir("", "")
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create temp dir:", err)
return false
}
defer os.RemoveAll(newGopath)
}
log.Println("Copying GOPATH...")
if !CopyGopath(pkgName, newGopath, keepTests) {
return false
}
enc := &Encrypter{Key: encKey}
log.Println("Obfuscating package names...")
if err := ObfuscatePackageNames(newGopath, enc); err != nil {
fmt.Fprintln(os.Stderr, "Failed to obfuscate package names:", err)
return false
}
log.Println("Obfuscating strings...")
if err := ObfuscateStrings(newGopath); err != nil {
fmt.Fprintln(os.Stderr, "Failed to obfuscate strings:", err)
return false
}
log.Println("Obfuscating symbols...")
if err := ObfuscateSymbols(newGopath, enc); err != nil {
fmt.Fprintln(os.Stderr, "Failed to obfuscate symbols:", err)
return false
}
if !outGopath {
ctx := build.Default
newPkg := encryptComponents(pkgName, enc)
cmd := exec.Command("go", "build", `-ldflags=-s -w -extldflags "-static"`, "-o", outPath, newPkg)
cmd.Env = []string{"GOROOT=" + ctx.GOROOT, "GOARCH=" + ctx.GOARCH,
"GOOS=" + ctx.GOOS, "GOPATH=" + newGopath, "PATH=" + os.Getenv("PATH")}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Failed to compile:", err)
return false
}
}
return true
}
func encryptComponents(pkgName string, enc *Encrypter) string {
comps := strings.Split(pkgName, "/")
for i, comp := range comps {
comps[i] = enc.Encrypt(comp)
}
return strings.Join(comps, "/")
}