PE64Dump - Dump the PE32+ Import table
Introduction
It is often very useful to know which DLLs and which functions are imported by an executable. The following program will show one way to access this information.
First of all, the PE (Portable Executable) is a specification of a format defined by Microsoft for executables, DLLs, device drivers... which is derived from the COFF (Common Object File Format) format of Unix. For detailed information about the PE header, you can refer to the official PECOFF specification of Microsoft. Apart from the code itself, a PE executable contains various headers which provide essential information to the OS in order to properly load the code in memory. A wrongly built PE header will, most of the time, result in a program that can not be started. PE64dump is a 64 bit program that can dump executables in 64 bit format. For this purpose, it loads a given executable in memory and reads the different fields of the PE header.
Using the code
PE64Dump does not include any GUI, it simply saves the dump into a text file. So first of all, the program opens two dialog boxes to let the user choose which program to dump and in which text file the dump has to be saved. Different methods would be possible to load the code in memory, by directly accessing the file on the hard drive every time we need an information, reading it into memory, or mapping it using the memory mapped file functions of WinAPI. PE64Dump uses the second method as it is quite simple, reliable, and fast.
The code
After having checked that we really deal with an x64 PE, we retrieve the relative virtual address of the import table. To do this, we must analyze the last parameter of IMAGE_OPTIONAL_HEADER
defined in Winnt.h. It looks like this:
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
IMAGE_DATA_DIRECTORY
is a structure of two parameters: one virtual address and one size. As IMAGE_NUMBEROF_DIRECTORY_ENTRIES
is equal to 16, it means that the end of the optional header is made up by 16 data directories. Each DataDirectory
contains important data such as the export table (functions exported by a program), the import table (functions imported by the program), resource tables, Thread Local Storage table... So by putting this line of code, we read the virtual address of the import table:
OptPEheader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress
Why is this address virtual? Because it is the address in memory when the OS has loaded the code, and not the offset of the data on the hard drive file. This difference is very important, and if we missed that, we would not be able to go further. To be more precise, all addresses are relative, i.e., they are an offset to the beginning of the code in memory. So our next step will be to translate this virtual address into a real address. This is the aim of the function VAtoFileOffset
. This function takes two parameters as input: the address of the structure IMAGE_NT_HEADERS
, and the virtual address we want to convert. The address of IMAGE_NT_HEADERS
will lead us to the section headers. We must iterate on each section header to check if the virtual address in the input is contained inside. If yes, then we just have to read the raw address of the beginning of the section and we can derive the actual address of the import table with the following formula:
dwFileOffset = dw_va - ImgSectionHdr1->VirtualAddress +
ImgSectionHdr1->PointerToRawData;
This is the actual address of a structure called IMAGE_IMPORT_DESCRIPTOR
(all elements of this structure are defined in Winnt.h as well). Each IMAGE_IMPORT_DESCRIPTOR
corresponds to one DLL; the structures are put after each other, the last one being filled with 0 to indicate the end of data.
The first important field of the structure is the Name
field, it is actually the virtual address of the name of the imported DLL. So again, we use our conversion function to read the actual address in the file from this VA. We simply have to iterate on each of these structures to get all DLLs imported by the exe.
The second important field is the OriginalFirstThunk
(OFT); it is the RVA of the first function imported by the program. It is important to notice that we do not use the FirstThunk
field of the structure. Actually, the FirstThunk
and the OFT both point to the same function inside the exe. So why use this one rather than the other? It is because when the executable is bound, the virtual address of the functions in the executable are overwritten by the actual address of the functions in memory. This drastically reduces the loading time of the executable, but it is then impossible to retrieve the address of the functions hard coded in the exe. For this reason, we will only work with the OFT.
The next thing to check is if we deal with an import by ordinal or an import by name. If the Most Significant Bit of the OFT RVA is set, then it means the import is done by ordinal. We use the macro available in winnt.h:
if (IMAGE_SNAP_BY_ORDINAL(*pdw_OFT) == 1){
//....
If it is the case, we simply write the string "import by ordinal" in our dump, and we loop to the next DLL. If we deal with an import by name, then we go on and dump the functions. The OFT points to the structure IMAGE_THUNK_DATA64
which contains the information we are looking for. The AddressOfData
field of the union u1
is the RVA of the imported function. So me proceed like for DLLs by looping until we find 0, which means we reached the last thunk. Here also, we can use the macro IMAGE_SNAP_BY_ORDINAL
to check if we are dealing with an ordinal. If yes, the lowest word of the field u1.ordinal
is the actual ordinal number. If the import is by name, then we simply print it, after having retrieved the executable offset. That's it for the code. Please do not forget, if you compile the program with Visual Studio, to specify x64 as the solution platform. Enjoy!
History
First release.
发表评论
itnkFW Your style is really unique in comparison to other folks I ave read stuff from. Thanks for posting when you have the opportunity, Guess I will just bookmark this web site.
AQa9JC Nice article! Also visit my blog about Clomid success stories
Is anyone else having this issue or is it a issue on my end?
It?s arduous to search out knowledgeable folks on this subject, but you sound like you recognize what you?re talking about! Thanks
Well I truly enjoyed studying it. This information provided by you is very practical for correct planning.
Really appreciate you sharing this article post. Fantastic.
Incredible! This blog looks just like my old one! It as on a completely different topic but it has pretty much the same page layout and design. Excellent choice of colors!
This particular blog is definitely entertaining and diverting. I have found a bunch of useful advices out of this amazing blog. I ad love to go back over and over again. Thanks a lot!
This blog was how do I say it? Relevant!! Finally I ave found something that helped me. Thanks a lot!
There as definately a great deal to know about this subject. I really like all the points you have made.
Keep up the fantastic piece of work, I read few content on this internet site and I conceive that your site is rattling interesting and has circles of superb information.
Your article continually have got much of really up to date info. Where do you come up with this? Just stating you are very imaginative. Thanks again
outstanding write-up A a greater level really wonderful along with utilitarian information employing this site, likewise My own partner and we think your style is composed with fantastic works.
Very nice post. I just stumbled upon your weblog and wished to say that I ave truly enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again very soon!
Thorn of Girl Great info can be discovered on this website website.
Peculiar article, just what I was looking for.
Thanks a lot for sharing this with all of us you really recognise what you are speaking approximately! Bookmarked. Please also visit my website =). We may have a hyperlink change agreement among us!
I wanted to thank you for this very good read!! I definitely enjoyed every bit of it. I ave got you saved as a favorite to check out new things you post
Some genuinely interesting information, well written and broadly user pleasant.
You may have an extremely good layout for your blog i want it to work with on my web site also.
What as up, I log on to your blogs regularly. Your story-telling style is witty, keep it up!
Some really interesting info , well written and generally user genial.
Too many times I passed over this blog, and that was a mistake. I am happy I will be back!
This website was how do I say it? Relevant!! Finally I ave found something that helped me. Thank you!
I really liked your article post.Really thank you! Really Cool.
Some really choice content on this site, saved to my bookmarks.
I truly appreciate this blog. Much obliged.
Way cool! Some extremely valid points! I appreciate you penning this post plus the rest of the site is also very good.
visit always a major fan of linking to bloggers that I enjoy but really don at get a great deal of link really like from
Really appreciate you sharing this article post.Much thanks again. Will read on...
You acquired a really useful blog site I have been here reading for about an hour. I am a newbie and your accomplishment is extremely considerably an inspiration for me.
Really informative post.Thanks Again. Great.
Im obliged for the post.Thanks Again. Fantastic.
Really appreciate you sharing this article post.Really looking forward to read more. Much obliged.
This particular blog is really awesome and diverting. I have picked up helluva handy things out of this blog. I ad love to visit it over and over again. Thanks!
uniform apparel survive year. This style flatters
No matter if some one searches for his vital thing, so he/she wishes to be available that in detail, thus that thing is maintained over here.|
Of course, what a great website and educative posts, I will bookmark your site.All the Best!
Your style is really unique in comparison to other people I have read stuff from. I appreciate you for posting when you ave got the opportunity, Guess I will just bookmark this web site.
you might have an incredible blog here! would you like to make some invite posts on my weblog?
I saw a lot of website but I conceive this one has something special in it in it
This blog is definitely entertaining additionally informative. I have picked a lot of helpful stuff out of it. I ad love to visit it again soon. Cheers!
Thank you for this great piece of content. Best Regards
slot machines for sale view of Three Gorges | Wonder Travel Blog
This unique blog is really entertaining and diverting. I have chosen many interesting advices out of this blog. I ad love to return over and over again. Thanks a bunch!
Really informative article post.Thanks Again. Great.
It as going to be ending of mine day, however before ending I am reading this impressive post to improve my experience.
This info is invaluable. How can I find out more?
Nice info! Also visit my blog about Clomid challenge test
Wohh just what I was searching for, appreciate it for putting up.