<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import signal <EOL> import socket <EOL> import logging <EOL> import multiprocessing <EOL> import subprocess <EOL> import tornado <EOL> from framework . dependency_management . dependency_resolver import BaseComponent <EOL> from framework . dependency_management . component_initialiser import ComponentInitialiser <EOL> from framework . utils import FileOperations , catch_io_errors , OutputCleaner , OWTFLogger <EOL> from framework . interface import server , cli <EOL> from framework . http . proxy import proxy , transaction_logger <EOL> from framework . plugin import worker_manager <EOL> from framework . lib . formatters import ConsoleFormatter , FileFormatter <EOL> class Core ( BaseComponent ) : <EOL> """<STR_LIT>""" <EOL> COMPONENT_NAME = "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . register_in_service_locator ( ) <EOL> self . file_handler = catch_io_errors ( logging . FileHandler ) <EOL> self . db = self . get_component ( "<STR_LIT>" ) <EOL> self . config = self . get_component ( "<STR_LIT>" ) <EOL> self . db_config = self . get_component ( "<STR_LIT>" ) <EOL> self . error_handler = self . get_component ( "<STR_LIT>" ) <EOL> self . create_dirs ( ) <EOL> self . pnh_log_file ( ) <EOL> self . enable_logging ( ) <EOL> self . tor_process = None <EOL> def create_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( self . config . FrameworkConfigGetLogsDir ( ) ) : <EOL> FileOperations . create_missing_dirs ( self . config . FrameworkConfigGetLogsDir ( ) ) <EOL> self . create_temp_storage_dirs ( ) <EOL> def create_temp_storage_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp_dir = os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not os . path . exists ( tmp_dir ) : <EOL> tmp_dir = os . path . join ( tmp_dir , str ( self . config . OwtfPid ) ) <EOL> if not os . path . exists ( tmp_dir ) : <EOL> FileOperations . make_dirs ( tmp_dir ) <EOL> def clean_temp_storage_dirs ( self ) : <EOL> """<STR_LIT>""" <EOL> curr_tmp_dir = os . path . join ( '<STR_LIT>' , '<STR_LIT>' , str ( self . config . OwtfPid ) ) <EOL> new_tmp_dir = os . path . join ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' % self . config . OwtfPid ) <EOL> if os . path . exists ( curr_tmp_dir ) and os . access ( curr_tmp_dir , os . W_OK ) : <EOL> os . rename ( curr_tmp_dir , new_tmp_dir ) <EOL> def pnh_log_file ( self ) : <EOL> self . path = self . config . FrameworkConfigGet ( '<STR_LIT>' ) <EOL> self . mode = "<STR_LIT:w>" <EOL> try : <EOL> if os . path . isfile ( self . path ) : <EOL> pass <EOL> else : <EOL> with FileOperations . open ( self . path , self . mode , owtf_clean = False ) : <EOL> pass <EOL> except IOError as e : <EOL> OWTFLogger . log ( "<STR_LIT>" . format ( e . errno , e . strerror ) ) <EOL> raise <EOL> def write_event ( self , content , mode ) : <EOL> self . content = content <EOL> self . mode = mode <EOL> self . file_path = self . config . FrameworkConfigGet ( '<STR_LIT>' ) <EOL> if ( os . path . isfile ( self . file_path ) and os . access ( self . file_path , os . W_OK ) ) : <EOL> try : <EOL> with FileOperations . open ( self . file_path , self . mode , owtf_clean = False ) as log_file : <EOL> log_file . write ( self . content ) <EOL> log_file . write ( "<STR_LIT:\n>" ) <EOL> return True <EOL> except IOError : <EOL> return False <EOL> def get_command ( self , argv ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:U+0020>" . join ( argv ) . replace ( argv [ <NUM_LIT:0> ] , os . path . basename ( argv [ <NUM_LIT:0> ] ) ) <EOL> def start_botnet_mode ( self , options ) : <EOL> ComponentInitialiser . intialise_proxy_manager ( options ) <EOL> def start_proxy ( self , options ) : <EOL> if True : <EOL> try : <EOL> temp_socket = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> temp_socket . bind ( ( <EOL> self . db_config . Get ( '<STR_LIT>' ) , <EOL> int ( self . db_config . Get ( '<STR_LIT>' ) ) ) ) <EOL> temp_socket . close ( ) <EOL> except socket . error : <EOL> self . error_handler . FrameworkAbort ( <EOL> "<STR_LIT>" + <EOL> self . db_config . Get ( '<STR_LIT>' ) + "<STR_LIT::>" + <EOL> self . db_config . Get ( "<STR_LIT>" ) + <EOL> "<STR_LIT>" ) <EOL> self . ProxyProcess = proxy . ProxyProcess ( ) <EOL> self . ProxyProcess . initialize ( <EOL> options [ '<STR_LIT>' ] , <EOL> options [ '<STR_LIT>' ] <EOL> ) <EOL> self . TransactionLogger = transaction_logger . TransactionLogger ( <EOL> cache_dir = self . db_config . Get ( '<STR_LIT>' ) <EOL> ) <EOL> logging . warn ( <EOL> "<STR_LIT>" , <EOL> self . db_config . Get ( '<STR_LIT>' ) , <EOL> self . db_config . Get ( "<STR_LIT>" ) ) <EOL> self . ProxyProcess . start ( ) <EOL> logging . debug ( "<STR_LIT>" ) <EOL> self . TransactionLogger . start ( ) <EOL> logging . debug ( <EOL> "<STR_LIT>" , <EOL> self . db_config . Get ( "<STR_LIT>" ) ) <EOL> else : <EOL> ComponentInitialiser . initialisation_phase_3 ( options [ '<STR_LIT>' ] ) <EOL> def enable_logging ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> process_name = kwargs . get ( <EOL> "<STR_LIT>" , <EOL> multiprocessing . current_process ( ) . name <EOL> ) <EOL> logger = logging . getLogger ( ) <EOL> logger . setLevel ( logging . DEBUG ) <EOL> file_handler = self . file_handler ( <EOL> self . config . FrameworkConfigGetLogPath ( process_name ) , <EOL> mode = "<STR_LIT>" <EOL> ) <EOL> file_handler . setLevel ( logging . DEBUG ) <EOL> file_handler . setFormatter ( FileFormatter ( ) ) <EOL> stream_handler = logging . StreamHandler ( sys . stdout ) <EOL> stream_handler . setLevel ( logging . INFO ) <EOL> stream_handler . setFormatter ( ConsoleFormatter ( ) ) <EOL> logger . handlers = [ file_handler , stream_handler ] <EOL> def disable_console_logging ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( ) <EOL> if isinstance ( logger . handlers [ - <NUM_LIT:1> ] , logging . StreamHandler ) : <EOL> logger . removeHandler ( logger . handlers [ - <NUM_LIT:1> ] ) <EOL> def start ( self , options ) : <EOL> """<STR_LIT>""" <EOL> if self . initialise_framework ( options ) : <EOL> if not options [ '<STR_LIT>' ] : <EOL> return self . run_server ( ) <EOL> else : <EOL> return self . run_cli ( ) <EOL> def initialise_framework ( self , options ) : <EOL> self . ProxyMode = options [ "<STR_LIT>" ] <EOL> logging . info ( "<STR_LIT>" ) <EOL> proxy_infos = [ self . db_config . Get ( '<STR_LIT>' ) , self . db_config . Get ( '<STR_LIT>' ) ] <EOL> ComponentInitialiser . initialisation_phase_3 ( proxy_infos , options ) <EOL> self . initialise_plugin_handler_and_params ( options ) <EOL> if options [ '<STR_LIT>' ] : <EOL> self . PluginHandler . show_plugin_list ( options [ '<STR_LIT>' ] ) <EOL> self . finish ( ) <EOL> self . config . ProcessOptions ( options ) <EOL> command = self . get_command ( options [ '<STR_LIT>' ] ) <EOL> self . start_botnet_mode ( options ) <EOL> self . start_proxy ( options ) <EOL> self . error_handler . SetCommand ( OutputCleaner . anonymise_command ( command ) ) <EOL> return True <EOL> def initialise_plugin_handler_and_params ( self , options ) : <EOL> self . PluginHandler = self . get_component ( "<STR_LIT>" ) <EOL> self . PluginParams = self . get_component ( "<STR_LIT>" ) <EOL> self . WorkerManager = worker_manager . WorkerManager ( <EOL> keep_working = not options [ '<STR_LIT>' ] ) <EOL> def run_server ( self ) : <EOL> """<STR_LIT>""" <EOL> self . interface_server = server . InterfaceServer ( ) <EOL> logging . warn ( <EOL> "<STR_LIT>" , <EOL> self . config . FrameworkConfigGet ( "<STR_LIT>" ) , <EOL> self . config . FrameworkConfigGet ( "<STR_LIT>" ) ) <EOL> logging . info ( "<STR_LIT>" ) <EOL> self . disable_console_logging ( ) <EOL> self . interface_server . start ( ) <EOL> self . FileServer = server . FileServer ( ) <EOL> self . FileServer . start ( ) <EOL> def run_cli ( self ) : <EOL> """<STR_LIT>""" <EOL> self . cli_server = cli . CliServer ( ) <EOL> self . cli_server . start ( ) <EOL> def finish ( self ) : <EOL> """<STR_LIT>""" <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . TOR_process . terminate ( ) <EOL> else : <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . PluginHandler . clean_up ( ) <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> logging . info ( <EOL> "<STR_LIT>" ) <EOL> self . ProxyProcess . clean_up ( ) <EOL> self . kill_children ( self . ProxyProcess . pid ) <EOL> self . ProxyProcess . join ( ) <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . TransactionLogger . poison_q . put ( '<STR_LIT>' ) <EOL> self . TransactionLogger . join ( ) <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . WorkerManager . clean_up ( ) <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . db . clean_up ( ) <EOL> if getattr ( self , "<STR_LIT>" , None ) is not None : <EOL> self . cli_server . clean_up ( ) <EOL> tornado . ioloop . IOLoop . instance ( ) . stop ( ) <EOL> exit ( <NUM_LIT:0> ) <EOL> def kill_children ( self , parent_pid , sig = signal . SIGINT ) : <EOL> ps_command = subprocess . Popen ( <EOL> "<STR_LIT>" % parent_pid , <EOL> shell = True , <EOL> stdout = subprocess . PIPE ) <EOL> ps_output = ps_command . stdout . read ( ) <EOL> for pid_str in ps_output . split ( "<STR_LIT:\n>" ) [ : - <NUM_LIT:1> ] : <EOL> self . kill_children ( int ( pid_str ) , sig ) <EOL> try : <EOL> os . kill ( int ( pid_str ) , sig ) <EOL> except : <EOL> logging . warning ( "<STR_LIT>" , pid_str ) </s>
