版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
32位/64位WINDOWS驱动之物理地址读写内存方法一、32位/64位WINDOWS驱动之\h物理地址读写内存方法GetPhysicalAddress;自写函数通过PIDZwMapViewOfSection;把物理地址映射到当前进程不使用时需要用ZwUnmapViewOfSectionBOOLEANReadPVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,IN\hUINT32nSize,OUTPVOIDpBuf);//BOOLEANWritePVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,INPVOID#include//#include//#include ArrayindexBitrange//0x80000008 0- Physicaladdress//0x80000008 8- Virtualaddress//0x80000008 16- //0x800000081- 0- UINT64g_maxPhysAddress=//利用cpuid取出物理地址UINT64if(g_maxPhysAddress==intUINT32r[4//四个整数的数组,包含在EAX、EBX、ECX和EDX中返回的有关CPUcpuid(r0x80000008//只有r[0]的前8位和8至15们有用后边3个全保留//getmaxphysicalphysicalbits=r[0]&g_maxPhysAddress=g_maxPhysAddress=g_maxPhysAddress>>physicalbits;//ifphysicalbits==36theng_maxPhysAddress=~(g_maxPhysAddress<<physicalbits);//<<36=0xfffffff000000000.afterinverse: return30//对于分页内存来说,调用MmProbeAndLockPages以及MmGetSystemAddressForMdlSafe来锁定内存页,以防止被pageout//对于非分页内存而言,调用MmBuildMdlForNonPagedPool//noop(("yjx:Toosmall//physicalBaseBOOLEANReadPhysicalMemory(char*physicalBase,UINT_PTRbytestoread,void
UNICODE_STRINGphysmemString;OBJECT_ATTRIBUTESattributes; *physmemName=L"\\device\\physicalmemory"; vaddress用于映射后的虚地址供访问 ntStatus= outputMDL;//KdPrint(("yjx:ReadPhysicalMemory(%p,%d,%p)",physicalBase,bytestoread,if(((UINT64)physicalBase>getg_maxPhysAddress())||((UINT64)physicalBase+bytestoread>KdPrint(("yjx:SYSInvalidphysicaladdress\n"));returnntStatus==FALSE;outputMDL=IoAllocateMdl(output,(ULONG)bytestoread,FALSE,FALSE,MmProbeAndLockPages(outputMDL,KernelMode,IoWriteAccess);exceptreturnFALSE;//// pMapedAddr=MmMapLockedPagesSpecifyCache(outputMDL,KernelMode,MmCached,NULL,FALSE,//if// KdPrint(("yjx:SYS:ErrorMmMapLockedPagesSpecifyCachepMapedAdd== return////except//KdPrint(("yjx:SYS:ErrorMmMapLockedPagesSpecifyCachefail//returnRtlInitUnicodeString(&physmemString,InitializeObjectAttributes(&attributes,&physmemString,OBJ_CASE_INSENSITIVE,NULL,NULL);ntStatus=ZwOpenSection(&physmem,SECTION_ALL_ACCESS,&attributes);if(ntStatus==//heylook,itdidn'tkillSIZE_T UINT_PTRoffset;UINT_PTRviewBase.QuadPart=length=0x2000;//pinp->bytestoread;//incaseofaoverlappingtoread=bytestoread;vaddress=NULL;KdPrint(("yjx:ReadPhysicalMemory:viewBase.QuadPart=%x",//ntStatus=ZwMapViewOfSection(physmem,//sectionhandleNtCurrentProcess(),//processhandle(shouldbe-&vaddress,0L,length,//CommitSize&viewBase,//SectionOffset&length,//ViewSize
if((ntStatus==STATUS_SUCCESS)&&(vaddress!=if(toread>length)toread=ifoffset=(UINT_PTR)(physicalBase)-(UINT_PTR)viewBase.QuadPart;//0if(offset+toread>length)KdPrint(("yjx:Toosmallnoop(("yjx:ToosmallRtlCopyMemory(output,&vaddress[offset],ZwUnmapViewOfSection(NtCurrentProcess(),exceptKdPrint(("yjx:Failuremappingphysical
KdPrint(("yjx:ReadPhysicalMemoryerror:ntStatus=%x",exceptKdPrint(("yjx:ErrorwhilereadingphysicalreturnntStatus==STATUS_SUCCESS?TRUE:
//noop(("yjx:Toosmall//physicalBaseBOOLEANWritePhysicalMemory(char*physicalBase,INUINT_PTRnSizeWrite,INPVOID UNICODE_STRINGphysmemString;OBJECT_ATTRIBUTESattributes; physmemNameL"\\device\\physicalmemory";//VMProtectDecryptStringW vaddress=NULL;//用于映射后的虚地址供访问 ntStatus=STATUS_UNSUCCESSFUL; pInBufMDL=NULL;KdPrint(("yjx:SYS:WritePhysicalMemory(%p,%d,%p)",physicalBase,nSizeWrite,if(((UINT64)physicalBase>getg_maxPhysAddress())||((UINT64)physicalBase+nSizeWrite>KdPrint(("yjx:SYS:ErrorInvalidphysicaladdress\n"));returnntStatus==FALSE;pInBufMDL=IoAllocateMdl(InBuf,(ULONG)nSizeWrite,FALSE,FALSE,MmProbeAndLockPages(pInBufMDL,KernelMode,exceptKdPrint(("yjx:SYS:ErrorInBufMmProbeAndLockPagesfail returnFALSE; pMapedAddr=//这里没有切换需要这样映射么pMapedAddr=MmMapLockedPagesSpecifyCache(pInBufMDL,KernelMode,MmCached,NULL,FALSE,ifKdPrint(("yjx:SYS:pMapedAdd==return exceptKdPrint(("yjx:SYS:MmMapLockedPagesSpecifyCachepMapedAddr=%p\n"return RtlInitUnicodeString(&physmemString, InitializeObjectAttributes(&attributes,&physmemString,OBJ_CASE_INSENSITIVE,NULL, ntStatus=ZwOpenSection(&physmem,SECTION_ALL_ACCESS, if(ntStatus== //heylook,itdidn'tkill SIZE_T viewBase;// UINT_PTR UINT_PTR viewBase.QuadPart= length=0x2000;//pinp->nSizeWrite;//incaseofaoverlapping toWriteSize= vaddress= KdPrint(("yjx:SYS:ReadPhysicalMemory:地址=%x",//ntStatus=physmem,NtCurrentProcess(),//processhandle(shouldbe-&vaddress,0L,length,&viewBase,&length,PAGE_READWRITE//if((ntStatus==STATUS_SUCCESS)&&(vaddress!=if(toWriteSize>toWriteSize=if offset=(UINT_PTR)(physicalBase)- if(offset+toWriteSize> KdPrint(("yjx:SYS:Toosmall noop(("yjx:SYS:Toosmallmap")); KdPrint(("yjx:SYS RtlCopyMemory(&vaddress[offsetInBuftoWriteSize//
ZwUnmapViewOfSection(NtCurrentProcess(),exceptKdPrint(("yjx:SYS:FailuremappingphysicalKdPrint(("yjx:SYS:ReadPhysicalMemoryerror:ntStatus=%x",exceptKdPrint(("yjx:SYS:ErrorwhilereadingphysicalreturnntStatus==STATUS_SUCCESS?TRUE://BOOLReadPhysicalMemory(PVOIDbuffer,UINT32address,UINT32//UINT32 ////PVOID //NTSTATUS NTDLL//LARGE_INTEGER //vaddress=//outlen=//base.QuadPart=//////status= (HANDLE)- (PVOID //if(status<// return////////memmove(buffer,vaddress,//////status=ZwUnmapViewOfSection((HANDLE)-1,//return(status>=////case//vBaseAddress//返回为物理地址PVOIDGetPhysicalAddress(UINT64ProcessID,PVOID//struct//UINT64ProcessID;//UINT64BaseAddress;//要转换的虚拟地址//}//pinp=Irp-PEPROCESSselectedprocess;//存放指定ProcessID所在PHYSICAL_ADDRESSphysical;//返回物理地址physical.QuadPart0//PHYSICAL_ADDRESS实际上就是INT648 ntStatus=//KdPrint(("yjx:GETPHYSICALADDRESS.ProcessID(%p)=%xBaseAddress(%p)=%x\n",&pinp->ProcessID,pinp->ProcessID,&pinp->BaseAddress,pinp-//switchtotheselectedif(PsLookupProcessByProcessId((PVOID)(UINT_PTR)(ProcessID),&selectedprocess)==KAPC_STATERtlZeroMemory(&apc_state,KeStackAttachProcess((PVOID)selectedprocess&apc_state//关键地方附加到指定进程KeStackAttachProcessphysical=MmGetPhysicalAddress((PVOID)vBaseAddress);//pinp->BaseAddress);//exceptntStatus=if(ntStatus==//RtlCopyMemory(Irp->AssociatedIrp.SystemBuffer,&physical.QuadPart,return(PVOID)physical.QuadPart;//return402//BOOLEANKReadProcessMemory(INPEPROCESSProcess,INPVOIDAddress,INUINT32Length,OUTPVOID
BOOLEANReadPVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,OUTPVOIDPVOIDphyBase=GetPhysicalAddress(ProcessID,VBaseAddress);if(phyBase)returnReadPhysicalMemory(phyBase,nSize, returnreturn//BOOLEANKReadProcessMemory(INPEPROCESSProcess,INPVOIDAddress,INUINT32Length,OUTPVOIDBOOLEANWritePVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,INPVOIDPVOIDphyBase=GetPhysicalAddress(ProcessID,VBaseAddress);if(phyBase)returnWritePhysicalMemory(phyBase,nSize, return441
return
//BOOLEANReadPVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,OUTPVOIDNTSTATUSIRP_ReadPVirtualMemory(PIRP//UNREFERENCED_PARAMETER(device_Object);未使用的参数DbgPrint("yjx:sys64%s行号=%d%s"FUNCDNAMELINE);NTSTATUSntStatus=STATUS_SUCCESS; irpStack=irpStack=#pragmapack(push)#pragmapack(8)typedefstructUINT64ProcessID;//目标进程PIDPVOIDVBaseAddress//目标进程地址UINT64nSize;//要读取的长度UINT64pBuf;//要写入数据的地址//#pragma//PVOIDBaseAddressNULL//PEPROCESSselectedprocessNULL;//目标进程SIZE_TRegionSize=0;//分配大小TINPUT_BUF*bufInput(TINPUT_BUF*)(pirp->AssociatedIrp.SystemBuffer//输入输出缓冲区ReadPVirtualMemory(bufInput->ProcessID,bufInput->VBaseAddress,bufInput->nSize,bufInput);//bufInput-//pirp->IoStatus.Status=pirp->IoStatus.Information4;//返回给DeviceIoControl中的倒数第二个参数//IoCompleteRequest(pirp,IO_NO_INCREMENT);//调用方已完成所有I/O请求处理操作并且不增加优先级if(irpStack)if(ntStatus==//成功则返回缓冲区大小pirp->IoStatus.Information=irpStack-//pirp->IoStatus.Information=//IoCompleteRequest(pirp,pirp->IoStatus.Status=ntStatus;returnntStatus;//ProcessID目标进程的//VBaseAddress//nSize//pBufBOOLEANReadPVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,OUTPVOID//ProcessID目标进程的//VBaseAddress//nSize//pBufBOOLEANWritePVirtualMemory(UINT64ProcessID,INPVOIDVBaseAddress,INUINT32nSize,INPVOID#defineCTL_IO_
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高校毕业就业合同模板
- 2024年绿色能源充电桩车位租赁与能源结构优化合同3篇
- 2024年度三方合同主体变更与合同争议解决方式协议3篇
- 2024年度公司、企业股权激励计划执行与监控合同2篇
- 焦炉维修合同范例
- 2024版典当质押借款合同(含提前还款条款)3篇
- 雇主移民中介合同范例
- 车子按揭贷款合同范例
- 福州房产合同范例
- 充电桩承包运营合同模板
- 汽车标准件手册
- 自动打印机机构设计课程设计
- 3、分段计费问题
- 基于8086的步进电机控制
- 印花税税源明细表(示范)
- 循环流化床锅炉氮氧化物生成与控制分析
- 关于申请创办宜康医院精神病医院的报告
- 《人间生活》高中美术鉴赏教案设计
- 公司优秀员工颁奖词
- 【学案】高中英语续后续写之环湖赛跑
- 电视剧剧组组成及职能
评论
0/150
提交评论