2016-07-02 2 views
2

Process.EnterDebugMode()과 함께 실행 해 보았지만 작동하지 않습니다.64bit ReadProcessMemory access denied

나는 Notepad-memory을 읽고 싶지만, 어떻게 접근 할 것인지, 아니면 64 비트 시스템이 문제를 일으키는 지 알지 못합니다.

이 내가 무슨 짓을했는지입니다 :

using System; 
using System.Diagnostics; 
using System.Runtime.InteropServices; 
using System.Text; 

public class MemoryRead 
{ 

    [DllImport("kernel32.dll")] 
    static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); 

    [DllImport("kernel32.dll")] 
    static extern bool ReadProcessMemory(int hProcess, Int64 lpBaseAddress, byte[] buffer, int size, ref int lpNumberOfBytesRead); 

    [DllImport("kernel32.dll")] 
    static extern bool CloseHandle(IntPtr hObject); 

    static void Main(string[] args) 
    { 
     var pid = 10956; //notepad.exe 
     var processHandle = OpenProcess(0x10, false, pid); 

     byte[] buffer = new byte[24]; 
     int bytesRead = 0; 
     ReadProcessMemory((int)processHandle, 0x21106B35770, buffer, buffer.Length, ref bytesRead); //0x21106B35770 is the address where "hello world" is written in notepad 

     Console.WriteLine(Encoding.Unicode.GetString(buffer) + 
      " (" + bytesRead.ToString() + "bytes)"); 
     Console.ReadLine(); 
     CloseHandle(processHandle); 

     Console.ReadLine(); 
    } 
} 
+0

응용 프로그램을 관리자로 실행하십시오. – dotctor

+0

나는 x86으로 실행해야한다고 생각합니다. 나는 전에 그것을했고 관리자 권한이 필요하지 않았다. –

+0

www.pinvoke.net에서 메소드의 Pinvoke 선언을 얻을 수있다. –

답변

0

ReadProcessMemory 귀하의 PInvoke 선언이 올바르지 않습니다 (이것은 32 비트 시스템에서 작동해야하지만).

로서이 함수

BOOL WINAPI ReadProcessMemory(
    _In_ HANDLE hProcess, 
    _In_ LPCVOID lpBaseAddress, 
    _Out_ LPVOID lpBuffer, 
    _In_ SIZE_T nSize, 
    _Out_ SIZE_T *lpNumberOfBytesRead 
); 

첫번째 파라미터 HANDLE된다 네이티브 선언에서 본, 및 it isPVOID 될 수

모든 유형에 대한 포인터.

다음과 같이 유형 WINNT.H 선언된다

공백 * PVOID 타입 정의

;

그리고 64 비트 프로세스의 포인터는 모두 64 비트 값인 IntPtr입니다.

기본적으로 동일한 매개 변수는 sizelpNumberOfBytesRead 매개 변수로 이동합니다. 매개 변수는 64 비트 프로세스에서도 64 비트입니다.

따라서 귀하의 선언은 같은 것을해야한다 :

[[DllImport("kernel32.dll", SetLastError = true)]] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern Boolean ReadProcessMemory(
    [In] IntPtr hProcess, 
    [In] IntPtr lpBaseAddress, 
    [Out] Byte[] lpBuffer, 
    [In] UIntPtr nSize, 
    [Out] out UIntPtr lpNumberOfBytesRead 
); 

PS : 그리고 뻔뻔한 자기 홍보의 비트 - 만약 당신이 PInvoke를 가진 많은 일을해야하는 경우, 나는 '는 few good recommendations있다 ve는 어려운 길을 배웠다.