Skip to content

Commit 5da577f

Browse files
committed
Optimise export code
1 parent bb95e0f commit 5da577f

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

nxc/modules/powershell_history.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import traceback
2-
import os
2+
from os import makedirs
3+
from os.path import join, abspath
4+
from nxc.paths import NXC_PATH
35

46

57
class NXCModule:
@@ -12,9 +14,9 @@ class NXCModule:
1214
multiple_hosts = True
1315

1416
def options(self, context, module_options):
15-
"""To export all the history you can add the following option: -o export=enable"""
17+
"""To export all the history you can add the following option: -o export=True"""
1618
context.log.info(f"Received module options: {module_options}")
17-
self.export = module_options.get("EXPORT", "disable").lower()
19+
self.export = bool(module_options.get("EXPORT", False))
1820
context.log.info(f"Option export set to: {self.export}")
1921

2022
def analyze_history(self, history):
@@ -51,18 +53,19 @@ def on_admin_login(self, context, connection):
5153

5254
# Check if export is enabled
5355
context.log.info(f"Export option is set to: {self.export}")
54-
if self.export == "enable":
56+
if self.export:
5557
host = connection.host # Assuming 'host' contains the target IP or hostname
56-
filename = f"{host}.powershell_history.txt"
57-
context.log.info(f"Export enabled, writing history to {filename}")
58+
filename = f"{host}_powershell_history.txt"
59+
export_path = join(NXC_PATH, "modules", "powershell_history")
60+
path = abspath(join(export_path, filename))
61+
makedirs(export_path, exist_ok=True)
62+
63+
context.log.info(f"Export enabled, writing history to {path}")
5864
try:
59-
with open(filename, "w") as file:
65+
with open(path, "w") as file:
6066
for cmd in history:
6167
file.write(cmd + "\n")
62-
context.log.info(f"History written to {filename}")
63-
# Print the full path to the file
64-
full_path = os.path.abspath(filename)
65-
print(f"PowerShell history written to: {full_path}")
68+
context.log.highlight(f"PowerShell history written to: {path}")
6669
except Exception as e:
6770
context.log.fail(f"Failed to write history to {filename}: {e}")
6871

0 commit comments

Comments
 (0)