forked from dnif-archive/snort-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnort-agent.py
235 lines (207 loc) · 9.01 KB
/
snort-agent.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
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
from dnif.consumer import AsyncHttpConsumer
from dnif.logger import DnifLogger
from idstools import unified2
from idstools import maps
import sys
import logging
import time
import os
import datetime
import pygeoip
import re
import logging
import redis
import ast
import json
LOGFILE = "/var/log/snort-agent.log"
logging.basicConfig(filename=LOGFILE, level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
try:
ADIPv4 = os.environ["AD"]
except KeyError:
logging.error("Please specify IPv4 Address for the Adapter using -e AD=x.y.z.a")
sys.exit(1)
url = 'http://' + ADIPv4 + ':9234/json/receive'
# DevSrcIP = sysconfig['localip']
#Deff :
now_clock = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
sigIDDict = {}
dt_evt = {}
classmap = maps.ClassificationMap()
#Load Classidication.config from both SNORT and ET-Rules
classmap.load_from_file(open("/etc/snort/rules/classification.config"))
#To check how many classification is loade uncomment below line
logging.warning("[+] No. of classificaton loaded = %s " %(classmap.size()))
# Set to go : Call using --> classmap.get(<ClassID>)
#======== End of Classification init =============
sigmap = maps.SignatureMap()
#Load gid & sid files from both SNORT and ET-Rules
sigmap.load_generator_map(open("/etc/snort/rules/gen-msg.map"))
sigmap.load_signature_map(open("/etc/snort/rules/sid-msg.map"))
#To check how many Signature-maps is loade uncomment below line
logging.warning("[+] No. of Signature loaded = %s " %(sigmap.size()))
# Set to go : Call using --> classmap.get(<ClassID>)
#======== End of Signature-map init =============
#Init GEOIP data for IP details
geo_lite_city = pygeoip.GeoIP('/usr/local/lookups/GeoLiteCity.dat')
geo_ip_asn = pygeoip.GeoIP('/usr/local/lookups/GeoIPASNum.dat')
logging.warning('Loaded latest ASN and City info')
nowtimedom = datetime.datetime.now()
updatedurationdom = datetime.timedelta(minutes=5)
updatetimedom = nowtimedom + updatedurationdom
# FOR ASN and City Info
nowtime = datetime.datetime.now()
updateduration = datetime.timedelta(hours=6)
updatetime = nowtime + updateduration
reader = unified2.SpoolEventReader("/var/log/snort", "snort.u2.*",follow=True,delete=False,bookmark=True)
httplist = []
max_buffer_size=1024
nowtime = datetime.datetime.now()
timeduration = datetime.timedelta(seconds=5)
endtime = nowtime + timeduration
dlog = DnifLogger(AsyncHttpConsumer(url, buffer_size=max_buffer_size))
dlog.start()
try:
for event in reader:
if datetime.datetime.now() > updatetime:
try:
geo_lite_city = pygeoip.GeoIP('GeoLiteCity.dat')
geo_ip_asn = pygeoip.GeoIP('GeoIPASNum.dat')
logging.warning('Loaded latest ASN and City info')
except Exception,e:
print e
nowtime = datetime.datetime.now()
updateduration = datetime.timedelta(hours=6)
updatetime = nowtime + updateduration
if datetime.datetime.now() > updatetimedom:
nowtimedom = datetime.datetime.now()
updatedurationdom = datetime.timedelta(minutes=5)
updatetimedom = nowtimedom + updatedurationdom
dt_evt = {}
if event['generator-id'] != 1 :
continue
signaturemap = sigmap.get(event['generator-id'],event['signature-id'])
classname = classmap.get(event['classification-id'])
dt_evt['AtkClass'] = str(classname['name'])
dt_evt['AtkDesc'] = str(classname['description'])
dt_evt['AtkMsg'] = str(signaturemap['msg'])
dt_evt['LogEvent'] = str(signaturemap['msg'])
dt_evt['EventID'] = str(event['signature-id'])
CVE =""
URL =""
BUG =""
if signaturemap['ref'] :
for i in signaturemap['ref'] :
m = i.split(",")
if m[0] == 'cve':
CVE += m[1] + ","
elif m[0] == 'url':
URL += m[1] + ","
elif m[0] == 'bugtraq':
BUG += m[1] + ","
if CVE != "" :
dt_evt['CVE'] = str(CVE[:-1])
if URL != "" :
dt_evt['URL'] = str(URL[:-1])
if BUG != "" :
dt_evt['BugTraq'] = str(BUG[:-1])
try:
for KEY, VALUE in event.items():
if KEY == 'packets' and VALUE != None:
dt_evt['PacketData']= str(VALUE[0]['data']).decode('utf-8', 'ignore').encode('utf-8')
elif KEY == 'extra-data' :
if len(VALUE) > 0 :
dt_evt['ExtraData']=str(VALUE).decode('utf-8', 'ignore').encode('utf-8')
elif KEY == 'protocol' and VALUE != None :
dt_evt['Proto'] = str(VALUE)
elif KEY == 'source-ip' and VALUE != None :
rec = None
try :
rec = geo_lite_city.record_by_addr(VALUE)
except Exception, e:
logging.warning('IN source geo_lite_city %s'%str(e))
if rec != None :
t_arry = """[%s,%s]""" %(rec['longitude'],rec['latitude'])
dt_evt['SrcLOC'] = t_arry
dt_evt['SrcCN'] = rec['country_code']
else:
dt_evt['SrcLOC'] = 'NA'
dt_evt['SrcCN'] = 'NA'
asn_obj = None
try:
asn_obj = geo_ip_asn.asn_by_addr(VALUE)
except Exception,e:
logging.warning('IN source asn_obj %s'%str(e))
if asn_obj != None :
match = None
match = re.findall("^(AS.*?)\s+(.*)", asn_obj)
dt_evt['SrcAS'] = match[0][0]
dt_evt['SrcISP'] = match[0][1]
else:
dt_evt['SrcAS'] = 'NA'
dt_evt['SrcISP'] = 'NA'
dt_evt['SrcIP'] = VALUE
elif KEY == 'destination-ip' and VALUE != None :
rec = None
try:
rec = geo_lite_city.record_by_addr(VALUE)
except Exception,e:
logging.warning('IN destination geo_lite_city %s'%str(e))
if rec != None :
t_arry = """[%s,%s]""" %(rec['longitude'],rec['latitude'])
dt_evt['DstLOC'] = t_arry
dt_evt['DstCN'] = rec['country_code']
else:
dt_evt['DstLOC'] = 'NA'
dt_evt['DstCN'] = 'NA'
asn_obj = None
try:
asn_obj = geo_ip_asn.asn_by_addr(VALUE)
except Exception,e:
logging.warning('IN destination asn_obj %s'%str(e))
if asn_obj != None :
match = None
match = re.findall("^(AS.*?)\s+(.*)", asn_obj)
dt_evt['DstAS'] = match[0][0]
dt_evt['DstISP'] = match[0][1]
else:
dt_evt['DstAS'] = 'NA'
dt_evt['DstISP'] = 'NA'
dt_evt['DstIP'] = VALUE
elif KEY == 'sport-itype' and VALUE != None :
dt_evt['SrcPort'] = str(VALUE)
elif KEY == 'dport-icode' and VALUE != None :
dt_evt['DstPort'] = str(VALUE)
elif KEY == 'priority' and VALUE != None :
dt_evt['Severity'] = str(VALUE)
elif KEY == 'vlan-id' and VALUE != None :
dt_evt['VlanID'] = str(VALUE)
dt_evt['LogType'] = 'DPI'
dt_evt['LogID'] = 45
dt_evt['PStatus'] = 'PAD'
dt_evt['LogName'] = 'NMSNORT'
dt_evt['ProductType'] = 'TM'
dt_evt['ScopeID'] = sysconfig['scopeid']
dt_evt['EvtLen'] = len(str(event))
dt_evt['CNAMTime'] = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3]
dt_evt['DevSrcIP'] = DevSrcIP
srcip = '.'.join(dt_evt['SrcIP'].split('.')[:3])
dstip = '.'.join(dt_evt['DstIP'].split('.')[:3])
dt_evt['Flow'] = "I"
httplist.append(dt_evt)
if 'PacketData' in dt_evt:
dt_evt.pop('PacketData', None)
logging.warning('%s' %dt_evt)
#rpr = dlog.log(dt_evt)
if (datetime.datetime.now() > endtime) or (len(httplist) > 15000):
nowtime = datetime.datetime.now()
timeduration = datetime.timedelta(seconds=5)
endtime = nowtime + timeduration
logging.warning('%s list' %httplist)
dlog.log(httplist)
httplist = []
except Exception,e:
logging.warning('Error %s'%str(e))
time.sleep(2)
except Exception,e:
logging.warning('Error %s'%str(e))
time.sleep(2)