forked from nerdalert/nflow-generator
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nflow-generator.go
169 lines (149 loc) · 4.48 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
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net"
ipfix "nflow-generator/ipfix"
leg "nflow-generator/legacy"
"os"
"strings"
"time"
"github.com/jessevdk/go-flags"
)
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"`
IPs string `short:"i" long:"ips" description:"use specific list of ips, comma separated"`
V10 bool `short:"v" long:"ipfix" description:"use ipfix version (10)"`
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",
opts.CollectorIP, opts.CollectorPort)
var ips []string
if len(opts.IPs) > 0 {
ips = strings.Split(opts.IPs, ",")
log.Info("specified ips:")
for _, ip := range ips {
log.Infof("%s", ip)
}
}
var byteArray []byte
for {
rand.Seed(time.Now().Unix())
n := leg.RandomNum(50, 1000)
if opts.V10 {
msg := ipfix.GenerateNetflow(ips)
js, _ := json.Marshal(msg)
fmt.Println(bytes.NewBuffer(js).String())
byteArray = ipfix.Encode(*msg, ipfix.GetSeqNum())
} else {
// add spike data
if opts.SpikeProto != "" {
leg.GenerateSpike(opts.SpikeProto)
}
recordCount := 16
if n > 900 {
recordCount = 8
}
data := leg.GenerateNetflow(recordCount, ips, opts.FalseIndex)
byteArray = leg.BuildNFlowPayload(data)
}
fmt.Println(byteArray)
_, err := conn.Write(byteArray)
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 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)
-i, --ips use specific list of ips, comma separated (Optional)
-v, --ipfix use ipfix version (10)
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 between ips 172.16.86.1, 172.16.86.2, 172.16.86.3 to device 172.16.86.138, port 9995
./nflow-generator -t 172.16.86.138 -p 9995 -i 172.16.86.1,172.16.86.2,172.16.86.3
-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
Help Options:
-h, --help Show this help message
`
fmt.Print(usage)
}