ntdll!strcmp

7c902583 8b542404        mov     edx,dword ptr [esp+4]
7c902587 8b4c2408        mov     ecx,dword ptr [esp+8]
7c90258b f7c203000000    test    edx,3
7c902591 753d            jne     ntdll!strcmp+0x4c (7c9025d0)
7c902593 8b02            mov     eax,dword ptr [edx]  ; 4 bytes aligning
7c902595 3a01            cmp     al,byte ptr [ecx]  ; compare the first byte
7c902597 752e            jne     ntdll!strcmp+0x44 (7c9025c7)
7c902599 0ac0            or      al,al
7c90259b 7426            je      ntdll!strcmp+0x40 (7c9025c3)
7c90259d 3a6101          cmp     ah,byte ptr [ecx+1]  ; compare the second byte
7c9025a0 7525            jne     ntdll!strcmp+0x44 (7c9025c7)
7c9025a2 0ae4            or      ah,ah
7c9025a4 741d            je      ntdll!strcmp+0x40 (7c9025c3)
7c9025a6 c1e810          shr     eax,10h
7c9025a9 3a4102          cmp     al,byte ptr [ecx+2]  ; comare the third byte
7c9025ac 7519            jne     ntdll!strcmp+0x44 (7c9025c7)
7c9025ae 0ac0            or      al,al
7c9025b0 7411            je      ntdll!strcmp+0x40 (7c9025c3)
7c9025b2 3a6103          cmp     ah,byte ptr [ecx+3]  ; compare the fourth byte
7c9025b5 7510            jne     ntdll!strcmp+0x44 (7c9025c7)
7c9025b7 83c104          add     ecx,4
7c9025ba 83c204          add     edx,4
7c9025bd 0ae4            or      ah,ah
7c9025bf 75d2            jne     ntdll!strcmp+0x10 (7c902593)  ; cycle
7c9025c1 8bff            mov     edi,edi
7c9025c3 33c0            xor     eax,eax
7c9025c5 c3              ret
7c9025c6 90              nop
7c9025c7 1bc0            sbb     eax,eax ; After comparison if edx is smaller than dcx, the CF will be set, return -1 otherwise return 1
7c9025c9 d1e0            shl     eax,1
7c9025cb 40              inc     eax
7c9025cc c3              ret
7c9025cd 90              nop
7c9025ce 8bff            mov     edi,edi
7c9025d0 f7c201000000    test    edx,1
7c9025d6 7414            je      ntdll!strcmp+0x68 (7c9025ec)
7c9025d8 8a02            mov     al,byte ptr [edx]  ; 1 byte aligning
7c9025da 42              inc     edx
7c9025db 3a01            cmp     al,byte ptr [ecx]
7c9025dd 75e8            jne     ntdll!strcmp+0x44 (7c9025c7)   ;
7c9025df 41              inc     ecx
7c9025e0 0ac0            or      al,al    ; Check if it is the end, the previous comparison equals, so the strings match.
7c9025e2 74df            je      ntdll!strcmp+0x40 (7c9025c3)
7c9025e4 f7c202000000    test    edx,2    ; Check if 4 bytes aligning, if the answer if true goto 2593
7c9025ea 74a7            je      ntdll!strcmp+0x10 (7c902593)
7c9025ec 668b02          mov     ax,word ptr [edx]            ; 2 bytes align
7c9025ef 83c202          add     edx,2
7c9025f2 3a01            cmp     al,byte ptr [ecx]
7c9025f4 75d1            jne     ntdll!strcmp+0x44 (7c9025c7)
7c9025f6 0ac0            or      al,al    ; is the end?
7c9025f8 74c9            je      ntdll!strcmp+0x40 (7c9025c3) ; return 0
7c9025fa 3a6101          cmp     ah,byte ptr [ecx+1]
7c9025fd 75c8            jne     ntdll!strcmp+0x44 (7c9025c7) 
7c9025ff 0ae4            or      ah,ah    ; is the end?
7c902601 74c0            je      ntdll!strcmp+0x40 (7c9025c3) ; return 0
7c902603 83c102          add     ecx,2
7c902606 eb8b            jmp     ntdll!strcmp+0x10 (7c902593)   ; finally 4 bytes aligned
private void Driver() { Process targetProcess = Process.GetProcessesByName("dmmdzz")[0]; int processHandle = targetProcess.Handle.ToInt32(); try { string driverPath = "C:\\Windows\\System32\\drivers\\Brmsdriver.sys"; InstallDriver("BrmsDriver", driverPath); IntPtr ntdllBase = GetModuleHandle("ntdll.dll"); IntPtr zwWriteAddr = GetProcAddress(ntdllBase, "ZwWriteVirtualMemory"); byte[] originalBytes = new byte[16]; if (ReadProcessMemory(processHandle, zwWriteAddr, originalBytes, originalBytes.Length, out _)) { IntPtr newMem = VirtualAllocEx(processHandle, IntPtr.Zero, 100, 0x3000, 0x40); byte[] hookBytes = { 0xC7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB8, 0x3A, 0x00, 0x00, 0x00 }; WriteProcessMemory(processHandle, newMem, hookBytes, hookBytes.Length, out _); byte[] jmpBytes = BitConverter.GetBytes((long)(newMem.ToInt64() - zwWriteAddr.ToInt64() - 5)); WriteProcessMemory(processHandle, zwWriteAddr, new byte[] { 0xE9 }.Concat(jmpBytes).ToArray(), 6, out _); } IntPtr scManager = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS); if (scManager != IntPtr.Zero) { IntPtr service = CreateService(scManager, "BmsDriver", "BmsDriver", SC_MANAGER_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_BOOT_START, 0, driverPath, null, IntPtr.Zero, null, null, null); } MessageBox.Show("操作完成!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show($"错误:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void InstallDriver(string serviceName, string driverPath) { if (!File.Exists(driverPath)) { MessageBox.Show("驱动文件不存在!"); return; } }把以上内容全部写进C++,分头源
最新发布
08-15
### ### 驱动与内存操作的C++实现结构 为了将 C# 编写的驱动操作、内存修改、进程操作和函数钩子逻辑完整地转换为 C++ 实现,并按照头文件(`.h`)和源文件(`.cpp`)的结构组织代码,需要对以下核心模块进行拆分和实现: #### 1. 进程操作模块 该模块负责获取目标进程的句柄、读写内存等操作。 ##### 头文件(ProcessUtils.h) ```cpp #ifndef PROCESS_UTILS_H #define PROCESS_UTILS_H #include <windows.h> class ProcessUtils { public: static HANDLE GetProcessHandle(const char* processName); static bool ReadMemory(HANDLE hProcess, LPVOID address, void* buffer, SIZE_T size); static bool WriteMemory(HANDLE hProcess, LPVOID address, const void* buffer, SIZE_T size); }; #endif // PROCESS_UTILS_H ``` ##### 源文件(ProcessUtils.cpp) ```cpp #include "ProcessUtils.h" #include <tlhelp32.h> #include <iostream> HANDLE ProcessUtils::GetProcessHandle(const char* processName) { HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot == INVALID_HANDLE_VALUE) { return nullptr; } PROCESSENTRY32 pe32; pe32.dwSize = sizeof(PROCESSENTRY32); if (Process32First(hSnapshot, &pe32)) { do { if (strcmp(pe32.szExeFile, processName) == 0) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID); CloseHandle(hSnapshot); return hProcess; } } while (Process32Next(hSnapshot, &pe32)); } CloseHandle(hSnapshot); return nullptr; } bool ProcessUtils::ReadMemory(HANDLE hProcess, LPVOID address, void* buffer, SIZE_T size) { return ReadProcessMemory(hProcess, address, buffer, size, nullptr); } bool ProcessUtils::WriteMemory(HANDLE hProcess, LPVOID address, const void* buffer, SIZE_T size) { return WriteProcessMemory(hProcess, address, (void*)buffer, size, nullptr); } ``` --- #### 2. 驱动安装模块 该模块用于加载和卸载内核驱动。 ##### 头文件(DriverInstaller.h) ```cpp #ifndef DRIVER_INSTALLER_H #define DRIVER_INSTALLER_H #include <windows.h> class DriverInstaller { public: static bool InstallDriver(const char* serviceName, const char* driverPath); static bool UninstallDriver(const char* serviceName); }; #endif // DRIVER_INSTALLER_H ``` ##### 源文件(DriverInstaller.cpp) ```cpp #include "DriverInstaller.h" bool DriverInstaller::InstallDriver(const char* serviceName, const char* driverPath) { if (!PathFileExistsA(driverPath)) { std::cerr << "驱动文件不存在!" << std::endl; return false; } SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS); if (!hSCManager) { return false; } SC_HANDLE hService = CreateServiceA(hSCManager, serviceName, serviceName, SC_MANAGER_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_BOOT_START, 0, driverPath, nullptr, nullptr, nullptr, nullptr, nullptr); if (!hService) { hService = OpenServiceA(hSCManager, serviceName, SERVICE_START); if (!hService) { CloseServiceHandle(hSCManager); return false; } } StartServiceA(hService, 0, nullptr); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return true; } bool DriverInstaller::UninstallDriver(const char* serviceName) { SC_HANDLE hSCManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_ALL_ACCESS); if (!hSCManager) { return false; } SC_HANDLE hService = OpenServiceA(hSCManager, serviceName, DELETE); if (!hService) { CloseServiceHandle(hSCManager); return false; } bool result = DeleteService(hService); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return result; } ``` --- #### 3. 函数钩子(Hook)模块 该模块实现函数挂钩,包括保存原始指令、写入跳转指令等功能。 ##### 头文件(HookUtils.h) ```cpp #ifndef HOOK_UTILS_H #define HOOK_UTILS_H #include <windows.h> class HookUtils { public: static void* CreateHook(HANDLE hProcess, void* targetFunc, const void* hookFunc, size_t hookSize); static void RemoveHook(HANDLE hProcess, void* targetFunc, void* originalBytes, size_t hookSize); }; #endif // HOOK_UTILS_H ``` ##### 源文件(HookUtils.cpp) ```cpp #include "HookUtils.h" void* HookUtils::CreateHook(HANDLE hProcess, void* targetFunc, const void* hookFunc, size_t hookSize) { DWORD oldProtect; VirtualProtect(targetFunc, hookSize, PAGE_EXECUTE_READWRITE, &oldProtect); byte* originalBytes = new byte[hookSize]; ReadProcessMemory(hProcess, targetFunc, originalBytes, hookSize, nullptr); byte* newMem = (byte*)VirtualAllocEx(hProcess, nullptr, 0x1000, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); WriteProcessMemory(hProcess, newMem, hookFunc, hookSize, nullptr); byte jmp[5] = { 0xE9, 0x00, 0x00, 0x00, 0x00 }; *(DWORD*)(jmp + 1) = (DWORD)((uintptr_t)newMem - (uintptr_t)targetFunc - 5); WriteProcessMemory(hProcess, targetFunc, jmp, 5, nullptr); return originalBytes; } void HookUtils::RemoveHook(HANDLE hProcess, void* targetFunc, void* originalBytes, size_t hookSize) { DWORD oldProtect; VirtualProtect(targetFunc, hookSize, PAGE_EXECUTE_READWRITE, &oldProtect); WriteProcessMemory(hProcess, targetFunc, originalBytes, hookSize, nullptr); } ``` --- #### 4. 主程序逻辑 将上述模块组合到主程序中,实现完整的驱动操作和内存修改流程。 ##### 主程序(main.cpp) ```cpp #include "ProcessUtils.h" #include "DriverInstaller.h" #include "HookUtils.h" #include <windows.h> #include <iostream> int main() { const char* processName = "dmmdzz.exe"; const char* driverPath = "C:\\Windows\\System32\\drivers\\Brmsdriver.sys"; const char* serviceName = "BrmsDriver"; HANDLE hProcess = ProcessUtils::GetProcessHandle(processName); if (!hProcess) { std::cerr << "无法获取进程句柄" << std::endl; return -1; } if (!DriverInstaller::InstallDriver(serviceName, driverPath)) { std::cerr << "驱动安装失败" << std::endl; return -1; } HMODULE hNtdll = GetModuleHandleA("ntdll.dll"); FARPROC pZwWriteVirtualMemory = GetProcAddress(hNtdll, "ZwWriteVirtualMemory"); byte hookCode[] = { 0xC7, 0x44, 0x24, 0x04, 0x00, 0x00, 0x00, 0x00, 0xB8, 0x3A, 0x00, 0x00, 0x00 }; void* originalBytes = HookUtils::CreateHook(hProcess, pZwWriteVirtualMemory, hookCode, sizeof(hookCode)); if (!originalBytes) { std::cerr << "钩子设置失败" << std::endl; return -1; } std::cout << "操作完成!" << std::endl; // 清理 HookUtils::RemoveHook(hProcess, pZwWriteVirtualMemory, originalBytes, sizeof(hookCode)); delete[] (byte*)originalBytes; CloseHandle(hProcess); return 0; } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值