-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.go
More file actions
88 lines (74 loc) · 2.05 KB
/
main.go
File metadata and controls
88 lines (74 loc) · 2.05 KB
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
package main
import (
_ "embed"
"fmt"
"os"
"os/exec"
"path/filepath"
"github.com/frida/frida-go/frida"
"github.com/nsecho/furlzz/cmd"
)
const (
agentFilename = "_agent.js"
)
var sc string
//go:embed script.ts
var scriptContent []byte
//go:embed package.json
var packageJSON []byte
//go:embed package-lock.json
var packageLockJSON []byte
var tempFiles = map[string][]byte{
"script.ts": scriptContent,
"package.json": packageJSON,
"package-lock.json": packageLockJSON,
}
func main() {
tempDir := filepath.Join(os.TempDir(), "furlzz")
os.MkdirAll(tempDir, os.ModePerm)
// we don't have agent compiled
if _, err := os.Stat(filepath.Join(tempDir, agentFilename)); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(tempDir, "script.ts")); os.IsNotExist(err) {
for fl, data := range tempFiles {
os.WriteFile(filepath.Join(tempDir, fl), data, os.ModePerm)
}
}
if _, err = os.Stat(filepath.Join(tempDir, "node_modules")); os.IsNotExist(err) {
// Install modules
pwd, _ := os.Getwd()
os.Chdir(tempDir)
command := exec.Command("npm", "install")
if err := command.Run(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to install node modules: %v\n", err)
os.Exit(1)
}
os.Chdir(pwd)
c := frida.NewCompiler()
c.On("diagnostics", func(diag string) {
fmt.Fprintf(os.Stderr, "Diagnostics: %s\n", diag)
os.Exit(1)
})
bopts := frida.NewCompilerOptions()
bopts.SetProjectRoot(tempDir)
bopts.SetJSCompression(frida.JSCompressionTerser)
bundle, err := c.Build("script.ts", bopts)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to compile script: %v\n", err)
os.Exit(1)
}
os.WriteFile(filepath.Join(tempDir, agentFilename), []byte(bundle), os.ModePerm)
sc = bundle
}
} else {
scriptContent, err = os.ReadFile(filepath.Join(tempDir, agentFilename))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read agent: %v\n", err)
os.Exit(1)
}
sc = string(scriptContent)
}
if err := cmd.Execute(sc); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}