MCPcopy Create free account
hub / github.com/ELMERIKH/PyinMemoryPE / build_import_table

Method build_import_table

pythonmemorymodule/__init__.py:808–873  ·  view source on GitHub ↗
(self, dlopen = LoadLibraryW)

Source from the content-addressed store, hash-verified

806
807
808 def build_import_table(self, dlopen = LoadLibraryW):
809 codebase = self._codebaseaddr
810 self.dbg("codebase:0x%x", codebase)
811 directory = self.OPTIONAL_HEADER.DATA_DIRECTORY[IMAGE_DIRECTORY_ENTRY_IMPORT]
812
813 if directory.Size <= 0:
814 self.dbg('Import directory\'s size appears to be zero or less. Skipping.. (Probably not good)&#x27;)
815 return
816 importdescaddr = codebase + directory.VirtualAddress
817 check = not bool(IsBadReadPtr(importdescaddr, sizeof(IMAGE_IMPORT_DESCRIPTOR)))
818 if not check:
819 self.dbg('IsBadReadPtr(address) at address: 0x%x returned true', importdescaddr)
820 i=0 # index for entry import struct
821 for i in range(0, len(self.DIRECTORY_ENTRY_IMPORT)):
822 self.dbg('Found importdesc at address: 0x%x', importdescaddr)
823 importdesc = directory.VirtualAddress
824
825 # ref: https://sites.google.com/site/peofcns/win32forth/pe-header-f/02-image_directory/02-import_descriptor
826 entry_struct=self.DIRECTORY_ENTRY_IMPORT[i].struct
827 entry_imports=self.DIRECTORY_ENTRY_IMPORT[i].imports
828 dll = self.DIRECTORY_ENTRY_IMPORT[i].dll.decode('utf-8')
829 if not bool(dll):
830 self.dbg('Importdesc at address 0x%x name is NULL. Skipping load library', importdescaddr)
831 hmod = dll
832 else:
833 self.dbg('Found imported DLL, %s. Loading..', dll)
834 hmod = dlopen(dll)
835 if not bool(hmod): raise WindowsError('Failed to load library, %s' % dll)
836 result_realloc= realloc(
837 self.pythonmemorymodule.contents.modules,
838 (self.pythonmemorymodule.contents.modules._b_base_.numModules + 1) * sizeof(HMODULE)
839 )
840 if not bool(result_realloc):
841 raise WindowsError('Failed to allocate additional room for our new import.')
842 self.pythonmemorymodule.contents.modules = cast(result_realloc, type(self.pythonmemorymodule.contents.modules))
843 self.pythonmemorymodule.contents.modules[self.pythonmemorymodule.contents.modules._b_base_.numModules] = hmod
844 self.pythonmemorymodule.contents.modules._b_base_.numModules += 1
845
846
847 thunkrefaddr = funcrefaddr = codebase + entry_struct.FirstThunk
848 if entry_struct.OriginalFirstThunk > 0:
849 thunkrefaddr = codebase + entry_struct.OriginalFirstThunk
850
851 for j in range(0, len(entry_imports)):
852
853 funcref = cast(funcrefaddr, PFARPROC)
854 if entry_imports[j].import_by_ordinal == True:
855 if 'decode' in dir(entry_imports[j].ordinal):
856 importordinal= entry_imports[j].ordinal.decode('utf-8')
857 else:
858 importordinal= entry_imports[j].ordinal
859 self.dbg('Found import ordinal entry, %s', cast(importordinal, LPCSTR))
860 funcref.contents = GetProcAddress(hmod, cast(importordinal, LPCSTR))
861 address = funcref.contents
862 else:
863 importname= entry_imports[j].name.decode('utf-8')
864 self.dbg('Found import by name entry %s , at address 0x%x', importname, entry_imports[j].address)
865 address= getprocaddr(hmod, importname.encode())

Callers 1

load_moduleMethod · 0.95

Calls 4

dbgMethod · 0.95
GetProcAddressFunction · 0.85
getprocaddrFunction · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected