[C++]Pattern Search Not Working

Posts 1–4 of 4 · Page 1 of 1
[C++]Pattern Search Not Working
it compiling perfectly but when i try to inject it and press F2 it would just kill the game but with CE it works can someone help me please??
Code:
#include "stdafx.h"

DWORD FindPattern( DWORD dwStart, DWORD dwLen, BYTE *pszPatt, char pszMask[] )
{
	unsigned int i = NULL;
	int iLen = strlen( pszMask ) - 1;

	for( DWORD dwRet = dwStart; dwRet < dwStart + dwLen; dwRet++ )
	{
		if( *( BYTE * ) dwRet == pszPatt[ i ] || pszMask[ i ] == '?' )
		{
			if( pszMask[ i + 1 ] == '\0' )
				return( dwRet - iLen );
			i++;
		}
		else i = NULL;
	}

	return NULL;
}

void memwrite(void *adr,void *ptr,int size)
{
	DWORD Protection;
	VirtualProtect(adr,size,PAGE_EXECUTE_READWRITE,&Protection);
	memcpy(adr,ptr,size);
	VirtualProtect(adr,size,Protection,&Protection);
}

DWORD client = (DWORD)GetModuleHandleA("client.exe");
DWORD speedhack = FindPattern(client,0xFFFFFF,(BYTE *)"\x66\x0F\x5A\xC0\xF3\x0F\x11\x45\xF0\x8B\x4E","xxxxxxxxxxx");

void Engine()
{
	while (TRUE)
	{
	//////////////////////////////////////////////////
	if (GetAsyncKeyState(VK_F2) & 0x8000)
	{
		memwrite((void*)(speedhack), (void *) (PBYTE) "\x66\x0F\x5A\xC1\xF3\x0F\x11\x45\xF0\x8B\x4E", 11);
		Beep(750, 500);
	}
	//////////////////////////////////////////////////
	}
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	CreateThread(0, 0, (LPTHREAD_START_ROUTINE)Engine, 0, 0, 0);
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}
@luizimloko
Make a Screenshot from the Cheat Engine Offsets.
Off Topic: Your Patternscanner has an Issue aswell:
Lets say you search for: "\x66\x0F\x5A\xC0\xF3\x0F\x11\x45\xF0\x8B\x4E" and in the Memory There is "\x66\x0F\x5A\x66\x0F\x5A\xC0\xF3\x0F\x11\x45\xF0\x8B\x4E"
Even the Instructions makes no sense anymore, what i want to show u is, what your pattern scanner will do is: it finds the first 66, the first 0F and the first 5A then it will break and go further with the second 0F because you dont check element [0] again
Code:
char* ptrScan(const std::string& pattern, const std::string& mask, int find)
{
	uintptr_t base = (uintptr_t)GetModuleHandleA(nullptr);
	char* ptr = reinterpret_cast<char*>(base);
	char* end = reinterpret_cast<char*>(getImageSize(base));
	size_t matchlen = mask.size();
	for (int i = 0, found = 0; ptr != end; ptr++)
	{
		if (*ptr == pattern[i] || mask[i] == '?')
		{
			if (++i == matchlen)
			{
				if (find != found)
				{
					i = 0;
					found++;
				}
				else
				{
					ptr -= matchlen - 1;
					break;
				}
			}
		}
		else if (i > 0 && (*ptr == pattern[0] || mask[0] == '?'))
		{
			i = 1;
		}
		else
		{
			i = 0;
		}
	}
	if (ptr == end)
		return nullptr;
	return ptr;
}


- - - Updated - - -

