[HELP]C++ Injector error??

Posts 1–15 of 49 · Page 1 of 4
[HELP]C++ Injector error??
I had 14 errors, then I fixed it up a bit to 12...
Can some one please fix up my code and post it here or PM me please??

Here is my code:
Code:
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;


char* GetCurrentDir()
{
	char* szRet = (char*)malloc(MAX_PATH);

	_getcwd(szRet, MAX_PATH);

	return szRet;

	)
		LPCTSTR SzToLPCTSTR(char* szString)
	{
		LPTSTR lpszRet;
		size_t side = strlen(szString)+1;

		lpszRet = (LPTSTR)malloc(MAX_PATH);
		mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);

		return lpszRet;

		)
			void WaitForProcessToAppear (LPCTSTR, lpsztProc, DWORD dwDelay)
		{
			HANDLE          hSnap;
			PROCESSENTRY32  peProc;
			BOOL            bAppeared = FALSE;
			
			while(!bAppeared)
			{
	if ((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE
		(
		peProc.dwSize = sizeof(PROCESSENTRY32);
	if(Process32First(hSnap, &peProc))
		while(Process32Next(hSnap, &peProc) && !bAppeared)
			if(!lstrcmp(lpcszProc, peProc.szExeFile))
				bAppeared = TRUE;
			}
			CloseHandle(hSnap);
			Sleep(dwDelay);
		}
		)
			DWORD GetProcessIdByName(LPCTSTR lpcszProc)
		{
			HANDLE          hSnap;
			PROCESSENTRY32  peProc;
			DWORD           dwRet = -1;
			
			if((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
{
	peProc.dwSize = sizeof(PROCESSENTRY32);
	if(Process32First(hSnap, &peProc))
		while(Process32Next(hSnap, &peProc))
			if(!lstrcmp(lpcszProc, peProc.szExeFile))
				dwRet = peProc.th32ProcessID;
			}
			CloseHandle(hSnap);
			
			return dwRet;
			)
				
				BOOL InjectDll(DWORD dwPid, char* szDllPath)
			{
				DWORD       dwMemSize;
				HANDLE      hProc;
				LPVOID      IpRemoteMem, IpLoadLibrary;
				BOOL        bRet = FALSE;
				
				if((hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, dwPid)) != NULL)
				{
					dwMemSize = strlen(szDllPath)+1;
					if((IpRemoteMem = VirtualAllocEx(hProc, NULL, dwMemSize, MEM_COMMIT, PAGE_READWRITE) != NULL)
						if(WriteProcessMemory(hProc, IpRemoteMem, (LPCVOID)szDllPath, dwMemSize, NULL))
						{
							IpLoadLibrary = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "LoadLibraryA");
							if(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)IpLoadLibrary, IpRemoteMem, 0, NULL)!= NULL)
								bRet = True
								)
						}
						CloseHandle(hProc);
						
						return bRet;
						)

							int main()
						{
							char szProc[MAX_PATH], szDll[MAX_PATH];
							char* szDllPath = (char*)malloc(MAX_PATH);
							LPTSTR lpszProc = NULL;

							for(;;)
							
							{
								cout << "Process: ";
								cin >> szProc;
								cout << "DLL: ";
								cin >> szDll;
								szDllPath = GetCurrentDir();
								strcat_s(szDllPath, MAX_PATH, "\\");
								strcat_s(szDllPath, MAX_PATH, szDll);
								cout << "Waiting for process..." << endl;
								WaitForProcessToAppear(SzToLPCTSTR(szProc), 100);
								if(InjectDll(GetProcessIdByName(SzToLPCTSTR(szProc)), szDllPath))
									cout << "Injection Success!" << endl;
								else
									cout << "Failed to inject!" << endl;
								cout << "\n";
							}
							
							return 0;
						}
The Error Log:

Code:
------ Build started: Project: Luigi, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(23) : error C2065: 'size' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(27) : error C2061: syntax error : identifier 'lpsztProc'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(35) : error C3861: 'CreateToolHelp32Snapshot': identifier not found
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(42) : error C2065: 'lpcszProc' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(48) : error C2065: 'dwDelay' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(59) : error C3861: 'CreateToolHelp32Snapshot': identifier not found
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(88) : error C2440: '=' : cannot convert from 'bool' to 'LPVOID'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(89) : error C2143: syntax error : missing ')' before '{'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(96) : error C2065: 'True' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(96) : error C2143: syntax error : missing ';' before '}'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(119) : error C2660: 'WaitForProcessToAppear' : function does not take 2 arguments
Build log was saved at "file://c:\Documents and Settings\-----\My Documents\Visual Studio 2008\Projects\Luigi\Luigi\Debug\BuildLog.htm"
Luigi - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
You're using ) instead of } as closing brackets o__O
Quote Originally Posted by Hell_Demon View Post
You're using ) instead of } as closing brackets o__O
So where should I put them, it's a Command Prompt Injector
Code:
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;


char* GetCurrentDir()
{
	char* szRet = (char*)malloc(MAX_PATH);

	_getcwd(szRet, MAX_PATH);

	return szRet;

}

LPCTSTR SzToLPCTSTR(char* szString)
{
	LPTSTR lpszRet;
	size_t side = strlen(szString)+1;
	lpszRet = (LPTSTR)malloc(MAX_PATH);
	mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
	return lpszRet;
}

void WaitForProcessToAppear (LPCTSTR, lpsztProc, DWORD dwDelay)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	BOOL            bAppeared = FALSE;

	while(!bAppeared)
	{
		if ((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
		{
			peProc.dwSize = sizeof(PROCESSENTRY32);
			if(Process32First(hSnap, &peProc))
			{
				while(Process32Next(hSnap, &peProc) && !bAppeared)
				{
					if(!lstrcmp(lpcszProc, peProc.szExeFile))
					{
						bAppeared = TRUE;
					}
					CloseHandle(hSnap);
				}
				Sleep(dwDelay);
			}
		}
	}
}
DWORD GetProcessIdByName(LPCTSTR lpcszProc)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	DWORD           dwRet = -1;
			
	if((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
	{
		peProc.dwSize = sizeof(PROCESSENTRY32);
		if(Process32First(hSnap, &peProc))
		{
			while(Process32Next(hSnap, &peProc))
			{
				if(!lstrcmp(lpcszProc, peProc.szExeFile))
				{
					dwRet = peProc.th32ProcessID;
				}
				CloseHandle(hSnap);
			}
			
		}
		return dwRet;
	}
}
				
BOOL InjectDll(DWORD dwPid, char* szDllPath)
{
	DWORD       dwMemSize;
	HANDLE      hProc;
	LPVOID      IpRemoteMem, IpLoadLibrary;
	BOOL        bRet = FALSE;
	
	if((hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, dwPid)) != NULL)
	{
		dwMemSize = strlen(szDllPath)+1;
		if((IpRemoteMem = VirtualAllocEx(hProc, NULL, dwMemSize, MEM_COMMIT, PAGE_READWRITE) != NULL)
		{
			if(WriteProcessMemory(hProc, IpRemoteMem, (LPCVOID)szDllPath, dwMemSize, NULL))
			{
				IpLoadLibrary = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "LoadLibraryA");
				if(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)IpLoadLibrary, IpRemoteMem, 0, NULL)!= NULL)
				{
					bRet = True
				}
				CloseHandle(hProc);
			}					
			return bRet;
		}
	}
}

