Sorry, you don't have access to the chatbox yet, VIPs and people with more than 2 posts get access to the chatbox :)


Thread Rating:
  • 3 Vote(s) - 2.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C#] Trainer Base
17-08-2010, 05:41 PM,
Post: #1
[C#] Trainer Base
The basics of a trainer (to modify a games' memory) I wrote in C#

Trainer.cs
PHP Code:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace 
MMOhaxCsharpTrainerBase
{
    public 
partial class Trainer Form
    
{
        public 
Trainer()
        {
            
InitializeComponent();
        }

        
// imports from WinAPI, for more information see http://www.pinvoke.net/ and http://msdn.microsoft.com/

        // http://www.pinvoke.net/default.aspx/kernel32/WriteProcessMemory.html
        // WriteProcessMemory writes memory to a specific address in the target process memory space
        
[DllImport("kernel32.dll"EntryPoint "WriteProcessMemory")]
        private static 
extern bool WriteProcessMemory(IntPtr hProcessIntPtr lpBaseAddressbyte[] lpBufferuint nSize, [Outint lpNumberOfBytesWritten);

        
// http://www.pinvoke.net/default.aspx/kernel32/ReadProcessMemory.html
        // ReadProcessMemory reads memory from a specified address in the target process memory space
        
[DllImport("kernel32.dll"EntryPoint "ReadProcessMemory")]
        private static 
extern bool ReadProcessMemory(IntPtr hProcessIntPtr lpBaseAddress, [Outbyte[] lpBufferint dwSize, [Outint lpNumberOfBytesRead);
        [
DllImport("kernel32.dll"EntryPoint "ReadProcessMemory")]
        private static 
extern bool ReadProcessMemory(IntPtr hProcessIntPtr lpBaseAddress, [OutIntPtr lpBufferint dwSize, [Outint lpNumberOfBytesRead);

        
// http://www.pinvoke.net/default.aspx/kernel32/OpenProcess.html
        // OpenProcess is used to open the process (obviously)
        
[DllImport("kernel32.dll"EntryPoint "OpenProcess")]
        private static 
extern IntPtr OpenProcess(uint dwDesiredAccessbool bInheritHandleint dwProcessId);

        private static 
int ProcessID = -1// will hold ID of the game process
        
private static IntPtr ProcessHandle IntPtr.Zero// will hold handle to the game process

        // Connect function will open the game process
        
private bool Connect()
        {
            
Process.EnterDebugMode(); // gain debug privileges

            // GetProcessesByName gets all running processes with the specified name
            
Process[] processes Process.GetProcessesByName("winmine"); // winmine.exe is Windows XP Minesweeper
            
ProcessID processes[0].Id// assume the first found process is the correct one, because otherwise 2 instances of the game would be running

            
if (ProcessID == 0)
            {
                
// game process not found
                
Process.LeaveDebugMode();
                return 
false;
            }

            
// open process and save the handle of it
            // we start looking up OpenProcess at MSDN http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx
            // "The access to the process object. This access right is checked against the security descriptor for the process. This parameter can be one or more of the process access rights."
            // click the link to "process access rights", http://msdn.microsoft.com/en-us/library/ms684880(v=VS.85).aspx
            // PROCESS_ALL_ACCESS  -  All possible access rights for a process object.
            // yeah, we might aswell use that
            // if we look at http://www.pinvoke.net/default.aspx/kernel32/OpenProcess.html
            // we see that All = 0x001F0FFF
            
ProcessHandle OpenProcess(0x001F0FFF/*PROCESS_ALL_ACCESS*/falseProcessID);

            return 
true;
        }

        
// Disconnect function will close the game process & clean up
        
private void Disconnect()
        {
            
Process.LeaveDebugMode(); // no need to still have debug privileges
        
}

        private 
uint adrTime 0x0100579C// this is the address where time-variable is located in Windows XP Minesweeper, get this with Cheat Engine
        
private uint freezeTime 0;

        private 
void checkBox_CheckedChanged(object senderEventArgs e)
        {
            if (
Connect() == false) { return; }

            
byte[] buffer = new byte[4];
            
ReadProcessMemory(ProcessHandle, (IntPtr)adrTimebufferbuffer.Length0);
            
freezeTime BitConverter.ToUInt32(buffer0);

            
Disconnect();
        }

        private 
void timer_Tick(object senderEventArgs e)
        {
            if (
Connect() == false) { return; }

            if (
checkBox.Checked == true// if the "freeze value" checkbox is ticked, make sure the value is same as when frozen
            
{
                
WriteProcessMemory(ProcessHandle, (IntPtr)adrTimeBitConverter.GetBytes(freezeTime), 4/*an int is 4 bytes in size*/0);
            }

            
byte[] buffer = new byte[4];
            
ReadProcessMemory(ProcessHandle, (IntPtr)adrTimebufferbuffer.Length0);

            
uint Time BitConverter.ToUInt32(buffer0);
            
textBox.Text Time.ToString();

            
Disconnect();
        }

        private 
void button_Click(object senderEventArgs e)
        {
            if (
Connect() == false) { return; }

            
// set game timer to 0 when button is clicked
            
WriteProcessMemory(ProcessHandle, (IntPtr)adrTimeBitConverter.GetBytes(0), 4/*an int is 4 bytes in size*/0);
            
freezeTime 0;

            
Disconnect();
        }

        private 
void pictureBox_Click(object senderEventArgs e)
        {
            
// when picture is clicked, open website
            
System.Diagnostics.Process.Start("http://www.mmohax.com/");
        }
    }


Download as Visual Studio 2010 Project
[Image: sitelogo.png]
Reply
18-08-2010, 09:16 AM,
Post: #2
RE: [C#] Trainer Base
appreciate the comments.
They are very informative.

I started using C# in college, and recently decided to pick it back up again.
it's one of the easier languages to learn imo.
So far, only language thats really pissed me off was Java, but maybe cuz it was the first language I learned.
Reply
18-08-2010, 03:21 PM,
Post: #3
RE: [C#] Trainer Base
(18-08-2010, 09:16 AM)phonz Wrote: it's one of the easier languages to learn imo.
Definently!
And it's a good way to the more advanced languages like C++.
[Image: sitelogo.png]
Reply




Users browsing this thread: 1 Guest(s)