-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacker.go
96 lines (81 loc) · 2.34 KB
/
packer.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/BlobbyBob/PtraceObfuscator/common"
"github.com/BlobbyBob/PtraceObfuscator/obfuscator"
"io/ioutil"
"log"
"os"
"os/exec"
)
// Packer
//
// The packer produces a single standalone obfuscated binary by compiling the runtime
// with the obfuscated binary and the metadata integrated as binary buffers.
func main() {
nop := flag.Bool("nop", false, "Use NOPs instead of random data")
var file string
flag.StringVar(&file, "f", "", "ELF file. Existing files with suffixes .obf, .strip and .packed in directory of the file will be overwritten")
flag.Parse()
if file == "" {
fmt.Println("Missing argument: filename")
os.Exit(1)
}
log.Print("Obfuscating ", file)
repl := 0
if !*nop {
repl = obfuscator.Rand
}
execute("strip", "-s", "-o", file+".strip", file)
elf, metadata, err := obfuscator.Obfuscate(file+".strip", obfuscator.Linear|repl)
if err != nil {
log.Fatal(err)
}
_ = ioutil.WriteFile(file+".obf", elf, 0644)
metadataJson, err := json.Marshal(common.ExportObfuscatedInstructions(*metadata))
if err != nil {
log.Fatal(err)
}
writeSourceFile(elf, "bin/obf.go", "Obf")
writeSourceFile(metadataJson, "bin/meta.go", "Meta")
log.Print("Packing binary")
execute("go", "build", "-o", file+".packed", "runtime.go")
log.Print("Stripping symbols")
execute("strip", "-s", file+".packed")
}
// A simple utility for executing a program on the command line
func execute(name string, arg ...string) {
cmd := exec.Command(name, arg...)
b, err := cmd.CombinedOutput()
fmt.Println(string(b))
if ee, isEE := err.(*exec.ExitError); isEE {
if ee.ExitCode() != 0 {
os.Exit(ee.ExitCode())
}
}
}
// Write a binary buffer into a Go source file
func writeSourceFile(input []byte, output, varname string) {
out, err := os.OpenFile(output, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
fmt.Println("can't open output file:", err)
os.Exit(1)
}
if _, err := out.WriteString("package bin; var " + varname + " = []byte{"); err != nil {
fmt.Println("can't write to file:", err)
os.Exit(1)
}
for _, b := range input {
if _, err := out.WriteString(fmt.Sprintf("%d,", b)); err != nil {
fmt.Println("can't write to file:", err)
os.Exit(1)
}
}
if _, err := out.WriteString("}"); err != nil {
fmt.Println("can't write to file:", err)
os.Exit(1)
}
_ = out.Close()
}