首页 技术 正文
技术 2022年11月12日
0 收藏 676 点赞 3,011 浏览 2255 个字

标 题: 【原创】使用ZwMapViewOfSection创建内存映射文件总结
作 者: 小覃
时 间: 2012-06-15,02:28:36
链 接: http://bbs.pediy.com/showthread.php?t=152144

在写驱动搜索内核模块内存时,你是不是也经常会遇到BSOD?
原因是内核模块INIT节调用完成后就取消了映射。
    解决这个问题鄙人的方法是,
自己来映射该内核模块文件内存进行内存操作。
我们使用ZwQuerySystemInformation遍历枚举模块,
得到模块名后使用ZwMapViewOfSection映射内存。

代码:

#define SEC_IMAGE 0x01000000
void* MapFileBaseAddress = NULL;
HANDLE  hFile = NULL;
HANDLE  hSection = NULL; /** 内存映射文件,返回基址:
*/
void* CreateMapFileAndGetBaseAddr(
  PUNICODE_STRING pDriverName
  ){
  //HANDLE  hFile;
  //HANDLE  hSection; 
  NTSTATUS status;
  SIZE_T size = 0;
  IO_STATUS_BLOCK io_status = {0};
  OBJECT_ATTRIBUTES oa = {0};  InitializeObjectAttributes(
    &oa,
  pDriverName,
    OBJ_CASE_INSENSITIVE,
    0,
    0
    );status = ZwOpenFile(&hFile, 
       FILE_EXECUTE | SYNCHRONIZE, 
       &oa,
       &io_status, 
       FILE_SHARE_READ, 
       FILE_SYNCHRONOUS_IO_NONALERT);
if(!NT_SUCCESS(status))
        {
                DbgPrint("ZwOpenFile failed\n");
                return NULL;
        }
 oa.ObjectName = 0;status = ZwCreateSection(&hSection,
        SECTION_ALL_ACCESS,
        &oa,
        0,
        PAGE_EXECUTE, 
        SEC_IMAGE, 
        hFile);
if(!NT_SUCCESS(status))
        {
                DbgPrint("ZwCreateSection failed\n");
        hFile = NULL;
        ZwClose(hFile);
                return NULL;
        }status = ZwMapViewOfSection(hSection,
           PsGetCurrentProcessId(),
           &MapFileBaseAddress, 
           0, 
           1024,
           0, 
           &size,
           ViewShare,
           MEM_TOP_DOWN, 
           PAGE_READWRITE); 
if(!NT_SUCCESS(status))
        {
                DbgPrint("ZwMapViewOfSection failed\n");
        hSection = NULL;
        ZwClose(hSection);
        hFile = NULL;
        ZwClose(hFile);
                return NULL;
        }
    
return MapFileBaseAddress;
}调用方法映射文件返回映射后基址,代码:PVOID BaseAddress = NULL;
UNICODE_STRING driverName;
......RtlInitUnicodeString( &driverName, L"\\??\\C:\\Documents and Settings\\Administrator\\桌面\\111.sys" );
BaseAddress = CreateMapFileAndGetBaseAddr(&driverName);
DbgPrint( "MapFile Return Address:%X\r\n", BaseAddress );释放清理:if( NULL != hFile )
  ZwClose(hFile);
if( NULL != hSection )
  ZwClose(hSection);

代码仅供参考!
PsGetCurrentProcessId()
也可以用NtCurrentProcess()代替。

以上代码在XPSP3 DDK下测试通过!*转载请注明来自看雪论坛@PEdiy.com

jpg 改 rar使用ZwMapViewOfSection创建内存映射文件总结

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902