; ------------------------------------------------------- ; K32B.INC ; An include to simplify and speed the development ; of applications that do not have an import section. ; ------------------------------------------------------- ; Copyright (c) Bryant Keller, 2005 ; ------------------------------------------------------- GetProcAddr proto :DWORD, :DWORD K32Base proto DEFINE_API macro api_id:REQ, api_string:VARARG &api_id&_str db api_string, 0 &api_id&_fn dd 0 endm LdAPI macro libname:REQ, procname:REQ push offset &procname&_str push libname call [GetProcAddr] mov &procname&_fn, eax endm ACall macro funcname:REQ,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,a21,a22 FOR arg, IFNB push arg ENDIF ENDM call [&funcname&_fn] endm K32B macro k32ref:REQ local ________GetKernel32_Delta_Method_Macro call ________GetKernel32_Delta_Method_Macro ________GetKernel32_Delta_Method_Macro: pop ebp sub ebp, OFFSET ________GetKernel32_Delta_Method_Macro call [K32Base] mov k32ref, eax endm .code ;------------------------------------------------------------------------------ ; Obtains the base address of KERNEL32.DLL ;------------------------------------------------------------------------------ K32Base proc mov ecx, [esp + 4] @@: xor edx, edx dec ecx mov dx, [ecx + 3ch] test dx, 0f800h jnz @b cmp ecx, [ecx + edx + 34h] jnz @b mov eax, ecx ret K32Base endp ;------------------------------------------------------------------------------ ; GetProcAddress replacement ;------------------------------------------------------------------------------ GetProcAddr proc uses esi edi ecx ebx edx, hLib:DWORD, szProc:DWORD mov esi, hLib cmp word ptr [esi], 5a4dh ; check the DOS MZ signature jnz @@error_exit add esi, [esi + 03ch] cmp dword ptr [esi], 00004550h ; check the PE signature jnz @@error_exit mov edi, szProc ; obtain the length of the api name mov ecx, 200 xor al, al repnz scasb mov ecx, edi sub ecx, szProc ; ecx = strlen ( szProc ) mov edx, [esi + 078h] ; trace the export table add edx, hLib ; edx = export table assume edx:ptr IMAGE_EXPORT_DIRECTORY mov ebx, [edx].AddressOfNames add ebx, hLib ; ebx = AddressOfNames xor eax, eax ; eax = AddressOfNames index @@: mov edi, [ebx] add edi, hLib mov esi, szProc push ecx repz cmpsb ; save the api string length jz @@sth_done pop ecx add ebx, 4 inc eax cmp eax, [edx].NumberOfNames jne @b jmp @f @@sth_done: add esp, 4 @@: cmp eax, [edx].NumberOfNames ; did we find sth? je @@error_exit mov esi, [edx].AddressOfNameOrdinals ; find the corresponding ordinal add esi, hLib push edx ; save the export table pointer mov ebx, 2 xor edx, edx mul ebx pop edx add eax, esi xor ecx, ecx mov word ptr cx, [eax] ; ecx = API ordinal mov edi, [edx].AddressOfFunctions ; get the address of the api xor edx, edx mov ebx, 4 mov eax, ecx mul ebx add eax, hLib add eax, edi mov eax, [eax] add eax, hLib jmp @f assume edx:nothing @@error_exit: xor eax, eax ; return zero on error @@: ret ; return address of api on success GetProcAddr endp