From 824e945bb57d29cf97218a8511ee4fe025059075 Mon Sep 17 00:00:00 2001 From: Elliot Chernofsky Date: Wed, 22 May 2024 18:11:13 -0400 Subject: [PATCH] Grab config from execution dir --- fakenet/diverters/diverterbase.py | 7 ++++++- fakenet/fakenet.py | 2 +- fakenet/listeners/ListenerBase.py | 2 +- fakenet/listeners/TFTPListener.py | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/fakenet/diverters/diverterbase.py b/fakenet/diverters/diverterbase.py index 7ebe8bd..f542629 100644 --- a/fakenet/diverters/diverterbase.py +++ b/fakenet/diverters/diverterbase.py @@ -1040,9 +1040,14 @@ def parse_diverter_config(self): sys.exit(1) if self.is_set('dumppackets'): - self.pcap_filename = '%s_%s.pcap' % (self.getconfigval( + pcap_filename = '%s_%s.pcap' % (self.getconfigval( 'dumppacketsfileprefix', 'packets'), time.strftime('%Y%m%d_%H%M%S')) + if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): + out_dir = os.path.dirname(sys.executable) + else: + out_dir = os.path.dirname(__file__) + self.pcap_filename = os.path.join(out_dir, pcap_filename) self.logger.info('Capturing traffic to %s', self.pcap_filename) self.pcap = dpkt.pcap.Writer(open(self.pcap_filename, 'wb'), linktype=dpkt.pcap.DLT_RAW) diff --git a/fakenet/fakenet.py b/fakenet/fakenet.py index 850d1c8..ba81303 100644 --- a/fakenet/fakenet.py +++ b/fakenet/fakenet.py @@ -64,7 +64,7 @@ def __init__(self, logging_level = logging.INFO): def parse_config(self, config_filename): # Handling Pyinstaller bundle scenario: https://pyinstaller.org/en/stable/runtime-information.html if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): - dir_path = os.getcwd() + dir_path = os.path.dirname(sys.executable) else: dir_path = os.path.dirname(__file__) diff --git a/fakenet/listeners/ListenerBase.py b/fakenet/listeners/ListenerBase.py index 19dd22e..f0db49a 100644 --- a/fakenet/listeners/ListenerBase.py +++ b/fakenet/listeners/ListenerBase.py @@ -34,7 +34,7 @@ def abs_config_path(path): return abspath if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): - relpath = os.path.join(os.getcwd(), path) + relpath = os.path.join(os.path.dirname(sys.executable), path) else: # Try to locate the location relative to application path diff --git a/fakenet/listeners/TFTPListener.py b/fakenet/listeners/TFTPListener.py index 4a40f45..2096279 100644 --- a/fakenet/listeners/TFTPListener.py +++ b/fakenet/listeners/TFTPListener.py @@ -211,7 +211,7 @@ def handle_data(self, socket, data): if hasattr(self.server, 'filename_path') and self.server.filename_path: safe_file = self.server.tftp_file_prefix + "_" + urllib.parse.quote(self.server.filename_path, '') - output_file = ListenerBase.safe_join(os.getcwd(), + output_file = ListenerBase.safe_join(os.path.dirname(sys.executable), safe_file) f = open(output_file, 'ab') f.write(data[4:])