forked from fancycode/MemoryModule
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDllLoader.cpp
More file actions
145 lines (124 loc) · 3.59 KB
/
DllLoader.cpp
File metadata and controls
145 lines (124 loc) · 3.59 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <malloc.h>
#include "../../ntinternals.h"
#include "../../MemoryModule.h"
typedef int (*addNumberProc)(int, int);
#define DLL_FILE "..\\..\\SampleDLL\\Debug\\SampleDLL.dll"
PPEB GetPEB(void)
{
// XXX: is there a better or even documented way to do this?
__asm {
// get PEB
mov eax, dword ptr fs:[30h]
}
}
void DumpPEB(void)
{
PPEB peb = GetPEB();
PPEB_LDR_DATA loaderData = peb->LoaderData;
PLDR_MODULE loaderModule;
printf("-------------------------------------\n");
printf("PEB at 0x%x\n", (DWORD)peb);
printf("Modules (Load Order)\n");
loaderModule = (PLDR_MODULE)(loaderData->InLoadOrderModuleList.Flink);
printf("Last: %x\n", (DWORD)loaderData->InLoadOrderModuleList.Blink);
while (1)
{
printf("Info: %x\n", (DWORD)loaderModule);
if (!IsBadReadPtr(loaderModule->BaseDllName.Buffer, loaderModule->BaseDllName.Length))
{
wprintf(L"Library: %s\n", loaderModule->BaseDllName.Buffer);
wprintf(L"Fullname: %s\n", loaderModule->FullDllName.Buffer);
} else
printf("Unknown module\n");
printf("Address: %x\n", (DWORD)loaderModule->BaseAddress);
printf("Load count: %d\n", loaderModule->LoadCount);
printf("Flags: %d\n", loaderModule->Flags);
printf("Size: %d\n", loaderModule->SizeOfImage);
printf("Entry: %x\n", (DWORD)loaderModule->EntryPoint);
printf("Hash: %x %x\n", (DWORD)loaderModule->HashTableEntry.Flink, (DWORD)loaderModule->HashTableEntry.Blink);
// advance to next module
loaderModule = (PLDR_MODULE)(loaderModule->InLoadOrderModuleList.Flink);
if (loaderModule == (PLDR_MODULE)(loaderData->InLoadOrderModuleList.Flink))
// we traversed through the complete list
// and didn't find the library
goto exit;
printf("\n");
}
exit:
printf("=====================================\n");
printf("\n");
}
static void
OutputLastError(const char *msg)
{
LPVOID tmp;
char *tmpmsg;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&tmp, 0, NULL);
tmpmsg = (char *)malloc(strlen(msg) + strlen((const char *)tmp) + 3);
sprintf(tmpmsg, "%s: %s", msg, tmp);
OutputDebugString(tmpmsg);
free(tmpmsg);
LocalFree(tmp);
}
void LoadFromFile(void)
{
addNumberProc addNumber;
HINSTANCE handle;
DumpPEB();
handle = LoadLibrary(DLL_FILE);
if (handle == INVALID_HANDLE_VALUE)
return;
DumpPEB();
addNumber = (addNumberProc)GetProcAddress(handle, "addNumbers");
printf("From file: %d\n", addNumber(1, 2));
FreeLibrary(handle);
DumpPEB();
}
void LoadFromMemory(void)
{
FILE *fp;
unsigned char *data=NULL;
size_t size;
HMEMORYMODULE module;
addNumberProc addNumber;
fp = fopen(DLL_FILE, "rb");
if (fp == NULL)
{
printf("Can't open DLL file \"%s\".", DLL_FILE);
goto exit;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
data = (unsigned char *)malloc(size);
fseek(fp, 0, SEEK_SET);
fread(data, 1, size, fp);
fclose(fp);
DumpPEB();
module = MemoryLoadLibrary(data, (unsigned char *)DLL_FILE);
if (module == NULL)
{
printf("Can't load library from memory.\n");
goto exit;
}
DumpPEB();
addNumber = (addNumberProc)MemoryGetProcAddress(module, "addNumbers");
if (addNumber)
printf("From memory: %d\n", addNumber(1, 2));
else
printf("Not found\n");
MemoryFreeLibrary(module);
exit:
if (data)
free(data);
}
int main(int argc, char* argv[])
{
LoadFromFile();
printf("\n\n");
LoadFromMemory();
return 0;
}