K32_api_retrieve proc Base:DWORD ,sApi:DWORD
push edx ;保存edx
xor eax,eax ;此时esi=sApi
Next_Api: ;edi=AddressOfNames
mov esi,sApi
xor edx,edx
dec edx
Match_Api_name:
mov bl,byte ptr [esi]
inc esi
cmp bl,0
jz foundit
inc edx
push eax
mov eax,[edi+eax*4] ;AddressOfNames的指针,递增
add eax,Base ;注意是RVA,一定要加Base值
cmp bl,byte ptr [eax+edx] ;逐字符比较
pop eax
jz Match_Api_name ;继续搜寻
inc eax ;不匹配,下一个api
loop Next_Api
jmp no_exist ;若全部搜完,即未存在
foundit:
pop edx ;edx=AddressOfNameOrdinals
shl eax,1 ;*2得到AddressOfNameOrdinals的指针
movzx eax,word ptr [edx+eax] ;eax返回指向AddressOfFunctions的指针
ret
no_exist:
pop edx
xor eax,eax
ret
K32_api_retrieve endp
;-----------------------------------------
GetApiA proc Base:DWORD,sApi:DWORD
local ADDRofFun:DWORD
pushad
mov edi,Base
add edi,IMAGE_DOS_HEADER.e_lfanew
mov edi,[edi] ;现在edi=off PE_HEADER
add edi,Base ;得到IMAGE_NT_HEADERS的偏移
mov ebx,edi
mov edi,[edi+IMAGE_NT_HEADERS.OptionalHeader.DataDirectory.VirtualAddress]
add edi,Base ;得到edi=IMAGE_EXPORT_DIRECTORY入口
mov eax,[edi+1ch] ;AddressOfFunctions的地址
add eax,Base
mov ADDRofFun,eax
;ecx=NumberOfNames
mov ecx,[edi+18h]
mov edx,[edi+24h]
add edx,Base ;edx=AddressOfNameOrdinals
mov edi,[edi+20h]
add edi,Base ;edi=AddressOfNames
invokeK32_api_retrieve,Base,sApi
mov ebx,ADDRofFun
shl eax,2 ;要*4才得到偏移
add eax,ebx
mov eax,[eax]
add eax,Base ;加上Base!
mov [esp+7*4],eax ;eax返回api地址
popad
ret
GetApiA endp
;-----------------------------------------
END__Start
;------------------------------------------End all
标 题:看看我的代码
发信人:wowocock
时 间:2002/08/22 08:55pm
详细信息:
看看我的代码
;适用系统Win9x/me/2k/xp/nt
.586p
.model flat, stdcall
option casemap :none ; case sensitive
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.libGetApiAddress PROTO :DWORD,:BYTE
.data
Kernel32Addrdd ?
ExportKernel dd ?GetProcAddr dd ?
GetModuleHandleAddr dd ?
LoadLibraryAddr dd ?
ExitProcessAddr dd ?aGetProcAddr db "GetProcAddress",0
GetProcAddLen equ $-aGetProcAddr-1aGetModuleHandle db "GetModuleHandle",0
GetModuleHandleLen equ $-aGetModuleHandle-1aLoadLibrary db "LoadLibrary",0
LoadLibraryLen equ $-aLoadLibrary-1aExitProcess db "ExitProcess",0
ExitProcessLen equ $-aExitProcess-1szTitle db "Test",0
temp1 db " Kernel32.dll Address is:%8x:",0dh,0ah
db " GetProcAddress Address is:%8x:",0dh,0ah
db "GetModuleHandle Address is:%8x",0dh,0ah
db " LoadLibrary Address is:%8x",0dh,0ah
db " ExitProcess Address is:%8x",0
temp2 db 256 dup(?)
.code