<commit_before>#!/usr/bin/env python3
# -*-coding:UTF-8 -*
"""
    Decoder module

    Dectect Binary and decode it
"""

##################################
# Import External packages
##################################
import time
import os
import redis
import base64
from hashlib import sha1
import magic
import json
import datetime
from pubsublogger import publisher
import re
import signal
from lib import Decoded


##################################
# Import Project packages
##################################
from module.abstract_module import AbstractModule
from Helper import Process
from packages import Item


class TimeoutException(Exception):
    pass

def timeout_handler(signum, frame):
    raise TimeoutException

signal.signal(signal.SIGALRM, timeout_handler)


class Decoder(AbstractModule):
    """
    Decoder module for AIL framework
    """

    # TODO to lambda expr
    def hex_decoder(self, hexStr):
        #hexStr = ''.join( hex_string.split(" ") )
        return bytes(bytearray([int(hexStr[i:i+2], 16) for i in range(0, len(hexStr), 2)]))


    # TODO to lambda expr
    def binary_decoder(self, binary_string):
        return bytes(bytearray([int(binary_string[i:i+8], 2) for i in range(0, len(binary_string), 8)]))


    # TODO to lambda expr
    def base64_decoder(self, base64_string):
        return base64.b64decode(base64_string)


    def __init__(self):
        super(Decoder, self).__init__(logger_channel="script:decoder")

        serv_metadata = redis.StrictRedis(
            host=self.process.config.get("ARDB_Metadata", "host"),
            port=self.process.config.getint("ARDB_Metadata", "port"),
            db=self.process.config.getint("ARDB_Metadata", "db"),
            decode_responses=True)

        regex_binary = '[0-1]{40,}'
        #regex_hex = '(0[xX])?[A-Fa-f0-9]{40,}'
        regex_hex = '[A-Fa-f0-9]{40,}'
        regex_base64 = '(?:[A-Za-z0-9+/]{4}){2,}(?:[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=|[A-Za-z0-9+/][AQgw]==)'

        cmp_regex_binary = re.compile(regex_binary)
        cmp_regex_hex = re.compile(regex_hex)
        cmp_regex_base64 = re.compile(regex_base64)

        # map decoder function
        self.decoder_function = {'binary':self.binary_decoder,'hexadecimal':self.hex_decoder, 'base64':self.base64_decoder}

        hex_max_execution_time = self.process.config.getint("Hex", "max_execution_time")
        binary_max_execution_time = self.process.config.getint("Binary", "max_execution_time")
        base64_max_execution_time = self.process.config.getint("Base64", "max_execution_time")

        # list all decoder with regex,
        decoder_binary = {'name': 'binary', 'regex': cmp_regex_binary, 'encoded_min_size': 300, 'max_execution_time': binary_max_execution_time}
        decoder_hexadecimal = {'name': 'hexadecimal', 'regex': cmp_regex_hex, 'encoded_min_size': 300, 'max_execution_time': hex_max_execution_time}
        decoder_base64 = {'name': 'base64', 'regex': cmp_regex_base64, 'encoded_min_size': 40, 'max_execution_time': base64_max_execution_time}
<commit_msg>fix: replace redis init with generic ConfigLoader<commit_after>#!/usr/bin/env python3
# -*-coding:UTF-8 -*
"""
    Decoder module

    Dectect Binary and decode it
"""

##################################
# Import External packages
##################################
import time
import os
import redis
import base64
from hashlib import sha1
import magic
import json
import datetime
from pubsublogger import publisher
import re
import signal
from lib import Decoded


##################################
# Import Project packages
##################################
from module.abstract_module import AbstractModule
from Helper import Process
from packages import Item
import ConfigLoader


class TimeoutException(Exception):
    pass

def timeout_handler(signum, frame):
    raise TimeoutException

signal.signal(signal.SIGALRM, timeout_handler)


class Decoder(AbstractModule):
    """
    Decoder module for AIL framework
    """

    # TODO to lambda expr
    def hex_decoder(self, hexStr):
        #hexStr = ''.join( hex_string.split(" ") )
        return bytes(bytearray([int(hexStr[i:i+2], 16) for i in range(0, len(hexStr), 2)]))


    # TODO to lambda expr
    def binary_decoder(self, binary_string):
        return bytes(bytearray([int(binary_string[i:i+8], 2) for i in range(0, len(binary_string), 8)]))


    # TODO to lambda expr
    def base64_decoder(self, base64_string):
        return base64.b64decode(base64_string)


    def __init__(self):
        super(Decoder, self).__init__()

        serv_metadata = ConfigLoader.ConfigLoader().get_redis_conn("ARDB_Metadata")

        regex_binary = '[0-1]{40,}'
        #regex_hex = '(0[xX])?[A-Fa-f0-9]{40,}'
        regex_hex = '[A-Fa-f0-9]{40,}'
        regex_base64 = '(?:[A-Za-z0-9+/]{4}){2,}(?:[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=|[A-Za-z0-9+/][AQgw]==)'

        cmp_regex_binary = re.compile(regex_binary)
        cmp_regex_hex = re.compile(regex_hex)
        cmp_regex_base64 = re.compile(regex_base64)

        # map decoder function
        self.decoder_function = {'binary':self.binary_decoder,'hexadecimal':self.hex_decoder, 'base64':self.base64_decoder}

        hex_max_execution_time = self.process.config.getint("Hex", "max_execution_time")
        binary_max_execution_time = self.process.config.getint("Binary", "max_execution_time")
        base64_max_execution_time = self.process.config.getint("Base64", "max_execution_time")

        # list all decoder with regex,
        decoder_binary = {'name': 'binary', 'regex': cmp_regex_binary, 'encoded_min_size': 300, 'max_execution_time': binary_max_execution_time}
        decoder_hexadecimal = {'name': 'hexadecimal', 'regex': cmp_regex_hex, 'encoded_min_size': 300, 'max_execution_time': hex_max_execution_time}
        decoder_base64 = {'name': 'base64', 'regex': cmp_regex_base64, 'encoded_min_size': 40, 'max_execution_time': base64_max_execution_time}

{'fraction_non_alphanumeric': 0.16722802290333447, 'fraction_numerical': 0.026608285618053215, 'mean_word_length': 4.274422735346358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5061037', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1875, 'n_words': 387}