Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX: history command #435

Merged
merged 14 commits into from
Jun 5, 2024
20 changes: 17 additions & 3 deletions pacu/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import random
import re
import readline
import shlex
import subprocess
import sys
Expand All @@ -16,7 +17,7 @@
from typing import List, Optional, Any, Dict, Union, Tuple

from pacu.core import lib
from pacu.core.lib import session_dir
from pacu.core.lib import session_dir, home_dir

try:
import jq # type: ignore
Expand All @@ -42,6 +43,10 @@
print('Refer to https://github.com/RhinoSecurityLabs/pacu/wiki/Installation')
sys.exit(1)

# arbitrary number, seems reasonable though
readline.set_history_length(200)
if os.path.isfile(f'{home_dir()}/command_history.txt') and os.access(f'{home_dir()}/command_history.txt', os.R_OK):
h00die marked this conversation as resolved.
Show resolved Hide resolved
readline.read_history_file(f'{home_dir()}/command_history.txt')

def load_categories() -> set:
categories = set()
Expand Down Expand Up @@ -126,6 +131,7 @@ def display_pacu_help():
swap_session <session name> Change the active Pacu session to another one in the database
delete_session Delete a Pacu session from the database. Note that the output
folder for that session will not be deleted
history List the previously typed commands

exit/quit Exit Pacu

Expand Down Expand Up @@ -179,7 +185,7 @@ class Main:
'aws', 'data', 'exec', 'exit', 'help', 'import_keys', 'assume_role', 'list', 'load_commands_file',
'ls', 'quit', 'regions', 'run', 'search', 'services', 'set_keys', 'set_regions',
'swap_keys', 'update_regions', 'set_ua_suffix', 'unset_ua_suffix', 'whoami', 'swap_session', 'sessions',
'list_sessions', 'delete_session', 'export_keys', 'open_console', 'console'
'list_sessions', 'delete_session', 'export_keys', 'open_console', 'console', 'history'
]

def __init__(self):
Expand Down Expand Up @@ -366,6 +372,11 @@ def get_regions(self, service, check_session=True) -> List[Optional[str]]:
else:
return valid_regions

def display_history(self):
# https://stackoverflow.com/a/7008316
for i in range(readline.get_current_history_length()):
print("{:>3}: {}".format(i+1, readline.get_history_item(i + 1)))

def display_all_regions(self):
for region in sorted(self.get_regions('all')):
print(' {}'.format(region))
Expand Down Expand Up @@ -595,6 +606,8 @@ def parse_command(self, command):
self.parse_commands_from_file(command)
elif command[0] == 'regions':
self.display_all_regions()
elif command[0] == 'history':
self.display_history()
elif command[0] == 'run' or command[0] == 'exec':
self.print_user_agent_suffix()
self.parse_exec_module_command(command)
Expand All @@ -620,6 +633,8 @@ def parse_command(self, command):
elif command[0] == 'whoami':
self.print_key_info()
elif command[0] == 'exit' or command[0] == 'quit':
# write out command history for loading later
readline.write_history_file(f'{home_dir()}/command_history.txt')
self.exit()
else:
print(' Error: Unrecognized command')
Expand Down Expand Up @@ -1577,7 +1592,6 @@ def get_boto3_resource(

def initialize_tab_completion(self) -> None:
try:
import readline
# Big thanks to samplebias: https://stackoverflow.com/a/5638688
MODULES = []
CATEGORIES = []
Expand Down
Loading