Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

释放LoadLibrary()加载的DLL #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions dllhijack.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "dllhijack.h"
#include <windows.h>
#include <list>

class HmoduleContainer {
public:
void Push(const HMODULE &handle) { Handles.push_back(handle); };
~HmoduleContainer() { for (auto handle : Handles) FreeLibrary(handle); }

private:
std::list<HMODULE> Handles;
} g_HmoduleContainer;

typedef struct _UNICODE_STRING {
USHORT Length;
Expand Down Expand Up @@ -75,10 +85,10 @@ PEB_LDR_DATA* NtGetPebLdr(void* peb)
}

/*
dllname: ���ٳ�dll��ԭʼ����
OrigDllPath: ���ٳ�dll�����������·��
dllname: 被劫持dll的原始名字
OrigDllPath: 被劫持dll改名后的完整路径
*/
void SuperDllHijack(LPCWSTR dllname, LPWSTR OrigDllPath)
void SuperDllHijack(LPCWSTR dllname, LPCWSTR OrigDllPath)
{
WCHAR wszDllName[100] = { 0 };
void* peb = NtCurrentPeb();
Expand All @@ -93,9 +103,10 @@ void SuperDllHijack(LPCWSTR dllname, LPWSTR OrigDllPath)
memcpy(wszDllName, data->BaseDllName.Buffer, data->BaseDllName.Length);

if (!_wcsicmp(wszDllName, dllname)) {
HMODULE hMod = LoadLibrary(OrigDllPath);
HMODULE hMod = LoadLibraryW(OrigDllPath);
data->DllBase = hMod;
g_HmoduleContainer.Push(hMod);
break;
}
}
}
}
6 changes: 3 additions & 3 deletions dllhijack.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <windows.h>

/*
dllname: ���ٳ�dll��ԭʼ����
OrigDllPath: ���ٳ�dll�����������·��
dllname: 被劫持dll的原始名字
OrigDllPath: 被劫持dll改名后的完整路径
*/
void SuperDllHijack(LPCWSTR dllname, LPWSTR OrigDllPath);
void SuperDllHijack(LPCWSTR dllname, LPCWSTR OrigDllPath);