int main()
{
	char szProc[MAX_PATH], szDll[MAX_PATH];
	char* szDllPath = (char*)malloc(MAX_PATH);
	LPTSTR lpszProc = NULL;
	for(;;)
	{
		cout << "Process: ";
		cin >> szProc;
		cout << "DLL: ";
		cin >> szDll;
		szDllPath = GetCurrentDir();
		strcat_s(szDllPath, MAX_PATH, "\\");
		strcat_s(szDllPath, MAX_PATH, szDll);
		cout << "Waiting for process..." << endl;
		WaitForProcessToAppear(SzToLPCTSTR(szProc), 100);
		if(InjectDll(GetProcessIdByName(SzToLPCTSTR(szProc)), szDllPath))
		{
			cout << "Injection Success!" << endl;
		}
		else
		{
			cout << "Failed to inject!" << endl;
			cout << "\n";
		}
	}
	return 0;
}
Fixed brackets and indention, no idea if it will work.
just use s1nject(closed source) or tatnium injector(open source)
Quote Originally Posted by Hell_Demon View Post
Code:
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;


char* GetCurrentDir()
{
	char* szRet = (char*)malloc(MAX_PATH);

	_getcwd(szRet, MAX_PATH);

	return szRet;

}

LPCTSTR SzToLPCTSTR(char* szString)
{
	LPTSTR lpszRet;
	size_t side = strlen(szString)+1;
	lpszRet = (LPTSTR)malloc(MAX_PATH);
	mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
	return lpszRet;
}

