-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetwork.go
169 lines (140 loc) · 3.01 KB
/
network.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
package network
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/csmith/goplum"
)
type Plugin struct{}
func (p Plugin) Alert(kind string) goplum.Alert {
switch kind {
default:
return nil
}
}
func (p Plugin) Check(kind string) goplum.Check {
switch kind {
case "connect":
return ConnectCheck{
Network: "tcp",
}
case "portscan":
return PortScanCheck{
Network: "tcp",
Start: 1,
End: 65535,
ConcurrentConnections: 100,
ConnectionTimeout: 5 * time.Second,
}
default:
return nil
}
}
type ConnectCheck struct {
Network string
Address string
}
func (c ConnectCheck) Execute(ctx context.Context) goplum.Result {
d := net.Dialer{}
conn, err := d.DialContext(ctx, c.Network, c.Address)
if err != nil {
return goplum.FailingResult("unable to connect to %s: %v", c.Address, err)
}
defer conn.Close()
return goplum.GoodResult()
}
func (c ConnectCheck) Validate() error {
if len(c.Address) == 0 {
return fmt.Errorf("missing required argument: address")
}
_, _, err := net.SplitHostPort(c.Address)
if err != nil {
return err
}
return nil
}
type PortScanCheck struct {
Network string
Address string
Start int
End int
Allow []int
ConcurrentConnections int `config:"concurrent_connections"`
ConnectionTimeout time.Duration `config:"connection_timeout"`
}
func (c PortScanCheck) Timeout() time.Duration {
return c.ConnectionTimeout * time.Duration((c.End-c.Start)/c.ConcurrentConnections)
}
func (c PortScanCheck) worker(queue <-chan int, job func(int)) {
for {
p, more := <-queue
if more {
job(p)
} else {
return
}
}
}
func (c PortScanCheck) check(port int) bool {
target := net.JoinHostPort(c.Address, strconv.Itoa(port))
conn, err := net.DialTimeout(c.Network, target, c.ConnectionTimeout)
if err == nil {
conn.Close()
return true
}
return false
}
func (c PortScanCheck) allowed(port int) bool {
for i := range c.Allow {
if c.Allow[i] == port {
return true
}
}
return false
}
func (c PortScanCheck) Execute(ctx context.Context) goplum.Result {
open := make(chan int, c.End-c.Start)
queue := make(chan int)
wg := sync.WaitGroup{}
for i := 0; i < c.ConcurrentConnections; i++ {
go c.worker(queue, func(port int) {
if c.check(port) {
open <- port
}
wg.Done()
})
}
for p := c.Start; p <= c.End; p++ {
wg.Add(1)
queue <- p
}
close(queue)
wg.Wait()
close(open)
var failures []int
for p := range open {
if !c.allowed(p) {
failures = append(failures, p)
}
}
if len(failures) == 0 {
return goplum.GoodResult()
} else {
return goplum.FailingResult("Unexpected open ports: %v", failures)
}
}
func (c PortScanCheck) Validate() error {
if len(c.Address) == 0 {
return fmt.Errorf("missing required argument: address")
}
if c.Start < 1 || c.End > 65535 || c.Start > c.End {
return fmt.Errorf("invalid ports: must satisfy 1 <= start <= end <= 65535")
}
if c.ConcurrentConnections <= 0 {
return fmt.Errorf("invalid number of concurrent connections: must be >0")
}
return nil
}