ReadProcessMemory/C++的探索[通俗易懂]

ReadProcessMemory/C++的探索[通俗易懂]ReadProcessMemory函数msdn说明:BOOLWINAPIReadProcessMemory(_In_   HANDLEhProcess,_In_   LPCVOIDlpBaseAddress,_Out_  LPVOIDlpBuffer,_In_   SIZE_TnSize,_Out_  SIZE_T*lpNumberOfByte

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

ReadProcessMemory 函数msdn说明:

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

Parameters

hProcess [in]

A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process.

进程句柄

lpBaseAddress [in]

A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.

读取内存的基址(关键,因为不是随便取一个就能读出来的)

lpBuffer [out]

A pointer to a buffer that receives the contents from the address space of the specified process.

读出内容后输出的缓冲区的头地址

nSize [in]

The number of bytes to be read from the specified process.

需要读取内存的大小

lpNumberOfBytesRead [out]

A pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.

实际读取内存的大小

Return value

If the function succeeds, the return value is nonzero.

如果成功,返回非0。

If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

如果失败,返回0,错误可通过GetLastError获得

The function fails if the requested read operation crosses into an area of the process that is inaccessible.

重点:当操作进入了进程不可读取的部分时会失败,大多数人犯的错误。

Remarks

ReadProcessMemory copies the data in the specified address range from the address space of the specified process into the specified buffer of the current process. Any process that has a handle with PROCESS_VM_READ access can call the function.

函数需要进程的PROCESS_VM_READ 权限

The entire area to be read must be accessible, and if it is not accessible, the function fails.

关于探索旅程的废话说一点:

开始按实验报告上的要求基址填了0x,其它查了下msdn函数原型简单写出了程序,运行一下,结果就悲剧的读取失败。

下面是错误的代码:

STARTUPINFO si;	PROCESS_INFORMATION pi;	HANDLE hProcess=NULL;	ZeroMemory( &si, sizeof(si) );	si.cb = sizeof(si);	ZeroMemory( &pi, sizeof(pi) );	if(!CreateProcess(L".\\memoryinfo_child2.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))	{		printf( "CreateProcess2 failed (%d)\n", GetLastError() );	}	WaitForSingleObject( pi.hProcess, 1000 );	byte *readtemp=new byte[256*16];	DWORD dwNumberOfBytesRead;	hProcess = OpenProcess(PROCESS_ALL_ACCESS,		FALSE, pi.dwProcessId );	if(hProcess !=NULL)	{		int i=0x00000000;		if(!ReadProcessMemory(hProcess,(LPCVOID)i,readtemp,0x10,&dwNumberOfBytesRead)){			i++;		}		printf("readsuccess:");		for(int i=0;i<dwNumberOfBytesRead;i++){			printf("\n");			printf("%X",readtemp[i]);		}		printf("\n");	}	CloseHandle( pi.hProcess );	CloseHandle( pi.hThread );

然后查了各种博客,讲得大同小异,有的说权限不对,有的说地址不对,然后我看到可以用GetLastError(菜鸟一枚,勿喷)获取错误代码,我用了后发现代码是5,然后用IDE工具中的错误查看器查出错误是:

仅完成部分的 ReadProcessMemory 或 WriteProcessMemory 请求。没啥帮助,不过 至少学会GetLastError(菜鸟一枚,勿喷),,,

对于基址不对,有人说得用CE工具查看(表示CE是神马东东),然后百度了下,原来CE的全称是ChartEngine,大神一般用来游戏作弊,没想到今日居然用来做作业。。。怒下。

居然支持中文版,感动,,,

用ChartEngine查看了子进程的内存,果然基址至少20000以上,还且经常会变。基址换了之后,真的读取成功了,感动得泪流满面。

后来由于基址在程序运行前会变化,所有用了while语句,直到成功时才跳出循环,苦逼,,,这么简单的东西搞了半天,主要基础没打好,,,,

下面是正确的代码:

STARTUPINFO si;	PROCESS_INFORMATION pi;	HANDLE hProcess=NULL;	ZeroMemory( &si, sizeof(si) );	si.cb = sizeof(si);	ZeroMemory( &pi, sizeof(pi) );	if(!CreateProcess(L".\\memoryinfo_child2.exe", NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))	{		printf( "CreateProcess2 failed (%d)\n", GetLastError() );	}	WaitForSingleObject( pi.hProcess, 1000 );	byte *readtemp=new byte[256*16];	DWORD dwNumberOfBytesRead;	hProcess = OpenProcess(PROCESS_ALL_ACCESS,		FALSE, pi.dwProcessId );	if(hProcess !=NULL)	{		int i=0x00020000;		while(!ReadProcessMemory(hProcess,(LPCVOID)i,readtemp,0x10,&dwNumberOfBytesRead)){			i++;		}		printf("readsuccess:");		for(int i=0;i<dwNumberOfBytesRead;i++){			printf("\n");			printf("%X",readtemp[i]);		}		printf("\n");	}	CloseHandle( pi.hProcess );	CloseHandle( pi.hThread );

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/185838.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • log4cpp 封装

    log4cpp 封装最近学习 log4cpp 试着封装了下 mark 下 includeiostr includelog4c Category hh includelog4c Appender hh includelog4c FileAppender hh includelog4c RollingFileA hh includelog4c Ostrea

    2025年9月16日
    1
  • 词向量简介「建议收藏」

    词向量简介「建议收藏」最近深度学习技术有了突飞猛进的发展,为语音识别、图像识别、自然语言处理(NLP)提供了强大的工具,为这些领域今后的快速发展提供了新的契机。深度学习为自然语言处理带来的最令人兴奋的突破是词向量(wordembedding)技术。词向量技术是将词转化成为稠密向量,并且对于相似的词,其对应的词向量也相近。在自然语言处理应用中,词向量作为深度学习模型的特征进行输入。因此,最终模型的效果很大程度上取

    2022年6月6日
    42
  • DSP开发,使用CCS软件建立工程以及烧录

    DSP开发,使用CCS软件建立工程1概述1.1资源概述2工程建立步骤1概述实验的代码已经上传。1.1资源概述开发板:普中DSP开发板CCS版本:6.1.3主控芯片型号:TMS320F283352工程建立步骤1,在需要建立的工程的文件夹内新建一个工程文件夹。2,打开CCS软件,在弹出的Workspace内指向刚才建立的文件夹。3,建立新工程4,填入工程的相关信息5,新建后的工程,只包含两个文件以及一个文件夹,系统必须的头文件,RAM连接的配置文件6,在工程文件

    2022年4月6日
    679
  • vscode新建html文件并快速生成标准的html代码_vscode怎么开始写代码

    vscode新建html文件并快速生成标准的html代码_vscode怎么开始写代码在vscode里新建html文件,总是要一行一行的写标准的html代码;而DW新建html文件,都会自动生成标准的html代码;所以在使用vscode是总觉得很麻烦,  各种百度终于找到了使用的方法(很好用的一个快捷键):步骤:一、先输入一个!      二、点击tab键      三、自动生成标准的html代码(见图)       …

    2022年8月21日
    8
  • C++多线程编程:同步之互斥量Mutex「建议收藏」

    C++多线程编程:同步之互斥量Mutex「建议收藏」文章目录5.示例代码文章目录1.CreateMutex()2.ReleaseMutex()3.WaitForSingleobject()4.CloseHandle()5.示例代码6.Mutex实现一个程序只允许允许一个实例(进程)5.示例代码文章目录1.CreateMutex()2.ReleaseMutex()3.WaitForSingleobject()4.CloseHandle()5.示例代码6.Mutex实现一个程序只允许允许一个实例(进程))5.示例代码文章目录1

    2022年6月26日
    38
  • 免费数据集下载网站【dataset】

    免费数据集下载网站【dataset】https://github.com/awesomedata/awesome-public-datasets转载于:https://www.cnblogs.com/jerrybaby/p/9293319.html

    2022年8月31日
    10

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号