Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import re
  4. import sys
  5. import base64
  6. import xml.etree.ElementTree as ET
  7. from hashlib import sha256
  8. from binascii import hexlify, unhexlify
  9. from Crypto.Cipher import AES
  10.  
  11. MAGIC = "::::MAGIC::::"
  12.  
  13. def usage():
  14. print "./decrypt.py <master.key> <hudson.util.Secret> <credentials.xml>"
  15. print " master.key can be found in $JENKINS_HOME/secrets"
  16. print " hudson.util.Secret can be found in $JENKINS_HOME/secrets"
  17. print " credentials.xml can be found in $JENKINS_HOME"
  18. sys.exit(0)
  19.  
  20. def decrypt(password, k):
  21. p = base64.decodestring(password)
  22. o = AES.new(k, AES.MODE_ECB)
  23. x = o.decrypt(p)
  24. assert MAGIC in x
  25. return re.findall('(.*)' + MAGIC, x)[0]
  26.  
  27. def parse_creds(creds_file):
  28. tree = ET.parse(creds_file)
  29. root = tree.getroot()
  30.  
  31. credentials = []
  32.  
  33. for cred in root.findall(".//com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl"):
  34. credentials.append((cred.find(".username").text, cred.find(".description").text, cred.find(".password").text))
  35.  
  36. for cred in root.findall(".//org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl"):
  37. credentials.append((None, cred.find(".description").text, cred.find(".secret").text))
  38.  
  39. return credentials
  40.  
  41. def main():
  42. if len(sys.argv) != 4:
  43. usage()
  44.  
  45. master_key = open(sys.argv[1]).read()
  46. hudson_secret_key = open(sys.argv[2], 'rb').read()
  47. hashed_master_key = sha256(master_key).digest()[:16]
  48. o = AES.new(hashed_master_key, AES.MODE_ECB)
  49. x = o.decrypt(hudson_secret_key)
  50. assert MAGIC in x
  51.  
  52. k = x[:-16]
  53. k = k[:16]
  54.  
  55. credentials = parse_creds(sys.argv[3])
  56.  
  57. for (username, description, enc_password) in credentials:
  58. password = decrypt(enc_password, k)
  59. if username is None:
  60. print "%s:\n\t%s" % (description, password)
  61. else:
  62. print "%s:\n\tusername: %s\n\tpassword: %s" % (description, username, password)
  63.  
  64.  
  65. if __name__ == '__main__':
  66. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement