-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsyn_test.go
191 lines (183 loc) · 4.65 KB
/
syn_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
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
package scanner
import (
"runtime"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestSynScanner(t *testing.T) {
start := time.Now()
targets := "8.8.8.8-8.8.8.10, 2606:4700:4700::1001-2606:4700:4700::1003"
ports := "53,54,55-57"
opt := Options{
Timeout: 5 * time.Second,
Rate: 2000,
Workers: 2,
}
scanner, err := New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
result := make(map[string]struct{})
for address := range scanner.Result {
result[address] = struct{}{}
t.Log(address)
}
expected := []string{
"8.8.8.8:53",
"[2606:4700:4700::1001]:53",
}
for i := 0; i < len(expected); i++ {
if _, ok := result[expected[i]]; !ok {
t.Fatal(expected[i], "is lost")
}
}
t.Log("result:", len(result), "time:", time.Since(start))
require.Equal(t, scanner.HostNum().String(), "30")
require.Equal(t, scanner.Scanned().String(), "30")
}
func TestSynScanner_simple(t *testing.T) {
start := time.Now()
targets := "127.0.0.1, ::1"
port := testListener(t)
scanner, err := New(targets, port, nil)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
result := make(map[string]struct{})
for address := range scanner.Result {
result[address] = struct{}{}
t.Log(address)
}
expected := []string{
"127.0.0.1:" + port,
"[::1]:" + port,
}
for i := 0; i < len(expected); i++ {
if _, ok := result[expected[i]]; !ok {
t.Fatal(expected[i], "is lost")
}
}
t.Log("result:", len(result), "time:", time.Since(start))
require.Equal(t, scanner.HostNum().String(), "2")
require.Equal(t, scanner.Scanned().String(), "2")
}
func TestSynScanner_Stop(t *testing.T) {
targets := "8.8.8.8/16"
ports := "53,54,55-57"
opt := Options{
Timeout: 10 * time.Second,
Rate: 10,
}
scanner, err := New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
go func() {
err = scanner.Start()
require.Error(t, err)
}()
time.Sleep(2 * time.Second)
scanner.Stop()
go func() { scanner.Stop() }()
time.Sleep(250 * time.Millisecond)
require.Equal(t, 2, runtime.NumGoroutine())
}
func TestSynScanner_Duplicate(t *testing.T) {
start := time.Now()
targets := "123.206.1.1/16"
ports := "80"
opt := Options{
Timeout: 5 * time.Second,
Rate: 30000,
}
scanner, err := New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
result := make(map[string]struct{})
for address := range scanner.Result {
if _, ok := result[address]; ok {
t.Fatal("duplicate:", address)
}
result[address] = struct{}{}
}
t.Log("result:", len(result), "time:", time.Since(start))
require.Equal(t, scanner.HostNum().String(), "65536")
require.Equal(t, scanner.Scanned().String(), "65536")
}
func TestSynScanner_Raw(t *testing.T) {
start := time.Now()
targets := "123.206.1.1/16"
ports := "80"
opt := Options{
Timeout: 5 * time.Second,
Rate: 2800,
Raw: true,
}
scanner, err := New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
result := make(map[string]struct{})
for address := range scanner.Result {
if _, ok := result[address]; ok {
t.Log("duplicate:", address)
continue
}
result[address] = struct{}{}
}
t.Log("result:", len(result), "time:", time.Since(start))
require.Equal(t, scanner.HostNum().String(), "65536")
require.Equal(t, scanner.Scanned().String(), "65536")
}
func TestSynScannerAccuracy(t *testing.T) {
targets := "123.206.1.1/16"
ports := "80"
opt := Options{
Device: "Ethernet0",
Method: MethodConnect,
Timeout: 5 * time.Second,
Rate: 1000,
}
// connect
start := time.Now()
scanner, err := New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
connectResult := make(map[string]struct{})
for address := range scanner.Result {
connectResult[address] = struct{}{}
}
t.Log("tcp ok", time.Since(start))
time.Sleep(2 * opt.Timeout)
// syn
start = time.Now()
opt.Method = MethodSYN
opt.Rate = 2000
opt.Workers = 16
scanner, err = New(targets, ports, &opt)
require.NoError(t, err)
err = scanner.Start()
require.NoError(t, err)
synResult := make(map[string]struct{})
for address := range scanner.Result {
synResult[address] = struct{}{}
}
t.Log("syn ok", time.Since(start))
// compare
var synScanned int
for address := range connectResult {
if _, ok := synResult[address]; ok {
synScanned += 1
} else {
t.Log(address, "is lost")
}
}
connectResultL := len(connectResult)
synResultL := len(synResult)
t.Logf("connect: %d syn: %d", connectResultL, synResultL)
accuracy := float64(synScanned) / float64(connectResultL) * 100
t.Logf("accuracy: %f%%", accuracy)
}