Reading or Writing to Another Processes Memory in C# z
http://www.jarloo.com/reading-and-writing-to-memory/
Declarations
[Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VMOperation = 0x00000008,
VMRead = 0x00000010,
VMWrite = 0x00000020,
DupHandle = 0x00000040,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
Synchronize = 0x00100000
} [DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId); [DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten); [DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead); [DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hProcess);
Reading from another processes Memory
public static byte[] ReadMemory(Process process, int address, int numOfBytes, out int bytesRead)
{
IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id); byte[] buffer = new byte[numOfBytes]; ReadProcessMemory(hProc, new IntPtr(address), buffer, numOfBytes, out bytesRead);
return buffer;
}
Here is an example of a call to this function:
Process process = Process.GetProcessesByName("My Apps Name").FirstOrDefault();
int address = 0x02ED2910; int bytesRead;
byte[] value = ReadMemory(process, address, , out bytesRead);
Writing to another processes memory
public static bool WriteMemory(Process process, int address, long value, out int bytesWritten)
{
IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id); byte[] val = BitConverter.GetBytes(value); bool worked = WriteProcessMemory(hProc, new IntPtr(address), val, (UInt32) val.LongLength, out bytesWritten); CloseHandle(hProc); return worked;
}
Here is an example of a call to this function:
Process process = Process.GetProcessesByName(
"My Apps Name"
).FirstOrDefault();
int
address = 0x02ED2910;
int
bytesWritten;
bool
worked = WriteMemory(process, address, value,
out
bytesWritten);
函数功能:该函数从指定的进程中读入内存信息,被读取的区域必须具有访问权限。
函数原型:BOOL ReadProcessMemory(HANDLE hProcess,LPCVOID lpBaseAddress,LPVOID lpBuffer,DWORD nSize,LPDWORD lpNumberOfBytesRead);
参数:
hProcess:进程句柄
lpBaseAddress:读出数据的地址
lpBuffer:存放读取数据的地址
nSize:读出的数据大小
lpNumberOfBytesRead:数据的实际大小
C#中使用该函数首先导入命名空间:
- using System.Runtime.InteropServices;
using System.Runtime.InteropServices;
然后写API引用部分的代码,放入 class 内部
- [DllImport("kernel32.dll ")]
- static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress,out int lpBuffer, int nSize, out int lpNumberOfBytesRead);
[DllImport("kernel32.dll ")]
static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress,out int lpBuffer, int nSize, out int lpNumberOfBytesRead);
这个函数有五个参数,第一个参数是 进程句柄,由OpenProcess函数获取;第二个参数是要读出数据的地址,使用CE等辅助工具可取得;第三个参数是用于存放读取数据的地址;第四个参数是 要读出的数据大小;第五个参数是读出数据的实际大小。例如:
- IntPtr hwnd = FindWindow(null, "计算器");
- const int PROCESS_ALL_ACCESS = 0x1F0FFF;
- const int PROCESS_VM_READ = 0x0010;
- const int PROCESS_VM_WRITE = 0x0020;
- if (hwnd != IntPtr.Zero)
- {
- int calcID;
- int calcProcess;
- int dataAddress;
- int readByte;
- GetWindowThreadProcessId(hwnd, out calcID);
- calcProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, calcID);
- //假设地址0X0047C9D4存在信息
- ReadProcessMemory(calcProcess, 0X0047C9D4, out dataAddress, 4, out readByte);
- MessageBox.Show(dataAddress.ToString());
- }
- else
- {
- MessageBox.Show("没有找到窗口");
- }
IntPtr hwnd = FindWindow(null, "计算器");
const int PROCESS_ALL_ACCESS = 0x1F0FFF;
const int PROCESS_VM_READ = 0x0010;
const int PROCESS_VM_WRITE = 0x0020;
if (hwnd != IntPtr.Zero)
{
int calcID;
int calcProcess;
int dataAddress;
int readByte;
GetWindowThreadProcessId(hwnd, out calcID);
calcProcess = OpenProcess(PROCESS_VM_READ | PROCESS_VM_WRITE, false, calcID);
//假设地址0X0047C9D4存在信息
ReadProcessMemory(calcProcess, 0X0047C9D4, out dataAddress, 4, out readByte);
MessageBox.Show(dataAddress.ToString());
}
else
{
MessageBox.Show("没有找到窗口");
}
如果我们读取的一段内存中的数据,我们引入部分可修改成如下:
- //二维数组
- [DllImport("kernel32.dll ")]
- static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[,] lpBuffer, int nSize, out int lpNumberOfBytesRead);
- //一维数组
- [DllImport("kernel32.dll ")]
- static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, out int lpNumberOfBytesRead);
//二维数组
[DllImport("kernel32.dll ")]
static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[,] lpBuffer, int nSize, out int lpNumberOfBytesRead);
//一维数组
[DllImport("kernel32.dll ")]
static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int nSize, out int lpNumberOfBytesRead);
由于数组是引用传递,我们不需要写out关键字。
Reading or Writing to Another Processes Memory in C# z的更多相关文章
- Reading and Writing CSV Files in C#
Introduction A common requirement is to have applications share data with other programs. Although t ...
- Reading and writing RData files
前面添加个lapply()或者dplyr::llply()就能读取,储存多个文件了.http://bluemountaincapital.github.io/FSharpRProvider/readi ...
- Reading and writing
A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory ...
- Analysis about different methods for reading and writing file in Java language
referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...
- Apache POI – Reading and Writing Excel file in Java
来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...
- PostgreSQL Reading Ad Writing Files、Execution System Instructions Vul
catalog . postgresql简介 . 文件读取/写入 . 命令执行 . 影响范围 . 恶意代码分析 . 缓解方案 1. postgresql简介 PostgreSQL 是一个自由的对象-关 ...
- 【转】Native Thread for Win32 C- Creating Processes(通俗易懂,非常好)
http://www.bogotobogo.com/cplusplus/multithreading_win32C.php To create a new process, we need to ca ...
- five kinds of IPC methods
Shared memory permits processes to communicate by simply reading and writing to a specified memory l ...
- Android Security
Android Security¶ 确认签名¶ Debug签名: $ jarsigner -verify -certs -verbose bin/TemplateGem.apk sm 2525 Sun ...
随机推荐
- 2013 Multi-University Training Contest 1 Partition
这题主要是推公式…… ;}
- cast——java类型转换
以下例说之: byte b = 3; //??? 3是一个int常量,但是会自动判断3是不是在byte类型的范围内 b = b + 2; //Type mismatch: cannot convert ...
- dynamic介绍
Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样. 在 ...
- leetcode 4 : Median of Two Sorted Arrays 找出两个数组的中位数
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- linux内核--进程与线程
http://blog.csdn.net/yusiguyuan/article/details/12154823 在<linux内核设计与实现>中第三章讲解了进程管理,在关于进程和线程的概 ...
- WCF揭秘(一)——简单的WCF开发实例
一.WCF是什么 WCF是微软为了实现各个开发平台之间的无疑缝连接而开发一种崭新工具,它是为分布式处理而开发.WCF将DCOM.Remoting.Web Service.WSE.MSMQ.AJAX服务 ...
- 10、JPA_映射双向多对多的关联关系
双向多对多的关联关系 双向多对多的关联关系(抽象成A-B)具体体现:A中有B的集合的引用,同时B中也有对A的集合的引用.A.B两个实体对应的数据表靠一张中间表来建立连接关系. 同时我们还知道,双向多对 ...
- Intellij IDEA新建一个EJB工程(二)
从博文:http://www.cnblogs.com/yangyquin/p/5328344.html 中可以知道如何利用Intellij IDEA建立一个EJB Module,还有新建一个测试Mod ...
- maven小试牛刀
Maven是一个采用纯Java编写的开源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xml ...
- iOS方法封装
(void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...