-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawscan_linux.go
More file actions
242 lines (206 loc) · 6.04 KB
/
Copy pathrawscan_linux.go
File metadata and controls
242 lines (206 loc) · 6.04 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//go:build linux
package gomap
import (
"bytes"
"context"
"encoding/binary"
"math/rand"
"net"
"time"
)
// TCP flag constants
const (
tcpFIN uint16 = 0x0001
tcpSYN uint16 = 0x0002
tcpRST uint16 = 0x0004
tcpPSH uint16 = 0x0008
tcpACK uint16 = 0x0010
tcpURG uint16 = 0x0020
)
// scanPortRaw sends a TCP packet with the specified flags and interprets the response.
// Used for FIN, Xmas, and Null scans.
func scanPortRaw(ctx context.Context, resultCh chan<- PortResult, hostname, service string, port int, laddr string, flags uint16, timeout time.Duration) {
result := PortResult{Port: port, Service: service}
if ctx.Err() != nil {
result.State = PortFiltered
resultCh <- result
return
}
responseCh := make(chan rawResponse, 1)
sport := uint16(randomPort(ephemeralPortMin, ephemeralPortMax))
go listenForResponse(laddr, hostname, uint16(port), sport, responseCh, timeout)
time.Sleep(5 * time.Millisecond)
err := sendTCPPacket(laddr, hostname, sport, uint16(port), flags)
if err != nil {
result.State = PortFiltered
resultCh <- result
return
}
select {
case <-ctx.Done():
result.setStateReason(PortFiltered, "no-response")
case resp := <-responseCh:
if resp.flags&tcpRST != 0 {
result.setStateReason(PortClosed, "reset")
} else {
result.setStateReason(PortOpenFiltered, "no-response")
}
case <-time.After(timeout):
result.setStateReason(PortOpenFiltered, "no-response")
}
resultCh <- result
}
// scanPortACK sends a TCP ACK packet for firewall rule mapping.
func scanPortACK(ctx context.Context, resultCh chan<- PortResult, hostname, service string, port int, laddr string, timeout time.Duration) {
result := PortResult{Port: port, Service: service}
if ctx.Err() != nil {
result.State = PortFiltered
resultCh <- result
return
}
responseCh := make(chan rawResponse, 1)
sport := uint16(randomPort(ephemeralPortMin, ephemeralPortMax))
go listenForResponse(laddr, hostname, uint16(port), sport, responseCh, timeout)
time.Sleep(5 * time.Millisecond)
err := sendTCPPacket(laddr, hostname, sport, uint16(port), tcpACK)
if err != nil {
result.State = PortFiltered
resultCh <- result
return
}
select {
case <-ctx.Done():
result.setStateReason(PortFiltered, "no-response")
case resp := <-responseCh:
if resp.flags&tcpRST != 0 {
result.setStateReason(PortUnfiltered, "reset")
} else {
result.setStateReason(PortFiltered, "no-response")
}
case <-time.After(timeout):
result.setStateReason(PortFiltered, "no-response")
}
resultCh <- result
}
// scanPortWindow examines TCP window size in RST responses.
func scanPortWindow(ctx context.Context, resultCh chan<- PortResult, hostname, service string, port int, laddr string, timeout time.Duration) {
result := PortResult{Port: port, Service: service}
if ctx.Err() != nil {
result.State = PortFiltered
resultCh <- result
return
}
responseCh := make(chan rawResponse, 1)
sport := uint16(randomPort(ephemeralPortMin, ephemeralPortMax))
go listenForResponse(laddr, hostname, uint16(port), sport, responseCh, timeout)
time.Sleep(5 * time.Millisecond)
err := sendTCPPacket(laddr, hostname, sport, uint16(port), tcpACK)
if err != nil {
result.State = PortFiltered
resultCh <- result
return
}
select {
case <-ctx.Done():
result.setStateReason(PortFiltered, "no-response")
case resp := <-responseCh:
if resp.flags&tcpRST != 0 {
if resp.window > 0 {
result.setStateReason(PortOpen, "window-nonzero")
} else {
result.setStateReason(PortClosed, "reset")
}
} else {
result.setStateReason(PortFiltered, "no-response")
}
case <-time.After(timeout):
result.setStateReason(PortFiltered, "no-response")
}
resultCh <- result
}
type rawResponse struct {
flags uint16
window uint16
}
// listenForResponse listens for a TCP response from the target.
func listenForResponse(laddr, raddr string, dport, sport uint16, ch chan<- rawResponse, timeout time.Duration) {
proto := ipProtocol(laddr, "tcp")
network := "ip4"
if IsIPv6(laddr) {
network = "ip6"
}
listenAddr, err := net.ResolveIPAddr(network, laddr)
if err != nil {
return
}
conn, err := net.ListenIP(proto, listenAddr)
if err != nil {
return
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(timeout + 100*time.Millisecond))
for {
buf := make([]byte, readBufferSize)
n, addr, err := conn.ReadFrom(buf)
if err != nil {
return
}
if addr.String() != raddr || n < 20 {
continue
}
srcPort := binary.BigEndian.Uint16(buf[0:2])
dstPort := binary.BigEndian.Uint16(buf[2:4])
if srcPort != dport || dstPort != sport {
continue
}
flags := binary.BigEndian.Uint16(buf[12:14]) & 0x003f
window := binary.BigEndian.Uint16(buf[14:16])
ch <- rawResponse{flags: flags, window: window}
return
}
}
// sendTCPPacket constructs and sends a raw TCP packet with the specified flags.
func sendTCPPacket(laddr, raddr string, sport, dport, flags uint16) error {
op := []tcpOption{
{Kind: 2, Length: 4, Data: []byte{0x05, 0xb4}},
{Kind: 0},
}
dataOffset := uint16(0x8000)
flagField := dataOffset | flags
tcpH := tcpHeader{
SrcPort: sport,
DstPort: dport,
SeqNum: rand.Uint32(),
AckNum: 0,
Flags: flagField,
Window: 8192,
ChkSum: 0,
UrgentPointer: 0,
}
conn, err := net.Dial(ipProtocol(raddr, "tcp"), raddr)
if err != nil {
return err
}
defer conn.Close()
buff := new(bytes.Buffer)
binary.Write(buff, binary.BigEndian, tcpH)
for i := range op {
binary.Write(buff, binary.BigEndian, op[i].Kind)
binary.Write(buff, binary.BigEndian, op[i].Length)
binary.Write(buff, binary.BigEndian, op[i].Data)
}
binary.Write(buff, binary.BigEndian, [6]byte{})
data := buff.Bytes()
checkSum := tcpChecksum(data, ipToBytes(laddr), ipToBytes(raddr))
tcpH.ChkSum = checkSum
buff = new(bytes.Buffer)
binary.Write(buff, binary.BigEndian, tcpH)
for i := range op {
binary.Write(buff, binary.BigEndian, op[i].Kind)
binary.Write(buff, binary.BigEndian, op[i].Length)
binary.Write(buff, binary.BigEndian, op[i].Data)
}
binary.Write(buff, binary.BigEndian, [6]byte{})
_, err = conn.Write(buff.Bytes())
return err
}