-
Notifications
You must be signed in to change notification settings - Fork 47
/
nflow-generator.go
156 lines (138 loc) · 4.25 KB
/
nflow-generator.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
// Run using:
// go run nflow-generator.go nflow_logging.go nflow_payload.go -t 172.16.86.138 -p 9995
// Or:
// go build
// ./nflow-generator -t <ip> -p <port>
package main
import (
"fmt"
"github.com/jessevdk/go-flags"
"math/rand"
"net"
"os"
"time"
)
type Proto int
const (
FTP Proto = iota + 1
SSH
DNS
HTTP
HTTPS
NTP
SNMP
IMAPS
MYSQL
HTTPS_ALT
P2P
BITTORRENT
)
var opts struct {
CollectorIP string `short:"t" long:"target" description:"target ip address of the netflow collector"`
CollectorPort string `short:"p" long:"port" description:"port number of the target netflow collector"`
SpikeProto string `short:"s" long:"spike" description:"run a second thread generating a spike for the specified protocol"`
FalseIndex bool `short:"f" long:"false-index" description:"generate false SNMP interface indexes, otherwise set to 0"`
FlowCount int `short:"c" long:"flow-count" description:"set the number of flows to generate in each iteration" default:"16" max:"128" min:"8"`
Help bool `short:"h" long:"help" description:"show nflow-generator help"`
}
func main() {
_, err := flags.Parse(&opts)
if err != nil {
showUsage()
os.Exit(1)
}
if opts.Help == true {
showUsage()
os.Exit(1)
}
if opts.CollectorIP == "" || opts.CollectorPort == "" {
showUsage()
os.Exit(1)
}
collector := opts.CollectorIP + ":" + opts.CollectorPort
udpAddr, err := net.ResolveUDPAddr("udp", collector)
if err != nil {
log.Fatal(err)
}
conn, err := net.DialUDP("udp", nil, udpAddr)
if err != nil {
log.Fatal("Error connecting to the target collector: ", err)
}
log.Infof("sending netflow data to a collector ip: %s and port: %s. \n"+
"Use ctrl^c to terminate the app.", opts.CollectorIP, opts.CollectorPort)
for {
rand.Seed(time.Now().Unix())
n := randomNum(50, 1000)
// add spike data
if opts.SpikeProto != "" {
GenerateSpike()
}
if n > 900 {
data := GenerateNetflow(8)
buffer := BuildNFlowPayload(data)
_, err := conn.Write(buffer.Bytes())
if err != nil {
log.Fatal("Error connecting to the target collector: ", err)
}
} else {
data := GenerateNetflow(opts.FlowCount)
buffer := BuildNFlowPayload(data)
_, err := conn.Write(buffer.Bytes())
if err != nil {
log.Fatal("Error connecting to the target collector: ", err)
}
}
// add some periodic spike data
if n < 150 {
sleepInt := time.Duration(3000)
time.Sleep(sleepInt * time.Millisecond)
}
sleepInt := time.Duration(n)
time.Sleep(sleepInt * time.Millisecond)
}
}
func randomNum(min, max int) int {
return rand.Intn(max-min) + min
}
func showUsage() {
var usage string
usage = `
Usage:
main [OPTIONS] [collector IP address] [collector port number]
Send mock Netflow version 5 data to designated collector IP & port.
Time stamps in all datagrams are set to UTC.
Application Options:
-t, --target= target ip address of the netflow collector
-p, --port= port number of the target netflow collector
-s, --spike run a second thread generating a spike for the specified protocol
protocol options are as follows:
ftp - generates tcp/21
ssh - generates tcp/22
dns - generates udp/54
http - generates tcp/80
https - generates tcp/443
ntp - generates udp/123
snmp - generates ufp/161
imaps - generates tcp/993
mysql - generates tcp/3306
https_alt - generates tcp/8080
p2p - generates udp/6681
bittorrent - generates udp/6682
-f, --false-index generate a false snmp index values of 1 or 2. The default is 0. (Optional)
-c, --flow-count set the number of flows to generate in each iteration. The default is 16. (Optional)
Example Usage:
-first build from source (one time)
go build
-generate default flows to device 172.16.86.138, port 9995
./nflow-generator -t 172.16.86.138 -p 9995
-generate default flows along with a spike in the specified protocol:
./nflow-generator -t 172.16.86.138 -p 9995 -s ssh
-generate default flows with "false index" settings for snmp interfaces
./nflow-generator -t 172.16.86.138 -p 9995 -f
-generate default flows with up to 256 flows
./nflow-generator -c 128 -t 172.16.86.138 -p 9995
Help Options:
-h, --help Show this help message
`
fmt.Print(usage)
}