Advertisement
xMeltdowNx

Access Memory Module (VB.NET)

Oct 31st, 2012
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 14.43 KB | None | 0 0
  1. '///////////////////////////////////////////////////////////////////////
  2. ' Access Memory Module (VB.NET Version) //
  3. '/////////////////////////////////////////
  4. '
  5. 'Credit to the original creator(s) of this module
  6. '
  7. 'Changes:
  8. '
  9. 'Fixed (Re-Wrote) All Dynamic Functions
  10. 'Re-Wrote Byte Functions
  11. 'Added Function To Get Dynamic Addresses From Static Address And Offsets
  12. '
  13. 'GuidedHacking.com
  14. '////////////////////////////////////////////////////////////////////////
  15.  
  16. Module accessMemory
  17.  
  18. #Region "Declarations"
  19.  
  20.     Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
  21.  
  22.     Private Declare Function WriteProcessMemory1 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
  23.     Private Declare Function WriteProcessMemory2 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Single
  24.     Private Declare Function WriteProcessMemory3 Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Long
  25.  
  26.     Private Declare Function ReadProcessMemory1 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
  27.     Private Declare Function ReadProcessMemory2 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Single, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Single
  28.     Private Declare Function ReadProcessMemory3 Lib "kernel32" Alias "ReadProcessMemory" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Long, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Long
  29.  
  30.     Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
  31.  
  32.     Const PROCESS_ALL_ACCESS = &H1F0FF
  33.  
  34. #End Region
  35.  
  36. #Region "Memory Functions"
  37.  
  38.     '//////////////////////////////////////////////
  39.     '///////////  Write Functions Below ///////////
  40.     '//////////////////////////////////////////////
  41.  
  42.     Public Function writeDynamicInt(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Integer)
  43.         Try
  44.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  45.             WriteInteger(processName, newAddress, Value)
  46.             Return True
  47.         Catch ex As Exception
  48.             Return False
  49.         End Try
  50.     End Function
  51.  
  52.     Public Function writeDynamicFloat(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Single)
  53.         Try
  54.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  55.             WriteFloat(processName, newAddress, Value)
  56.             Return True
  57.         Catch ex As Exception
  58.             Return False
  59.         End Try
  60.     End Function
  61.  
  62.     Public Function writeDynamicLong(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String, ByVal Value As Long)
  63.         Try
  64.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  65.             WriteLong(processName, newAddress, Value)
  66.             Return True
  67.         Catch ex As Exception
  68.             Return False
  69.         End Try
  70.     End Function
  71.  
  72.     Public Sub WriteFloat(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Single, Optional ByVal nsize As Integer = 4)
  73.         If ProcessName.EndsWith(".exe") Then
  74.             ProcessName = ProcessName.Replace(".exe", "")
  75.         End If
  76.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  77.         If MyP.Length = 0 Then
  78.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  79.             Exit Sub
  80.         End If
  81.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  82.         If hProcess = IntPtr.Zero Then
  83.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  84.             Exit Sub
  85.         End If
  86.         Dim hAddress As Integer
  87.         Dim vBuffer As Single
  88.         hAddress = Address
  89.         vBuffer = Value
  90.         WriteProcessMemory2(hProcess, hAddress, vBuffer, nsize, 0)
  91.         CloseHandle(hProcess)
  92.     End Sub
  93.  
  94.     Public Sub WriteLong(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Long, Optional ByVal nsize As Integer = 4)
  95.         If ProcessName.EndsWith(".exe") Then
  96.             ProcessName = ProcessName.Replace(".exe", "")
  97.         End If
  98.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  99.         If MyP.Length = 0 Then
  100.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  101.             Exit Sub
  102.         End If
  103.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  104.         If hProcess = IntPtr.Zero Then
  105.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  106.             Exit Sub
  107.         End If
  108.  
  109.         Dim hAddress As Integer
  110.         Dim vBuffer As Long
  111.  
  112.         hAddress = Address
  113.         vBuffer = Value
  114.         WriteProcessMemory3(hProcess, hAddress, vBuffer, nsize, 0)
  115.         CloseHandle(hProcess)
  116.     End Sub
  117.  
  118.     Public Sub WriteNOPs(ByVal ProcessName As String, ByVal Address As Long, ByVal NOPNum As Integer)
  119.         Dim C As Integer
  120.         Dim B As Integer
  121.         If ProcessName.EndsWith(".exe") Then
  122.             ProcessName = ProcessName.Replace(".exe", "")
  123.         End If
  124.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  125.         If MyP.Length = 0 Then
  126.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  127.             Exit Sub
  128.         End If
  129.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  130.         If hProcess = IntPtr.Zero Then
  131.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  132.             Exit Sub
  133.         End If
  134.  
  135.         B = 0
  136.         For C = 1 To NOPNum
  137.             Call WriteProcessMemory1(hProcess, Address + B, &H90, 1, 0&)
  138.             B = B + 1
  139.         Next C
  140.         CloseHandle(hProcess)
  141.     End Sub
  142.  
  143.     Public Sub WriteInteger(ByVal ProcessName As String, ByVal Address As Integer, ByVal Value As Integer, Optional ByVal nsize As Integer = 4)
  144.         If ProcessName.EndsWith(".exe") Then
  145.             ProcessName = ProcessName.Replace(".exe", "")
  146.         End If
  147.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  148.         If MyP.Length = 0 Then
  149.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  150.             Exit Sub
  151.         End If
  152.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  153.         If hProcess = IntPtr.Zero Then
  154.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  155.             Exit Sub
  156.         End If
  157.  
  158.         Dim hAddress, vBuffer As Integer
  159.         hAddress = Address
  160.         vBuffer = Value
  161.         WriteProcessMemory1(hProcess, hAddress, CInt(vBuffer), nsize, 0)
  162.         CloseHandle(hProcess)
  163.     End Sub
  164.  
  165.     Public Function writeBytes(ByVal processName As String, ByVal baseAddress As Integer, ByVal Bytes As String, ByVal byteLength As Integer)
  166.         Try
  167.             Dim oldByte As Integer = ReadInteger(processName, baseAddress)
  168.             Dim revByte As String = byteReverse(oldByte.ToString("X"))
  169.             Dim byteTemp As String = Bytes & revByte.Remove(0, byteLength)
  170.             Dim byteNew As String = Val("&H" & byteReverse(byteTemp))
  171.             WriteInteger(processName, baseAddress, byteNew)
  172.             Return True
  173.         Catch ex As Exception
  174.             Return False
  175.         End Try
  176.     End Function
  177.  
  178.     '//////////////////////////////////////////////
  179.     '///////////  Read Functions Below  ///////////
  180.     '//////////////////////////////////////////////
  181.     '///////////  Write Functions Above ///////////
  182.     '//////////////////////////////////////////////
  183.  
  184.     Public Function ReadInteger(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Integer
  185.         If ProcessName.EndsWith(".exe") Then
  186.             ProcessName = ProcessName.Replace(".exe", "")
  187.         End If
  188.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  189.         If MyP.Length = 0 Then
  190.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  191.             Return vbEmpty
  192.             Exit Function
  193.         End If
  194.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  195.         If hProcess = IntPtr.Zero Then
  196.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  197.             Return vbEmpty
  198.             Exit Function
  199.         End If
  200.  
  201.         Dim hAddress, vBuffer As Integer
  202.         hAddress = Address
  203.         ReadProcessMemory1(hProcess, hAddress, vBuffer, nsize, 0)
  204.         CloseHandle(hProcess)
  205.         Return vBuffer
  206.     End Function
  207.  
  208.     Public Function ReadFloat(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Single
  209.         If ProcessName.EndsWith(".exe") Then
  210.             ProcessName = ProcessName.Replace(".exe", "")
  211.         End If
  212.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  213.         If MyP.Length = 0 Then
  214.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  215.             Return vbEmpty
  216.             Exit Function
  217.         End If
  218.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  219.         If hProcess = IntPtr.Zero Then
  220.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  221.             Return vbEmpty
  222.             Exit Function
  223.         End If
  224.  
  225.         Dim hAddress As Integer
  226.         Dim vBuffer As Single
  227.  
  228.         hAddress = Address
  229.         ReadProcessMemory2(hProcess, hAddress, vBuffer, nsize, 0)
  230.         CloseHandle(hProcess)
  231.         Return vBuffer
  232.     End Function
  233.  
  234.     Public Function ReadLong(ByVal ProcessName As String, ByVal Address As Integer, Optional ByVal nsize As Integer = 4) As Long
  235.         If ProcessName.EndsWith(".exe") Then
  236.             ProcessName = ProcessName.Replace(".exe", "")
  237.         End If
  238.         Dim MyP As Process() = Process.GetProcessesByName(ProcessName)
  239.         If MyP.Length = 0 Then
  240.             MsgBox(ProcessName & " isn't open!", vbExclamation, "Error")
  241.             Return vbEmpty
  242.             Exit Function
  243.         End If
  244.         Dim hProcess As IntPtr = OpenProcess(PROCESS_ALL_ACCESS, 0, MyP(0).Id)
  245.         If hProcess = IntPtr.Zero Then
  246.             MsgBox("Failed to open " & ProcessName & "!", vbExclamation, "Error")
  247.             Return vbEmpty
  248.             Exit Function
  249.         End If
  250.  
  251.         Dim hAddress As Integer
  252.         Dim vBuffer As Long
  253.  
  254.         hAddress = Address
  255.         ReadProcessMemory3(hProcess, hAddress, vBuffer, nsize, 0)
  256.         CloseHandle(hProcess)
  257.         Return vBuffer
  258.     End Function
  259.  
  260.     Public Function readDynamicInt(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  261.         Try
  262.             Dim newValue As Integer
  263.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  264.             newValue = ReadInteger(processName, newAddress)
  265.             Return newValue
  266.         Catch ex As Exception
  267.             Return False
  268.         End Try
  269.     End Function
  270.  
  271.     Public Function readDynamicFloat(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  272.         Try
  273.             Dim newValue As Single
  274.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  275.             newValue = ReadFloat(processName, newAddress)
  276.             Return newValue
  277.         Catch ex As Exception
  278.             Return False
  279.         End Try
  280.     End Function
  281.  
  282.     Public Function readDynamicLong(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  283.         Try
  284.             Dim newValue As Long
  285.             Dim newAddress As Integer = getAddress(processName, baseAddress, Offsets)
  286.             newValue = ReadLong(processName, newAddress)
  287.             Return newValue
  288.         Catch ex As Exception
  289.             Return False
  290.         End Try
  291.     End Function
  292.  
  293.     Public Function readBytes(ByVal processName As String, ByVal baseAddress As String, ByVal byteLength As Integer)
  294.         Try
  295.             Dim oldByte As Integer = ReadInteger(processName, baseAddress)
  296.             Dim revByte As String = byteReverse(oldByte.ToString("X"))
  297.             Dim byteNew As String = revByte.Remove(byteLength)
  298.             Return byteNew
  299.         Catch ex As Exception
  300.             Return False
  301.         End Try
  302.     End Function
  303.  
  304.     '//////////////////////////////////////////////
  305.     '///////////  Other Functions Below ///////////
  306.     '//////////////////////////////////////////////
  307.  
  308.     Public Function getAddress(ByVal processName As String, ByVal baseAddress As Integer, ByVal Offsets As String)
  309.         Dim genAddress As String = baseAddress
  310.         Dim offSplit As String() = Offsets.Split(New Char() {":"c})
  311.         Dim offTemp As String
  312.         For Each offTemp In offSplit
  313.             Dim grabAddress As Integer = ReadInteger(processName, genAddress)
  314.             Dim offConvert As Integer = Convert.ToInt32(offTemp, 16)
  315.             Dim addAddress = grabAddress + offConvert
  316.             genAddress = "&H" & addAddress.ToString("X")
  317.         Next
  318.         Return genAddress
  319.     End Function
  320.  
  321.     Public Function byteReverse(ByVal Input As String) As String
  322.         Try
  323.             Dim byteTemp As New List(Of String)
  324.             Dim newByte As String = ""
  325.             For i = 0 To Input.Length - 1 Step 2
  326.                 If i + 1 < Input.Length Then
  327.                     byteTemp.Add(Input.Substring(i, 2))
  328.                 Else
  329.                     byteTemp.Add(Input.Substring(i))
  330.                 End If
  331.             Next
  332.             For i = 0 To byteTemp.Count - 1
  333.                 newByte = byteTemp(i).ToString & newByte
  334.             Next
  335.             Return newByte
  336.         Catch ex As Exception
  337.             Return False
  338.         End Try
  339.     End Function
  340.  
  341. #End Region
  342.  
  343. End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement