-
Notifications
You must be signed in to change notification settings - Fork 15
/
scan.go
580 lines (516 loc) · 13.1 KB
/
scan.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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package gomasscan
import (
"context"
"fmt"
"golang.org/x/time/rate"
"log"
"math/rand"
"net"
"os"
"strings"
"sync/atomic"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/projectdiscovery/ipranger"
"github.com/projectdiscovery/networkpolicy"
)
var logger = Logger(log.New(os.Stderr, "[scanner]", log.Ldate|log.Ltime))
type Logger interface {
Println(...interface{})
Printf(string, ...interface{})
}
func SetLogger(log Logger) {
logger = log
}
// PkgFlag represent the TCP packet flag
type PkgFlag int
const (
SYN PkgFlag = iota
ACK
)
type Scanner struct {
//发包源IP
sourceIP net.IP
//发包源网卡
networkInterface *net.Interface
//发包源端口
listenPort int
//目的IP,地址范围,便于处理返回数据包
ipRanger *ipranger.IPRanger
//监听网卡组
handlers Handlers
//端口扫描成功结果发送信道
tcpResultChan chan *PkgResult
//发包相关
tcpSequencer *TCPSequencer
tcpPacketListener net.PacketConn
tcpPacketSend chan *PkgSend
serializeOptions gopacket.SerializeOptions
//结束标识符
done bool
HandlerOpen func(ip string, port int)
//发包速率
limit *rate.Limiter
limitCount uint64
//retries int
//rate int
//timeout time.Duration
//icmpPacketListener net.PacketConn
//proxyDialer proxy.Dialer
//Ports []int
//icmpChan chan *PkgResult
//icmpPacketSend chan *PkgSend
}
// PkgSend is a TCP package
type PkgSend struct {
ip string
port int
flag PkgFlag
SourceIP string
}
// PkgResult contains the results of sending TCP packages
type PkgResult struct {
ip string
port int
}
var (
newScannerCallback func(s *Scanner) error
setupHandlerCallback func(s *Scanner, interfaceName string) error
tcpReadWorkerPCAPCallback func(s *Scanner)
cleanupHandlersCallback func(s *Scanner)
)
const (
maxRetries = 10
sendDelayMsec = 10
chanSize = 1000
packetSendSize = 2500
snapLength = 65536
readTimeout = 1500
)
// NewScanner creates a new full port scanner that scans all ports using SYN packets.
func NewScanner() (*Scanner, error) {
rand.Seed(time.Now().UnixNano())
iprang, err := ipranger.New()
if err != nil {
return nil, err
}
var nPolicyOptions networkpolicy.Options
//黑名单
//nPolicyOptions.DenyList = append(nPolicyOptions.DenyList, options.ExcludedIps...)
nPolicy, err := networkpolicy.New(nPolicyOptions)
if err != nil {
return nil, err
}
iprang.Np = nPolicy
scanner := &Scanner{
serializeOptions: gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
},
tcpSequencer: newTCPSequencer(),
ipRanger: iprang,
HandlerOpen: func(ip string, port int) {},
limitCount: 0,
//timeout: time.Second * 3,
//retries: 3,
//rate: 1024,
}
scanner.SetRate(1024)
if newScannerCallback != nil {
if err := newScannerCallback(scanner); err != nil {
return nil, err
}
}
//初始化
err = scanner.init()
if err != nil {
return nil, err
}
return scanner, nil
}
// Done the scanner and terminate all workers
func (s *Scanner) Done() {
s.cleanupHandlers()
s.tcpPacketListener.Close()
s.done = true
}
// Add the scanner and add ip filter in ipRanger
func (s *Scanner) Add(host string) error {
return s.ipRanger.Add(host)
}
// Count of scanner send syn packet
func (s *Scanner) Count() uint64 {
return s.limitCount
}
// startWorkers of the scanner
func (s *Scanner) startWorkers() {
go s.tcpReadWorker()
go s.tcpReadWorkerPCAP()
go s.tcpWriteWorker()
go s.tcpResultWorker()
}
// tcpWriteWorker that sends out TCP packets
func (s *Scanner) tcpWriteWorker() {
for pkg := range s.tcpPacketSend {
s.sendAsyncPkg(pkg.ip, pkg.port, pkg.flag)
}
}
//sendAsyncPkg sends a single packet to a port
func (s *Scanner) sendAsyncPkg(ip string, port int, pkgFlag PkgFlag) {
// Construct all the network layers we need.
ip4 := layers.IPv4{
SrcIP: s.sourceIP,
DstIP: net.ParseIP(ip),
Version: 4,
TTL: 255,
Protocol: layers.IPProtocolTCP,
}
tcpOption := layers.TCPOption{
OptionType: layers.TCPOptionKindMSS,
OptionLength: 4,
OptionData: []byte{0x05, 0xB4},
}
tcp := layers.TCP{
SrcPort: layers.TCPPort(s.listenPort),
DstPort: layers.TCPPort(port),
Window: 1024,
Seq: s.tcpSequencer.Next(),
Options: []layers.TCPOption{tcpOption},
}
if pkgFlag == SYN {
tcp.SYN = true
} else if pkgFlag == ACK {
tcp.ACK = true
}
err := tcp.SetNetworkLayerForChecksum(&ip4)
if err != nil {
logger.Printf("Can not set network layer for %s:%d port: %s\n", ip, port, err)
} else {
err = s.send(ip, s.tcpPacketListener, &tcp)
if err != nil {
logger.Printf("Can not send packet to %s:%d port: %s\n", ip, port, err)
}
}
}
// send sends the given layers as a single packet on the network.
func (s *Scanner) send(destIP string, conn net.PacketConn, l ...gopacket.SerializableLayer) error {
buf := gopacket.NewSerializeBuffer()
if err := gopacket.SerializeLayers(buf, s.serializeOptions, l...); err != nil {
return err
}
var (
retries int
err error
)
send:
if retries >= maxRetries {
return err
}
_, err = conn.WriteTo(buf.Bytes(), &net.IPAddr{IP: net.ParseIP(destIP)})
if err != nil {
retries++
// introduce a small delay to allow the network interface to flush the queue
time.Sleep(time.Duration(sendDelayMsec) * time.Millisecond)
goto send
}
return err
}
// tcpReadWorker reads and parse incoming TCP packets
func (s *Scanner) tcpReadWorker() {
defer s.tcpPacketListener.Close()
data := make([]byte, 4096)
for {
if s.done == true {
break
}
// nolint:errcheck // just empty the buffer
s.tcpPacketListener.ReadFrom(data)
}
}
// tcpReadWorkerPCAP reads and parse incoming TCP packets with pcap
func (s *Scanner) tcpReadWorkerPCAP() {
if tcpReadWorkerPCAPCallback != nil {
tcpReadWorkerPCAPCallback(s)
}
}
// tcpResultWorker handles probes and scan results
// 输出处理函数
func (s *Scanner) tcpResultWorker() {
for ip := range s.tcpResultChan {
s.HandlerOpen(ip.ip, ip.port)
}
}
// SendSYN outgoing TCP packets
func (s *Scanner) SendSYN(ip string, port int, pkgtype PkgFlag) {
err := s.limit.Wait(context.Background())
if err != nil {
panic(err)
}
s.tcpPacketSend <- &PkgSend{ip, port, pkgtype, ""}
atomic.AddUint64(&s.limitCount, 1)
}
func (s *Scanner) init() error {
//初始化发包源IP及网卡
err := s.initSource(DefaultExternalTargetForGetSource)
if err != nil {
return err
}
//初始化监听网卡流量
err = s.listen()
if err != nil {
return err
}
//启动相关函数
s.startWorkers()
return nil
}
// listen to listen on all interfaces
func (s *Scanner) listen() error {
if s.networkInterface != nil {
return s.listenInterface(s.networkInterface.Name)
}
itfs, err := net.Interfaces()
if err != nil {
return err
}
for _, itf := range itfs {
if itf.Flags&net.FlagUp == 0 {
continue // interface down
}
if err := s.listenInterface(itf.Name); err != nil {
logger.Printf("Error on interface %s: %s", itf.Name, err)
}
}
return nil
}
// listenInterface to listen on the specified interface
func (s *Scanner) listenInterface(interfaceName string) error {
if setupHandlerCallback != nil {
return setupHandlerCallback(s, interfaceName)
}
return nil
}
// cleanupHandlers for all interfaces
func (s *Scanner) cleanupHandlers() {
if cleanupHandlersCallback != nil {
cleanupHandlersCallback(s)
}
}
var DefaultExternalTargetForGetSource = "8.8.8.8"
// initSource automatically with ip and interface
func (s *Scanner) initSource(ip string) error {
var err error
s.sourceIP, s.networkInterface, err = getSrcParameters(ip)
if err != nil {
return err
}
return nil
}
func (s *Scanner) SetRate(i int) {
s.limit = rate.NewLimiter(rate.Every(time.Second/time.Duration(i)), 1)
}
// getSrcParameters gets the network parameters from the destination ip
func getSrcParameters(destIP string) (srcIP net.IP, networkInterface *net.Interface, err error) {
srcIP, err = getSourceIP(net.ParseIP(destIP))
if err != nil {
return
}
networkInterface, err = getInterfaceFromIP(srcIP)
if err != nil {
return
}
return
}
// getSourceIP gets the local ip based on our destination ip
func getSourceIP(dstip net.IP) (net.IP, error) {
serverAddr, err := net.ResolveUDPAddr("udp", dstip.String()+":12345")
if err != nil {
return nil, err
}
con, dialUpErr := net.DialUDP("udp", nil, serverAddr)
if dialUpErr != nil {
return nil, dialUpErr
}
defer con.Close()
if udpaddr, ok := con.LocalAddr().(*net.UDPAddr); ok {
return udpaddr.IP, nil
}
return nil, nil
}
// getInterfaceFromIP gets the name of the network interface from local ip address
func getInterfaceFromIP(ip net.IP) (*net.Interface, error) {
address := ip.String()
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, i := range interfaces {
byNameInterface, err := net.InterfaceByName(i.Name)
if err != nil {
return nil, err
}
addresses, err := byNameInterface.Addrs()
if err != nil {
return nil, err
}
for _, v := range addresses {
// Check if the IP for the current interface is our
// source IP. If yes, return the interface
if strings.HasPrefix(v.String(), address+"/") {
return byNameInterface, nil
}
}
}
return nil, fmt.Errorf("no interface found for ip %s", address)
}
// ICMPReadWorker reads packets from the network layer
//func (s *Scanner) ICMPReadWorker() {
// defer s.icmpPacketListener.Close()
// data := make([]byte, 1500)
// for {
// if s.State == done {
// break
// }
// n, addr, err := s.icmpPacketListener.ReadFrom(data)
// if err != nil {
// continue
// }
//
// if s.State == Guard {
// continue
// }
//
// rm, err := icmp.ParseMessage(ProtocolICMP, data[:n])
// if err != nil {
// continue
// }
//
// switch rm.Type {
// case ipv4.ICMPTypeEchoReply, ipv4.ICMPTypeTimestamp:
// s.icmpChan <- &PkgResult{ip: addr.String()}
// }
// }
//}
//// ConnectPort a single host and port
//func (s *Scanner) ConnectPort(host string, port int, timeout time.Duration) (bool, error) {
// hostport := net.JoinHostPort(host, fmt.Sprint(port))
// var (
// err error
// conn net.Conn
// )
// if s.proxyDialer != nil {
// conn, err = s.proxyDialer.Dial("tcp", hostport)
// if err != nil {
// return false, err
// }
// } else {
// conn, err = net.DialTimeout("tcp", hostport, timeout)
// }
// if err != nil {
// return false, err
// }
// conn.Close()
// return true, err
//}
// ScanSyn a target ip
//func (s *Scanner) ScanSyn(ip string) {
// for _, port := range s.Ports {
// s.SendSYN(ip, port, SYN)
// }
//}
// ACKPort sends an ACK packet to a port
//func (s *Scanner) ACKPort(dstIP string, port int, timeout time.Duration) (bool, error) {
// conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
// if err != nil {
// return false, err
// }
// defer conn.Close()
//
// rawPort, err := freeport.GetFreePort()
// if err != nil {
// return false, err
// }
//
// // Construct all the network layers we need.
// ip4 := layers.IPv4{
// SrcIP: s.sourceIP,
// DstIP: net.ParseIP(dstIP),
// Version: 4,
// TTL: 255,
// Protocol: layers.IPProtocolTCP,
// }
// tcpOption := layers.TCPOption{
// OptionType: layers.TCPOptionKindMSS,
// OptionLength: 4,
// OptionData: []byte{0x12, 0x34},
// }
//
// tcp := layers.TCP{
// SrcPort: layers.TCPPort(rawPort),
// DstPort: layers.TCPPort(port),
// ACK: true,
// Window: 1024,
// Seq: s.tcpSequencer.Next(),
// Options: []layers.TCPOption{tcpOption},
// }
//
// err = tcp.SetNetworkLayerForChecksum(&ip4)
// if err != nil {
// return false, err
// }
//
// err = s.send(dstIP, conn, &tcp)
// if err != nil {
// return false, err
// }
//
// data := make([]byte, 4096)
// for {
// n, addr, err := conn.ReadFrom(data)
// if err != nil {
// break
// }
//
// // not matching ip
// if addr.String() != dstIP {
// logger.Printf("Discarding TCP packet from non target ip %s for %s\n", dstIP, addr.String())
// continue
// }
//
// packet := gopacket.NewPacket(data[:n], layers.LayerTypeTCP, gopacket.Default)
// if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
// tcp, ok := tcpLayer.(*layers.TCP)
// if !ok {
// continue
// }
// // We consider only incoming packets
// if tcp.DstPort != layers.TCPPort(rawPort) {
// logger.Printf("Discarding TCP packet from %s:%d not matching %s:%d port\n", addr.String(), tcp.DstPort, dstIP, rawPort)
// continue
// } else if tcp.RST {
// logger.Printf("Accepting RST packet from %s:%d\n", addr.String(), tcp.DstPort)
// return true, nil
// }
// }
// }
//
// return false, nil
//}
// EnqueueICMP outgoing ICMP packets
//func (s *Scanner) EnqueueICMP(ip string, pkgtype PkgFlag) {
// s.icmpPacketSend <- &PkgSend{
// ip: ip,
// flag: pkgtype,
// }
//}
// ICMPWriteWorker writes packet to the network layer
//func (s *Scanner) ICMPWriteWorker() {
// for pkg := range s.icmpPacketSend {
// if pkg.flag == ICMPECHOREQUEST && pingIcmpEchoRequestAsyncCallback != nil {
// pingIcmpEchoRequestAsyncCallback(s, pkg.ip)
// } else if pkg.flag == ICMPTIMESTAMPREQUEST && pingIcmpTimestampRequestAsyncCallback != nil {
// pingIcmpTimestampRequestAsyncCallback(s, pkg.ip)
// }
// }
//}