void WaitForProcessToAppear (LPCTSTR, lpsztProc, DWORD dwDelay)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	BOOL            bAppeared = FALSE;

	while(!bAppeared)
	{
		if ((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
		{
			peProc.dwSize = sizeof(PROCESSENTRY32);
			if(Process32First(hSnap, &peProc))
			{
				while(Process32Next(hSnap, &peProc) && !bAppeared)
				{
					if(!lstrcmp(lpcszProc, peProc.szExeFile))
					{
						bAppeared = TRUE;
					}
					CloseHandle(hSnap);
				}
				Sleep(dwDelay);
			}
		}
	}
}
DWORD GetProcessIdByName(LPCTSTR lpcszProc)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	DWORD           dwRet = -1;
			
	if((hSnap = CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
	{
		peProc.dwSize = sizeof(PROCESSENTRY32);
		if(Process32First(hSnap, &peProc))
		{
			while(Process32Next(hSnap, &peProc))
			{
				if(!lstrcmp(lpcszProc, peProc.szExeFile))
				{
					dwRet = peProc.th32ProcessID;
				}
				CloseHandle(hSnap);
			}
			
		}
		return dwRet;
	}
}
				
BOOL InjectDll(DWORD dwPid, char* szDllPath)
{
	DWORD       dwMemSize;
	HANDLE      hProc;
	LPVOID      IpRemoteMem, IpLoadLibrary;
	BOOL        bRet = FALSE;
	
	if((hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, dwPid)) != NULL)
	{
		dwMemSize = strlen(szDllPath)+1;
		if((IpRemoteMem = VirtualAllocEx(hProc, NULL, dwMemSize, MEM_COMMIT, PAGE_READWRITE) != NULL)
		{
			if(WriteProcessMemory(hProc, IpRemoteMem, (LPCVOID)szDllPath, dwMemSize, NULL))
			{
				IpLoadLibrary = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "LoadLibraryA");
				if(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)IpLoadLibrary, IpRemoteMem, 0, NULL)!= NULL)
				{
					bRet = True
				}
				CloseHandle(hProc);
			}					
			return bRet;
		}
	}
}

int main()
{
	char szProc[MAX_PATH], szDll[MAX_PATH];
	char* szDllPath = (char*)malloc(MAX_PATH);
	LPTSTR lpszProc = NULL;
	for(;;)
	{
		cout << "Process: ";
		cin >> szProc;
		cout << "DLL: ";
		cin >> szDll;
		szDllPath = GetCurrentDir();
		strcat_s(szDllPath, MAX_PATH, "\\");
		strcat_s(szDllPath, MAX_PATH, szDll);
		cout << "Waiting for process..." << endl;
		WaitForProcessToAppear(SzToLPCTSTR(szProc), 100);
		if(InjectDll(GetProcessIdByName(SzToLPCTSTR(szProc)), szDllPath))
		{
			cout << "Injection Success!" << endl;
		}
		else
		{
			cout << "Failed to inject!" << endl;
			cout << "\n";
		}
	}
	return 0;
}
Fixed brackets and indention, no idea if it will work.
just use s1nject(closed source) or tatnium injector(open source)
Titanium injector??

