@@ -6740,3 +6740,72 @@ def stop_ssh_tunnel(data_set):
67406740
67416741 return "passed"
67426742
6743+ @logger
6744+ def proxy_server (data_set ):
6745+ import os
6746+ sModuleInfo = inspect .currentframe ().f_code .co_name + " : " + MODULE_NAME
6747+
6748+ proxy_var = None
6749+ action = None
6750+ port = 8080
6751+ for left , mid , right in data_set :
6752+ if left .lower ().strip () == 'action' :
6753+ action = 'start' if right .lower ().strip () == 'start' else 'stop'
6754+ if left .lower ().strip () == 'port' :
6755+ port = int (right .strip ())
6756+ if left .lower ().strip () == 'proxy server' :
6757+ proxy_var = right .strip ()
6758+
6759+ if action == None :
6760+ CommonUtil .ExecLog (sModuleInfo , "Incorrect dataset" , 3 )
6761+ return "zeuz_failed"
6762+
6763+
6764+ if action == 'start' :
6765+ CommonUtil .ExecLog (sModuleInfo , f"{ action .capitalize ()} ing proxy server on port { port } " , 1 )
6766+
6767+ proxy_log_dir = Path (sr .Get_Shared_Variables ("zeuz_download_folder" )).parent / 'proxy_log'
6768+ os .makedirs (proxy_log_dir , exist_ok = True )
6769+ mitm_proxy_path = Path (__file__ ).parent / "mitm_proxy.py"
6770+ output_file_path = proxy_log_dir / 'mitm.log' # Output file to save the logs
6771+ CommonUtil .ExecLog (sModuleInfo , f"Proxy Log file: { output_file_path } " , 1 )
6772+
6773+ captured_network_file_path = proxy_log_dir / 'captured_network_data.csv'
6774+ CommonUtil .ExecLog (sModuleInfo , f"Captured Network file: { output_file_path } " , 1 )
6775+ # Open the output file in append mode
6776+ with open (r'{}' .format (output_file_path ), 'a' ) as output_file :
6777+ # Start the subprocess
6778+ process = subprocess .Popen (
6779+ [
6780+ "mitmdump" ,
6781+ "-s" ,
6782+ f"{ mitm_proxy_path } " ,
6783+ "-p" ,
6784+ str (port ),
6785+ "--set" ,
6786+ f"output_file_path={ captured_network_file_path } " ,
6787+ ],
6788+ stdout = output_file , # Redirect stdout to the file
6789+ stderr = output_file , # Redirect stderr to the file
6790+ )
6791+
6792+ pid = process .pid
6793+ CommonUtil .mitm_proxy_pids .append (pid )
6794+ CommonUtil .ExecLog (sModuleInfo , f"Started process with PID: { pid } " , 1 )
6795+
6796+ sr .Set_Shared_Variables (proxy_var , {"pid" :pid ,"captured_network_file_path" :captured_network_file_path ,"log_file" :output_file_path })
6797+ return "passed"
6798+ else :
6799+ import signal
6800+
6801+ if CommonUtil .mitm_proxy_pids :
6802+ try :
6803+ pid = CommonUtil .mitm_proxy_pids [0 ]
6804+ os .kill (pid , signal .SIGTERM )
6805+ CommonUtil .ExecLog (sModuleInfo ,f"Process with PID { pid } has been terminated." ,1 )
6806+ CommonUtil .mitm_proxy_pids .pop ()
6807+ except OSError as e :
6808+ CommonUtil .ExecLog (sModuleInfo ,f"Error: { e } " , 3 )
6809+
6810+ CommonUtil .ExecLog (sModuleInfo , f"{ action .capitalize ()} ing proxy server on port { port } " , 1 )
6811+ return "passed"
0 commit comments