找回密码
 立即注册→加入我们

QQ登录

只需一步,快速开始

搜索
热搜: 下载 VB C 实现 编写
查看: 2191|回复: 1

简单的虚拟设备驱动 ramdisk masm32源码

[复制链接]

30

主题

210

回帖

2774

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2155 个
贡献
206 次
宅之契约
0 份
在线时间
478 小时
注册时间
2016-7-12
发表于 2016-7-15 12:46:26 | 显示全部楼层 |阅读模式

欢迎访问技术宅的结界,请注册或者登录吧。

您需要 登录 才可以下载或查看,没有账号?立即注册→加入我们

×
其余部分看附件 有bug

  1. .code


  2. DispatchAddDevice proc uses esi edi ebx pDriverObject:PDRIVER_OBJECT,TargetDevice:PDEVICE_OBJECT
  3.         LOCAL DestinationString:UNICODE_STRING
  4.         LOCAL SourceString:UNICODE_STRING
  5.         LOCAL pDrvExt:DWORD
  6.         LOCAL pDevObj:DWORD
  7.         LOCAL DevExt:_DEVICE_EXTENSION_u
  8.        
  9.        
  10.         $DbgPrint $CTA0("DispatchAddDevice\n")
  11.        
  12.        
  13.         invoke IoGetDriverObjectExtension,pDriverObject,offset DriverEntry
  14.        
  15.         mov pDrvExt,eax

  16.        
  17.         .if DWORD ptr (_DRIVER_EXTENSION_u ptr [eax]).DeviceInitialized == TRUE

  18.                 mov edi,STATUS_DEVICE_ALREADY_ATTACHED        ;0C0000038h
  19.                 jmp done0
  20.         .endif
  21.        
  22.         lea esi,DevExt
  23.         assume esi:ptr _DEVICE_EXTENSION_u
  24.        
  25.         invoke RtlZeroMemory,esi,SizeOf _DEVICE_EXTENSION_u
  26.        

  27.         invoke ExAllocatePoolWithTag,PagedPool,20,"2maR"
  28.         mov [esi].DiskRegInfo.DriveLetter.Buffer,eax
  29.         .if !eax
  30.                 mov edi,STATUS_INSUFFICIENT_RESOURCES        ;0C000009Ah
  31.                 jmp done1
  32.         .endif       
  33.                
  34.         mov [esi].DiskRegInfo.DriveLetter.MaximumLength,20

  35.                
  36.         invoke _QueryDiskRegParameters,pDrvExt,addr [esi].DiskRegInfo       
  37.        
  38.        
  39.         invoke RtlInitUnicodeString,addr DestinationString,$CTW0("\\Device\\HarddiskVolumeRD")
  40.        
  41.        
  42.         mov eax,[esi].DiskRegInfo.MediaType        ;1~4
  43.        


  44.         .if eax==2       
  45.                 mov eax,0
  46.                 mov ecx,FILE_DEVICE_DISK        ;7       
  47. ;        .elseif eax==1
  48. ;                mov eax,0
  49. ;                mov ecx,FILE_DEVICE_VIRTUAL_DISK ;24h
  50.         .elseif eax==3
  51.                 mov eax,1        ;FILE_REMOVABLE_MEDIA
  52.                 mov ecx,FILE_DEVICE_DISK        ;7
  53.         .elseif eax==4
  54.                 mov eax,5        ;FILE_FLOPPY_DISKETTE or FILE_REMOVABLE_MEDIA
  55.                 mov ecx,FILE_DEVICE_DISK        ;7
  56.         .else
  57.                 mov edi,STATUS_INSUFFICIENT_RESOURCES        ;0C000009Ah
  58.                 jmp done0                               
  59.         .endif
  60.        
  61.         or eax,FILE_DEVICE_SECURE_OPEN        ;100h
  62.        
  63.         lea edx,pDevObj
  64.         invoke IoCreateDevice,pDriverObject,\
  65.                                                   SizeOf _DEVICE_EXTENSION_u,\                ;DevExt size
  66.                                                   addr DestinationString,\
  67.                                                   ecx,\
  68.                                                   eax,\
  69.                                                   0,\
  70.                                                   edx          
  71.                                                   
  72.         .if eax < SDWORD ptr 0
  73.                 mov edi,eax
  74.                 invoke ExFreePoolWithTag,[esi].DiskRegInfo.DriveLetter.Buffer,0
  75.                 jmp done0
  76.         .endif
  77.                
  78.                
  79.         mov ebx,pDevObj
  80.         assume ebx:ptr DEVICE_OBJECT
  81.        
  82.        
  83.         mov esi,[ebx].DeviceExtension
  84.        
  85.         invoke RtlMoveMemory,esi,addr DevExt,sizeof _DEVICE_EXTENSION_u
  86.        
  87.        
  88.         mov eax,TargetDevice
  89.        
  90.         mov [esi].TagDev,eax
  91.         mov [esi].DeviceObject,ebx
  92.        
  93.         and dword ptr [esi].DevState,0
  94.        
  95.         invoke IoInitializeRemoveLockEx,addr [esi].RemoveLock,\
  96.                                                                         "lmaR",\
  97.                                                                         1,\
  98.                                                                         0Ah,\
  99.                                                                         SizeOf IO_REMOVE_LOCK

  100.         or [ebx].Flags,DO_POWER_PAGABLE OR DO_DIRECT_IO; 2010h
  101.        
  102.         invoke KeInitializeSpinLock,addr [esi].Spinlock
  103.        
  104.        
  105.         mov eax,[esi].DiskRegInfo.NumDevicePage
  106.         .if !eax
  107.                 mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
  108.                 jmp done1
  109.         .endif       
  110.        
  111.         mov [esi].MaxNumMDL,eax
  112.        
  113.         mov ecx,eax
  114.         shl ecx,6               
  115.         mov [esi].Cylinders,ecx
  116.        
  117.         mov ecx,[esi].DiskRegInfo.DisksizeM
  118.         shl ecx,6        ;1024*1024/512/32
  119.         mov [esi].Tracks,ecx
  120.        
  121.         shl eax,2        ;eax*4
  122.        
  123.         $DbgPrint $CTA0("Initialize Disk Space %d\n")
  124.         invoke ExAllocatePoolWithTag,NonPagedPool,eax,"3maR"
  125.         mov [esi].MDL,eax
  126.         .if !eax
  127.                 invoke DeviceContextCleanup,ebx
  128.                 mov edi,STATUS_INSUFFICIENT_RESOURCES        ;0C000009Ah
  129.                 jmp done1
  130.         .endif
  131.                        
  132.         mov ecx,[esi].MaxNumMDL
  133.         shl ecx,2
  134.        
  135.         invoke RtlZeroMemory,eax,ecx
  136.                        
  137.         invoke DeviceAllocMdl,esi       
  138.         .if (eax < [esi].MaxNumMDL)
  139.                 mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
  140.                 jmp done1
  141.         .endif       
  142.        
  143.        
  144.         invoke DeviceLockPresentPage,esi,0       
  145.         .if !eax
  146.                 mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
  147.                 jmp done1
  148.         .endif
  149.                        
  150.         invoke FormatDisk,ebx
  151.                
  152.         invoke ExAllocatePoolWithTag,PagedPool,2Eh,"4maR"
  153.         mov [esi].SymbolicLinkName.Buffer,eax
  154.         .if !eax
  155.                 mov edi,STATUS_INSUFFICIENT_RESOURCES;0C000009Ah
  156.                 jmp done1
  157.         .endif
  158.        
  159.         .if [esi].DiskRegInfo.DriveLetter._Length
  160.                 invoke RtlInitUnicodeString,addr SourceString,$CTW0("\\DosDevices\")
  161.                
  162.                
  163.                 lea edi,[esi].SymbolicLinkName
  164.                 assume edi:PTR UNICODE_STRING
  165.                
  166.                 mov ax,SourceString._Length
  167.                 mov [edi]._Length,ax
  168.                 mov [edi].MaximumLength,2Eh
  169.                
  170.                 assume edi:nothing
  171.                
  172.                 invoke RtlCopyUnicodeString,edi,addr SourceString
  173.                 invoke RtlAppendUnicodeStringToString,edi,addr [esi].DiskRegInfo.DriveLetter
  174.                 invoke IoCreateSymbolicLink,edi,addr DestinationString
  175.                
  176.                 mov edi,eax
  177.                 .if eax < SDWORD ptr 0
  178.                         jmp done1
  179.                 .endif
  180.                 or dword ptr [esi].flag,1
  181.         .endif
  182.        
  183.         invoke IoAttachDeviceToDeviceStack,pDevObj,TargetDevice
  184.         mov [esi].LoDeviceObject,eax
  185.         .if !eax
  186.                 mov edi,STATUS_NO_SUCH_DEVICE        ;0C000000Eh
  187.                 jmp done1
  188.         .endif
  189.        
  190.         mov eax,pDrvExt
  191.        
  192.         mov (_DRIVER_EXTENSION_u ptr [eax]).DeviceInitialized,TRUE       
  193.        
  194.         and [ebx].Flags,7Fh
  195.        
  196.         mov edi,STATUS_SUCCESS
  197.         jmp done0
  198.        
  199.         assume esi:nothing
  200. done1:       
  201.         invoke DeviceContextCleanup,pDevObj               

  202. done0:       
  203.         $DbgPrint $CTA0("DeviceInitialize %08X\n"),edi
  204.         mov eax,edi
  205.        
  206. ;done:       
  207.         ret
  208. DispatchAddDevice endp
复制代码
回复

使用道具 举报

30

主题

210

回帖

2774

积分

用户组: 版主

UID
1821
精华
7
威望
69 点
宅币
2155 个
贡献
206 次
宅之契约
0 份
在线时间
478 小时
注册时间
2016-7-12
 楼主| 发表于 2016-7-15 20:30:01 | 显示全部楼层
本帖最后由 Ayala 于 2018-7-31 20:20 编辑

原来附件没上传成功!

ramdisk.rar

612.18 KB, 下载次数: 2

回复 赞! 靠!

使用道具 举报

QQ|Archiver|小黑屋|技术宅的结界 ( 滇ICP备16008837号 )|网站地图

GMT+8, 2024-4-17 07:09 , Processed in 0.033340 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表