2018-03-07 18:07:40 +01:00
|
|
|
#!/usr/bin/env python3
|
2017-08-24 16:02:28 +02:00
|
|
|
|
2017-10-25 10:41:46 +02:00
|
|
|
import argparse
|
2019-05-29 01:30:57 +02:00
|
|
|
import configparser
|
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
import logging
|
2017-09-11 14:53:06 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2019-05-29 01:30:57 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
import redis
|
|
|
|
import zmq
|
2017-08-24 16:02:28 +02:00
|
|
|
|
2018-03-31 12:36:17 +02:00
|
|
|
configfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'config/config.cfg')
|
2017-09-11 14:53:06 +02:00
|
|
|
cfg = configparser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
2017-12-05 10:23:40 +01:00
|
|
|
logDir = cfg.get('Log', 'directory')
|
2019-05-29 03:41:43 +02:00
|
|
|
logfilename = cfg.get('Log', 'subscriber_filename')
|
2017-12-05 10:23:40 +01:00
|
|
|
logPath = os.path.join(logDir, logfilename)
|
|
|
|
if not os.path.exists(logDir):
|
|
|
|
os.makedirs(logDir)
|
2019-05-29 02:48:26 +02:00
|
|
|
try:
|
|
|
|
logging.basicConfig(filename=logPath, filemode='a', level=logging.INFO)
|
|
|
|
except PermissionError as error:
|
|
|
|
print(error)
|
|
|
|
print("Please fix the above and try again.")
|
|
|
|
sys.exit(126)
|
2017-12-05 09:56:32 +01:00
|
|
|
logger = logging.getLogger('zmq_subscriber')
|
2017-12-04 16:44:44 +01:00
|
|
|
|
2017-10-25 12:12:14 +02:00
|
|
|
CHANNEL = cfg.get('RedisLog', 'channel')
|
2017-11-30 08:17:53 +01:00
|
|
|
LISTNAME = cfg.get('RedisLIST', 'listName')
|
2017-11-03 09:32:07 +01:00
|
|
|
|
2017-11-30 08:17:53 +01:00
|
|
|
serv_list = redis.StrictRedis(
|
2017-10-27 16:36:27 +02:00
|
|
|
host=cfg.get('RedisGlobal', 'host'),
|
|
|
|
port=cfg.getint('RedisGlobal', 'port'),
|
2019-06-19 11:32:06 +02:00
|
|
|
db=cfg.getint('RedisLIST', 'db'),
|
|
|
|
decode_responses=True)
|
2017-09-11 14:53:06 +02:00
|
|
|
|
|
|
|
|
2017-11-08 17:37:03 +01:00
|
|
|
###############
|
|
|
|
## MAIN LOOP ##
|
|
|
|
###############
|
|
|
|
|
2017-11-30 08:17:53 +01:00
|
|
|
def put_in_redis_list(zmq_name, content):
|
2017-11-30 16:04:03 +01:00
|
|
|
content = content.decode('utf8')
|
2017-11-30 08:17:53 +01:00
|
|
|
to_add = {'zmq_name': zmq_name, 'content': content}
|
2017-11-30 16:04:03 +01:00
|
|
|
serv_list.lpush(LISTNAME, json.dumps(to_add))
|
2017-12-04 16:44:44 +01:00
|
|
|
logger.debug('Pushed: {}'.format(json.dumps(to_add)))
|
2017-10-13 15:03:09 +02:00
|
|
|
|
2019-02-26 15:45:38 +01:00
|
|
|
def main(zmqName, zmqurl):
|
2017-10-25 12:12:14 +02:00
|
|
|
context = zmq.Context()
|
|
|
|
socket = context.socket(zmq.SUB)
|
2019-02-26 15:45:38 +01:00
|
|
|
socket.connect(zmqurl)
|
2017-10-25 12:12:14 +02:00
|
|
|
socket.setsockopt_string(zmq.SUBSCRIBE, '')
|
|
|
|
|
2017-10-13 15:03:09 +02:00
|
|
|
while True:
|
2017-11-27 10:05:28 +01:00
|
|
|
try:
|
|
|
|
content = socket.recv()
|
2017-11-30 08:17:53 +01:00
|
|
|
put_in_redis_list(zmqName, content)
|
2019-02-26 15:45:38 +01:00
|
|
|
print(zmqName, content)
|
2017-11-27 10:05:28 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
2019-06-17 11:41:14 +02:00
|
|
|
except Exception as e:
|
|
|
|
logger.warning('Error:' + str(e))
|
2017-10-13 15:03:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-10-25 10:41:46 +02:00
|
|
|
|
2019-02-26 15:45:38 +01:00
|
|
|
parser = argparse.ArgumentParser(description='A zmq subscriber. It subscribes to a ZMQ then redispatch it to the misp-dashboard')
|
2017-10-27 08:49:47 +02:00
|
|
|
parser.add_argument('-n', '--name', required=False, dest='zmqname', help='The ZMQ feed name', default="MISP Standard ZMQ")
|
2019-02-26 15:45:38 +01:00
|
|
|
parser.add_argument('-u', '--url', required=False, dest='zmqurl', help='The URL to connect to', default="tcp://localhost:50000")
|
2017-10-25 10:41:46 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2019-05-29 02:48:26 +02:00
|
|
|
try:
|
2019-06-21 15:32:59 +02:00
|
|
|
main(args.zmqname, args.zmqurl)
|
2019-05-29 02:48:26 +02:00
|
|
|
except redis.exceptions.ResponseError as error:
|
|
|
|
print(error)
|