1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include"stdafx.h" #include "..\CommonFiles\CmnHdr.h" #include<windowsx.h> #include<stdio.h> #include<tchar.h> #include<malloc.h> #include<TlHelp32.h> #include "Resource.h" #include<strsafe.h>
#ifdef UNICODE #define InjectLib InjectLibW #define EjectLib EjectLibW #else #define InjectLib InjectLibA #define EjectLib EjectLibA #endif
BOOL WINAPI InjectLibW(DWORD dwProcessId,PCWSTR pszLibFile){ BOOL bok=FALSE; HANDLE hProcess =NULL,hThread =NULL; PWSTR pszLibFileRemote=NULL; __try{ hProcess=OpenProcess( PROCESS_QUERY_INFORMATION| PROCESS_CREATE_THREAD| PROCESS_VM_OPERATION| PROCESS_VM_WRITE,FALSE,dwProcessId); if (hProcess==NULL) __leave; int cch =1+lstrlenW(pszLibFile); int cb=cch*sizeof(wchar_t); pszLibFileRemote=(PWSTR)VirtualAllocEx(hProcess,NULL,cb,MEM_COMMIT,PAGE_READWRITE); if (pszLibFileRemote==NULL) __leave; if(!WriteProcessMemory(hProcess,pszLibFileRemote,(PVOID)pszLibFile,cb,NULL)) __leave; PTHREAD_START_ROUTINE pfnThreadRtn =(PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")),"LoadLibraryW"); if(pfnThreadRtn==NULL) __leave; hThread=CreateRemoteThread(hProcess,NULL,0,pfnThreadRtn,pszLibFileRemote,0,NULL); if(hThread == NULL) __leave; WaitForSingleObject(hThread,INFINITE); bok=TRUE; } __finally{ if(pszLibFileRemote!=NULL) VirtualFreeEx(hProcess,pszLibFileRemote,0,MEM_RELEASE); if(hThread!=NULL) CloseHandle(hThread); if(hProcess!=NULL) CloseHandle(hProcess);
} return(bok); }
BOOL WINAPI InjectLibA(DWORD dwProcessId,PCSTR pszLibFile){ SIZE_T cchSize=lstrlenA(pszLibFile)+1; PWSTR pszLibFileW=(PWSTR)_alloca(cchSize*sizeof(wchar_t)); StringCchPrintfW(pszLibFileW,cchSize,L"%S",pszLibFile); return(InjectLibW(dwProcessId,pszLibFileW)); }
BOOL WINAPI EjectLibW(DWORD dwProcessId,PCWSTR pszLibFile){ BOOL bok=FALSE; HANDLE hthSnapshot=NULL; HANDLE hProcess=NULL,hThread =NULL; __try{ hthSnapshot=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,dwProcessId); if(hthSnapshot==INVALID_HANDLE_VALUE) __leave; MODULEENTRY32W me={sizeof(me)}; BOOL bFound=FALSE; BOOL bMoreMods=Module32FirstW(hthSnapshot,&me); for(;bMoreMods;bMoreMods=Module32NextW(hthSnapshot,&me)){ bFound=(_wcsicmp(me.szModule,pszLibFile)==0)||(_wcsicmp(me.szExePath,pszLibFile)==0); if(bFound) break;
} if(!bFound) __leave; hProcess=OpenProcess(PROCESS_QUERY_INFORMATION| PROCESS_CREATE_THREAD| PROCESS_VM_OPERATION,FALSE,dwProcessId); if(hProcess==NULL) __leave; PTHREAD_START_ROUTINE pfnThreadRtn=(PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")),"FreeLibrary"); if (pfnThreadRtn==NULL) __leave; hThread=CreateRemoteThread(hProcess,NULL,0,pfnThreadRtn,me.modBaseAddr,0,NULL); if(hThread==NULL) __leave; WaitForSingleObject(hThread,INFINITE); bok=TRUE;
}__finally{ if(hthSnapshot!=NULL) CloseHandle(hthSnapshot); if(hThread!=NULL) CloseHandle(hThread); if(hProcess!=NULL) CloseHandle(hProcess); } return(bok); }
BOOL WINAPI EjectLibA(DWORD dwProcessId,PCSTR pszLibFile){ SIZE_T cchSize=lstrlenA(pszLibFile)+1; PWSTR pszLibFileW=(PWSTR)_alloca(cchSize*sizeof(wchar_t)); StringCchPrintfW(pszLibFileW,cchSize,L"%s",pszLibFile); return(EjectLibW(dwProcessId,pszLibFileW)); }
BOOL Dlg_OnInitDialog(HWND hwnd,HWND hwndFocus,LPARAM lParam){ chSETDLGICONS(hwnd,IDI_INJLIB); return(TRUE); }
void Dlg_OnCommand(HWND hwnd,int id,HWND hwndCtrl,UINT codeNotity){ switch(id){ case IDCANCEL: EndDialog(hwnd,id); break; case IDC_INJECT: DWORD dwProcessId=GetDlgItemInt(hwnd,IDC_PROCESSID,NULL,FALSE); if(dwProcessId==0) dwProcessId=GetCurrentProcessId(); TCHAR szLibFile[MAX_PATH]; GetModuleFileName(NULL,szLibFile,_countof(szLibFile)); PTSTR pFilename=_tcsrchr(szLibFile,TEXT('\\'))+1; _tcscpy_s(pFilename,_countof(szLibFile)-(pFilename-szLibFile),TEXT("22_ImgWalk.DLL")); if(InjectLib(dwProcessId,szLibFile)){ chVERIFY(EjectLib(dwProcessId,szLibFile)); chMB("DLL注入/撤销成功!"); } else{ chMB("DLL注入/撤销失败!"); } break; } }
INT_PTR WINAPI Dlg_Proc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam){ switch(uMsg){ chHANDLE_DLGMSG(hWnd,WM_INITDIALOG,Dlg_OnInitDialog); chHANDLE_DLGMSG(hWnd,WM_COMMAND,Dlg_OnCommand); } return(FALSE); }
int WINAPI _tWinMain(HINSTANCE hInstExe, HINSTANCE, PTSTR pszCmdLine, int){ DialogBox(hInstExe, MAKEINTRESOURCE(IDD_INJLIB), NULL, Dlg_Proc); return (0); }
|