diff --git a/fakenet/diverters/diverterbase.py b/fakenet/diverters/diverterbase.py index 7ebe8bd..3edd9f6 100644 --- a/fakenet/diverters/diverterbase.py +++ b/fakenet/diverters/diverterbase.py @@ -1040,9 +1040,16 @@ def parse_diverter_config(self): sys.exit(1) if self.is_set('dumppackets'): - self.pcap_filename = '%s_%s.pcap' % (self.getconfigval( + if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): + output_dir = os.path.dirname(sys.executable) + else: + output_dir = os.path.dirname(__file__) + + pcap_filename = '%s_%s.pcap' % (self.getconfigval( 'dumppacketsfileprefix', 'packets'), time.strftime('%Y%m%d_%H%M%S')) + + self.pcap_filename = os.path.join(output_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) @@ -1956,7 +1963,7 @@ def generate_html_report(self): """ if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): # Inside a Pyinstaller bundle - fakenet_dir_path = os.getcwd() + fakenet_dir_path = os.path.dirname(sys.executable) else: fakenet_dir_path = os.fspath(Path(__file__).parents[1]) @@ -1967,11 +1974,12 @@ def generate_html_report(self): timestamp = time.strftime('%Y%m%d_%H%M%S') output_filename = f"report_{timestamp}.html" + output_file_path = os.path.join(fakenet_dir_path, output_filename) - with open(output_filename, "w") as output_file: + with open(output_file_path, "w") as output_file: output_file.write(template.render(nbis=self.nbis)) - self.logger.info(f"Generated new HTML report: {output_filename}") + self.logger.info(f"Generated new HTML report: {output_file_path}") 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..77dcc52 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 @@ -43,4 +43,4 @@ def abs_config_path(path): if os.path.exists(relpath): return os.path.abspath(relpath) - return None \ No newline at end of file + return None diff --git a/fakenet/listeners/TFTPListener.py b/fakenet/listeners/TFTPListener.py index 4a40f45..ff76b4d 100644 --- a/fakenet/listeners/TFTPListener.py +++ b/fakenet/listeners/TFTPListener.py @@ -211,8 +211,12 @@ 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(), - safe_file) + + if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'): + output_dir = os.path.dirname(sys.executable) + else: + output_dir = os.path.dirname(__file__) + output_file = ListenerBase.safe_join(output_dir, safe_file) f = open(output_file, 'ab') f.write(data[4:]) f.close()