Skip to content

NIR3X/safe_ptr.hpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safe_ptr - A C++ utility for safe memory reading

A C++ utility for safely reading memory from a specified process using ReadProcessMemory. This module provides a safe_ptr class that allows for optional memory protection when accessing addresses.

Features

  • Safe memory reading using ReadProcessMemory.
  • Supports optional memory protection via VirtualProtectEx.
  • Works with different data types using std::optional<T>.
  • Can be used for introspection and debugging purposes.

Installation

Clone the repository and include safe_ptr.hpp in your project:

git clone https://github.com/NIR3X/safe_ptr.hpp
cd safe_ptr.hpp

Include the header file in your C++ project:

#include "safe_ptr.hpp"

Usage

Basic Example

#include "safe_ptr.hpp"
#include <iostream>

int main() {
	int someVariable = 42;
	safe_ptr intPtr(&someVariable);
	
	auto value = intPtr._<int>(0);
	if (value.has_value()) {
		std::cout << "Value: " << *value << std::endl;
	} else {
		std::cout << "Failed to read memory" << std::endl;
	}
}

Test Code

A test file (test.cpp) is provided to verify the functionality:

#include "safe_ptr.hpp"
#include <cassert>
#include <cstdio>

int main() {
	safe_ptr xFFFFPtr((PVOID)0xFFFF);
	assert(!xFFFFPtr._<uint8_t>(0).has_value());

	safe_ptr mainPtr(&main);
	assert(mainPtr._<uint8_t>(0).has_value());

	safe_ptr modulePtr(GetModuleHandleW(NULL));
	assert(modulePtr._<uint16_t>(0) == IMAGE_DOS_SIGNATURE);

	auto imageDosHeaderOpt = modulePtr._<IMAGE_DOS_HEADER>(0);
	assert(imageDosHeaderOpt.has_value());
	
	auto imageNtHeaderOpt = modulePtr._<IMAGE_NT_HEADERS>(imageDosHeaderOpt->e_lfanew);
	assert(imageNtHeaderOpt.has_value());
	assert(imageNtHeaderOpt->Signature == IMAGE_NT_SIGNATURE);

	puts("PASS");
}

Requirements

  • C++17 or later
  • Windows platform (uses windows.h APIs)

License

GNU AGPLv3 Image

This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy