Results 1 to 4 of 4
  1. #1
    normiescum's Avatar
    Join Date
    Sep 2016
    Gender
    female
    Posts
    5
    Reputation
    10
    Thanks
    2

    Write Process Memory 64bit

    Trying to code a tool. Got the main part of the code from mpgh have had it saved for ages can't remember where exactly I got it from though. It's all working for 32bit applications but I can not add the addresses 64bit ones without getting an error due to them being 64bit. Have tried to write them as int64 to avoid this and built successfully however it simply did not work I think I'm just doing something stupid can someone take a look at the code for me and help me out it's been quite a while my last release was in 2009. ps this is the working code for writing 32bit addresses.







    ' Memory Module

    Option Strict On

    Imports System.Runtime.InteropServices
    Imports System.Text

    Module MemoryModule
    <DllImport("kernel32.dll")>
    Private Function OpenProcess(ByVal dwDesiredAccess As UInteger, <MarshalAs(UnmanagedType.Bool)> ByVal bInheritHandle As Boolean, ByVal dwProcessId As Integer) As IntPtr
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>
    Private Function WriteProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer As Byte(), ByVal nSize As IntPtr, <Out()> ByRef lpNumberOfBytesWritten As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>
    Private Function ReadProcessMemory(ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, <Out()> ByVal lpBuffer() As Byte, ByVal dwSize As IntPtr, ByRef lpNumberOfBytesRead As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)>
    Private Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
    End Function

    Private Const PROCESS_VM_WRITE As UInteger = &H20
    Private Const PROCESS_VM_READ As UInteger = &H10
    Private Const PROCESS_VM_OPERATION As UInteger = &H8
    Private Const TargetProcess As String = "tetris"
    Private ProcessHandle As IntPtr = IntPtr.Zero
    Private LastKnownPID As Integer = -1

    Public Function ReadMemory(Of T)(ByVal address As Integer) As T
    Return ReadMemory(Of T)(address, 0, False)
    End Function

    Public Function ReadMemory(ByVal address As Integer, ByVal length As Integer) As Byte()
    Return ReadMemory(Of Byte())(address, length, False)
    End Function

    Private Function ProcessIDExists(ByVal pID As Integer) As Boolean
    For Each p As Process In Process.GetProcessesByName(TargetProcess)
    If p.Id = pID Then Return True
    Next
    Return False
    End Function

    Private Function UpdateProcessHandle() As Boolean
    If LastKnownPID = -1 OrElse Not ProcessIDExists(LastKnownPID) Then
    If ProcessHandle <> IntPtr.Zero Then CloseHandle(ProcessHandle)
    Dim p() As Process = Process.GetProcessesByName(TargetProcess)
    If p.Length = 0 Then Return False
    LastKnownPID = p(0).Id
    ProcessHandle = OpenProcess(PROCESS_VM_READ Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, False, p(0).Id)
    If ProcessHandle = IntPtr.Zero Then Return False
    End If
    Return True
    End Function

    Public Function ReadMemory(Of T)(ByVal address As Integer, ByVal length As Integer, ByVal unicodeString As Boolean) As T
    Dim buffer() As Byte
    If GetType(T) Is GetType(String) Then
    If unicodeString Then buffer = New Byte(length * 2 - 1) {} Else buffer = New Byte(length - 1) {}
    ElseIf GetType(T) Is GetType(Byte()) Then
    buffer = New Byte(length - 1) {}
    Else
    buffer = New Byte(Marshal.SizeOf(GetType(T)) - 1) {}
    End If
    If Not UpdateProcessHandle() Then Return Nothing
    Dim success As Boolean = ReadProcessMemory(ProcessHandle, New IntPtr(address), buffer, New IntPtr(buffer.Length), IntPtr.Zero)
    If Not success Then Return Nothing
    If GetType(T) Is GetType(Byte()) Then Return CType(CType(buffer, Object), T)
    If GetType(T) Is GetType(String) Then
    If unicodeString Then Return CType(CType(Encoding.Unicode.GetString(buffer), Object), T)
    Return CType(CType(Encoding.ASCII.GetString(buffer), Object), T)
    End If
    Dim gcHandle As GCHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned)
    Dim returnObject As T
    returnObject = CType(Marshal.PtrToStructure(gcHandle.AddrOfPinned Object, GetType(T)), T)
    gcHandle.Free()
    Return returnObject
    End Function

    Private Function GetObjectBytes(ByVal value As Object) As Byte()
    If value.GetType() Is GetType(Byte()) Then Return CType(value, Byte())
    Dim buffer(Marshal.SizeOf(value) - 1) As Byte
    Dim ptr As IntPtr = Marshal.AllocHGlobal(buffer.Length)
    Marshal.StructureToPtr(value, ptr, True)
    Marshal.Copy(ptr, buffer, 0, buffer.Length)
    Marshal.FreeHGlobal(ptr)
    Return buffer
    End Function

    Public Function WriteMemory(ByVal address As Integer, ByVal value As Object) As Boolean
    Return WriteMemory(address, value, False)
    End Function

    Public Function WriteMemory(ByVal address As Integer, ByVal value As Object, ByVal unicode As Boolean) As Boolean
    If Not UpdateProcessHandle() Then Return False
    Dim buffer() As Byte
    If TypeOf value Is String Then
    If unicode Then buffer = Encoding.Unicode.GetBytes(value.ToString()) Else buffer = Encoding.ASCII.GetBytes(value.ToString())
    Else
    buffer = GetObjectBytes(value)
    End If
    Dim result As Boolean = WriteProcessMemory(ProcessHandle, New IntPtr(address), buffer, New IntPtr(buffer.Length), IntPtr.Zero)
    Return result
    End Function
    End Module





    'main form

    Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    WriteMemory(&H26E6AE0, 1) ' Writes the value as Integer 'gives error when 64bit address is used. When I tell it to treat it as int64 rather that integer it gives no error and builds but simply doesn't work
    End Sub
    End Class




    all working in 32 bit programs but not 64 bit ones as I can't use a 64bit address without getting errors and I'm just doing something stupid when i try to change the code to write as long so I can actually import the 64bit address into the code for button1 as it simply does not work

    - - - Updated - - -

    tried changing Public Function WriteMemory(ByVal address As Integer, ByVal value As Object) As Boolean to Public Function WriteMemory(ByVal address As int64, ByVal value As Object) As Boolean on all the functions now I'm getting intptr errors in

    Dim result As Boolean = WriteProcessMemory(ProcessHandle, New IntPtr(address), buffer, New IntPtr(buffer.Length), IntPtr.Zero)
    Return resul

  2. The Following User Says Thank You to normiescum For This Useful Post:

    kenjifurry (11-07-2016)

  3. #2
    kenjifurry's Avatar
    Join Date
    Oct 2015
    Gender
    male
    Location
    Meme
    Posts
    250
    Reputation
    10
    Thanks
    969
    My Mood
    Aggressive
    nice for that I try to make to plo to dll :/



    [IMG]https://www.dooomca*****m/alison/stockgroup.jpg[/IMG]
    [IMG]https://www.dooomca*****m/alison/ad_Neat_min.jpg[/IMG]
    [IMG]https://www.dooomca*****m/alison/SStotoro.jpg[/IMG]
    [IMG]https://dooomca*****m/webcomic/BNbio.jpg[/IMG]


  4. #3
    H4X0RL33TT's Avatar
    Join Date
    Nov 2016
    Gender
    male
    Posts
    12
    Reputation
    10
    Thanks
    4
    WriteMemory(Of Integer)(&H26E6AE0, 1)
    Try that

  5. #4
    Hugo Boss's Avatar
    Join Date
    Oct 2011
    Gender
    male
    Posts
    28,752
    Reputation
    4790
    Thanks
    5,902
    My Mood
    Angelic
    No response for more than a week.
    Marking it as unresolved.

     
    Super User since 08-29-2017
    Global Moderator from 10-02-2016 - 08-29-2017
    Premium Seller since 11-16-2016
    Moderator from 09-24-2015 - 01-09-2016
    Alliance of Valiant Arms Minion from 11-12-2015 - 01-09-2016
    Market place Minion from 09-24-2015 - 01-09-2016
    Crossfire Minion from 09-11-2015 - 01-09-2016

    Middleman from 07-07-2015 - 01-09-2016
    Market Place Minion from 03-03-2014 - 08-01-2014
    Middleman from 01-30-2014 - 08-01-2014
    Moderator from 03-29-2013 - 04-04-2013
    Market Place Minion from 03-07-2013 - 04-04-2013
    Premium Member since 01-25-2013
    Middleman from 12-04-2012 - 04-04-2013
    Registered since 10-9-2011

Similar Threads

  1. [Help Request] c++ need help about read/write process memory
    By nepafter37 in forum C++/C Programming
    Replies: 15
    Last Post: 04-08-2016, 01:00 AM
  2. [Help] Write Process Memory Class
    By ViPeR124 in forum Visual Basic Programming
    Replies: 1
    Last Post: 08-29-2013, 05:33 AM
  3. [Help Request] C+ Memory Hacking in Writing to Process Memory
    By oilhackzone in forum C++/C Programming
    Replies: 1
    Last Post: 08-13-2012, 03:59 AM
  4. [Help] Memory editing (Read/Write Process Memory)
    By wolfguardiann in forum Visual Basic Programming
    Replies: 31
    Last Post: 06-04-2011, 03:23 AM
  5. [Help] Write Process Memory
    By tremaster in forum Visual Basic Programming
    Replies: 6
    Last Post: 03-22-2010, 03:28 PM