Reputation: 1
I am trying to do a Process Injection in C with the windws api (windows.h) and with tlhelp32.h. But I can not perform it. First of all I get the process and I make a comparison to know if the process I want to inject is executing but when I do the comparison I get an error saying that the types are wrong:
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
Process32First(snapshot, &pe32);
do {
if(wcscmp(pe32.szExeFile, L"mspaint.exe")==0) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
LPVOID allocated_mem = VirtualAllocEx(hProcess, NULL, sizeof(shellcode), (MEM_RESERVE | MEM_COMMIT), PAGE_EXECUTE_READWRITE);
if (allocated_mem == NULL){
printf("Memory allocation failed: %u\n", GetLastError());
return 1;
}
printf("Memory page allocated at: 0x%p\n", allocated_mem);
WriteProcessMemory(hProcess, allocated_mem, shellcode, sizeof(shellcode), NULL);
HANDLE hThread = CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)allocated_mem, NULL, 0, NULL);
if (hThread == NULL){
printf("Failed to obtain handle to process: %u\n", GetLastError());
return 1;
}
WaitForSingleObject(hThread, INFINITE);
VirtualFreeEx(hThread, allocated_mem, 0, MEM_RELEASE);
CloseHandle(hThread);
CloseHandle(hProcess);
break;
}
} while(Process32Next(snapshot, &pe32));
I also tried without checking this and just puting the process ID but It dont work either. The shellcode (message box) is injected in the memory of the process but the message box is not displayed and I do not know why. I need help, ty.
Upvotes: 0
Views: 111
Reputation: 596256
You are using the TCHAR
version of the TLHelp32 API, and are likely compiling your project for ANSI not UNICODE, thus PROCESSENTRY32
and Process32(First|Next)
would be mapped to PROCESSENTRY32A
and Process32(First|Next)A
respectively. As such, the pe32.szExeFile
field would be a char
string, and you are trying to pass that to wcscmp()
, which expects a wchar_t
string instead, hence the compiler error.
So, you need to either:
Leave your code as-is, but change your project config to enable UNICODE
for the character set.
Use the TCHAR
-based _tcscmp()
or lstrcmp()
instead of wcscmp()
:
#include <tchar.h>
...
if (_tcscmp(pe32.szExeFile, _T("mspaint.exe")) == 0) {
#include <windows.h>
...
if (lstrcmp(pe32.szExeFile, TEXT("mspaint.exe")) == 0) {
strcmp()
instead of wcscmp()
:#include <string.h>
...
if (strcmp(pe32.szExeFile, "mspaint.exe") == 0) {
TCHAR
-based API altogether. Use the wchar_t
-based API instead:PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
...
Process32FirstW(snapshot, &pe32);
do {
if (wcscmp(pe32.szExeFile, L"mspaint.exe") == 0) {
...
}
while (Process32NextW(snapshot, &pe32));
...
Upvotes: 2