mansz81

ciscot7.py

Mar 8th, 2022 (edited)
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.24 KB | None | 0 0
  1. import re
  2. import random
  3. import optparse
  4. """
  5. http://pen-testing.sans.org/resources/papers/gcih/cisco-ios-type-7-password-vulnerability-100566
  6.  
  7. To calculate xlat use the full p or full a password, after 51 charaters it's repeating.
  8. The first 2 numbers (salt) are taken off
  9.  
  10. p = "140316144B1B161F315C5E19091507021B1C143A3B3438232532031706131146494843441E130806494847434245441B4B16174847"
  11. a = "051207055A0A070E204D4F08180416130A0D052B2A2529323423120617020057585952550F021917585956525354550A5A07065956"
  12. xlat = []
  13. for i in range(len(a)/2):
  14.     xlat.append(hex(int(a[2*i:2*i+2],16) ^ ord("a")))
  15.  
  16. print xlat
  17.  
  18. """
  19.  
  20. xlat = [0x64, 0x73, 0x66, 0x64, 0x3b, 0x6b, 0x66, 0x6f, 0x41, 0x2c, 0x2e, 0x69, 0x79, 0x65, 0x77, 0x72, 0x6b, 0x6c, 0x64
  21. , 0x4a, 0x4b, 0x44, 0x48, 0x53, 0x55, 0x42, 0x73, 0x67, 0x76, 0x63, 0x61, 0x36, 0x39, 0x38, 0x33, 0x34, 0x6e, 0x63,
  22. 0x78, 0x76, 0x39, 0x38, 0x37, 0x33, 0x32, 0x35, 0x34, 0x6b, 0x3b, 0x66, 0x67, 0x38, 0x37]
  23.  
  24.            
  25. def decrypt_type7(ep):
  26.     """
  27.     Based on http://pypi.python.org/pypi/cisco_decrypt/
  28.     Regex improved
  29.     """
  30.     dp = ''
  31.     regex = re.compile('(^[0-9A-Fa-f]{2})([0-9A-Fa-f]+)')
  32.     result = regex.search(ep)
  33.     s, e = int(result.group(1)), result.group(2)
  34.     for pos in range(0, len(e), 2):
  35.         magic = int(e[pos] + e[pos+1], 16)
  36.         if s <= 50:
  37.             # xlat length is 51
  38.             newchar = '%c' % (magic ^ xlat[s])
  39.             s += 1
  40.         if s == 51: s = 0
  41.         dp += newchar
  42.     return dp
  43.  
  44. def encrypt_type7(pt):
  45.     """
  46.     Make Type 7 Cisco password from a string
  47.     """
  48.     salt = random.randrange(0,15);
  49.     ep = "%02d" % salt
  50.     for i in range(len(pt)):
  51.         ep += "%02x" % (ord(pt[i]) ^ xlat[salt])
  52.         salt += 1
  53.         if salt == 51: salt = 0
  54.     return ep
  55.  
  56. def main():
  57.     usage = "Usage: %prog [options]"
  58.     parser = optparse.OptionParser(usage=usage)
  59.     parser.add_option('-e', '--encrypt', action='store_true', dest='encrypt', default=False, help='Encrypt password')
  60.     parser.add_option('-d', '--decrypt', action='store_true', dest='decrypt',default=True, help='Decrypt password. This is the default')
  61.     parser.add_option('-p', '--password', action='store', dest="password", help='Password to encrypt / decrypt')
  62.     parser.add_option('-f', '--file', action='store', dest="file", help='Cisco config file, only for decryption')
  63.     options, args = parser.parse_args()
  64.     render_as = "files"
  65.  
  66.     #fix issue 1, if encrypt is selected, that takes precedence
  67.     if (options.encrypt):
  68.         options.decrypt = False
  69.     if (options.password is not None):
  70.         if(options.decrypt):
  71.             print("Decrypted password: " + decrypt_type7(options.password))
  72.         elif(options.encrypt):
  73.             print("Encrypted password: " + encrypt_type7(options.password))
  74.     elif (options.file is not None):
  75.         if(options.decrypt):
  76.             try:
  77.                 f = open(options.file)
  78.                 regex = re.compile('(7 )([0-9A-Fa-f]+)($)')
  79.                 for line in f:
  80.                     result = regex.search(line)
  81.                     if(result):
  82.                         print("Decrypted password: " + decrypt_type7(result.group(2)))
  83.             except IOError:
  84.                 print("Couldn't open file: " + options.file)
  85.         elif(options.encrypt):
  86.             parser.error("You can't encrypt a config file\nPlease run 'python ciscot7.py --help' for usage instructions.")
  87.     else:
  88.         parser.error("Password or config file is not specified!\nPlease run 'python ciscot7.py --help' for usage instructions.")
  89.  
  90.  
  91. if __name__ == '__main__':
  92.     main()
Add Comment
Please, Sign In to add comment