And i saw something else:
Code:
DWORD speedhack = FindPattern(client,0xFFFFFF,(BYTE *)"\x66\x0F\x5A\xC0\xF3\x0F\x11\x45\xF0\x8B\x4E","xxxxxxxxxxx");
// ...
memwrite((void*)(speedhack), (void *) (PBYTE) "\x66\x0F\x5A\xC1\xF3\x0F\x11\x45\xF0\x8B\x4E", 11);
You writes 11 Chars, but wants to change 1:
Code:
unsigned char* speedhack = reinterpret_cast<unsigned char*>(FindPattern(client,0xFFFFFF,(BYTE *)"\x66\x0F\x5A\xC0\xF3\x0F\x11\x45\xF0\x8B\x4E","xxxxxxxxxxx"));
speedhack[3] = '\C1'; /* 0 = 66, 1 = 0F, 2= 5A, 3 = C0 */
that's more complicated than i thought
i can make memory patching with static offset i just want to try memory patching with pattern scanning
can u recommend any pattern scanner that i can import as header and it's usage
I have no recommendation for you, because i've written my own one and doesn't know any others
mem.h
Code:
#ifndef __MEMORY_SCANNER__
#include <string>
#include <sstream>

char* ptrScan(const std::string& pattern, const std::string& mask, int find = 0);
char* ptrScan(const std::string& pattern, int find = 0);
char* rel(char* addr, int rel = 3);
#define __MEMORY_SCANNER__
#endif
mem.cpp
Code:
bool isHex(char c)
{
	return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
}

bool createPattern(const std::string& pattern, std::string& pattern_result, std::string& mask_result)
{
	bool result = false;
	size_t l = pattern.size();
	char buffer[2];
	if (l-- > 0)
	{
		std::stringstream pattern_s;
		std::stringstream mask_s;
		for (size_t i = 0; i < l; i++)
		{
			if (!isHex(pattern[i]))
			{
				if (pattern[i] == 63)
				{
					pattern_s << "\x90";
					mask_s << '?';
				}
			}
			else
			{
				buffer[0] = pattern[i];
				buffer[1] = (l >= i + 1 && isHex(pattern[i + 1])) ? pattern[++i] : 0;
				pattern_s << static_cast<char>(strtol(buffer, nullptr, 16));
				mask_s << 'x';
			}
		}
		result = true;
		pattern_result = pattern_s.str();
		mask_result = mask_s.str();
	}
	return result;
}

uintptr_t getImageSize(uintptr_t moduleBase)
{
	const IMAGE_DOS_HEADER*	headerDos = reinterpret_cast<const IMAGE_DOS_HEADER*>(moduleBase);
	const IMAGE_NT_HEADERS*	headerNt = reinterpret_cast<const IMAGE_NT_HEADERS*>(headerDos + headerDos->e_lfanew);
	return moduleBase + headerNt->OptionalHeader.SizeOfCode;
}

char* ptrScan(const std::string& pattern, const std::string& mask, int find)
{
	uintptr_t base = (uintptr_t)GetModuleHandleA(nullptr);
	char* ptr = reinterpret_cast<char*>(base);
	char* end = reinterpret_cast<char*>(getImageSize(base));
	size_t matchlen = mask.size();
	for (int i = 0, found = 0; ptr != end; ptr++)
	{
		if (*ptr == pattern[i] || mask[i] == '?')
		{
			if (++i == matchlen)
			{
				if (find != found)
				{
					i = 0;
					found++;
				}
				else
				{
					ptr -= matchlen - 1;
					break;
				}
			}
		}
		else if (i > 0 && (*ptr == pattern[0] || mask[0] == '?'))
		{
			i = 1;
		}
		else
		{
			i = 0;
		}
	}
	if (ptr == end)
		return nullptr;
	return ptr;
}

char* ptrScan(const std::string& pattern, int find)
{
	std::string sub_ptr;
	std::string sub_mask;
	createPattern(pattern, sub_ptr, sub_mask);
	return ptrScan(sub_ptr, sub_mask, find);
}

char* rel(char* addr, int rel)
{
	const int JUMP_SIZE = 4;
	return *reinterpret_cast<char**>(addr + *reinterpret_cast<int*>(addr + rel) + (JUMP_SIZE + rel));
}
in your stdafx.h:
Code:
#include "mem.h"
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us