Flag: Tornado! Hurricane!

 Forums >>  Debuggers  >>  PEFile Error: PE instance has no attribute 'DIRECTORY_ENTRY_IMPORT'

Topic created on: May 17, 2010 23:37 CDT by tr4nce .

I am trying to create a python program that hooks on to every imported function in an executable and sets up a log breakpoint on each of them.  When the breakpoint is hit it logs it so that all the API calls made by a program can be seen.  
To do this I'm using Pydbg & PEFile.  I am able to enumerate modules using iterate_modules() function call in PyDbg.  But when I look at the PEFile
The code is as follows:
[snip]
  for modlist in dbg.iterate_modules():
      pe = pefile.PE(modlist.szExePath)
      for entry in pe.DIRECTORY_ENTRY_IMPORT:
          for imp in entry.imports:
              print '\t', hex(imp.address), imp.name
[/snip]
This works well for the .exe module but when the ntdll.dll module is hit I get the error:
AttributeError: PE instance has no attribute 'DIRECTORY_ENTRY_IMPORT'
Upon debugging I do see that pe.DIRECTORY_ENTRY_IMPORT is not present.  Is there some way to have a conditional statement (using some flag in pefile) that can help me in the enumeration?

  neoxfx     May 18, 2010 02:06.53 CDT
yea ntdll.dll does not have an import table.

to fix it, you can do a fast load and parse import table only if import data dir is present, like shown below.

pe=pefile.PE(filename,fast_load=True)
if pe.OPTIONAL_HEADER.DATA_DIRECTORY[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT']].VirtualAddress != 0:
    pe.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT']])
    for entry in pe.DIRECTORY_ENTRY_IMPORT:
          for imp in entry.imports:
              print '\t', hex(imp.address), imp.name

HTH,

neox

  tr4nce     May 18, 2010 23:15.25 CDT
neoxfx...thanks a lot! Your solution worked wonders!

Note: Registration is required to post to the forums.

There are 28,224 total registered users.


Recently Created Topics
Reverse Engineering ...
Jan/23
Career: DoD Agency I...
Jan/22
"Disappearing&q...
Jan/17
Career: Software Sec...
Jan/11
Where is the call st...
Jan/07
IDA Pro 6.1 Breakpoi...
Jan/01
How to create data s...
Dec/30
can i search all mod...
Dec/23
IDA symbol table exp...
Dec/20
An anti-attach trick
Dec/17


Recent Forum Posts
Reverse Engineering ...
NirIzr
"Disappearing&q...
NirIzr
Reverse Engineering ...
charlie
"Disappearing&q...
charlie
An anti-attach trick
Bass
An anti-attach trick
waleeda...
An anti-attach trick
Bass
An anti-attach trick
waleeda...
An anti-attach trick
Bass
Looking for value in...
NirIzr


Recent Blog Entries
cmathieu
Feb/07
Hacker Carnival

waleedassar
Feb/06
OllyDbg v1.10 And Hardware ...

waleedassar
Jan/31
Yet Another Anti-Debug Trick

RolfRolles
Jan/22
Finding Bugs in VMs with a ...

waleedassar
Jan/13
An OllyDbg Bug Disables Sof...

More ...


Recent Blog Comments
waleedassar on:
Feb/07
OllyDbg v1.10 And Hardware ...

NirIzr on:
Feb/07
OllyDbg v1.10 And Hardware ...

NirIzr on:
Feb/05
Yet Another Anti-Debug Trick

trolotou on:
Feb/05
Doudoune Moncler -Pennies F...

waleedassar on:
Feb/01
Yet Another Anti-Debug Trick

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit