C++注入記事本

 1 #include <iostream>;
 2 using namespace std;
 3 #include <windows.h>;
 4 #include <tlhelp32.h>;
 5 #include <tchar.h>;
 6 
 7 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
 8 HWND GetMainWindow();
 9 
10 extern "C" BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
11 {
12     HWND hWnd;
13     HWND hParWnd, hButWnd;
14     int d, d1;
15     switch (fdwReason)
16     {
17     case DLL_PROCESS_ATTACH:
18 
19         hWnd = GetMainWindow();
20         if (hWnd)
21             hWnd = ::FindWindowEx(hWnd, 0, TEXT("EDIT"), NULL);
22         if (hWnd)
23         {
24             ::MessageBox(hWnd, TEXT("開始注入"), TEXT("提示"), MB_OK);
25             for (int i = 0; i < 100; i++)
26             {
27                 PostMessageW(hWnd, WM_CHAR, L'', 1);
28                 PostMessageW(hWnd, WM_CHAR, L'', 1);
29                 PostMessageW(hWnd, WM_CHAR, L'', 1);
30                 PostMessageW(hWnd, WM_CHAR, L'', 1);
31                 PostMessageW(hWnd, WM_KEYDOWN,VK_RETURN, 1);
32             }
33         }
34         else
35         {
36             ::MessageBox(hWnd, TEXT("記事本不存在"), TEXT("提示"), MB_OK);
37         }
38         break;
39     case DLL_PROCESS_DETACH:
40         // detach from process
41         break;
42 
43     case DLL_THREAD_ATTACH:
44         // attach to thread
45         break;
46 
47     case DLL_THREAD_DETACH:
48         // detach from thread
49         break;
50     }
51     return TRUE; // succesful
52 }
53 
54 BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
55 {
56     DWORD dwCurProcessId = *((DWORD*)lParam);
57     DWORD dwProcessId = 0;
58 
59     GetWindowThreadProcessId(hwnd, &dwProcessId);
60     if (dwProcessId == dwCurProcessId && GetParent(hwnd) == NULL)
61     {
62         *((HWND *)lParam) = hwnd;
63         return FALSE;
64     }
65     return TRUE;
66 }
67 
68 
69 HWND GetMainWindow()
70 {
71     DWORD dwCurrentProcessId = GetCurrentProcessId();
72     if (!EnumWindows(EnumWindowsProc, (LPARAM)&dwCurrentProcessId))
73     {
74         return (HWND)dwCurrentProcessId;
75     }
76     return NULL;
77 }
Dll文件
  1 #include <iostream>;
  2 using namespace std;
  3 #include <windows.h>;
  4 #include <tlhelp32.h>;
  5 #include <tchar.h>;
  6 
  7 HANDLE hThread = NULL;
  8 //進程名稱查找進程ID
  9 DWORD ProcessFind(LPCTSTR Exename) //進程名稱
 10 {
 11     HANDLE hProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
 12     if (!hProcess)
 13     {
 14         return FALSE;
 15     }
 16     PROCESSENTRY32 info;
 17     info.dwSize = sizeof(PROCESSENTRY32);
 18     if (!Process32First(hProcess, &info))
 19     {
 20         return FALSE;
 21     }
 22     while (TRUE)
 23     {
 24         /*for (int i = 0; i <= 25; i++) {
 25             char c = info.szExeFile[i];
 26             cout << c;
 27         }*/
 28         cout << endl;
 29         if (_tcscmp(info.szExeFile, Exename) == 0)
 30         {
 31             return info.th32ProcessID;//返回進程的ID
 32         }
 33         if (!Process32Next(hProcess, &info))
 34         {
 35             return FALSE;
 36         }
 37     }
 38     return FALSE;
 39 
 40 }
 41 
 42 int dll_inject() {
 43     //Dll文件地址,改爲你本身的地址
 44     const TCHAR *pLocDll = TEXT("F:\\工做\\項目\\控制檯\\injection\\injection\\x64\\Release\\injectionDll.dll");
 45 
 46     HANDLE hThread = NULL;
 47 
 48     //記事本進程名稱
 49     DWORD ProcessID = ProcessFind(TEXT("notepad.exe"));
 50     if (!ProcessID) {
 51         cout << "查找不到當前程序" << endl;
 52     }
 53     else {
 54         //獲取進程ID
 55         HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, TRUE, ProcessID);
 56 
 57         //獲取dll大小
 58         SIZE_T PathSize = (_tcslen(TEXT("injectionDll.dll")) + 1) * sizeof(TCHAR);
 59 
 60         //申請內存
 61         LPVOID StartAddress = VirtualAllocEx(hProcess, NULL, PathSize, MEM_COMMIT, PAGE_READWRITE);
 62 
 63         //寫入內存
 64         bool bSuccess = WriteProcessMemory(hProcess, StartAddress, TEXT("injectionDll.dll"), PathSize, 0);
 65         if (!bSuccess)
 66         {
 67             cout << "寫入失敗" << endl;
 68         }
 69         else {
 70             //在寄主申請內存
 71             LPVOID strRmt = VirtualAllocEx(hProcess, nullptr, MAX_PATH, MEM_COMMIT, PAGE_READWRITE);
 72             //得到注入DLL大小
 73             size_t lenLocDll = 2 * _tcslen(pLocDll);
 74             //判斷寄主申請內存是否成功
 75             if (strRmt) {
 76                 //把DLL寫入寄主內存
 77                 BOOL ret = WriteProcessMemory(hProcess, strRmt, pLocDll, lenLocDll, nullptr);
 78                 //得到LoadLibraryW的函數地址以使用LoadLibrary函數
 79                 LPTHREAD_START_ROUTINE loadlib = LPTHREAD_START_ROUTINE(GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW"));
 80                 //注入
 81                 hThread = CreateRemoteThread(hProcess, nullptr, 0, loadlib, LPVOID(strRmt), 0, nullptr);
 82             }
 83             
 84             /*
 85             HANDLE hThread = CreateRemoteThread(hProcess, 0, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "LoadLibrary"), StartAddress, 0, 0);*/
 86 
 87             if (hThread == NULL)
 88             {
 89                 cout << "在進程中注入失敗:";
 90                 cout << GetLastError() << endl;
 91                 return -1;
 92             }
 93 
 94             WaitForSingleObject(hThread, INFINITE);
 95             //到這裏已經完成dll的加載即注入了,經過dll函數執行咱們要完成的任務
 96             //釋放
 97             VirtualFreeEx(hProcess, StartAddress,0, MEM_RELEASE);
 98             CloseHandle(hThread);
 99             CloseHandle(hProcess);
100         }
101     }
102 }
103 int main()
104 {
105     dll_inject();
106     system("pause");
107 }
主程序

注入DLL以後釋放失敗了,每次注入過一次以後,第二次注入都要重啓記事本才能從新注入,有沒有大神告訴我怎麼解決ios

相關文章
相關標籤/搜索