这是一个反汇编的例子,反汇编的代码是使用bochs 中的反汇编的代码,由于bochs 反汇编模块做的比较独立,所以可以直接把反汇编模块包含到程序,不过为了使用这个反汇编的代码,需要自己写一小段对pe 进行简单解析的代码,下面是这个代码:
#include "stdafx.h"
#include "windows.h"
#include "disasm.h"
dword rva2fileoffset(pimage_section_header nthead, int nsection , dword rva);
int main(int argc, char* argv[])
{
image_dos_header dos;
image_nt_headers fileheader ;
dword cbread ;
//printf("hello world!\n");
if(argc != 2)
{
printf("uage : %s pe file name , ex : %s a.exe \r\n",argv[0],argv[0]);
return 0;
}
disassembler obj;
handle hfile = createfile(argv[1],generic_read , file_share_read , null,open_existing , file_attribute_normal, null);
if(hfile == invalid_handle_value)
{
printf("can not open file : %s",argv[1]);
return 0;
}
printf("begin reading file ... ...\r\n");
//read the file dos header
readfile(hfile,&dos,sizeof(image_dos_header), &cbread, null );
setfilepointer(hfile,dos.e_lfanew , null, file_begin);
//read the file pe header
readfile(hfile,&fileheader, sizeof(image_nt_headers), &cbread, null );
int nentryaddress = fileheader.optionalheader.addressofentrypoint ;
int nsectionnumber = fileheader.fileheader.numberofsections ;
pimage_section_header psection = new image_section_header [nsectionnumber];
readfile(hfile,psection, sizeof(image_section_header ) * nsectionnumber, &cbread, null);
printf("the address of entry is : %.8x\r\n", nentryaddress);
printf("the section number is : %d\r\n", nsectionnumber);
int nentryaddressofffile = rva2fileoffset(psection, nsectionnumber, nentryaddress);
setfilepointer(hfile,nentryaddressofffile,null,file_begin);
unsigned char szinbuf[1024];
char szoutbuf[4096];
readfile(hfile,szinbuf,1024,&cbread,null);
unsigned char *pstart = (unsigned char *)&szinbuf[0] ;
int ntotallen = 0;
for(int i = 0 ; ntotallen < 1024 ; i ++)
{
int nlen = obj.disasm(true,
fileheader.optionalheader.imagebase,
nentryaddress,
pstart,
( char *)&szoutbuf[0]);
pstart += nlen ;
ntotallen += nlen ;
printf(szoutbuf );
printf("\r\n");
}
closehandle(hfile);
return 0;
}
dword rva2fileoffset(pimage_section_header nthead, int nsection , dword rva)
{
pimage_section_header psection = (pimage_section_header) nthead ;
int ntotalsection = nsection;
for(int i = 0 ; i < ntotalsection ; i ++)
{
if ((psection->virtualaddress <= rva) && (psection->virtualaddress + psection->misc.virtualsize >= rva))
{
break;
}
}
if(i >= ntotalsection ){
return 0;
}
return psection->pointertorawdata + rva - psection->virtualaddress ;
}
看了上面的代码,你就会知道disassembler 这个类不是我自己写的,这个类就是来自bochs 中的反汇编模块
。 这个模块的代码可以从bochs 的源代码中获得。