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 31,322 total registered users.


Recently Created Topics
[help] Unpacking VMP...
Mar/12
Reverse Engineering ...
Jul/06
hi!
Jul/01
let 'IDAPython' impo...
Sep/24
set 'IDAPython' as t...
Sep/24
GuessType return une...
Sep/20
About retrieving the...
Sep/07
How to find specific...
Aug/15
How to get data depe...
Jul/07
Identify RVA data in...
May/06


Recent Forum Posts
Finding the procedur...
rolEYder
Question about debbu...
rolEYder
Identify RVA data in...
sohlow
let 'IDAPython' impo...
sohlow
How to find specific...
hackgreti
Problem with ollydbg
sh3dow
How can I write olly...
sh3dow
New LoadMAP plugin v...
mefisto...
Intel pin in loaded ...
djnemo
OOP_RE tool available?
Bl4ckm4n


Recent Blog Entries
halsten
Mar/14
Breaking IonCUBE VM

oleavr
Oct/24
Anatomy of a code tracer

hasherezade
Sep/24
IAT Patcher - new tool for ...

oleavr
Aug/27
CryptoShark: code tracer ba...

oleavr
Jun/25
Build a debugger in 5 minutes

More ...


Recent Blog Comments
nieo on:
Mar/22
IAT Patcher - new tool for ...

djnemo on:
Nov/17
Kernel debugger vs user mod...

acel on:
Nov/14
Kernel debugger vs user mod...

pedram on:
Dec/21
frida.github.io: scriptable...

capadleman on:
Jun/19
Using NtCreateThreadEx for ...

More ...


Imagery
SoySauce Blueprint
Jun 6, 2008

[+] expand

View Gallery (11) / Submit