- You can not believe in the impossible!
“You just have little experience,” said the Queen. - At your age, I gave it half an hour every day! On other days, I managed to believe in a dozen of impossibilities before breakfast!
public partial class MainForm : Form { [DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type); public MainForm() { InitializeComponent(); this.Text = "ProcessID: " + Process.GetCurrentProcess().Id; } private void btnShowMessage_Click(Object sender, EventArgs e) { MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0); } }
#include "stdafx.h" #include <iostream> #include <Windows.h> #include <cstdio> int Wait(); int main() { // , . DWORD processId = 55; char* dllName = "C:\\_projects\\CustomHook\\Hooking\\Debug\\HookDll.dll"; // PID . printf("Enter PID to inject dll: "); std::cin >> processId; // . HANDLE openedProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId); if (openedProcess == NULL) { printf("OpenProcess error code: %d\r\n", GetLastError()); return Wait(); } // kernel32.dll HMODULE kernelModule = GetModuleHandleW(L"kernel32.dll"); if (kernelModule == NULL) { printf("GetModuleHandleW error code: %d\r\n", GetLastError()); return Wait(); } // LoadLibrary ( A ANSI, ) LPVOID loadLibraryAddr = GetProcAddress(kernelModule, "LoadLibraryA"); if (loadLibraryAddr == NULL) { printf("GetProcAddress error code: %d\r\n", GetLastError()); return Wait(); } // LoadLibrary, - DLL LPVOID argLoadLibrary = (LPVOID)VirtualAllocEx(openedProcess, NULL, strlen(dllName), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); if (argLoadLibrary == NULL) { printf("VirtualAllocEx error code: %d\r\n", GetLastError()); return Wait(); } // . int countWrited = WriteProcessMemory(openedProcess, argLoadLibrary, dllName, strlen(dllName), NULL); if (countWrited == NULL) { printf("WriteProcessMemory error code: %d\r\n", GetLastError()); return Wait(); } // , LoadLibrary HANDLE threadID = CreateRemoteThread(openedProcess, NULL, 0, (LPTHREAD_START_ROUTINE)loadLibraryAddr, argLoadLibrary, NULL, NULL); if (threadID == NULL) { printf("CreateRemoteThread error code: %d\r\n", GetLastError()); return Wait(); } else { printf("Dll injected!"); } // . CloseHandle(openedProcess); return 0; } int Wait() { char a; printf("Press any key to exit"); std::cin >> a; return 0; }
public class Exporter { [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, int processId); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetModuleHandle(string lpModuleName); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr VirtualAllocEx(IntPtr hProcess, IntPtr lpAddress, IntPtr dwSize, AllocationType flAllocationType, MemoryProtection flProtect); [DllImport("kernel32.dll", SetLastError = true)] public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, UIntPtr nSize, out IntPtr lpNumberOfBytesWritten); [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr CreateRemoteThread(IntPtr hProcess, IntPtr lpThreadAttributes, uint dwStackSize, IntPtr lpStartAddress, IntPtr lpParameter, uint dwCreationFlags, out IntPtr lpThreadId); [DllImport("kernel32.dll", SetLastError = true)] public static extern Int32 CloseHandle(IntPtr hObject); } public class Injector { public static void Inject(Int32 pid, String dllPath) { IntPtr openedProcess = Exporter.OpenProcess(ProcessAccessFlags.All, false, pid); IntPtr kernelModule = Exporter.GetModuleHandle("kernel32.dll"); IntPtr loadLibratyAddr = Exporter.GetProcAddress(kernelModule, "LoadLibraryA"); Int32 len = dllPath.Length; IntPtr lenPtr = new IntPtr(len); UIntPtr uLenPtr = new UIntPtr((uint)len); IntPtr argLoadLibrary = Exporter.VirtualAllocEx(openedProcess, IntPtr.Zero, lenPtr, AllocationType.Reserve | AllocationType.Commit, MemoryProtection.ReadWrite); IntPtr writedBytesCount; Boolean writed = Exporter.WriteProcessMemory(openedProcess, argLoadLibrary, System.Text.Encoding.ASCII.GetBytes(dllPath), uLenPtr, out writedBytesCount); IntPtr threadIdOut; IntPtr threadId = Exporter.CreateRemoteThread(openedProcess, IntPtr.Zero, 0, loadLibratyAddr, argLoadLibrary, 0, out threadIdOut); Exporter.CloseHandle(threadId); } }
// dllmain.cpp : Defines the entry point for the DLL application. #include "stdafx.h" #include <Windows.h> #define SIZE 6 // typedef int (WINAPI *pMessageBoxW)(HWND, LPCWSTR, LPCWSTR, UINT); int WINAPI MyMessageBoxW(HWND, LPCWSTR, LPCWSTR, UINT); void BeginRedirect(LPVOID); pMessageBoxW pOrigMBAddress = NULL; BYTE oldBytes[SIZE] = { 0 }; BYTE JMP[SIZE] = { 0 }; DWORD oldProtect, myProtect = PAGE_EXECUTE_READWRITE; BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: // . MessageBoxW(NULL, L"I hook MessageBox!", L"Hello", MB_OK); // MessageBox pOrigMBAddress = (pMessageBoxW)GetProcAddress(GetModuleHandleW(L"user32.dll"), "MessageBoxW"); if (pOrigMBAddress != NULL) { BeginRedirect(MyMessageBoxW); } break; case DLL_THREAD_ATTACH: break; case DLL_THREAD_DETACH: break; case DLL_PROCESS_DETACH: break; } return TRUE; } void BeginRedirect(LPVOID newFunction) { // - BYTE tempJMP[SIZE] = { 0xE9, 0x90, 0x90, 0x90, 0x90, 0xC3 }; memcpy(JMP, tempJMP, SIZE); // DWORD JMPSize = ((DWORD)newFunction - (DWORD)pOrigMBAddress - 5); // VirtualProtect((LPVOID)pOrigMBAddress, SIZE, PAGE_EXECUTE_READWRITE, &oldProtect); // memcpy(oldBytes, pOrigMBAddress, SIZE); // 4 . , x86 memcpy(&JMP[1], &JMPSize, 4); // memcpy(pOrigMBAddress, JMP, SIZE); // VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); } int WINAPI MyMessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uiType) { // VirtualProtect((LPVOID)pOrigMBAddress, SIZE, myProtect, NULL); // ( ) memcpy(pOrigMBAddress, oldBytes, SIZE); // , int retValue = MessageBoxW(hWnd, lpText, L"Hooked", uiType); // memcpy(pOrigMBAddress, JMP, SIZE); // VirtualProtect((LPVOID)pOrigMBAddress, SIZE, oldProtect, NULL); return retValue; }
Source: https://habr.com/ru/post/324718/
All Articles