/
ThugHPFeeds.py
186 lines (148 loc) · 5.95 KB
/
ThugHPFeeds.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
#!/usr/bin/env python
#
# ThugHPFeeds.py
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
import os
import sys
import time
import hashlib
import datetime
import json
import logging
import threading
import ConfigParser
import hpfeeds
class ThugFiles(threading.Thread):
def __init__(self, opts):
self.opts = opts
self.logging_init()
threading.Thread.__init__(self)
def logging_init(self):
if self.opts['logdir'] is None:
return
if not os.path.exists(self.opts['logdir']):
os.mkdir(self.opts['logdir'])
self.log = logging.getLogger("ThugFiles.HPFeeds")
handler = logging.FileHandler(os.path.join(self.opts['logdir'], 'thugfiles.log'))
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.log.setLevel(logging.INFO)
def run(self):
def on_message(identifier, channel, payload):
try:
decoded = json.loads(str(payload))
except:
decoded = {'raw': payload}
if not 'md5' in decoded or not 'data' in decoded:
self.log.info("Received message does not contain hash or data - Ignoring it")
return
csv = ', '.join(['{0} = {1}'.format(i, decoded[i]) for i in ['md5', 'sha1', 'type']])
outmsg = 'PUBLISH channel = %s, identifier = %s, %s' % (channel, identifier, csv)
self.log.info(outmsg)
if self.opts['logdir'] is None:
return
filedata = decoded['data'].decode('base64')
fpath = os.path.join(self.opts['logdir'], decoded['md5'])
with open(fpath, 'wb') as fd:
fd.write(filedata)
def on_error(payload):
self.log.critical("Error message from server: %s" % (payload, ))
self.hpc.stop()
while True:
try:
self.hpc = hpfeeds.new(self.opts['host'], int(self.opts['port']), self.opts['ident'], self.opts['secret'])
self.log.info("Connected to %s" % (self.hpc.brokername, ))
self.hpc.subscribe([self.opts['channel'], ])
except hpfeeds.FeedException:
break
try:
self.hpc.run(on_message, on_error)
except:
self.hpc.close()
time.sleep(20)
class ThugEvents(threading.Thread):
def __init__(self, opts):
self.opts = opts
self.logging_init()
threading.Thread.__init__(self)
def logging_init(self):
if self.opts['logdir'] is None:
return
if not os.path.exists(self.opts['logdir']):
os.mkdir(self.opts['logdir'])
self.log = logging.getLogger("ThugEvents.HPFeeds")
handler = logging.FileHandler(os.path.join(self.opts['logdir'], 'thugevents.log'))
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.log.setLevel(logging.INFO)
def run(self):
def on_message(identifier, channel, payload):
m = hashlib.md5()
m.update(payload)
outmsg = 'PUBLISH channel = %s, identifier = %s, MAEC = %s' % (channel, identifier, m.hexdigest())
self.log.info(outmsg)
if self.opts['logdir'] is None:
return
fpath = os.path.join(self.opts['logdir'], m.hexdigest())
with open(fpath, 'wb') as fd:
fd.write(payload)
def on_error(payload):
self.log.critical("Error message from server: %s" % (payload, ))
self.hpc.stop()
while True:
try:
self.hpc = hpfeeds.new(self.opts['host'], int(self.opts['port']), self.opts['ident'], self.opts['secret'])
self.log.info("Connected to %s" % (self.hpc.brokername, ))
self.hpc.subscribe(self.opts['channel'])
except hpfeeds.FeedException:
break
try:
self.hpc.run(on_message, on_error)
except:
self.hpc.close()
time.sleep(20)
class ThugHPFeeds:
def __init__(self):
self.events_opts = dict()
self.files_opts = dict()
self.config_init()
def config_init(self):
config = ConfigParser.ConfigParser()
conf_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'hpfeeds.conf')
config.read(conf_file)
for option in config.options('HPFeeds'):
opt = config.get('HPFeeds', option)
self.events_opts[option] = opt
self.files_opts[option] = opt
for option in config.options('ThugFiles'):
self.files_opts[option] = config.get('ThugFiles', option)
for option in config.options('ThugEvents'):
self.events_opts[option] = config.get('ThugEvents', option)
def run(self):
if self.files_opts['enable']:
files = ThugFiles(self.files_opts)
files.start()
if self.events_opts['enable']:
events = ThugEvents(self.events_opts)
events.start()
if __name__ == '__main__':
try:
f = ThugHPFeeds()
f.run()
except KeyboardInterrupt:
sys.exit(0)