Agent: Improve the commandline argument parsing

This commit is contained in:
vakarisz 2022-05-30 15:58:35 +03:00
parent 8d225b5c1b
commit 7310cc0b15
3 changed files with 14 additions and 13 deletions

View File

@ -46,8 +46,7 @@ class MonkeyDrops(object):
arg_parser.add_argument("-d", "--depth", type=int) arg_parser.add_argument("-d", "--depth", type=int)
arg_parser.add_argument("-l", "--location") arg_parser.add_argument("-l", "--location")
arg_parser.add_argument("-vp", "--vulnerable-port") arg_parser.add_argument("-vp", "--vulnerable-port")
self.monkey_args = args[1:] self.opts = arg_parser.parse_args(args)
self.opts, _ = arg_parser.parse_known_args(args)
self._config = { self._config = {
"source_path": os.path.abspath(sys.argv[0]), "source_path": os.path.abspath(sys.argv[0]),

View File

@ -44,25 +44,27 @@ LOG_CONFIG = {
def main(): def main():
global logger global logger
if 2 > len(sys.argv):
return True
freeze_support() # required for multiprocessing + pyinstaller on windows freeze_support() # required for multiprocessing + pyinstaller on windows
monkey_mode = sys.argv[1]
if not (monkey_mode in [MONKEY_ARG, DROPPER_ARG]):
return True
arg_parser = argparse.ArgumentParser() arg_parser = argparse.ArgumentParser()
_, monkey_args = arg_parser.parse_known_args(sys.argv[2:]) arg_parser.add_argument(
"mode",
choices=[MONKEY_ARG, DROPPER_ARG],
help=f"'{MONKEY_ARG}' mode will run the agent in the current session/terminal."
f"'{DROPPER_ARG}' will detach the agent from the current session "
f"and will start it on a separate process.",
)
mode_args, mode_specific_args = arg_parser.parse_known_args()
mode = mode_args.mode
formatted_config = pformat(WormConfiguration.hide_sensitive_info(WormConfiguration.as_dict())) formatted_config = pformat(WormConfiguration.hide_sensitive_info(WormConfiguration.as_dict()))
print(f"Loaded Configuration:\n{formatted_config}") print(f"Loaded Configuration:\n{formatted_config}")
try: try:
if MONKEY_ARG == monkey_mode: if MONKEY_ARG == mode:
log_path = get_agent_log_path() log_path = get_agent_log_path()
monkey_cls = InfectionMonkey monkey_cls = InfectionMonkey
elif DROPPER_ARG == monkey_mode: elif DROPPER_ARG == mode:
log_path = get_dropper_log_path() log_path = get_dropper_log_path()
monkey_cls = MonkeyDrops monkey_cls = MonkeyDrops
else: else:
@ -98,7 +100,7 @@ def main():
logger.info(f"version: {get_version()}") logger.info(f"version: {get_version()}")
logger.info(f"writing log file to {log_path}") logger.info(f"writing log file to {log_path}")
monkey = monkey_cls(monkey_args) monkey = monkey_cls(mode_specific_args)
try: try:
monkey.start() monkey.start()

View File

@ -102,7 +102,7 @@ class InfectionMonkey:
arg_parser.add_argument("-t", "--tunnel") arg_parser.add_argument("-t", "--tunnel")
arg_parser.add_argument("-s", "--server") arg_parser.add_argument("-s", "--server")
arg_parser.add_argument("-d", "--depth", type=int) arg_parser.add_argument("-d", "--depth", type=int)
opts, _ = arg_parser.parse_known_args(args) opts = arg_parser.parse_args(args)
InfectionMonkey._log_arguments(opts) InfectionMonkey._log_arguments(opts)
return opts return opts