Results 1 to 7 of 7
  1. #1
    spide112's Avatar
    Join Date
    May 2012
    Gender
    female
    Posts
    3
    Reputation
    10
    Thanks
    0

    [Help] VB.NET ReadProcessMemory

    Based on abuckau907 posts about reading process memory (Part1 & Part2) i have created the following code :
    Code:
     Structure OSVERSIONINFO
            Public dwOSVersionInfoSize As Long
            Public dwMajorVersion As Long
            Public dwMinorVersion As Long
            Public dwBuildNumber As Long
            Public dwPlatformId As Long
            Public szCSDVersion As String
        End Structure
    
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure SYSTEM_INFO
            Dim dwOemID As Integer
            Dim dwPageSize As Integer
            Dim lpMinimumApplicationAddress As Integer
            Dim lpMaximumApplicationAddress As Integer
            Dim dwActiveProcessorMask As Integer
            Dim dwNumberOrfProcessors As Integer
            Dim dwProcessorType As Integer
            Dim dwAllocationGranularity As Integer
            Dim dwReserved As Integer
        End Structure
     Private Declare Function VirtualQueryEx Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal lpAddress As UInteger, ByRef lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Integer) As Integer
        Public Declare Sub GetSystemInfo Lib "kernel32" (ByRef lpSystemInfo As SYSTEM_INFO)
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal blnheritHandle As Boolean, ByVal dwAppProcessId As Integer) As IntPtr
        Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
        Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
        Const GW_HWNDNEXT = 2
        Private Const PROCESS_VM_READ = (&H10)
        Private Const PROCESS_VM_OPERATION = (&H8)
        Private Const PROCESS_QUERY_INFORMATION = (&H400)
        Const PROCESS_READ_WRITE_QUERY = PROCESS_VM_READ + PROCESS_VM_OPERATION + PROCESS_QUERY_INFORMATION
    
     Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
            Dim x2 As Integer
            For Each process As ManagementObject In searcher.Get()
                If process("Name") = "FlashPlayerPlugin_16_0_0_296.exe" Then
                    x2 = Convert.ToInt32(process("ProcessId"))
                End If
            Next
            Dim hProcess As Integer
            Dim lpMem As UInt32, ret As UInt16, lLenMBI As Long
            Dim si As New SYSTEM_INFO
            Dim mbi As MEMORY_BASIC_INFORMATION
            hProcess = OpenProcess(PROCESS_READ_WRITE_QUERY, False, x2)
            lLenMBI = Len(mbi)
            Call GetSystemInfo(si)
            lpMem = si.lpMinimumApplicationAddress
            Do While lpMem < si.lpMaximumApplicationAddress
                mbi.RegionSize = 0
                ret = VirtualQueryEx(hProcess, lpMem, mbi, lLenMBI)
                If ret = lLenMBI Then
                    If ((mbi.lType = &H20000) And (mbi.State = &H1000) And (mbi.RegionSize > 0)) Then
                        Dim mybytes(mbi.RegionSize) As Byte
                        ReadProcessMemory(hProcess, mbi.BaseAddress, mybytes, mbi.RegionSize, vbNull)
                        Debug.WriteLine("String : " & System.Text.Encoding.Unicode.GetString(mybytes))
                    End If
                    lpMem = mbi.BaseAddress + mbi.RegionSize
                Else
                    Exit Do
                End If
            Loop
            CloseHandle(hProcess)
    It works (thx to abuckau907 ) , but is really really slow, like 1-2 minute to complete. My question is : what can i do to make it faster? Am I reading to much at one ? Thanks in advance.

  2. #2
    R3DDOT's Avatar
    Join Date
    Dec 2013
    Gender
    male
    Location
    C://Windows/system32
    Posts
    347
    Reputation
    38
    Thanks
    2,366
    My Mood
    Cheerful
    Quote Originally Posted by spide112 View Post
    Based on abuckau907 posts about reading process memory (Part1 & Part2) i have created the following code :
    Code:
     Structure OSVERSIONINFO
            Public dwOSVersionInfoSize As Long
            Public dwMajorVersion As Long
            Public dwMinorVersion As Long
            Public dwBuildNumber As Long
            Public dwPlatformId As Long
            Public szCSDVersion As String
        End Structure
    
    
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure SYSTEM_INFO
            Dim dwOemID As Integer
            Dim dwPageSize As Integer
            Dim lpMinimumApplicationAddress As Integer
            Dim lpMaximumApplicationAddress As Integer
            Dim dwActiveProcessorMask As Integer
            Dim dwNumberOrfProcessors As Integer
            Dim dwProcessorType As Integer
            Dim dwAllocationGranularity As Integer
            Dim dwReserved As Integer
        End Structure
     Private Declare Function VirtualQueryEx Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal lpAddress As UInteger, ByRef lpBuffer As MEMORY_BASIC_INFORMATION, ByVal dwLength As Integer) As Integer
        Public Declare Sub GetSystemInfo Lib "kernel32" (ByRef lpSystemInfo As SYSTEM_INFO)
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal blnheritHandle As Boolean, ByVal dwAppProcessId As Integer) As IntPtr
        Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
        Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal iSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Boolean
        Const GW_HWNDNEXT = 2
        Private Const PROCESS_VM_READ = (&H10)
        Private Const PROCESS_VM_OPERATION = (&H8)
        Private Const PROCESS_QUERY_INFORMATION = (&H400)
        Const PROCESS_READ_WRITE_QUERY = PROCESS_VM_READ + PROCESS_VM_OPERATION + PROCESS_QUERY_INFORMATION
    
     Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_Process")
            Dim x2 As Integer
            For Each process As ManagementObject In searcher.Get()
                If process("Name") = "FlashPlayerPlugin_16_0_0_296.exe" Then
                    x2 = Convert.ToInt32(process("ProcessId"))
                End If
            Next
            Dim hProcess As Integer
            Dim lpMem As UInt32, ret As UInt16, lLenMBI As Long
            Dim si As New SYSTEM_INFO
            Dim mbi As MEMORY_BASIC_INFORMATION
            hProcess = OpenProcess(PROCESS_READ_WRITE_QUERY, False, x2)
            lLenMBI = Len(mbi)
            Call GetSystemInfo(si)
            lpMem = si.lpMinimumApplicationAddress
            Do While lpMem < si.lpMaximumApplicationAddress
                mbi.RegionSize = 0
                ret = VirtualQueryEx(hProcess, lpMem, mbi, lLenMBI)
                If ret = lLenMBI Then
                    If ((mbi.lType = &H20000) And (mbi.State = &H1000) And (mbi.RegionSize > 0)) Then
                        Dim mybytes(mbi.RegionSize) As Byte
                        ReadProcessMemory(hProcess, mbi.BaseAddress, mybytes, mbi.RegionSize, vbNull)
                        Debug.WriteLine("String : " & System.Text.Encoding.Unicode.GetString(mybytes))
                    End If
                    lpMem = mbi.BaseAddress + mbi.RegionSize
                Else
                    Exit Do
                End If
            Loop
            CloseHandle(hProcess)
    It works (thx to abuckau907 ) , but is really really slow, like 1-2 minute to complete. My question is : what can i do to make it faster? Am I reading to much at one ? Thanks in advance.
    What are you trying to do?

  3. #3
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Code:
                        Dim mybytes(mbi.RegionSize) As Byte
                        ReadProcessMemory(hProcess, mbi.BaseAddress, mybytes, mbi.RegionSize, vbNull)
                        Debug.WriteLine("String : " & System.Text.Encoding.Unicode.GetString(mybytes))
    1. you can re-use the same byte array (assuming it's large enough) - reallocating it so often isn't good, for obvious reasons.
    2. don't make a string that long...if it actually gets drawn to the screen, it's going to take a while to create all those characters on screen.
    Last edited by abuckau907; 01-29-2015 at 10:16 AM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  4. #4
    spide112's Avatar
    Join Date
    May 2012
    Gender
    female
    Posts
    3
    Reputation
    10
    Thanks
    0
    R3DDOT ; i try to find code, and then type it in textbox.
    the code is hidden in a html text "< input type=hidden" , and I can find it, but it takes too long.
    abuckau907 ;
    doing this is safe ?
    Code:
    Array.Clear(mybytes, mybytes.GetLowerBound(0), mybytes.Length)
    Would you kindly help me please ?
    I type it on screen for debug purpose , this job is done in a separated thred.

  5. #5
    R3DDOT's Avatar
    Join Date
    Dec 2013
    Gender
    male
    Location
    C://Windows/system32
    Posts
    347
    Reputation
    38
    Thanks
    2,366
    My Mood
    Cheerful
    Quote Originally Posted by abuckau907 View Post
    Code:
                        Dim mybytes(mbi.RegionSize) As Byte
                        ReadProcessMemory(hProcess, mbi.BaseAddress, mybytes, mbi.RegionSize, vbNull)
                        Debug.WriteLine("String : " & System.Text.Encoding.Unicode.GetString(mybytes))
    1. you can re-use the same byte array (assuming it's large enough) - reallocating it so often isn't good, for obvious reasons.
    2. don't make a string that long...if it actually gets drawn to the screen, it's going to take a while to create all those characters on screen.
    1. It's true, but, if I'm not mistaken, the region size of the may vary.
    2. This is also true, and it's probably what makes this process so slow.

    Quote Originally Posted by spide112 View Post
    R3DDOT ; i try to find code, and then type it in textbox.
    the code is hidden in a html text "< input type=hidden" , and I can find it, but it takes too long.
    abuckau907 ;
    doing this is safe ?
    Code:
    Array.Clear(mybytes, mybytes.GetLowerBound(0), mybytes.Length)
    Would you kindly help me please ?
    I type it on screen for debug purpose , this job is done in a separated thred.
    I don't advise you to re-use the same array only because the region size may vary. But, you don't need to clean the array.
    As long as you re-write the entire array, you shouldn't have any problems with

    And what do you mean by "the code is hidden in a html text "< input type=hidden"" ?

  6. #6
    abuckau907's Avatar
    Join Date
    Dec 2012
    Gender
    male
    Location
    other side of the wire
    Posts
    1,342
    Reputation
    162
    Thanks
    239
    My Mood
    Cold
    Quote Originally Posted by R3DDOT View Post


    1. It's true, but, if I'm not mistaken, the region size of the may vary.
    1. That's why I said 'If the buffer is large enough'; does it being too large cause a problem?
    Code:
      If (currentRegion.RegionSize > bufferSize) Then
        ''create larger buffer
     End If
      ReadProcessMemory(..,..,buffer,..,..)
    -----------------------------
    And what do you mean by "the code is hidden in a html text "< input type=hidden"" ?
    He's attaching to a Web Browser..apparently trying to find some section of html via memory scanning?
    Code:
     If process("Name") = "FlashPlayerPlugin_16_0_0_296.exe" Then
    Last edited by abuckau907; 01-29-2015 at 02:59 PM.
    'Some things that can be counted, don't matter. And some things that matter, can't be counted' - A.E.
    --
     

    My posts have some inaccuracies/are wrong/wrong keyword(s) used.
    They're (maybe) pretty close, and I hope they helped you, not created confusion. Take with grain of salt.

    -if you give rep, please leave a comment, else it means less.

  7. #7
    spide112's Avatar
    Join Date
    May 2012
    Gender
    female
    Posts
    3
    Reputation
    10
    Thanks
    0
    But does't this means that i create the buffer as large as the region size ?
    Code:
    Dim mybytes(mbi.RegionSize) As Byte
    cause it's in a loop ? Or this is wrong ?

Similar Threads

  1. [Help]VB.NET (2008)
    By CptnDutch in forum Visual Basic Programming
    Replies: 17
    Last Post: 12-07-2010, 10:36 AM
  2. [Help]VB.Net - Video Editor[Binder or Splitter or Converter]
    By aLcohoL_95 in forum Visual Basic Programming
    Replies: 10
    Last Post: 10-21-2010, 10:09 AM
  3. [Help] paint.net users..
    By Tyler930 in forum Combat Arms Mod Discussion
    Replies: 4
    Last Post: 04-18-2010, 04:59 AM
  4. [Help] VB.NET 2008 - Saving ListBox Items in Settings
    By Samueldo in forum Visual Basic Programming
    Replies: 12
    Last Post: 03-17-2010, 09:14 PM
  5. [HELP]Mpgh.net
    By Comet in forum General
    Replies: 21
    Last Post: 02-07-2010, 01:08 AM

Tags for this Thread