Edit: It didn't work, it only fixed one error...
An error log would make it a lot easier.
Quote Originally Posted by Micheltjuh View Post
An error log would make it a lot easier.
Error Log:
Code:
------ Build started: Project: Luigi, Configuration: Debug Win32 ------
Compiling...
main.cpp
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(23) : error C2065: 'size' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(27) : error C2061: syntax error : identifier 'lpsztProc'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(35) : error C3861: 'CreateToolHelp32Snapshot': identifier not found
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(42) : error C2065: 'lpcszProc' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(48) : error C2065: 'dwDelay' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(59) : error C3861: 'CreateToolHelp32Snapshot': identifier not found
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(88) : error C2440: '=' : cannot convert from 'bool' to 'LPVOID'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(89) : error C2143: syntax error : missing ')' before '{'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(96) : error C2065: 'True' : undeclared identifier
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(96) : error C2143: syntax error : missing ';' before '}'
c:\documents and settings\-----\my documents\visual studio 2008\projects\luigi\luigi\main.cpp(119) : error C2660: 'WaitForProcessToAppear' : function does not take 2 arguments
Build log was saved at "file://c:\Documents and Settings\-----\My Documents\Visual Studio 2008\Projects\Luigi\Luigi\Debug\BuildLog.htm"
Luigi - 11 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hmmmm... well you've got a bunch of uncdeclared identifiers from what I can see. You either need to imports some files that declare those identifiers or use unmanaged ones. Let me look into it.

EDIT: (3 minutes after looking at code)
Wow. There are way to many managed to unmanaged C++ conversions in there. Maybe HD or BA would have a better time understanding it. I suck with managed.



PS: Does anyone know somewhere I could get acquainted with managed C++? A book or a web page or something. Not exactly sure what I should look for.
Quote Originally Posted by why06 View Post
Hmmmm... well you've got a bunch of uncdeclared identifiers from what I can see. You either need to imports some files that declare those identifiers or use unmanaged ones. Let me look into it.

EDIT: (3 minutes after looking at code)
Wow. There are way to many managed to unmanaged C++ conversions in there. Maybe HD or BA would have a better time understanding it. I suck with managed.



PS: Does anyone know somewhere I could get acquainted with managed C++? A book or a web page or something. Not exactly sure what I should look for.
I fixed it up a little, and changed the BOOL, and the FALSE and TRUE to undercase letters.

Now, I only have 10 errors...
MSDN

one way to approach is it to think that Managed C++ = C# using C++ syntax..


Dont' realyl have time to go over all of this, but for instance this:

...\luigi\main.cpp(23) : error C2065: 'size' : undeclared identifier



Look in that block of code
Code:
LPCTSTR SzToLPCTSTR(char* szString)
{
	LPTSTR lpszRet;
	size_t side = strlen(szString)+1;
	lpszRet = (LPTSTR)malloc(MAX_PATH);
	mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
	return lpszRet;
}
notice anything what it probably should've been?
Why would anyone call the length of a string 'side' instead of size ?
I'm sure there's more like those..
Quote Originally Posted by B1ackAnge1 View Post
MSDN

one way to approach is it to think that Managed C++ = C# using C++ syntax..
Hmmm I had a feeling someone would say that. Basically MSDN = the manual xD.

Oh well and looking at the small code snipet I see what you mean. whoever wrote this didn't pay much attention to what they were doing or at least didnt debug it. Anyway it also looks like they added 1 to the string length.. o_O wth?

Meh. I'll try to figure it out. it will take a while because I have less experience, I just have to break each function down part by part. Could take about a week, but this should give me some good practice with managed.
Couple of missing brackets, semi colon, few too many comma's in places they shouldn't be, variable name mis-spellings, Upper vs Lower Case ..

And btw - no managed code in here.. just good old-fashioned Win32 C++

Not trying to sound like an arrogant prick ('cause i'm really not) lol but but this should be beginner level to figure this stuff out, especially when running it through a compiler and it basically telling you what the problem is Perhaps now that you see what the changes are, compare and see if it make sense with what the error message was for each of the errors. Learning opportunity here


Code:
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;


char* GetCurrentDir()
{
	char* szRet = (char*)malloc(MAX_PATH);

	_getcwd(szRet, MAX_PATH);

	return szRet;

}

LPCTSTR SzToLPCTSTR(char* szString)
{
	LPTSTR lpszRet;
	size_t size = strlen(szString)+1;
	lpszRet = (LPTSTR)malloc(MAX_PATH);
	mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
	return lpszRet;
}

void WaitForProcessToAppear(LPCTSTR lpszProc, DWORD dwDelay)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	BOOL            bAppeared = FALSE;

	while(!bAppeared)
	{
		if ((hSnap = CreateToolhelp32Snapshot((DWORD)TH32CS_SNAPPROCESS, (DWORD)0)) != INVALID_HANDLE_VALUE)
		{
			peProc.dwSize = sizeof(PROCESSENTRY32);
			if(Process32First(hSnap, &peProc))
			{
				while(Process32Next(hSnap, &peProc) && !bAppeared)
				{
					if(!lstrcmp(lpszProc, peProc.szExeFile))
					{
						bAppeared = TRUE;
					}
					CloseHandle(hSnap);
				}
				Sleep(dwDelay);
			}
		}
	}
}
DWORD GetProcessIdByName(LPCTSTR lpcszProc)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	DWORD           dwRet = -1;
			
	if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
	{
		peProc.dwSize = sizeof(PROCESSENTRY32);
		if(Process32First(hSnap, &peProc))
		{
			while(Process32Next(hSnap, &peProc))
			{
				if(!lstrcmp(lpcszProc, peProc.szExeFile))
				{
					dwRet = peProc.th32ProcessID;
				}
				CloseHandle(hSnap);
			}
			
		}
		return dwRet;
	}
}
				
BOOL InjectDll(DWORD dwPid, char* szDllPath)
{
	DWORD       dwMemSize;
	HANDLE      hProc;
	LPVOID      IpRemoteMem, IpLoadLibrary;
	BOOL        bRet = FALSE;
	
	if((hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, dwPid)) != NULL)
	{
		dwMemSize = strlen(szDllPath)+1;
		if( (IpRemoteMem = VirtualAllocEx(hProc, NULL, dwMemSize, MEM_COMMIT, PAGE_READWRITE)) != NULL)
		{
			if(WriteProcessMemory(hProc, IpRemoteMem, (LPCVOID)szDllPath, dwMemSize, NULL))
			{
				IpLoadLibrary = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "LoadLibraryA");
				if(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)IpLoadLibrary, IpRemoteMem, 0, NULL)!= NULL)
				{
					bRet = TRUE;
				}
				CloseHandle(hProc);
			}					
			return bRet;
		}
	}
}

int main()
{
	char szProc[MAX_PATH], szDll[MAX_PATH];
	char* szDllPath = (char*)malloc(MAX_PATH);
	LPTSTR lpszProc = NULL;
	for(;;)
	{
		cout << "Process: ";
		cin >> szProc;
		cout << "DLL: ";
		cin >> szDll;
		szDllPath = GetCurrentDir();
		strcat_s(szDllPath, MAX_PATH, "\\");
		strcat_s(szDllPath, MAX_PATH, szDll);
		cout << "Waiting for process..." << endl;
		WaitForProcessToAppear(SzToLPCTSTR(szProc), 100);
		if(InjectDll(GetProcessIdByName(SzToLPCTSTR(szProc)), szDllPath))
		{
			cout << "Injection Success!" << endl;
		}
		else
		{
			cout << "Failed to inject!" << endl;
			cout << "\n";
		}
	}
	return 0;
}
disclaimer: it BUILDS, that's it - didn't test if it runs or does what it's supposed to
Quote Originally Posted by B1ackAnge1 View Post
Couple of missing brackets, semi colon, few too many comma's in places they shouldn't be, variable name mis-spellings, Upper vs Lower Case ..

And btw - no managed code in here.. just good old-fashioned Win32 C++

Not trying to sound like an arrogant prick ('cause i'm really not) lol but but this should be beginner level to figure this stuff out, especially when running it through a compiler and it basically telling you what the problem is Perhaps now that you see what the changes are, compare and see if it make sense with what the error message was for each of the errors. Learning opportunity here


Code:
#include <iostream>
#include <direct.h>
#include <windows.h>
#include <tlhelp32.h>
using namespace std;


char* GetCurrentDir()
{
	char* szRet = (char*)malloc(MAX_PATH);

	_getcwd(szRet, MAX_PATH);

	return szRet;

}

LPCTSTR SzToLPCTSTR(char* szString)
{
	LPTSTR lpszRet;
	size_t size = strlen(szString)+1;
	lpszRet = (LPTSTR)malloc(MAX_PATH);
	mbstowcs_s(NULL, lpszRet, size, szString, _TRUNCATE);
	return lpszRet;
}

