[D] Hydra - Memory manipulation library for the D language menu

User Tag List

Results 1 to 2 of 2
  1. #1
    Hybro's Avatar Private
    Reputation
    10
    Join Date
    Apr 2013
    Posts
    2
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [D] Hydra - Memory manipulation library for the D language

    Hey there, I've been playing around with the D programming language lately, which is an pretty cool alternative to c and c++ (in my opinion).
    I have created a simple memory manipulation class to test how easy it is to use for such things.
    There's much room for improvements, but it works
    Are there any other dudes around here using D ?

    memory.d :
    Code:
    module hydra.memory;
    
    import core.sys.windows.windows;
    import imports.win32;
    import std.string;
    
    class memory
    {
    	
    	// ----------------------------------------------------------------------
    	// Constructors and Destructors
    
    	/**
    	 * Destructor
    	 */
    	~this()
    	{
    		if(m_isAttached)
    		{
    			closeProcess();
    		}
    	}
    
    	
    	// ----------------------------------------------------------------------
    	// Open Process
    
    	/**
    	 * Opens a Process for editing
    	 * 
    	 * Params:
    	 * 		processId = the ID of the Process 
    	 * 
    	 * Throws: Exception on failure
    	 */
    	public void openProcess(uint processId)
    	{
    		if(!m_isAttached)
    		{
    			m_processHandle = OpenProcess(ProcessAccess.AllAccess, 0, processId);
    
    			if(!m_processHandle)
    			{
    				throw new Exception(std.string.format("Can't open Process with ID: %s -- Error: %s", processId, GetLastError()));
    			}
    
    			scope(success)
    			{
    				m_processId = processId;
    				m_isAttached = true;
    			}
    
    			scope(failure)
    			{
    				if(m_processHandle)
    				{
    					imports.win32.CloseHandle(m_processHandle);
    				}
    			}
    		}
    		else
    		{
    			closeProcess();
    			openProcess(processId);
    		}
    	}
    
    	// ----------------------------------------------------------------------
    	// Read and Write Memory
    
    	/**
    	 * Writes the data to memory at the specified address
    	 * 
    	 * Params:
    	 * 		address = the memory address you want to read from
    	 * 		value = the data you want to write
    	 * 
    	 * Throws: Exception on failure
    	 */
    	void write(T)(DWORD address, T value)
    	{
    		if(!m_isAttached)
    			throw new Exception("Not attached to Process");
    
    		PDWORD bytesRead;
    		if(!WriteProcessMemory(cast(HANDLE)m_processHandle, cast(LPVOID)address, cast(LPCVOID)&value, cast(SIZE_T)(T.sizeof), cast(SIZE_T*)&bytesRead))
    			throw new Exception(std.string.format("Can't write memory at: %s -- Error: %s", address, GetLastError()));
    	}
    
    	/**
    	 * Writes a char array to the specified address
    	 * 
    	 * Params:
    	 * 		address = the memory address you want to read from
    	 * 		value = the string you want to write
    	 * 
    	 * Throws: Exception on failure
    	 */
    	void writeString(DWORD address, char[1024] value)
    	{
    		if(!m_isAttached)
    			throw new Exception("Not attached to Process");
    
    		PDWORD bytesRead;
    		if(!WriteProcessMemory(cast(HANDLE)m_processHandle, cast(LPVOID)address, cast(LPCVOID)&value, cast(SIZE_T)(value.sizeof), cast(SIZE_T*)&bytesRead))
    			throw new Exception(std.string.format("Can't write memory at: %s -- Error: %s", address, GetLastError()));
    	}
    
    	/**
    	 * Reads memory at the specified address
    	 * 
    	 * Params:
    	 * 		address = the memory address you want to read from
    	 * 
    	 * Throws: Exception on failure
    	 */
    	T read(T)(DWORD address)
    	{
    		if(!m_isAttached)
    			throw new Exception("Not attached to Process");
    
    		T data;
    		PDWORD bytesRead;
    
    		if(!ReadProcessMemory(cast(HANDLE)m_processHandle, cast(PCVOID)address, cast(PVOID)&data, cast(DWORD)T.sizeof, cast(PDWORD)&bytesRead))
    			throw new Exception(std.string.format("Can't read memory at: %s -- Error: %s", address, GetLastError()));
    
    		return data;
    	}
    
    	/**
    	 * Reads a char array at the specified address from memory
    	 * 
    	 * Params:
    	 * 		address = the memory address you want to read from
    	 * 
    	 * Throws: Exception on failure
    	 */
    	char[1024] readString(DWORD address)
    	{
    		if(!m_isAttached)
    			throw new Exception("Not attached to Process");
    
    		char[1024] data;
    
    		PDWORD bytesRead;
    
    		if(!ReadProcessMemory(cast(HANDLE)m_processHandle, cast(PCVOID)address, cast(PVOID)&data, cast(DWORD)1024, cast(PDWORD)&bytesRead))
    			throw new Exception(std.string.format("Can't read memory at: %s -- Error: %s", address, GetLastError()));
    		
    		return data;
    	}
    
    	/**
    	 * Closes the open Process
    	 */
    	private void closeProcess()
    	{
    		if(m_isAttached)
    		{
    			if(!imports.win32.CloseHandle(m_processHandle))
    				throw new Exception(std.string.format("Can't close Process -- Error: %s", GetLastError()));
    
    			scope(success)
    			{
    				m_isAttached = false;
    				m_processId = 0;
    			}
    		}
    	}
    
    	private bool 	m_isAttached;
    	private uint 	m_processId;
    	private HANDLE 	m_processHandle;
    }
    win32.d :
    Code:
    module imports.win32;
    
    import core.sys.windows.windows;
    
    extern(Windows)
    {
    	// ----------------------------------------------------------------------
    	// Vartypes
    	alias const(void)* PCVOID, LPCVOID;
    
    	// ----------------------------------------------------------------------
    	// Functions
    	HANDLE 	OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
    	BOOL    ReadProcessMemory(HANDLE hProcess, LPCVOID lpBaseAddress, LPVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesRead);
    	BOOL    WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten);
    	BOOL 	CloseHandle(HANDLE);
    
    	// ----------------------------------------------------------------------
    	// Enums
    	enum ProcessAccess : DWORD
    	{
    		CreateThread = 0x0002,
    		SetSessionId = 0x0004,
    		VmOperation = 0x0008,
    		VmRead = 0x0010,
    		VmWrite = 0x0020,
    		DupHandle = 0x0040,
    		CreateProcess = 0x0080,
    		SetQuota = 0x0100,
    		SetInformation = 0x0200,
    		QueryInformation = 0x0400,
    		SuspendResume = 0x0800,
    		QueryLimitedInformation = 0x1000,
    		Synchronize = 0x100000,
    		Delete = 0x00010000,
    		ReadControl = 0x00020000,
    		WriteDac = 0x00040000,
    		WriteOwner = 0x00080000,
    		StandardRightsRequired = 0x000F0000,
    		
    		AllAccess = StandardRightsRequired | Synchronize | 0xFFFF
    	}
    }

    Usage:
    Code:
    module main;
    
    import std.stdio;
    
    import hydra.memory;
    
    void main(string[] args)
    {
    	memory mem = new memory();
    
    	try
    	{
    		mem.openProcess(17484);
    		mem.read!(int)(0x1212);
    	}
    	catch(Exception ex)
    	{
    		writeln(ex.msg);
    		writeln(ex.info);
    	}
    
    	stdin.readln();
    }

    Feel free to criticize as much as you can
    Last edited by Hybro; 08-31-2013 at 05:35 PM.

    [D] Hydra - Memory manipulation library for the D language
  2. Thanks Kirth (1 members gave Thanks to Hybro for this useful post)
  3. #2
    Bananenbrot's Avatar Contributor
    Reputation
    153
    Join Date
    Nov 2009
    Posts
    384
    Thanks G/R
    1/3
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm very much interested in using D, too. But lately, I sadly don't have the time to try it in a botting scenario.
    Some minor nit picks: You should conform with the D naming convention, in particular your class should be named Memory.
    Also instead of hardcoding char[1024], you should expose the 1024 in a template parameter.
    Also you should avoid mutating functions like openProcess and just use a static factory method or use do the opening in the ctor directly if you want to.
    Since D got UFCS, I would even extract a struct for the process handle and define those as free functions. If you would ever need polymorphism, consider declaring an interface and using wrap/unwrap from std.typecons (structural inheritance).
    Just noticing that it hasn't yet made it into the docs... should be there however.

  4. Thanks Kirth (1 members gave Thanks to Bananenbrot for this useful post)

Similar Threads

  1. [Release] SHInject - A small Memory Editing library for 3.3.5:12340
    By Blackplayer27 in forum WoW Memory Editing
    Replies: 4
    Last Post: 09-22-2020, 06:06 PM
  2. Memory editing/reading library for Go
    By Nikentic in forum Programming
    Replies: 0
    Last Post: 05-10-2016, 08:54 AM
  3. HadesMem - A Windows Memory Hacking Library for C++
    By Cypher in forum WoW Memory Editing
    Replies: 81
    Last Post: 02-10-2013, 03:24 PM
  4. ok for the gift wrap scam dnt do
    By krazy12766 in forum World of Warcraft General
    Replies: 24
    Last Post: 10-19-2006, 07:27 AM
  5. I apologize for the downtime.
    By Matt in forum Community Chat
    Replies: 7
    Last Post: 06-21-2006, 07:26 PM
All times are GMT -5. The time now is 09:47 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search