2016-05-06 5 views
0

Редактировать
Я обнаружил, что мой дескриптор возвращает значение нуля. Разве это не обнаружение процесса?C# ReadProcessMemory - адрес чтения с известным значением

Редактировать 2
Укоротил код и нашел проблему.
Отправленный ответ.


Хорошо, так что давайте прыгать прямо. Я пытаюсь прочитать значение адреса, который я знаю значение, но по какой-то причине я получаю возвращаемое значение "", по существу, она возвращается байт 00-00-00 .... и т.д..

Мой вопрос: Это мой код или это мой адрес? У меня есть еще одна итерация этого кода для 64-битного, который я тестировал в блокноте, и он работает нормально; код почти идентичен моему 64-битовому коду.

У меня есть чувство, что мне, возможно, придется копать глубже и найти больше указателей и смещений, и что код в порядке, но давайте начнем с кода, потому что я новичок во всем этом кодировании.

//Memory_Manager using_memory_manager = new Memory_Manager(); 
//Memory_Resources using_memory_resources = new Memory_Resources(); 
class Memory_Manager 
{ 
    public string memory_manager(string _command, string _offset , string _panelid, string _typeid, string _textboxid) 
    { 
     var activeform = Application.OpenForms.OfType<Form1>().Single(); 
     Misc_Tools using_misc_tools = new Misc_Tools(); 
     Converters using_converters = new Converters(); 
     Splitters using_splitters = new Splitters(); 
     Form_Tools using_form_tools = new Form_Tools(); 
     Process[] p = Process.GetProcessesByName(activeform.comboBoxProcessList.Text); 

     uint DELETE = 0x00010000; 
     uint READ_CONTROL = 0x00020000; 
     uint WRITE_DAC = 0x00040000; 
     uint WRITE_OWNER = 0x00080000; 
     uint SYNCHRONIZE = 0x00100000; 
     uint END = 0xFFF; //if you have Windows XP or Windows Server 2003 you must change this to 0xFFFF 
     uint PROCESS_ALL_ACCESS = (DELETE | READ_CONTROL | WRITE_DAC | WRITE_OWNER | SYNCHRONIZE | END); 

     string gettext = using_form_tools.form_control_search(_panelid, _typeid, _textboxid); 
     string _address = activeform.textBoxRead.Text; 
     int object_size = Convert.ToInt32(activeform.textBoxObjectSize.Text); //set the size that will be array size 
     byte[] readbuffer = new byte[object_size];//create an array of bytes for reading based on size 
     byte[] bytestowrite = Encoding.Unicode.GetBytes(gettext); 
     IntPtr ptrBytes;   
     IntPtr processHandle = Memory_Resources.OpenProcess(PROCESS_ALL_ACCESS, 1, Convert.ToInt32(p[0].Id)); 
     int size = gettext.Length*2; 
     int bytesReaded; 

     if (_address.Length == 11 && _command == "read") 
     { 
      Int64 _offsett = Int64.Parse(_offset, System.Globalization.NumberStyles.HexNumber); 
      Int64 _address64bit = Int64.Parse(activeform.textBoxRead.Text, System.Globalization.NumberStyles.HexNumber); 
      Int64 _finaladdress = _address64bit + _offsett; 
      Console.WriteLine("Reading 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      activeform.textBoxUpdate.AppendText("Reading 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      Memory_Resources.ReadProcessMemory(processHandle, _finaladdress, readbuffer, object_size, out ptrBytes); 
      bytesReaded = ptrBytes.ToInt32(); 
      Memory_Resources.CloseHandle(processHandle); 
      return Encoding.Unicode.GetString(readbuffer); 
     }    
     else if (_address.Length == 8 && _command == "read") 
     { 
      Int32 _offsett = Int32.Parse(_offset, System.Globalization.NumberStyles.HexNumber); 
      Int32 _address32bit = Int32.Parse(activeform.textBoxRead.Text, System.Globalization.NumberStyles.HexNumber); 
      Int32 _finaladdress = _address32bit + _offsett; 
      Console.WriteLine("Reading 32bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      activeform.textBoxUpdate.AppendText("Reading 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      Memory_Resources.ReadProcessMemory(processHandle, _finaladdress, readbuffer, object_size, out ptrBytes); 
      bytesReaded = ptrBytes.ToInt32(); 
      Memory_Resources.CloseHandle(processHandle); 
      return Encoding.Unicode.GetString(readbuffer); 
     } 
     else if (_address.Length == 11 && _command == "write") 
     { 
      Int64 _offsett = Int64.Parse(_offset, System.Globalization.NumberStyles.HexNumber); 
      Int64 _address64bit = Int64.Parse(activeform.textBoxRead.Text, System.Globalization.NumberStyles.HexNumber); 
      Int64 _finaladdress = _address64bit + _offsett; 
      Console.WriteLine("Writing 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to write set to " + Encoding.Unicode.GetString(bytestowrite) + "\r\n"); 
      activeform.textBoxUpdate.AppendText("Reading 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      Memory_Resources.WriteProcessMemory(processHandle, _finaladdress, bytestowrite, size, out ptrBytes); 
      bytesReaded = ptrBytes.ToInt32(); 
      Memory_Resources.CloseHandle(processHandle); 
      return BitConverter.ToString(bytestowrite); 
     } 
     else if (_address.Length == 8 && _command == "write") 
     { 
      Int32 _offsett = Int32.Parse(_offset, System.Globalization.NumberStyles.HexNumber); 
      Int32 _address32bit = Int32.Parse(activeform.textBoxRead.Text, System.Globalization.NumberStyles.HexNumber); 
      Int32 _finaladdress = _address32bit + _offsett; 
      Console.WriteLine("Writing 32bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to write set to " + Encoding.Unicode.GetString(bytestowrite) + "\r\n"); 
      activeform.textBoxUpdate.AppendText("Reading 64bit memory " + "\r\n" + "Address set to " + _finaladdress + "\r\n" + "Bytes to read set to " + object_size + "\r\n"); 
      Memory_Resources.WriteProcessMemory(processHandle, _finaladdress, bytestowrite, size, out ptrBytes); 
      bytesReaded = ptrBytes.ToInt32(); 
      Memory_Resources.CloseHandle(processHandle); 
      return BitConverter.ToString(bytestowrite); 
     } 
     return ("Could not read memory " + "\r\n"); 
    } 
} 

class Memory_Resources 
{ 
    [DllImport("kernel32.dll")] 
    public static extern bool ReadProcessMemory(IntPtr hProcess, Int32 lpBaseAddress, byte[] buffer, int size, out IntPtr lpNumberOfBytesRead); 

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

    [DllImport("kernel32.dll")] 
    public static extern bool WriteProcessMemory(IntPtr hProcess, Int32 lpBaseAddress, byte[] buffer, int size, out IntPtr lpNumberOfBytesWritten); 

    [DllImport("kernel32.dll")] 
    public static extern bool WriteProcessMemory(IntPtr hProcess, Int64 lpBaseAddress, byte[] buffer, int size, out IntPtr lpNumberOfBytesWritten); 

    [DllImport("kernel32.dll")] 
    public static extern IntPtr OpenProcess(uint dwDesiredAccess, Int32 bInheritHandle, Int32 dwProcessId); 

    [DllImport("kernel32.dll")] 
    public static extern Int32 CloseHandle(IntPtr hObject); 
} 

ответ

0

Предыдущий код использует информацию из другого TextBox, поэтому он не возвращал правильное значение, я хотел прочитать.

По существу это была ошибка пользователя.

+0

Это не дает ответа на вопрос. * «Я переместил код, пока все чудом не приступило к работе» * не очень полезно для будущих посетителей. – IInspectable