'Creates new import table for missing imports in a new section'

def create_new_iat(self):

    print '[*]    Adding    New    Section    for    updated    Import    Table'
    if ('UPX'.lower() in self.flItms['textSectionName'].lower()):
        print '[!]    Cannot    patch    a    new    IAT    into    a    UPX    binary    at    this    time.'
        return False
    with open(self.flItms['backdoorfile'], 'r+b') as self.binary:
        self.flItms['NewSectionSize'] = 4096
        self.flItms['SectionName'] = 'rdata1'
        self.flItms['newSectionPointerToRawData'] = (self.flItms['Sections'][(-1)][3] + self.flItms['Sections'][(-1)][4])
        self.flItms['VirtualSize'] = self.flItms['NewSectionSize']
        self.flItms['SizeOfRawData'] = self.flItms['VirtualSize']
        self.flItms['NewSectionName'] = ('.' + self.flItms['SectionName'])
        self.flItms['newSectionFlags'] = int('C0000040', 16)
        filesize = os.stat(self.flItms['filename']).st_size
        if (filesize > self.flItms['SizeOfImage']):
            print '[!]    File    has    extra    data    after    last    section,    cannot    add    new    section'
            return False
        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:', 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['ImportTableFileOffset'], 0)
        self.flItms['Import_Directory_Table'] = ''
        while True:
            check_chars = ('\x00' * 20)
            read_data = self.binary.read(20)
            if (read_data == check_chars):
                break
            self.flItms['Import_Directory_Table'] += read_data
        self.binary.seek(self.flItms['newSectionPointerToRawData'], 0)
        self.binary.write(self.flItms['Import_Directory_Table'])
        self.flItms['BeginningOfNewImports'] = (self.flItms['SizeOfImage'] + len(self.flItms['Import_Directory_Table']))
        self.build_imports()
        self.binary.write(self.flItms['addedIAT'])
        self.binary.write((struct.pack('<B', 0) * (((self.flItms['NewSectionSize'] - len(self.flItms['addedIAT'])) - len(self.flItms['Import_Directory_Table'])) + 20)))
        self.binary.seek(self.flItms['ImportTableLOCInPEOptHdrs'], 0)
        self.binary.write(struct.pack('<I', self.flItms['SizeOfImage']))
        self.binary.write(struct.pack('<I', ((self.flItms['ImportTableSize'] + (self.flItms['apiCount'] * 8)) + 20)))
        self.binary.seek(0)
    with open(self.flItms['backdoorfile'], 'r+b') as self.binary:
        if (self.gather_file_info_win() is False):
            return False
    return True