void WaitForProcessToAppear(LPCTSTR lpszProc, DWORD dwDelay)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	BOOL            bAppeared = FALSE;

	while(!bAppeared)
	{
		if ((hSnap = CreateToolhelp32Snapshot((DWORD)TH32CS_SNAPPROCESS, (DWORD)0)) != INVALID_HANDLE_VALUE)
		{
			peProc.dwSize = sizeof(PROCESSENTRY32);
			if(Process32First(hSnap, &peProc))
			{
				while(Process32Next(hSnap, &peProc) && !bAppeared)
				{
					if(!lstrcmp(lpszProc, peProc.szExeFile))
					{
						bAppeared = TRUE;
					}
					CloseHandle(hSnap);
				}
				Sleep(dwDelay);
			}
		}
	}
}
DWORD GetProcessIdByName(LPCTSTR lpcszProc)
{
	HANDLE          hSnap;
	PROCESSENTRY32  peProc;
	DWORD           dwRet = -1;
			
	if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) != INVALID_HANDLE_VALUE)
	{
		peProc.dwSize = sizeof(PROCESSENTRY32);
		if(Process32First(hSnap, &peProc))
		{
			while(Process32Next(hSnap, &peProc))
			{
				if(!lstrcmp(lpcszProc, peProc.szExeFile))
				{
					dwRet = peProc.th32ProcessID;
				}
				CloseHandle(hSnap);
			}
			
		}
		return dwRet;
	}
}
				
BOOL InjectDll(DWORD dwPid, char* szDllPath)
{
	DWORD       dwMemSize;
	HANDLE      hProc;
	LPVOID      IpRemoteMem, IpLoadLibrary;
	BOOL        bRet = FALSE;
	
	if((hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, dwPid)) != NULL)
	{
		dwMemSize = strlen(szDllPath)+1;
		if( (IpRemoteMem = VirtualAllocEx(hProc, NULL, dwMemSize, MEM_COMMIT, PAGE_READWRITE)) != NULL)
		{
			if(WriteProcessMemory(hProc, IpRemoteMem, (LPCVOID)szDllPath, dwMemSize, NULL))
			{
				IpLoadLibrary = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "LoadLibraryA");
				if(CreateRemoteThread(hProc, NULL, 0, (LPTHREAD_START_ROUTINE)IpLoadLibrary, IpRemoteMem, 0, NULL)!= NULL)
				{
					bRet = TRUE;
				}
				CloseHandle(hProc);
			}					
			return bRet;
		}
	}
}

int main()
{
	char szProc[MAX_PATH], szDll[MAX_PATH];
	char* szDllPath = (char*)malloc(MAX_PATH);
	LPTSTR lpszProc = NULL;
	for(;;)
	{
		cout << "Process: ";
		cin >> szProc;
		cout << "DLL: ";
		cin >> szDll;
		szDllPath = GetCurrentDir();
		strcat_s(szDllPath, MAX_PATH, "\\");
		strcat_s(szDllPath, MAX_PATH, szDll);
		cout << "Waiting for process..." << endl;
		WaitForProcessToAppear(SzToLPCTSTR(szProc), 100);
		if(InjectDll(GetProcessIdByName(SzToLPCTSTR(szProc)), szDllPath))
		{
			cout << "Injection Success!" << endl;
		}
		else
		{
			cout << "Failed to inject!" << endl;
			cout << "\n";
		}
	}
	return 0;
}
Is that like a major changed code?? And the Changing the side to Size brought me down by 1 error, now I'm at 9...
The code I pasted has 0 build errors, so copy & paste that
Quote Originally Posted by B1ackAnge1 View Post
Couple of missing brackets, semi colon, few too many comma's in places they shouldn't be, variable name mis-spellings, Upper vs Lower Case ..

And btw - no managed code in here.. just good old-fashioned Win32 C++

Not trying to sound like an arrogant prick ('cause i'm really not) lol but but this should be beginner level to figure this stuff out, especially when running it through a compiler and it basically telling you what the problem is Perhaps now that you see what the changes are, compare and see if it make sense with what the error message was for each of the errors. Learning opportunity here

disclaimer: it BUILDS, that's it - didn't test if it runs or does what it's supposed to
ok one last question. What is MAX_PATH. It comes up all throughout the code and I have no idea what it is. not even the slightest.
Posts 1–15 of 49 · Page 1 of 4
This thread is closed for replies.

Similar Threads

Tags for this Thread

Need help?