2023-04-14 14:43:07 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# This file is part of AIL framework - Analysis Information Leak framework
|
|
|
|
#
|
|
|
|
# This a simple feeder script feeding data from pystemon to AIL.
|
|
|
|
#
|
|
|
|
# Don't forget to set your pystemonpath and ensure that the
|
|
|
|
# configuration matches this script. Default is Redis DB 10.
|
|
|
|
# https://github.com/cvandeplas/pystemon/blob/master/pystemon.yaml#L52
|
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import redis
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from importer.abstract_importer import AbstractImporter
|
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
|
|
|
|
2023-10-11 12:06:01 +02:00
|
|
|
from lib.objects.Items import Item
|
|
|
|
|
2023-04-14 14:43:07 +02:00
|
|
|
class PystemonImporter(AbstractImporter):
|
|
|
|
def __init__(self, pystemon_dir, host='localhost', port=6379, db=10):
|
|
|
|
super().__init__()
|
|
|
|
# Check Pystemon Redis Config:
|
|
|
|
# https://github.com/cvandeplas/pystemon/blob/master/pystemon.yaml#L54
|
|
|
|
self.r_pystemon = redis.StrictRedis(host=host, port=port, db=db, decode_responses=True)
|
|
|
|
self.dir_pystemon = pystemon_dir
|
|
|
|
|
|
|
|
def importer(self):
|
|
|
|
item_id = self.r_pystemon.lpop("pastes")
|
|
|
|
print(item_id)
|
|
|
|
if item_id:
|
|
|
|
print(item_id)
|
2023-06-22 16:34:14 +02:00
|
|
|
full_item_path = os.path.join(self.dir_pystemon, item_id) # TODO SANITIZE PATH
|
2023-04-14 14:43:07 +02:00
|
|
|
# Check if pystemon file exists
|
|
|
|
if not os.path.isfile(full_item_path):
|
|
|
|
print(f'Error: {full_item_path}, file not found')
|
|
|
|
return None
|
|
|
|
# Get Item Content
|
|
|
|
try:
|
|
|
|
with open(full_item_path, 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
if not content:
|
|
|
|
return None
|
|
|
|
|
2023-06-22 16:34:14 +02:00
|
|
|
if full_item_path[-3:] == '.gz':
|
|
|
|
gzipped = True
|
|
|
|
else:
|
|
|
|
gzipped = False
|
|
|
|
|
2023-10-05 16:24:28 +02:00
|
|
|
# TODO handle multiple objects
|
2023-10-11 12:06:01 +02:00
|
|
|
source = 'pystemon'
|
|
|
|
message = self.create_message(content, gzipped=gzipped, source=source)
|
|
|
|
self.logger.info(f'{source} {item_id}')
|
|
|
|
return item_id, message
|
2023-06-02 11:03:32 +02:00
|
|
|
|
2023-04-14 14:43:07 +02:00
|
|
|
except IOError as e:
|
2023-10-11 12:06:01 +02:00
|
|
|
self.logger.error(f'Error {e}: {full_item_path}, IOError')
|
2023-04-14 14:43:07 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
class PystemonModuleImporter(AbstractModule):
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.pending_seconds = 10
|
|
|
|
config_loader = ConfigLoader()
|
|
|
|
# TODO MIGRATE OLD CONFIG
|
|
|
|
# dir_pystemon = config_loader.get_config_str("Directories", "pystemonpath")
|
|
|
|
# Check Pystemon Redis Config:
|
|
|
|
# https://github.com/cvandeplas/pystemon/blob/master/pystemon.yaml#L54
|
|
|
|
dir_pystemon = config_loader.get_config_str("Pystemon", "dir")
|
|
|
|
host = config_loader.get_config_str("Pystemon", "redis_host")
|
|
|
|
port = config_loader.get_config_str("Pystemon", "redis_port")
|
|
|
|
db = config_loader.get_config_str("Pystemon", "redis_db")
|
|
|
|
self.importer = PystemonImporter(dir_pystemon, host=host, port=port, db=db)
|
|
|
|
|
|
|
|
def get_message(self):
|
|
|
|
return self.importer.importer()
|
|
|
|
|
|
|
|
def compute(self, message):
|
2023-10-11 12:06:01 +02:00
|
|
|
if message:
|
|
|
|
item_id, message = message
|
|
|
|
item = Item(item_id)
|
|
|
|
self.add_message_to_queue(obj=item, message=message)
|
2023-04-14 14:43:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
module = PystemonModuleImporter()
|
|
|
|
module.run()
|