-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
373 lines (320 loc) · 7.5 KB
/
main.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
package main
import (
"math"
"math/rand"
"net"
"time"
"github.com/spf13/viper"
"github.com/google/gopacket/pcap"
)
const NUM_SEQ_SAMPLES = 6
const NUM_FPTESTS = 13
const MAX_FP_RESULTS = 36
const (
OP_FAILURE = -1
OP_SUCCESS = 0
)
type OFProbeType uint32
const (
OFP_UNSET OFProbeType = 1 << (32 - 1 - iota)
OFP_TSEQ
OFP_TOPS
OFP_TECN
OFP_T1_7
OFP_TICMP
OFP_TUDP
)
type dist_calc_method uint32
const (
DIST_METHOD_NONE dist_calc_method = 1 << (32 - 1 - iota)
DIST_METHOD_LOCALHOST
DIST_METHOD_DIRECT
DIST_METHOD_ICMP
DIST_METHOD_TRACEROUTE
)
var c *Config
var perf scan_performance_vars
type Config struct {
NmapOs NmapOs
}
type NmapOs struct {
min_parallelism int
max_parallelism int
db_path string
host_timeout uint64
}
type scan_performance_vars struct {
low_cwnd int
host_initial_cwnd int
group_initial_cwnd int
max_cwnd int
slow_incr int
ca_incr int
cc_scale_max int
initial_ssthresh int
group_drop_cwnd_divisor float64
group_drop_ssthresh_divisor float64
host_drop_ssthresh_divisor float64
}
func (s *scan_performance_vars) init(){
var maxOpInt int
if c.NmapOs.min_parallelism != 0 {
s.low_cwnd = c.NmapOs.min_parallelism
}else {
s.low_cwnd = 1
}
if c.NmapOs.max_parallelism != 0 {
maxOpInt = c.NmapOs.max_parallelism
}else {
maxOpInt = 300
}
if s.max_cwnd > maxOpInt{
s.max_cwnd = maxOpInt
}
}
func os_scan_ipv4(Targets []*Target) int {
var itry int
var unMatchedHosts []*HostOsScanInfo
perf.init()
OSI, err := newOsScanInfo(Targets)
if err != nil || len(Targets) == 0 || OSI.numIncompleteHosts() == 0{
return OP_FAILURE
}
OSI.starttime =
}
type HostOsScanInfo struct {
target *Target
FPR *FingerPrintResultsIPv4
OSI *OsScanInfo
}
type OsScanInfo struct {
starttime float64
numInitialTargets uint32
incompleteHosts []*HostOsScanInfo
}
func (i *OsScanInfo) numIncompleteHosts() int {
return len(i.incompleteHosts)
}
type FingerPrintResultsIPv4 struct {
numFPs int
}
type state_reason_t struct {
reason_id uint16
ttl uint16
}
type FingerPrintResults struct {
num_perfect_matches int
num_matches int
overall_results int
osscan_opentcpport int
osscan_closedtcpport int
osscan_closedudpport int
distance int
distance_guess int
distance_calculation_method dist_calc_method
maxTimingRatio float64
incomplete bool
isClassified bool
OSR OS_Classification_Results
}
type OS_Classification_Results struct {
OSC *[MAX_FP_RESULTS]OS_Classification
OSC_Accuracy [MAX_FP_RESULTS]float64
OSC_num_perfect_matches int
OSC_num_matches int
overall_results int
}
type ScanStats struct {
num_probes_active int
num_probes_send int
num_probes_sent_at_last_wait int
}
type HostOsScanStats struct {
si seq_info
ipid ipid_info
distance int
distance_guess int
openTCPPort, closedTCPPort, closedUDPPort int
probesToSend []*OFProbe
probesActive []*OFProbe
num_probes_sent uint
sendDelayMs uint
lastProbeSent time.Time
timing ultra_timing_vals
FP *FingerPrint
FPtests [NUM_FPTESTS]*FingerTest
TOps_AVs [6]*AVal
TWin_AVs [6]*AVal
lastipid uint16
seq_send_times [NUM_SEQ_SAMPLES]time.Time
TWinReplyNum int
TOpsReplyNum int
storedIcmpReply int
upi udpprobeinfo
}
func newOsScanInfo(targets []*Target) (*OsScanInfo, error) {
OSI := &OsScanInfo{}
var targetno uint16
var hsi *HostOsScanInfo
num_timedout := 0
OSI.numInitialTargets = 0
for i:=0; i<len(targets);i++ {
if targets[i].timedOut(time.Now()){
num_timedout++
continue
}
}
OSI.nextTarget =
return OSI, nil
}
func (s *HostOsScanStats) addNewProbe(probeType OFProbeType, i int) {
var probe *OFProbe
probe.type_ = probeType
probe.subid = i
s.probesToSend = append(s.probesToSend, probe)
}
func (s *HostOsScanStats) removeActiveProbe(probeI *OFProbeType) {
}
type FingerPrint struct {
match FingerMatch
tests []FingerTest
}
type FingerTest struct {
name string
}
type AVal struct {
attribute string
value string
}
type FingerMatch struct {
line int
numprints uint16
OS_name string
OS_class []OS_Classification
}
type OS_Classification struct {
OS_Vendor string
OS_Family string
OS_Generation string
Device_Type string
cpe []string
}
type ultra_timing_vals struct {
cwnd float64
ssthresh int
num_replies_expected int
num_replies_received int
num_updates int
last_drop time.Time
}
type OFProbe struct {
subid int
tryno int
type_ OFProbeType
retransmitted bool
sent time.Time
prevSent time.Time
}
type nrand_handle struct {
i, j uint8
s [256]uint8
tmp *uint8
tmplen int
}
type HostOsScan struct {
pacap_t *pcap.Handle
stats *ScanStats
rawsd int
ethsd net.Interface
tcpSeqBase uint32
tcpAck uint32
tcpMss int
udpttl int
icmpEchoId uint16
icmpEchoSeq uint16
tcpPortBase int
udpPortBase int
}
type seq_info struct {
responses int
ts_seqclasses int
ipid_seqclasses int
seqs [NUM_SEQ_SAMPLES]int
timestamps [NUM_SEQ_SAMPLES]int
index int
ipids [NUM_SEQ_SAMPLES]int
lastboot time.Time
}
type ipid_info struct {
tcp_ipids [NUM_SEQ_SAMPLES]uint32
tcp_closed_ipids [NUM_SEQ_SAMPLES]uint32
icmp_ipids [NUM_SEQ_SAMPLES]uint32
}
type udpprobeinfo struct {
iptl uint16
ipid uint16
ipck uint16
sport uint16
dport uint16
udpck uint16
udplen uint16
patternbyte uint8
target in_addr
}
type in_addr_t uint32
type in_addr struct {
s_addr in_addr_t
}
func (h *HostOsScan) reInitScanSystem() {
h.tcpSeqBase = get_random_u32()
h.tcpAck = get_random_u32()
h.tcpMss = 265
h.icmpEchoId = get_random_u16()
h.icmpEchoSeq = 295
h.udpttl = randIntRange(51, 69)
}
func (h *HostOsScan) buildSeqProbeList(hss *HostOsScanStats) {
if hss == nil {
return
}
var i int
if hss.openTCPPort == -1 {
return
}
if hss.FPtests[0] == nil {
return
}
for i = 0; i < NUM_SEQ_SAMPLES; i++ {
hss.addNewProbe(OFP_TSEQ, i)
}
}
func get_random_u16() uint16 {
return uint16(randIntRange(0, math.MaxUint16))
}
func randIntRange(min, max int) int {
if min == max {
return min
}
return rand.Intn((max+1)-min) + min
}
func get_random_u32() uint32 {
var a = rand.Uint32()
return a
}
func main() {
v := viper.New()
v.SetConfigFile("./config.yaml")
v.SetConfigType("yaml")
if err1 := v.ReadInConfig(); err1 != nil {
return
}
c.NmapOs.max_parallelism = v.GetInt("NmapOs.max_parallelism")
c.NmapOs.min_parallelism = v.GetInt("NmapOS.min_parallelism")
c.NmapOs.db_path = v.GetString("NmapOS.db_path")
}
func getTargetOsFingerPrint(target net.IPAddr) string {
return ""
}
func matchFingerPrintDB(fingerPrint string) string {
return ""
}