免责声明:内容仅供学习参考,请合法利用知识,禁止进行违法犯罪活动!
内容参考于:易道云信息技术研究院
上一个内容:63.利用PEB获取模块列表
效果图:
隐藏模块简单实现:
#include <iostream>
#include <Windows.h>
#include <winternl.h>
/**
_PEB_LDR_DATA原本的结构
typedef struct _PEB_LDR_DATA {
BYTE Reserved1[8];
PVOID Reserved2[3];
LIST_ENTRY InMemoryOrderModuleList;
} PEB_LDR_DATA, *PPEB_LDR_DATA;
下方是通过windbg分析出的 _PEB_LDR_DATA 真实的结构
typedef struct _PEB_LDR_DATA {
BOOL Initialized;
LIST_ENTRY InInitializationOrderModuleList;
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderModuleList;
} PEB_LDR_DATA, * PPEB_LDR_DATA;
*/
/**
它们的顺序要去windbg看
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderLinks;
LIST_ENTRY InInitializationOrderModuleList;
在加载一个模块时首先会分别写入 InLoadOrderModuleList、InMemoryOrderLinks、InInitializationOrderModuleList这三个链表
也就是这三个链表都会写入模块数据,写入的顺序是:首先发现没有这个模块它是先在 InMemoryOrderLinks加东西
InMemoryOrderLinks加完以后在给InLoadOrderModuleList加东西,这两个地方加完以后再在 InInitializationOrderModuleList 里增加
在 INInitializationOrderModuleList 增加完以后会调用dllmain的函数,有时候会遍历这些链表的时候会遍历到链表的点为0的时候
当遍历到0的时候表示这个dll正在卸载,卸载一个dll模块的时候也是操作的这三个链表,操作的顺序是先去掉 InMemoryOrderLinks这个
然后InInitializationOrderModuleList去掉然后调用dllmain函数,最后给InLoadOrderModuleList处理掉
这里的 InLoadOrderModuleList、InMemoryOrderLinks、InInitializationOrderModuleList排序可能是不正确的,加载与卸载的顺序根据位置来就行
*/
typedef struct _LDR_DATA_TABLE_ENTRY {
LIST_ENTRY InLoadOrderModuleList;
LIST_ENTRY InMemoryOrderLinks;
LIST_ENTRY InInitializationOrderModuleList;
PVOID DllBase;
PVOID Reserved3[2];
UNICODE_STRING FullDllName;
BYTE Reserved4[8];
PVOID Reserved5[3];
#pragma warning(push)
#pragma warning(disable: 4201) // we'll always use the Microsoft compiler
union {
ULONG CheckSum;
PVOID Reserved6;
} DUMMYUNIONNAME;
#pragma warning(pop)
ULONG TimeDateStamp;
} LDR_DATA_TABLE_ENTRY, * PLDR_DATA_TABLE_ENTRY;
int main() {
PPEB _peb;
_asm {
mov eax, fs: [0x30]
mov _peb, eax
}
PPEB_LDR_DATA Pldr = _peb->Ldr;
PLIST_ENTRY FIRST = &(Pldr->InMemoryOrderModuleList);
PLIST_ENTRY CURLST = FIRST->Flink;
PLDR_DATA_TABLE_ENTRY lModule;
PLDR_DATA_TABLE_ENTRY lDis{};
int StructDiff = (int)&lDis->InMemoryOrderLinks;
do {
lModule = (PLDR_DATA_TABLE_ENTRY)((unsigned)CURLST - StructDiff);
// std::wcout.imbue(std::locale());
std::locale::global(std::locale(""));
std::wcout << L"模块名:" << lModule->FullDllName.Buffer << L" 基址:" << lModule->DllBase << L" 大小:" << lModule->Reserved3 << std::endl;
if (lModule->DllBase = (LPVOID)0x7A50000) {
lModule->InLoadOrderModuleList.Blink->Flink = lModule->InLoadOrderModuleList.Flink;
lModule->InLoadOrderModuleList.Flink->Blink = lModule->InLoadOrderModuleList.Blink;
lModule->InMemoryOrderLinks.Blink->Flink = lModule->InMemoryOrderLinks.Flink;
lModule->InMemoryOrderLinks.Flink->Blink = lModule->InMemoryOrderLinks.Blink;
lModule->InInitializationOrderModuleList.Blink->Flink = lModule->InInitializationOrderModuleList.Flink;
lModule->InInitializationOrderModuleList.Flink->Blink = lModule->InInitializationOrderModuleList.Blink;
}
CURLST = CURLST->Flink;
} while (FIRST != CURLST);
while (true);
}
隐藏模块整合进辅助里:也就是它 61.列表重绘 的代码
CWndMain.h文件做出了修改:
#pragma once
#include "afxdialogex.h"
#include "htdHook2.h"
#include "htdModule.h"
// CWndMain 对话框
class CWndMain : public CDialogEx
{
DECLARE_DYNAMIC(CWndMain)
public:
CWndMain(CWnd* pParent = nullptr); // 标准构造函数
virtual ~CWndMain();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_WNDMAIN };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
CString wInfo;
htdHook2 hook;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
};
CWndMain.cpp文件做出了修改:
void CWndMain::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
//hook.SetHook((LPVOID)0x41FDB2, Wudi, (LPVOID)0);
//hook.Init();
htdModule* p{};
p->HideDll(L"Dlls.dll");
}
htdModule.cpp文件内容:
#include "pch.h"
#include "htdModule.h"
void htdModule::HideDll(HMODULE _hMod)
{
PPEB _peb;
_asm {
mov eax, fs: [0x30]
mov _peb, eax
}
PPEB_LDR_DATA Pldr = _peb->Ldr;
PLIST_ENTRY FIRST = &(Pldr->InMemoryOrderModuleList);
PLIST_ENTRY CURLST = FIRST->Flink;
PLDR_DATA_TABLE_ENTRY lModule;
PLDR_DATA_TABLE_ENTRY lDis{};
int StructDiff = (int)&lDis->InMemoryOrderLinks;
do {
lModule = (PLDR_DATA_TABLE_ENTRY)((unsigned)CURLST - StructDiff);
// std::wcout.imbue(std::locale());
if (lModule->DllBase == _hMod) {
lModule->InLoadOrderModuleList.Blink->Flink = lModule->InLoadOrderModuleList.Flink;
lModule->InLoadOrderModuleList.Flink->Blink = lModule->InLoadOrderModuleList.Blink;
lModule->InMemoryOrderLinks.Blink->Flink = lModule->InMemoryOrderLinks.Flink;
lModule->InMemoryOrderLinks.Flink->Blink = lModule->InMemoryOrderLinks.Blink;
lModule->InInitializationOrderModuleList.Blink->Flink = lModule->InInitializationOrderModuleList.Flink;
lModule->InInitializationOrderModuleList.Flink->Blink = lModule->InInitializationOrderModuleList.Blink;
}
CURLST = CURLST->Flink;
} while (FIRST != CURLST);
}
void htdModule::HideDll(wchar_t* dllName)
{
HMODULE hMod = GetModuleHandleW(dllName);
if (hMod)HideDll(hMod);
}
htdModule.h文件内容:
#include "pch.h"
#include "htdModule.h"
void htdModule::HideDll(HMODULE _hMod)
{
PPEB _peb;
_asm {
mov eax, fs: [0x30]
mov _peb, eax
}
PPEB_LDR_DATA Pldr = _peb->Ldr;
PLIST_ENTRY FIRST = &(Pldr->InMemoryOrderModuleList);
PLIST_ENTRY CURLST = FIRST->Flink;
PLDR_DATA_TABLE_ENTRY lModule;
PLDR_DATA_TABLE_ENTRY lDis{};
int StructDiff = (int)&lDis->InMemoryOrderLinks;
do {
lModule = (PLDR_DATA_TABLE_ENTRY)((unsigned)CURLST - StructDiff);
// std::wcout.imbue(std::locale());
if (lModule->DllBase == _hMod) {
lModule->InLoadOrderModuleList.Blink->Flink = lModule->InLoadOrderModuleList.Flink;
lModule->InLoadOrderModuleList.Flink->Blink = lModule->InLoadOrderModuleList.Blink;
lModule->InMemoryOrderLinks.Blink->Flink = lModule->InMemoryOrderLinks.Flink;
lModule->InMemoryOrderLinks.Flink->Blink = lModule->InMemoryOrderLinks.Blink;
lModule->InInitializationOrderModuleList.Blink->Flink = lModule->InInitializationOrderModuleList.Flink;
lModule->InInitializationOrderModuleList.Flink->Blink = lModule->InInitializationOrderModuleList.Blink;
}
CURLST = CURLST->Flink;
} while (FIRST != CURLST);
}
void htdModule::HideDll(wchar_t* dllName)
{
HMODULE hMod = GetModuleHandleW(dllName);
if (hMod)HideDll(hMod);
}