Today's tutorial is about...processes' memory! In this article I'll show you how to read/write a process' memory using C#. This is a good way to learn a part of WinAPI and also understand the basics of memory allocation.

Before starting, we need a "target" - I choose notepad.exe.

1.Finding the Memory Address

As you might probably know, applications store each variable's value at a specific memory address,
we need to know that memory adress in order to edit anything. Since
there's not other way around (or I'm not aware of it?) the only solution
is to start searching, using a debugger.

To get that memory address, I used OllyDbg - don't worry, all the steps are written below.

First, open notepad.exe, type some text (like "hello world") and attach OllyDbg (File->Attach). Press F9 and then ALT+M to open the Memory Map.

It should look like this:

Press CTRL+B and it will open the Binary Search Window. Now, because the value is stored in memory as Unicode, you have to type the string you're looking for in the 2nd textbox:

Once you hit Ok another window will pop up - the Memory Dump. Here, look at the very first memory address
(on the left) - from that address we'll start reading. In the image
below, the highlighted part contains the message I typed in Notepad.

Note: don't use the memory address from the image - it's not the same memory address every time

We got the memory address, now...don't close/restart the application. If you restart it, the memory for the text will be reallocated, so the address will most likely be changed.

2.Read Process' Memory

In order to read the value from that memory address, we need to import 2 functions into C#: OpenProcess() and ReadProcessMemory() from kernel32.dll.

  1. [DllImport("kernel32.dll")]
  2. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  3. [DllImport("kernel32.dll")]
  4. public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);

When a process is opened, you must also specify the desired access (this
time, you request access for reading the memory), so this constant is
needed:

  1. const int PROCESS_WM_READ = 0x0010;

Since the whole code is self explanatory, I'll just add short comments where they're needed:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.InteropServices;
  4. using System.Text;
  5. public class MemoryRead
  6. {
  7. const int PROCESS_WM_READ = 0x0010;
  8. [DllImport("kernel32.dll")]
  9. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  10. [DllImport("kernel32.dll")]
  11. public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
  12. public static void Main()
  13. {
  14. Process process = Process.GetProcessesByName("notepad")[0];
  15. IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
  16. int bytesRead = 0;
  17. byte[] buffer = new byte[24]; //'Hello World!' takes 12*2 bytes because of Unicode
  18. // 0x0046A3B8 is the address where I found the string, replace it with what you found
  19. ReadProcessMemory((int)processHandle, 0x0046A3B8, buffer, buffer.Length, ref bytesRead);
  20. Console.WriteLine(Encoding.Unicode.GetString(buffer) + " (" + bytesRead.ToString() + "bytes)");
  21. Console.ReadLine();
  22. }
  23. }

3.Write Process' Memory

Writing to a memory address is a little bit different: you'll need OpenProcess() and WriteProcessMemory().

  1. [DllImport("kernel32.dll")]
  2. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  3. [DllImport("kernel32.dll", SetLastError = true)]
  4. static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);

However, special permissions are required: while opening the process request the following access: PROCESS_VM_WRITE | PROCESS_VM_OPERATION.

  1. const int PROCESS_VM_WRITE = 0x0020;
  2. const int PROCESS_VM_OPERATION = 0x0008;

Note: notepad's textbox is storing the number of bytes
it has to read from the memory - that value is updated only when the
text is changed by user. If you write to the memory address a longer
string, it will be truncated.

The complete code is available below:

    1. using System;
    2. using System.Diagnostics;
    3. using System.Runtime.InteropServices;
    4. using System.Text;
    5. public class MemoryRead
    6. {
    7. const int PROCESS_VM_WRITE = 0x0020;
    8. const int PROCESS_VM_OPERATION = 0x0008;
    9. [DllImport("kernel32.dll")]
    10. public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
    11. [DllImport("kernel32.dll", SetLastError = true)]
    12. static extern bool WriteProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesWritten);
    13. public static void Main()
    14. {
    15. Process process = Process.GetProcessesByName("notepad")[0];
    16. IntPtr processHandle = OpenProcess(0x1F0FFF, false, process.Id);
    17. int bytesWritten = 0;
    18. byte[] buffer = Encoding.Unicode.GetBytes("It works!\0"); // '\0' marks the end of string
    19. // replace 0x0046A3B8 with your address
    20. WriteProcessMemory((int)processHandle, 0x0046A3B8, buffer, buffer.Length, ref bytesWritten);
    21. Console.ReadLine();
    22. }
    23. }

