-
Notifications
You must be signed in to change notification settings - Fork 158
/
main.go
167 lines (145 loc) · 4.09 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"crypto/rand"
"flag"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
// Command line arguments.
var (
customPadding string
tags string
outputGopath bool
keepTests bool
winHide bool
noStaticLink bool
preservePackageName bool
verbose bool
)
func main() {
flag.StringVar(&customPadding, "padding", "", "use a custom padding for hashing sensitive information (otherwise a random padding will be used)")
flag.BoolVar(&outputGopath, "outdir", false, "output a full GOPATH")
flag.BoolVar(&keepTests, "keeptests", false, "keep _test.go files")
flag.BoolVar(&winHide, "winhide", false, "hide windows GUI")
flag.BoolVar(&noStaticLink, "nostatic", false, "do not statically link")
flag.BoolVar(&preservePackageName, "noencrypt", false,
"no encrypted package name for go build command (works when main package has CGO code)")
flag.BoolVar(&verbose, "verbose", false, "verbose mode")
flag.StringVar(&tags, "tags", "", "tags are passed to the go compiler")
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 !obfuscate(pkgName, outPath) {
os.Exit(1)
}
}
func obfuscate(pkgName, outPath string) bool {
var newGopath string
if outputGopath {
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 err := CopyGopath(pkgName, newGopath, keepTests); err != nil {
moreInfo := "\nNote: Setting GO111MODULE env variable to `off` may resolve the above error."
fmt.Fprintln(os.Stderr, "Failed to copy into a new GOPATH:", err, moreInfo)
return false
}
var n NameHasher
if customPadding == "" {
buf := make([]byte, 32)
rand.Read(buf)
n = buf
} else {
n = []byte(customPadding)
}
log.Println("Obfuscating package names...")
if err := ObfuscatePackageNames(newGopath, n); 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, n); err != nil {
fmt.Fprintln(os.Stderr, "Failed to obfuscate symbols:", err)
return false
}
if outputGopath {
return true
}
ctx := build.Default
newPkg := pkgName
if !preservePackageName {
newPkg = encryptComponents(pkgName, n)
}
ldflags := `-s -w`
if winHide {
ldflags += " -H=windowsgui"
}
if !noStaticLink {
ldflags += ` -extldflags '-static'`
}
goCache := newGopath + "/cache"
os.Mkdir(goCache, 0755)
arguments := []string{"build", "-trimpath", "-ldflags", ldflags, "-tags", tags, "-o", outPath, newPkg}
environment := []string{
"GO111MODULE=off", // needs to be off to make Go search GOPATH
"GOROOT=" + ctx.GOROOT,
"GOARCH=" + ctx.GOARCH,
"GOOS=" + ctx.GOOS,
"GOPATH=" + newGopath,
"PATH=" + os.Getenv("PATH"),
"GOCACHE=" + goCache,
}
cmd := exec.Command("go", arguments...)
cmd.Env = environment
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if verbose {
fmt.Println()
fmt.Println("[Verbose] Temporary path:", newGopath)
fmt.Println("[Verbose] Go build command: go", strings.Join(arguments, " "))
fmt.Println("[Verbose] Environment variables:")
for _, envLine := range environment {
fmt.Println(envLine)
}
fmt.Println()
}
if err := cmd.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Failed to compile:", err)
return false
}
return true
}
func encryptComponents(pkgName string, n NameHasher) string {
comps := strings.Split(pkgName, "/")
for i, comp := range comps {
comps[i] = n.Hash(comp)
}
return strings.Join(comps, "/")
}