forked from unixpickle/gobfuscate
/
main.go
191 lines (165 loc) · 4.81 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
goos string
goarch string
)
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.StringVar(&goos, "goos", build.Default.GOOS, "the GOOS variables to build on (can be multiple)")
flag.StringVar(&goarch, "goarch", build.Default.GOARCH, "the GOARCH variable to build on (can be multiple)")
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)
}
if err := CopyGopath(pkgName, newGopath, keepTests); err != nil {
fmt.Fprintln(os.Stderr, "Failed to copy into a new GOPATH:", err)
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)
operatingSytems := strings.Split(goos, " ")
arches := strings.Split(goarch, " ")
// Build once for each OS/arch combo
for _, operatingSytem := range operatingSytems {
for _, arch := range arches {
packagePath := outPath
if operatingSytem == "windows" {
packagePath += ".exe"
}
cgo := os.Getenv("CGO_ENABLED_" + operatingSytem + "_" + arch)
if cgo == "" {
cgo = "0"
}
arguments := []string{"build", "-ldflags", ldflags, "-tags", tags, "-o", packagePath, newPkg}
environment := []string{
"GOROOT=" + ctx.GOROOT,
"GOARCH=" + arch,
"GOOS=" + operatingSytem,
"GOPATH=" + newGopath,
"PATH=" + os.Getenv("PATH"),
"GOCACHE=" + goCache,
"CGO_ENABLED=" + cgo,
"CC=" + os.Getenv("CC_"+operatingSytem+"_"+arch),
"CXX=" + os.Getenv("CXX_"+operatingSytem+"_"+arch),
"MACOSX_DEPLOYMENT_TARGET=" + os.Getenv("MACOSX_DEPLOYMENT_TARGET"),
}
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, "/")
}