C# Read/Write another Process' Memory ZZ的更多相关文章

  1. C# Read/Write another Process' Memory z

    http://www.codeproject.com/Articles/670373/Csharp-Read-Write-another-Process-Memory This article aim ...

  2. ORA-04030: out of process memory when trying to allocate 152 bytes (Logminer LCR c,krvtadc)

    今天使用LogMiner找回误更新的数据时,查询v$logmnr_contents时,遇到了"ORA-04030: out of process memory when trying to ...

  3. SAP work process Memory allocate

    Memory allocation sequence to dialog work processes in SAP What is the memory allocation sequence to ...

  4. C# Read/Write another Process' Memory

    https://codingvision.net/security/c-read-write-another-process-memory Today’s tutorial is about…proc ...

  5. Automated Memory Analysis

    catalogue . 静态分析.动态分析.内存镜像分析对比 . Memory Analysis Approach . volatility: An advanced memory forensics ...

  6. Process Explorer使用图文教程

    这是一款由Sysinternals开发的Windows系统和应用程序监视工具,目前Sysinternals已经被微软收购,此款不仅结合了文件监视和注册表监视两个工具的功能,还增加了多项重要的增强功能, ...

  7. mm/memory

    /* *  linux/mm/memory.c * *  Copyright (C) 1991, 1992  Linus Torvalds */ /* * demand-loading started ...

  8. Read ListViewItem content from another process z

    Normal Windows GUI applications work with messages that are sent to a window or control and the cont ...

  9. 通过ctypes获得python windows process的内存使用情况

    通过ctypes 类库中的win32方法GetProcessMemoryInfo()获得当前进程的内存使用情况.该函数可以在32或者64位,python2.6+及python3.x之上都能有用. &q ...

随机推荐

  1. 解决VirtualBox错误:“FATAL:No bootable medium found!”

    VirtualBox错误:“FATAL:No bootable medium found!” 用VirtualBox安装系统出现这个错误的几率极高,因为当哥出现同样问题的时候股沟了下”FATAL:No ...

  2. 工作中的问题解决 -- (win2003 asp.net) Session和带页面回传的方法无法正常使用解决方案

    公司BP&IT项目组.从上上个月成立开始开发BP&IT软件.这个月开始测试我悲剧的发现他尽然不支持我电脑上的IE11.半个多月还没解决 我们先来分析下原因首页 登陆页面正常浏览 htt ...

  3. 常用数据与VARIANT之间的转换---从网上整理

    //头文件 1 #pragma once class VariantConvert { public: VariantConvert(void); ~VariantConvert(void); pub ...

  4. c++ primer复习(五):类

    一:基本内容 1 类 数据成员:用于存储与类对象相关联的状态 成员函数:对数据成员进行操作 类将接口与实现分离,接口指定了类支持的操作,操作的具体实现细节是类的设计者才需要了解 2 类成员 类成员可以 ...

  5. Java中ArrayList源码分析

    一.简介 ArrayList是一个数组队列,相当于动态数组.每个ArrayList实例都有自己的容量,该容量至少和所存储数据的个数一样大小,在每次添加数据时,它会使用ensureCapacity()保 ...

  6. IIS配置及防黑

    安装IIS.部署网站(发布或者拷贝都可以).修改连接字符串,compilation设为false,删掉cs代码 上传文件夹不给执行权限: 在iis管理器中找到上传文件夹,选择属性--执行权限,设置为“ ...

  7. erlang 里的if 和 case

    case Expression of Pattern1 [when Guard1] -> Expr_seq1; Pattern2 [when Guard2] -> Expr_seq2; … ...

  8. 关于Linux内核学习的误区以及相关书籍介绍

    http://www.hzlitai.com.cn/article/ARM9-article/system/1605.html 写给Linux内核新手-关于Linux内核学习的误区 先说句正经的:其实 ...

  9. checkbox在jquery版本1.9 以上用attr不可重复操作的问题【附解决方案】

    最近做个项目,需要重复多次更改checkbox的状态,使用jquery 1.10.2的最新版本时发现,对checkbox的选中状态无法多次选中.测试代码如下: <!DOCTYPE html PU ...

  10. Android Activity 生命周期详解

    学习android开发这么久对于activity的生命周期还没有仔细思考过,所以,我大致的把这些东西整理一下,希望通过这使自己理解的更透彻点吧! 首先看一下Activity生命周期图和它的的四个阶段 ...