Modify what commands are passed to `subprocess.Popen` in the dropper and windows_upgrader

This commit is contained in:
Shreya 2021-04-28 18:57:04 +05:30 committed by Mike Salvatore
parent b50faceba7
commit d4e277c70b
2 changed files with 31 additions and 24 deletions

View File

@ -14,11 +14,7 @@ from ctypes import c_char_p
from common.utils.attack_utils import ScanStatus, UsageEnum from common.utils.attack_utils import ScanStatus, UsageEnum
from infection_monkey.config import WormConfiguration from infection_monkey.config import WormConfiguration
from infection_monkey.exploit.tools.helpers import build_monkey_commandline_explicitly from infection_monkey.exploit.tools.helpers import build_monkey_commandline_explicitly
from infection_monkey.model import ( from infection_monkey.model import MONKEY_CMDLINE_LINUX, MONKEY_CMDLINE_WINDOWS
GENERAL_CMDLINE_LINUX,
MONKEY_CMDLINE_LINUX,
MONKEY_CMDLINE_WINDOWS,
)
from infection_monkey.system_info import OperatingSystem, SystemInfoCollector from infection_monkey.system_info import OperatingSystem, SystemInfoCollector
from infection_monkey.telemetry.attack.t1106_telem import T1106Telem from infection_monkey.telemetry.attack.t1106_telem import T1106Telem
@ -151,20 +147,27 @@ class MonkeyDrops(object):
MONKEY_CMDLINE_WINDOWS % {"monkey_path": self._config["destination_path"]} MONKEY_CMDLINE_WINDOWS % {"monkey_path": self._config["destination_path"]}
+ monkey_options + monkey_options
) )
monkey_cmdline_split = shlex.split(
monkey_cmdline,
posix=False, # won't try resolving "\" in paths as part of escape sequences
)
monkey_process = subprocess.Popen(
monkey_cmdline_split,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
close_fds=True,
creationflags=DETACHED_PROCESS,
)
else: else:
dest_path = self._config["destination_path"] dest_path = self._config["destination_path"]
# In linux we have a more complex commandline. There's a general outer one, # In Linux, we need to change the directory first, which is done
# and the inner one which actually # using thw `cwd` argument in `subprocess.Popen` below
# runs the monkey monkey_cmdline = (
inner_monkey_cmdline = (
MONKEY_CMDLINE_LINUX % {"monkey_filename": dest_path.split("/")[-1]} MONKEY_CMDLINE_LINUX % {"monkey_filename": dest_path.split("/")[-1]}
+ monkey_options + monkey_options
) )
monkey_cmdline = GENERAL_CMDLINE_LINUX % {
"monkey_directory": dest_path[0 : dest_path.rfind("/")],
"monkey_commandline": inner_monkey_cmdline,
}
monkey_cmdline_split = shlex.split(monkey_cmdline) monkey_cmdline_split = shlex.split(monkey_cmdline)
monkey_process = subprocess.Popen( monkey_process = subprocess.Popen(
@ -173,6 +176,7 @@ class MonkeyDrops(object):
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, close_fds=True,
cwd="/".join(dest_path.split("/")[0:-1]),
creationflags=DETACHED_PROCESS, creationflags=DETACHED_PROCESS,
) )

View File

@ -51,7 +51,10 @@ class WindowsUpgrader(object):
+ monkey_options + monkey_options
) )
monkey_cmdline_split = shlex.split(monkey_cmdline) monkey_cmdline_split = shlex.split(
monkey_cmdline,
posix=False, # won't try resolving "\" in paths as part of escape sequences
)
monkey_process = subprocess.Popen( monkey_process = subprocess.Popen(
monkey_cmdline_split, monkey_cmdline_split,