-
Notifications
You must be signed in to change notification settings - Fork 6
/
ssllib_finder.go
73 lines (55 loc) · 1.38 KB
/
ssllib_finder.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/go-errors/errors"
"github.com/rs/zerolog/log"
)
func findSsllib(procfs string, pid uint32) (string, error) {
binary, err := os.Readlink(fmt.Sprintf("%s/%d/exe", procfs, pid))
if err != nil {
return "", errors.Wrap(err, 0)
}
log.Debug().Int("pid", int(pid)).Str("binary", binary).Msg("Binary that uses libssl:")
if strings.HasSuffix(binary, "/node") {
return findLibraryByPid(procfs, pid, binary)
} else {
return findLibraryByPid(procfs, pid, "libssl.so")
}
}
func findLibraryByPid(procfs string, pid uint32, libraryName string) (string, error) {
file, err := os.Open(fmt.Sprintf("%v/%v/maps", procfs, pid))
if err != nil {
return "", err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
parts := strings.Fields(scanner.Text())
if len(parts) <= 5 {
continue
}
fpath := parts[5]
if libraryName != "" {
found := strings.Contains(fpath, libraryName)
for _, name := range sslLibsGlobal {
if found {
break
}
found = strings.Contains(fpath, name)
}
if !found {
continue
}
}
fullpath := fmt.Sprintf("%v/%v/root%v", procfs, pid, fpath)
if _, err := os.Stat(fullpath); os.IsNotExist(err) {
continue
}
return fullpath, nil
}
return "", errors.Errorf("%s not found for PID %d", libraryName, pid)
}