-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
176 lines (146 loc) · 5.11 KB
/
main.py
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
#!/usr/bin/env python
import json
import os
import platform
import sys
import time
from modules.endpoint import Endpoint
from modules.payloads.netflow_v9 import *
from modules.pcapfile import PcapFile
from modules.utils import random_ephemeral_port
OPTIONS_COMMAND = "generate"
STRICT = False
VERSION = 9
OPTIONS_OUTPUT_FILE = None
OPTIONS_INPUT_FILE = None
ENDPOINT_SENSOR, PORT_SENSOR = None, random_ephemeral_port()
ENDPOINT_COLLECTOR, PORT_COLLECTOR = None, 2055
def usage():
print("Usage: " + os.path.basename(sys.argv[0]) + ' command -f flows')
print()
print("Command:")
print(" generate : Generate Pcap containing netflows")
print()
print("Options:")
print(" -h, --help : Show help")
print(" -f, --flows : Input file (JSON flows) - required")
print(" -o, --output : Output file (default: output.pcap)")
print(" -s, --sensor : Flow sensor (IP:PORT)")
print(" -c, --collector : Flow collector (IP:PORT)")
print(" --strict : Disable flow autocompletion")
print()
sys.exit(0)
def parse_args():
global OPTIONS_INPUT_FILE, OPTIONS_OUTPUT_FILE, ENDPOINT_SENSOR, ENDPOINT_COLLECTOR, PORT_SENSOR, PORT_COLLECTOR, STRICT, VERSION, OPTIONS_COMMAND
i = 1
while i < len(sys.argv):
if sys.argv[i] in ["-h", "--help"]:
usage()
elif sys.argv[i] in ["-f", "--flows"]:
OPTIONS_INPUT_FILE = sys.argv[i + 1]
i += 1
elif sys.argv[i] in ["-o", "--output"]:
OPTIONS_OUTPUT_FILE = sys.argv[i + 1]
i += 1
elif sys.argv[i] in ["--strict"]:
STRICT = True
elif sys.argv[i] in ["-s", "--sensor"]:
sp = sys.argv[i + 1].split(":")
ENDPOINT_SENSOR = sp[0]
if len(sp) > 1:
PORT_SENSOR = int(sp[1])
i += 1
elif sys.argv[i] in ["-c", "--collector"]:
sp = sys.argv[i + 1].split(":")
ENDPOINT_COLLECTOR = sp[0]
if len(sp) > 1:
PORT_COLLECTOR = int(sp[1])
i += 1
else:
OPTIONS_COMMAND = sys.argv[i + 1]
i += 1
i += 1
if OPTIONS_OUTPUT_FILE is None:
OPTIONS_OUTPUT_FILE = "output.pcap"
def load_flows(filename):
print("[+] Loading flows from %s" % os.path.basename(filename))
with open(filename, "r") as f:
data = json.load(f)
if len(data) > 0:
ref = data[0].keys()
if len(data) > 1:
i = 2
for flow in data[1:]:
if ref != flow.keys():
print("[!] Different set of fields in flow %s" % i)
return None
i += 1
return data
def create_netflow_packet():
if VERSION == 9:
return NetFlowV9Packet()
else:
raise Exception("Unsupported flow version %s" % str(VERSION))
def create_netflow_template(flow, strict_mode):
if VERSION == 9:
return NetFlowV9Template(flow, strict_mode)
else:
raise Exception("Unsupported flow template version %s" % str(VERSION))
def create_netflow_flow(flow, strict_mode):
if VERSION == 9:
return NetFlowV9Flow(flow, strict_mode)
else:
raise Exception("Unsupported flow template version %s" % str(VERSION))
def create_netflow_set(id=None):
if VERSION == 9:
if id == NetFlowID.TemplateAuto:
return NetFlowV9Set(NetFlowID.TemplateV9)
else:
return NetFlowV9Set(id)
else:
raise Exception("Unsupported flow template version %s" % str(VERSION))
def process(flows, strict_mode):
pkt = create_netflow_packet()
print("[+] Building flow template")
flowset_template = create_netflow_set(id=NetFlowID.TemplateAuto)
flowset_template.add_flow(create_netflow_template(flows[0], STRICT))
pkt.add_flowset(flowset_template)
print("[+] Building flow data")
flowset = create_netflow_set()
i = 1
for flow in flows:
nf = create_netflow_flow(flow, strict_mode)
flowset.add_flow(nf)
print(" %2d : %s" % (i, nf))
i += 1
pkt.add_flowset(flowset)
print("[+] Writing to %s" % OPTIONS_OUTPUT_FILE)
f = PcapFile(OPTIONS_OUTPUT_FILE)
f.add_flow_udp(
Endpoint(ip=ENDPOINT_SENSOR),
PORT_SENSOR,
Endpoint(ip=ENDPOINT_COLLECTOR),
PORT_COLLECTOR,
pkt)
f.close()
def edit(filename):
pass
def main():
print("Starting %s at %s (%s version)\n" % (
os.path.basename(sys.argv[0]), time.asctime(time.localtime(time.time())), platform.architecture()[0]))
parse_args()
if OPTIONS_COMMAND == "generate":
if OPTIONS_INPUT_FILE is None:
print("[!] Missing input file option")
return
elif ENDPOINT_SENSOR is None:
print("[!] Missing sensor option")
return
elif ENDPOINT_COLLECTOR is None:
print("[!] Missing collector option")
return
flows = load_flows(OPTIONS_INPUT_FILE)
if flows is not None:
process(flows, STRICT)
if __name__ == '__main__':
main()