-
Notifications
You must be signed in to change notification settings - Fork 22
/
functions.c
64 lines (52 loc) · 1.67 KB
/
functions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "stdafx.h"
#include "functions.h"
void **FindImportPtr(HMODULE hFindInModule, char *pModuleName, char *pImportName)
{
IMAGE_DOS_HEADER *pDosHeader;
IMAGE_NT_HEADERS *pNtHeader;
ULONG_PTR ImageBase;
IMAGE_IMPORT_DESCRIPTOR *pImportDescriptor;
ULONG_PTR *pOriginalFirstThunk;
ULONG_PTR *pFirstThunk;
ULONG_PTR ImageImportByName;
// Init
pDosHeader = (IMAGE_DOS_HEADER *)hFindInModule;
pNtHeader = (IMAGE_NT_HEADERS *)((char *)pDosHeader + pDosHeader->e_lfanew);
if(!pNtHeader->OptionalHeader.DataDirectory[1].VirtualAddress)
return NULL;
ImageBase = (ULONG_PTR)hFindInModule;
pImportDescriptor = (IMAGE_IMPORT_DESCRIPTOR *)(ImageBase + pNtHeader->OptionalHeader.DataDirectory[1].VirtualAddress);
// Search!
while(pImportDescriptor->OriginalFirstThunk)
{
if(lstrcmpiA((char *)(ImageBase + pImportDescriptor->Name), pModuleName) == 0)
{
pOriginalFirstThunk = (ULONG_PTR *)(ImageBase + pImportDescriptor->OriginalFirstThunk);
ImageImportByName = *pOriginalFirstThunk;
pFirstThunk = (ULONG_PTR *)(ImageBase + pImportDescriptor->FirstThunk);
while(ImageImportByName)
{
if(!(ImageImportByName & IMAGE_ORDINAL_FLAG))
{
if((ULONG_PTR)pImportName & ~0xFFFF)
{
ImageImportByName += sizeof(WORD);
if(lstrcmpA((char *)(ImageBase + ImageImportByName), pImportName) == 0)
return (void **)pFirstThunk;
}
}
else
{
if(((ULONG_PTR)pImportName & ~0xFFFF) == 0)
if((ImageImportByName & 0xFFFF) == (ULONG_PTR)pImportName)
return (void **)pFirstThunk;
}
pOriginalFirstThunk++;
ImageImportByName = *pOriginalFirstThunk;
pFirstThunk++;
}
}
pImportDescriptor++;
}
return NULL;
}