forked from vela-ssoc/vela-radar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan_test.go
156 lines (131 loc) · 3.47 KB
/
scan_test.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
package radar
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/netip"
"sync"
"testing"
"time"
wappalyzer "github.com/projectdiscovery/wappalyzergo"
"github.com/vela-ssoc/vela-kit/iputil"
"github.com/vela-ssoc/vela-kit/thread"
"github.com/vela-ssoc/vela-radar/fingerprintx/plugins"
"github.com/vela-ssoc/vela-radar/fingerprintx/scan"
"github.com/vela-ssoc/vela-radar/host"
"github.com/vela-ssoc/vela-radar/port"
"github.com/vela-ssoc/vela-radar/port/syn"
"github.com/vela-ssoc/vela-radar/util"
)
func TestScan(t *testing.T) {
pn := false
// 解析端口字符串并且优先发送 TopTcpPorts 中的端口, eg: 1-65535,top1000
ports, err := port.ShuffleParseAndMergeTopPorts("1-65535")
if err != nil {
log.Fatal(err)
}
fn := func(tx port.OpenIpPort) {
t.Logf("ip:%s port:%d", tx.Ip.String(), tx.Port)
addr, _ := netip.AddrFromSlice(tx.Ip)
target := plugins.Target{
Address: netip.AddrPortFrom(addr, tx.Port),
Host: "localhost",
}
cfg := scan.Config{
UDP: false,
FastMode: false,
DefaultTimeout: time.Second,
Verbose: false,
}
srv, err := scan.Do(target, cfg)
if err != nil {
t.Logf("callback fingerprint fail %v", err)
return
}
if srv == nil {
return
}
h := Service{
IP: tx.Ip,
Port: tx.Port,
Protocol: srv.Protocol,
TLS: srv.TLS,
Transport: srv.Transport,
Version: srv.Version,
Banner: srv.Raw,
}
t.Logf("%s\n", h.String())
}
var wgFinger sync.WaitGroup
fingerPool, _ := thread.NewPoolWithFunc(300, func(v interface{}) {
tx := v.(port.OpenIpPort)
fn(tx)
wgFinger.Done()
})
call := func(v port.OpenIpPort) {
wgFinger.Add(1)
fingerPool.Invoke(v)
}
// parse ip
it, startIp, _ := iputil.NewIter("172.31.61.0/24")
// scanner
ss, err := syn.NewSynScanner(startIp, call, port.Option{
Rate: 1500,
Timeout: 800,
})
// port scan func
portScan := func(ip net.IP) {
for _, _port := range ports { // port
ss.WaitLimiter() // limit rate
ss.Scan(ip, _port)
}
}
// host group scan func
var wgHostScan sync.WaitGroup
hostScan, _ := thread.NewPoolWithFunc(200, func(ip interface{}) {
_ip := ip.(net.IP)
portScan(_ip)
wgHostScan.Done()
})
defer hostScan.Release()
var wgPing sync.WaitGroup
// Pool - ping and port scan
poolPing, _ := thread.NewPoolWithFunc(300, func(ip interface{}) {
_ip := ip.(net.IP)
if host.IsLive(_ip.String(), false, 800*time.Millisecond) {
wgHostScan.Add(1)
hostScan.Invoke(_ip)
}
wgPing.Done()
})
defer poolPing.Release()
shuffle := util.NewShuffle(it.TotalNum()) // shuffle
for i := uint64(0); i < it.TotalNum(); i++ { // ip index
ip := make(net.IP, len(it.GetIpByIndex(0)))
copy(ip, it.GetIpByIndex(shuffle.Get(i))) // Note: dup copy []byte when concurrent (GetIpByIndex not to do dup copy)
if !pn { // ping
wgPing.Add(1)
_ = poolPing.Invoke(ip)
} else {
wgHostScan.Add(1)
hostScan.Invoke(ip)
}
}
wgPing.Wait()
wgHostScan.Wait()
ss.Wait()
ss.Close()
}
func TestWeb(t *testing.T) {
resp, err := http.DefaultClient.Get("http://172.31.61.168:8081")
if err != nil {
log.Fatal(err)
}
data, _ := ioutil.ReadAll(resp.Body) // Ignoring error for example
wappalyzerClient, err := wappalyzer.New()
fingerprints := wappalyzerClient.Fingerprint(resp.Header, data)
fmt.Printf("%v\n", fingerprints)
// Output: map[Acquia Cloud Platform:{} Amazon EC2:{} Apache:{} Cloudflare:{} Drupal:{} PHP:{} Percona:{} React:{} Varnish:{}]
}