Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code enhancements #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nbqmemory/Source.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "nbqmemory.h"
#include <stdio.h>

#include "nbqmemory.h"

int main()
{
int main() {
nbqmemory memory("csgo.exe", PROCESS_ALL_ACCESS);
getchar();
(void)getchar();
return 0;
}
124 changes: 70 additions & 54 deletions nbqmemory/nbqmemory.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
#include "nbqmemory.h"

nbqmemory::nbqmemory()
{
#include <cstdio>
#include <TlHelp32.h>

template <typename... Args>
void debug_log(Args... args) {
#ifdef _DEBUG
(void)printf(args...);
#endif // _DEBUG
}

nbqmemory::nbqmemory(const char* process_name, DWORD access_rights) {
if (!attach(process_name, access_rights))
debug_log("[-] Failed to attach to the process %s with access rights %lu\n", process_name, access_rights);
}

nbqmemory::nbqmemory(const char* process_name, DWORD access_rights)
{
attach(process_name, access_rights);
void nbqmemory::detach() {
if (this->process_handle && !CloseHandle(this->process_handle)) {
debug_log("[-] Failed to detach\n");
}
}

nbqmemory::~nbqmemory()
{
nbqmemory::~nbqmemory() {
detach();
}

bool nbqmemory::attach(const char* process_name, DWORD access_rights)
{
HANDLE ss = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (ss)
{
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
if (Process32First(ss, &pe))
{
do
{
if (!strcmp(pe.szExeFile, process_name))
{
this->process_handle = OpenProcess(access_rights, false, pe.th32ProcessID);
}
} while (Process32Next(ss, &pe));
}
CloseHandle(ss);
DWORD search_for_process_id(const char* process_name) {
HANDLE process_snapshot_handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (!process_snapshot_handle) {
debug_log("[-] Failed to take a snapshot of processes, last error %lu\n", GetLastError());
return 0;
}

PROCESSENTRY32 process_entry{ sizeof(PROCESSENTRY32) };

if (Process32First(process_snapshot_handle, &process_entry)) {
do {
if (strcmp(process_entry.szExeFile, process_name) == 0) {
return process_entry.th32ProcessID;
}
} while (Process32Next(process_snapshot_handle, &process_entry));
}
printf("process_handle(%s): 0x%x\n", process_name, (DWORD)this->process_handle);
if (this->process_handle) { return true; } else { return false; }
else {
debug_log("[-] Failed to copy the first process entry to the buffer, last error %lu\n", GetLastError());
}

(void)CloseHandle(process_snapshot_handle);

return 0;
}

bool nbqmemory::detach()
{
return CloseHandle(this->process_handle);
bool nbqmemory::attach(const char* process_name, DWORD access_rights) {
DWORD pid = search_for_process_id(process_name);
this->process_handle = OpenProcess(access_rights, false, pid);

// This will print even when we fail to open a handle, not sure if you really want this...
debug_log("[=] process_handle(%s): 0x%08X\n", process_name, reinterpret_cast<DWORD>(this->process_handle));

return static_cast<bool>(this->process_handle);
}

module nbqmemory::get_module(const char* module_name)
{

module nbqmemory::get_module(const char* module_name) {
module mod = { 0 };
HANDLE ss = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, GetProcessId(this->process_handle));
if (ss)
{
if (ss) {
MODULEENTRY32 me;
me.dwSize = sizeof(MODULEENTRY32);
if (Module32First(ss, &me))
{
if (Module32First(ss, &me)) {
do
{
if (!strcmp(me.szModule, module_name))
{
if (!strcmp(me.szModule, module_name)) {
mod.base = (DWORD)me.modBaseAddr;
mod.size = (DWORD)me.modBaseSize;
break;
Expand All @@ -67,31 +81,34 @@ module nbqmemory::get_module(const char* module_name)
return mod;
}

bool nbqmemory::compare_memory(const byte* data, const char* pattern)
{
for (; *pattern; *pattern != ' ' ? ++data : data, ++pattern)
{
if (*pattern == ' ' || *pattern == '?') continue;
if (*data != get_byte(pattern)) return false;
bool nbqmemory::compare_memory(const byte* data, const char* pattern) {
for (; *pattern; *pattern != ' ' ? ++data : data, ++pattern) {

if (*pattern == ' ' || *pattern == '?')
continue;
if (*data != get_byte(pattern))
return false;

++pattern;
}
return true;
}

DWORD nbqmemory::pattern_scan(module mod, const char* pattern, int offset, int extra, bool relative, bool subtract)
{
DWORD nbqmemory::pattern_scan(module mod, const char* pattern, int offset, int extra, bool relative, bool subtract) {
DWORD address = 0;

byte* data = new byte[mod.size];
ReadProcessMemory(this->process_handle, (LPCVOID)mod.base, data, mod.size, NULL);

for (DWORD i = 0x1000; i < mod.size; i++)
{
if (compare_memory((const byte*)(data + i), pattern))
{
for (DWORD i = 0x1000; i < mod.size; i++) {
if (compare_memory((const byte*)(data + i), pattern)) {
address = mod.base + i + offset;
if (relative) { ReadProcessMemory(this->process_handle, LPCVOID(address), &address, sizeof(DWORD), NULL); }
if (subtract) { address -= mod.base; }

if (relative)
ReadProcessMemory(this->process_handle, LPCVOID(address), &address, sizeof(DWORD), NULL);
if (subtract)
address -= mod.base;

address += extra;
break;
}
Expand All @@ -100,4 +117,3 @@ DWORD nbqmemory::pattern_scan(module mod, const char* pattern, int offset, int e
delete[] data;
return address;
}

33 changes: 15 additions & 18 deletions nbqmemory/nbqmemory.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <Windows.h>
#include <TlHelp32.h>
#include <iostream>

#define in_range(x, a, b) (x >= a && x <= b)
#define get_bits(x) (in_range(x, '0', '9') ? (x - '0') : ((x & (~0x20)) - 'A' + 0xa))
Expand All @@ -16,34 +14,33 @@ typedef struct _module

class nbqmemory
{
private:
private:

HANDLE process_handle = NULL;
HANDLE process_handle = NULL;

public:
public:

nbqmemory();
nbqmemory() = default;

nbqmemory(const char* process_name, DWORD access_rights);
nbqmemory(const char* process_name, DWORD access_rights);

~nbqmemory();
void detach();

bool attach(const char* process_name, DWORD access_rights);
~nbqmemory();

bool detach();
bool attach(const char* process_name, DWORD access_rights);

module get_module(const char* module_name);
module get_module(const char* module_name);

bool compare_memory(const byte* data, const char* pattern);
bool compare_memory(const byte* data, const char* pattern);

DWORD pattern_scan(module mod, const char* pattern, int offset, int extra, bool relative, bool subtract);
DWORD pattern_scan(module mod, const char* pattern, int offset, int extra, bool relative, bool subtract);

template <typename t>
t read_memory(DWORD address);
template <typename t>
t read_memory(DWORD address);

template <typename t>
void write_memory(DWORD address, t value);

template <typename t>
void write_memory(DWORD address, t value);
};

template<typename t>
Expand Down