-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
173 lines (140 loc) · 4.11 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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"os/exec"
"slices"
"time"
"github.com/shirou/gopsutil/v4/process"
)
func findRemoteIDA(ida_debugger_name string) (*process.Process, error) {
processes, err := process.Processes()
if err != nil {
return &process.Process{}, fmt.Errorf("Error getting processes: %v", err)
}
for _, process := range processes {
name, _ := process.Name()
if name == ida_debugger_name {
return process, nil
}
}
return &process.Process{}, fmt.Errorf("IDA remote debugger process not found")
}
func getChildern(proc *process.Process) ([]process.Process, error) {
procs := make([]process.Process, 1)
childern, err := proc.Children()
if err != nil {
return nil, fmt.Errorf("No childern found: %v", err)
}
for _, child := range childern {
name, err := child.Name()
if err != nil {
fmt.Printf("Error getting process name: %v\n\n", err)
break
}
if name != "conhost.exe" {
procs = append(procs, *child)
}
}
return procs, nil
}
func inject(pid int32, injector string, hookdll string) error {
//command := fmt.Sprintf("\"%v\" pid:%v \"%v\" nowait", injector, pid, hookdll)
stderr, err := execute(injector, hookdll, fmt.Sprintf("pid:%v", pid))
if stderr != "" {
return fmt.Errorf("%v", stderr)
}
if err != nil {
return fmt.Errorf("%v", err)
}
return nil
}
func execute(injector string, hookdll string, pid string) (string, error) {
stderr := new(bytes.Buffer)
cmd := exec.Command(injector, pid, hookdll, "nowait")
cmd.Stderr = stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
scanner := bufio.NewScanner(stdout)
err = cmd.Start()
if err != nil {
return "", err
}
for scanner.Scan() {
fmt.Println(scanner.Text())
}
if scanner.Err() != nil {
cmd.Process.Kill()
cmd.Wait()
return "", scanner.Err()
}
return stderr.String(), cmd.Wait()
}
func main() {
var injector string
flag.StringVar(&injector, "injector", "", "The Scylla CLI Injector")
var hookdll string
flag.StringVar(&hookdll, "hookdll", "", "The Scylla HookDll")
var ida_remote_debugger string
flag.StringVar(&ida_remote_debugger, "debugger", "win64_remote64.exe", "The process name of IDA's remote debugger")
var sleep_val int
flag.IntVar(&sleep_val, "sleep", 2, "The number of seconds to sleep between looking for new ida debugger child processes to inject into.")
var delay_diff int
flag.IntVar(&delay_diff, "delay", 5, "The number of seconds to wait before injecting the scylla dll into the process once found. This is to make sure the executable gets a chance to load properly before the injection occurs.")
var monitor_for_ida bool
flag.BoolVar(&monitor_for_ida, "monitor", false, "Keep running, monitoring for IDA's debugger versus exiting when not found.")
flag.Parse()
if injector == "" {
fmt.Println("injector parameter not passed")
return
}
if hookdll == "" {
fmt.Println("hookdll parameter not passed")
return
}
fmt.Printf("\nLooking for %v\n", ida_remote_debugger)
pids := make([]int32, 1)
ida_pid := int32(0)
for {
proc, err := findRemoteIDA(ida_remote_debugger)
if err != nil {
if fmt.Sprint(err) == "IDA remote debugger process not found" && !monitor_for_ida {
fmt.Printf("%v\nExiting...", err)
break
} else {
fmt.Printf("%v\nRetrying in %v seconds...\n\n", err, sleep_val)
time.Sleep(time.Duration(sleep_val) * time.Second)
continue
}
}
if proc.Pid != ida_pid {
fmt.Printf("Found %v (%v)\nSilently monitoring for child processes.\n\n", ida_remote_debugger, proc.Pid)
ida_pid = proc.Pid
}
childern, err := getChildern(proc)
if err != nil {
fmt.Printf("Error getting childern: %v\n\n", err)
}
for _, child := range childern {
if !slices.Contains(pids, child.Pid) {
time.Sleep(time.Duration(delay_diff) * time.Second)
err := inject(child.Pid, injector, hookdll)
if err != nil {
fmt.Println(err)
} else {
name, err := child.Name()
if err != nil {
name = "unknown"
}
fmt.Printf("Injected into process %v (%v)\n\n", name, child.Pid)
pids = append(pids, child.Pid)
}
}
}
time.Sleep(time.Duration(sleep_val) * time.Second)
}
}