forked from mlsecproject/combine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baler.py
executable file
·221 lines (184 loc) · 8.63 KB
/
baler.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
import ConfigParser
import datetime as dt
import gzip
import json
import logging
import os
import re
import requests
import sys
import time
import unicodecsv
import threading
from logger import get_logger
from Queue import Queue
logger = get_logger('baler')
def tiq_output(reg_file, enr_file):
config = ConfigParser.SafeConfigParser()
cfg_success = config.read('combine.cfg')
if not cfg_success:
logger.error('tiq_output: Could not read combine.cfg.')
logger.error('HINT: edit combine-example.cfg and save as combine.cfg.')
return
tiq_dir = os.path.join(config.get('Baler', 'tiq_directory'), 'data')
today = dt.datetime.today().strftime('%Y%m%d')
with open(reg_file, 'rb') as f:
reg_data = json.load(f)
with open(enr_file, 'rb') as f:
enr_data = json.load(f)
logger.info('Preparing tiq directory structure under %s' % tiq_dir)
if not os.path.isdir(tiq_dir):
os.makedirs(os.path.join(tiq_dir, 'raw', 'public_inbound'))
os.makedirs(os.path.join(tiq_dir, 'raw', 'public_outbound'))
os.makedirs(os.path.join(tiq_dir, 'enriched', 'public_inbound'))
os.makedirs(os.path.join(tiq_dir, 'enriched', 'public_outbound'))
inbound_data = [row for row in reg_data if row[2] == 'inbound']
outbound_data = [row for row in reg_data if row[2] == 'outbound']
try:
bale_reg_csvgz(inbound_data, os.path.join(tiq_dir, 'raw', 'public_inbound', today + '.csv.gz'))
bale_reg_csvgz(outbound_data, os.path.join(tiq_dir, 'raw', 'public_outbound', today + '.csv.gz'))
except:
pass
inbound_data = [row for row in enr_data if row[2] == 'inbound']
outbound_data = [row for row in enr_data if row[2] == 'outbound']
try:
bale_enr_csvgz(inbound_data, os.path.join(tiq_dir, 'enriched', 'public_inbound', today + '.csv.gz'))
bale_enr_csvgz(outbound_data, os.path.join(tiq_dir, 'enriched', 'public_outbound', today + '.csv.gz'))
except:
pass
# oh my god this is such a hack
def bale_reg_csvgz(harvest, output_file):
""" bale the data as a gziped csv file"""
logger.info('Output regular data as GZip CSV to %s' % output_file)
with gzip.open(output_file, 'wb') as csv_file:
bale_writer = unicodecsv.writer(csv_file, quoting=unicodecsv.QUOTE_ALL)
# header row
bale_writer.writerow(('entity', 'type', 'direction', 'source', 'notes', 'date'))
bale_writer.writerows(harvest)
def bale_reg_csv(harvest, output_file):
""" bale the data as a csv file"""
logger.info('Output regular data as CSV to %s' % output_file)
with open(output_file, 'wb') as csv_file:
bale_writer = unicodecsv.writer(csv_file, quoting=unicodecsv.QUOTE_ALL)
# header row
bale_writer.writerow(('entity', 'type', 'direction', 'source', 'notes', 'date'))
bale_writer.writerows(harvest)
def bale_enr_csv(harvest, output_file):
""" output the data as an enriched csv file"""
logger.info('Output enriched data as CSV to %s' % output_file)
with open(output_file, 'wb') as csv_file:
bale_writer = unicodecsv.writer(csv_file, quoting=unicodecsv.QUOTE_ALL)
# header row
bale_writer.writerow(('entity', 'type', 'direction', 'source', 'notes', 'date', 'asnumber', 'asname', 'country', 'host', 'rhost'))
bale_writer.writerows(harvest)
def bale_enr_csvgz(harvest, output_file):
""" output the data as an enriched gziped csv file"""
logger.info('Output enriched data as GZip CSV to %s' % output_file)
with gzip.open(output_file, 'wb') as csv_file:
bale_writer = unicodecsv.writer(csv_file, quoting=unicodecsv.QUOTE_ALL)
# header row
bale_writer.writerow(('entity', 'type', 'direction', 'source', 'notes', 'date', 'asnumber', 'asname', 'country', 'host', 'rhost'))
bale_writer.writerows(harvest)
def bale_CRITs_indicator(base_url, data, indicator_que):
""" One thread of adding indicators to CRITs"""
while not indicator_que.empty():
indicator = indicator_que.get()
if indicator[1] == 'IPv4':
# using the IP API
url = base_url + 'ips/'
data['add_indicator'] = "true"
data['ip'] = indicator[0]
data['ip_type'] = 'Address - ipv4-addr'
data['reference'] = indicator[3]
# getting the source automatically:
source = re.findall(r'\/\/(.*?)\/', data['reference'])
if source:
data['source'] = source[0]
res = requests.post(url, data=data, verify=False)
if not res.status_code in [201, 200, 400]:
logger.info("Issues with adding: %s" % data['ip'])
elif indicator[1] == "FQDN":
# using the Domain API
url = base_url + 'domains/'
data['add_indicator'] = "true"
data['domain'] = indicator[0]
data['reference'] = indicator[3]
# getting the source automatically:
source = re.findall(r'\/\/(.*?)\/', data['reference'])
if source:
data['source'] = source[0]
res = requests.post(url, data=data, verify=False)
if not res.status_code in [201, 200, 400]:
logger.info("Issues with adding: %s" % data['domain'])
else:
logger.info("don't yet know what to do with: %s[%s]" % (indicator[1], indicator[0]))
def bale_CRITs(harvest, filename):
""" taking the output from combine and pushing it to the CRITs web API"""
# checking the minimum requirements for parameters
# it would be nice to have some metadata on the feeds that can be imported in the intel library:
# -> confidence
# -> type of feed (bot vs spam vs ddos, you get the picture)
data = {'confidence': 'medium'}
start_time = time.time()
config = ConfigParser.SafeConfigParser()
cfg_success = config.read('combine.cfg')
if not cfg_success:
logger.error('tiq_output: Could not read combine.cfg.\n')
logger.error('HINT: edit combine-example.cfg and save as combine.cfg.\n')
return
if config.has_option('Baler', 'crits_username'):
data['username'] = config.get('Baler', 'crits_username')
else:
raise 'Please check the combine.cnf file for the crits_username field in the [Baler] section'
if config.has_option('Baler', 'crits_api_key'):
data['api_key'] = config.get('Baler', 'crits_api_key')
else:
raise 'Please check the combine.cnf file for the crits_api_key field in the [Baler] section'
if config.has_option('Baler', 'crits_campaign'):
data['campaign'] = config.get('Baler', 'crits_campaign')
else:
logger.info('Lacking a campaign name, we will default to "combine." Errors might ensue if it does not exist in CRITs')
data['campaign'] = 'combine'
if config.has_option('Baler', 'crits_url'):
base_url = config.get('Baler', 'crits_url')
else:
raise 'Please check the combine.cnf file for the crits_url field in the [Baler] section'
if config.has_option('Baler', 'crits_maxThreads'):
maxThreads = int(config.get('Baler', 'crits_maxThreads'))
else:
logger.info('No number of maximum Threads has been given, defaulting to 10')
maxThreads = 10
data['source'] = 'Combine'
data['method'] = 'trawl'
# initializing the Queue to the list of indicators in the harvest
ioc_queue = Queue()
for indicator in harvest:
ioc_queue.put(indicator)
total_iocs = ioc_queue.qsize()
for x in range(maxThreads):
th = threading.Thread(target=bale_CRITs_indicator, args=(base_url, data, ioc_queue))
th.start()
for x in threading.enumerate():
if x.name == "MainThread":
continue
x.join()
logger.info('Output %d indicators to CRITs using %d threads. Operation tool %d seconds\n' %
(total_iocs, maxThreads, time.time() - start_time))
def bale(input_file, output_file, output_format, is_regular):
config = ConfigParser.SafeConfigParser()
cfg_success = config.read('combine.cfg')
if not cfg_success:
logger.error('Baler: Could not read combine.cfg.')
logger.error('HINT: edit combine-example.cfg and save as combine.cfg.')
return
logger.info('Reading processed data from %s' % input_file)
with open(input_file, 'rb') as f:
harvest = json.load(f, encoding='utf8')
# TODO: also need plugins here (cf. #23)
if is_regular:
format_funcs = {'csv': bale_reg_csv, 'crits': bale_CRITs}
else:
format_funcs = {'csv': bale_enr_csv, 'crits': bale_CRITs}
format_funcs[output_format](harvest, output_file)
if __name__ == "__main__":
bale('crop.json', 'harvest.csv', 'csv', True)