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
package portscan
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
)
const tcpListen = "0A"
const udpBound = "07"
type listenSocket struct {
Port int
Inode uint64
}
func parseProcNetTCP(r io.Reader) ([]listenSocket, error) {
return parseProcNet(r, tcpListen)
}
func parseProcNetUDP(r io.Reader) ([]listenSocket, error) {
return parseProcNet(r, udpBound)
}
func parseProcNet(r io.Reader, state string) ([]listenSocket, error) {
sc := bufio.NewScanner(r)
var out []listenSocket
for sc.Scan() {
fields := strings.Fields(sc.Text())
if len(fields) == 0 || fields[0] == "sl" {
continue
}
if len(fields) < 10 {
return nil, fmt.Errorf("portscan: %q is not a socket-table row", sc.Text())
}
if fields[3] != state {
continue
}
_, portHex, ok := strings.Cut(fields[1], ":")
if !ok {
return nil, fmt.Errorf("portscan: local_address %q has no port", fields[1])
}
port, err := strconv.ParseUint(portHex, 16, 16)
if err != nil {
return nil, fmt.Errorf("portscan: local port %q: %w", portHex, err)
}
inode, err := strconv.ParseUint(fields[9], 10, 64)
if err != nil {
return nil, fmt.Errorf("portscan: inode %q: %w", fields[9], err)
}
out = append(out, listenSocket{Port: int(port), Inode: inode})
}
if err := sc.Err(); err != nil {
return nil, fmt.Errorf("portscan: reading the socket table: %w", err)
}
return out, nil
}
func socketInode(link string) (uint64, bool) {
rest, ok := strings.CutPrefix(link, "socket:[")
if !ok {
return 0, false
}
rest, ok = strings.CutSuffix(rest, "]")
if !ok {
return 0, false
}
inode, err := strconv.ParseUint(rest, 10, 64)
if err != nil {
return 0, false
}
return inode, true
}
func walkInodeOwners(root string, want map[uint64]bool) (map[uint64][]int, error) {
entries, err := os.ReadDir(root)
if err != nil {
return nil, fmt.Errorf("portscan: reading the process table %s: %w", root, err)
}
out := map[uint64][]int{}
for _, entry := range entries {
pid, err := strconv.Atoi(entry.Name())
if err != nil {
continue
}
fdDir := filepath.Join(root, entry.Name(), "fd")
fds, err := os.ReadDir(fdDir)
if err != nil {
continue
}
for _, fd := range fds {
target, err := os.Readlink(filepath.Join(fdDir, fd.Name()))
if err != nil {
continue
}
inode, ok := socketInode(target)
if !ok || !want[inode] {
continue
}
out[inode] = append(out[inode], pid)
}
}
return out, nil
}
func listenersIn(root string) (map[int][]int, error) {
return ownersIn(root, "tcp", parseProcNetTCP)
}
func datagramListenersIn(root string) (map[int][]int, error) {
return ownersIn(root, "udp", parseProcNetUDP)
}
func ownersIn(root, table string, parse func(io.Reader) ([]listenSocket, error)) (map[int][]int, error) {
socks, err := readTable(filepath.Join(root, "net", table), true, parse)
if err != nil {
return nil, err
}
six, err := readTable(filepath.Join(root, "net", table+"6"), false, parse)
if err != nil {
return nil, err
}
socks = append(socks, six...)
want := make(map[uint64]bool, len(socks))
for _, s := range socks {
want[s.Inode] = true
}
owners, err := walkInodeOwners(root, want)
if err != nil {
return nil, err
}
byPort := map[int][]int{}
for _, s := range socks {
if len(owners[s.Inode]) == 0 {
continue
}
byPort[s.Port] = append(byPort[s.Port], owners[s.Inode]...)
}
return byPort, nil
}
func readTable(path string, required bool, parse func(io.Reader) ([]listenSocket, error)) ([]listenSocket, error) {
f, err := os.Open(path)
if err != nil {
if !required && errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("portscan: opening %s: %w", path, err)
}
defer f.Close()
return parse(f)
}