|
1 | 1 | package ssh |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
| 5 | + "context" |
4 | 6 | "errors" |
5 | 7 | "golang.org/x/crypto/ssh" |
6 | 8 | "io" |
7 | 9 | "path/filepath" |
8 | 10 | "strconv" |
9 | | - "sync" |
10 | 11 | ) |
11 | 12 |
|
12 | | -func (s *SSH) procReadLink(sesh *ssh.Session, pid int) (procfs, abs string) { |
13 | | - procFSPath := filepath.Join("/proc", strconv.Itoa(pid), "exe") |
14 | | - var pthB = []byte(procFSPath) |
15 | | - rlCmd := "readlink -f " + procFSPath |
16 | | - var err error |
17 | | - if pthB, err = s.Run(sesh, rlCmd); err != nil { |
18 | | - s.verbLn("[io] readlink -f error: %s", err) |
19 | | - pthB = []byte(procFSPath) |
| 13 | +const getPIDs = `bash -c 'for proc in /proc/*/exe; do if test -r "$proc" > /dev/null; then echo -n "$proc" | grep -v self | tr -d "/exeproc" | tr "\n" " "; fi; done'` |
| 14 | + |
| 15 | +func (s *SSH) whoami(sesh *ssh.Session) (string, error) { |
| 16 | + var whoiam = "" |
| 17 | + usr, err := s.Run(sesh, "whoami") |
| 18 | + if usr != nil { |
| 19 | + whoiam = string(bytes.TrimSpace(usr)) |
20 | 20 | } |
21 | | - return procFSPath, string(pthB) |
| 21 | + return whoiam, err |
22 | 22 | } |
23 | 23 |
|
24 | | -// GetSessions returns a slice of SSH sessions. |
25 | | -// It creates the sessions concurrently. |
26 | | -func (s *SSH) GetSessions(i int) ([]*ssh.Session, error) { |
27 | | - var wg = new(sync.WaitGroup) |
28 | | - wg.Add(i) |
29 | | - var seshi = make([]*ssh.Session, i) |
30 | | - var errs = make([]error, i) |
31 | | - for j := 0; j < i; j++ { |
32 | | - if s.Closed() { |
33 | | - s.verbLn("[session] parent is closed") |
34 | | - return nil, io.ErrClosedPipe |
| 24 | +// GetPIDs returns a list of process IDs from the remote host that the user has access to. |
| 25 | +func (s *SSH) GetPIDs() ([]int, error) { |
| 26 | + var ( |
| 27 | + pids = make([]int, 0) |
| 28 | + ps []byte |
| 29 | + err error |
| 30 | + sesh *ssh.Session |
| 31 | + ) |
| 32 | + |
| 33 | + if sesh, err = s.GetSession(context.Background()); err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + if ps, err = s.Run(sesh, getPIDs); err != nil { |
| 38 | + return nil, err |
| 39 | + } |
| 40 | + |
| 41 | + for _, p := range bytes.Fields(ps) { |
| 42 | + var pid int |
| 43 | + if pid, err = strconv.Atoi(string(p)); err == nil { |
| 44 | + pids = append(pids, pid) |
35 | 45 | } |
36 | | - go func(e int) { |
37 | | - s.traceLn("[session] creating session %d", e) |
38 | | - seshi[e], errs[e] = s.client.NewSession() |
39 | | - wg.Done() |
40 | | - }(j) |
41 | 46 | } |
42 | | - wg.Wait() |
43 | | - return seshi, errors.Join(errs...) |
| 47 | + |
| 48 | + s.verbLn("found %d PIDs with read permissions: %+v", len(pids), pids) |
| 49 | + |
| 50 | + return pids, nil |
44 | 51 | } |
45 | 52 |
|
46 | | -// CloseSessions closes a slice of SSH sessions. |
47 | | -func (s *SSH) CloseSessions(sesh []*ssh.Session) error { |
48 | | - var wg = new(sync.WaitGroup) |
49 | | - wg.Add(len(sesh)) |
50 | | - var errs = make([]error, len(sesh)) |
51 | | - for j := 0; j < len(sesh); j++ { |
52 | | - if s.Closed() { |
53 | | - s.verbLn("[session] parent is closed") |
54 | | - return io.ErrClosedPipe |
55 | | - } |
56 | | - go func(e int) { |
57 | | - if sesh[e] == nil { |
58 | | - s.traceLn("[session] session %d is nil", e) |
59 | | - wg.Done() |
60 | | - return |
61 | | - } |
62 | | - s.traceLn("[session] closing session %d", e) |
63 | | - errs[e] = sesh[e].Close() |
64 | | - wg.Done() |
65 | | - }(j) |
| 53 | +func (s *SSH) procReadLink(sesh *ssh.Session, pid int) (procfs, abs string) { |
| 54 | + procFSPath := filepath.Join("/proc", strconv.Itoa(pid), "exe") |
| 55 | + var pthB = []byte(procFSPath) |
| 56 | + |
| 57 | + var err error |
| 58 | + if pthB, err = s.Run(sesh, "readlink -f "+procFSPath); err != nil { |
| 59 | + pthB = []byte(procFSPath) |
66 | 60 | } |
67 | | - wg.Wait() |
68 | | - return errors.Join(errs...) |
| 61 | + |
| 62 | + s.verbLn("procfs path: %s", string(pthB)) |
| 63 | + |
| 64 | + return procFSPath, string(bytes.TrimSpace(pthB)) |
69 | 65 | } |
70 | 66 |
|
71 | 67 | // ReadProc reads the executable of a process from the remote host. |
72 | 68 | func (s *SSH) ReadProc(pid int) (path string, data []byte, err error) { |
73 | | - s.traceLn("[io] reading procfs, PID %d...", pid) |
| 69 | + s.traceLn("reading procfs, PID %d...", pid) |
74 | 70 | if s.Closed() { |
75 | | - s.verbLn("[io] parent is closed") |
| 71 | + s.verbLn("parent is closed") |
76 | 72 | return "", nil, io.ErrClosedPipe |
77 | 73 | } |
78 | 74 |
|
79 | | - seshi, err := s.GetSessions(2) |
80 | | - if err != nil { |
81 | | - return "", nil, err |
| 75 | + var seshi = make([]*ssh.Session, 2) |
| 76 | + ctx, cancel := context.WithTimeout(context.Background(), s.tout) |
| 77 | + for i := range seshi { |
| 78 | + var sesh *ssh.Session |
| 79 | + if sesh, err = s.GetSession(ctx); err != nil { |
| 80 | + cancel() |
| 81 | + return "", nil, err |
| 82 | + } |
| 83 | + seshi[i] = sesh |
82 | 84 | } |
83 | 85 |
|
84 | 86 | proc, abs := s.procReadLink(seshi[0], pid) |
85 | 87 | data, err = s.Run(seshi[1], "cat "+proc) |
86 | 88 |
|
| 89 | + cancel() |
87 | 90 | return abs, data, err |
88 | 91 | } |
89 | 92 |
|
90 | 93 | // Run executes a command on the remote host. |
91 | 94 | func (s *SSH) Run(sesh *ssh.Session, cmd string) (output []byte, err error) { |
92 | 95 | s.verbLn("$ " + cmd) |
93 | | - output, err = sesh.Output(cmd) |
| 96 | + if output, err = sesh.Output(cmd); err != nil { |
| 97 | + s.verbLn("run error: %s", err.Error()) |
| 98 | + } |
| 99 | + if errors.Is(err, io.EOF) { |
| 100 | + err = nil |
| 101 | + } |
| 102 | + |
94 | 103 | cerr := sesh.Close() |
| 104 | + if errors.Is(cerr, io.EOF) { |
| 105 | + cerr = nil |
| 106 | + } |
| 107 | + |
| 108 | + s.verbLn("\tresulting output: %d bytes", len(output)) |
| 109 | + |
95 | 110 | return output, errors.Join(err, cerr) |
96 | 111 | } |
0 commit comments