1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| ;===================================================================
; compilation : nasm -f win32 file.asm
; link : golink /wdm file.obj ntoskrnl.exe
; code testé avec windbg et virtualbox (winxp sp3 32)
;===================================================================
BITS 32
;===================================================================
; APIs ntoskrnl.exe
;===================================================================
extern DbgPrint
;extern DbgBreakPoint
extern RtlInitUnicodeString
extern IoCreateDevice
extern IoCreateSymbolicLink
extern IoDeleteDevice
extern IoDeleteSymbolicLink
;===================================================================
; Define
;===================================================================
%define pDeviceName PUNICODE_STRING
%define pSymbolicLinkName PUNICODE_STRING
%define pDriverObject ebp+8
;===================================================================
; Equate
;===================================================================
STATUS_SUCCESS equ 0h
FILE_DEVICE_UNKNOWN equ 22h
STATUS_DEVICE_CONFIGURATION_ERROR equ 00c0000182h
IRP_MJ_MAXIMUM_FUNCTION equ 1bh
IO_NO_INCREMENT equ 0h
IRP_MJ_CREATE equ 0h
IRP_MJ_CLOSE equ 2h
IRP_MJ_DEVICE_CONTROL equ 0eh
;===================================================================
; DriverEntry
;===================================================================
segment .code
start:
push ebp
mov ebp, esp
push dword msg_drv
call DbgPrint
; initialisation chaîne unicode
push dword DeviceName
push dword pDeviceName
call RtlInitUnicodeString
; fonction iocreatedevice
push dword pDeviceObject
push dword 0h
push dword 0h
push dword FILE_DEVICE_UNKNOWN
push dword pDeviceName
push dword 0h
push dword [pDriverObject]
call IoCreateDevice
; if eax = non zéro
cmp eax, STATUS_SUCCESS
jne err_IoCreateDevice
; else
push dword IoCD_ok
call DbgPrint
; initialisation chaîne unicode
push dword SymbolicLinkName
push dword pSymbolicLinkName
call RtlInitUnicodeString
; fonction iocreatesymboliclink
push dword pDeviceName
push dword pSymbolicLinkName
call IoCreateSymbolicLink
; if eax non zéro
cmp eax, STATUS_SUCCESS
jne err_IoCreateSymbolicLink
; else
push dword IoCSL_ok
call DbgPrint
mov eax, dword [pDriverObject] ; ebp+8
mov dword [eax+DRIVER_OBJECT.DriverUnload], UnloadRoutine
xor eax, eax ; return status_success
jmp drv_exit
; gestion erreur
err_IoCreateDevice:
push dword IoCD_err
call DbgPrint
mov eax, STATUS_DEVICE_CONFIGURATION_ERROR
err_IoCreateSymbolicLink:
mov eax, dword [pDriverObject]
push dword [eax+DRIVER_OBJECT.DeviceObject]
call IoDeleteDevice
push dword IoCSL_err
call DbgPrint
; fin de procédure
drv_exit:
mov esp, ebp
pop ebp
ret 8h
; fonction unload
UnloadRoutine:
push ebp
mov ebp, esp
push SymbolicLinkName
call IoDeleteSymbolicLink
mov eax, dword [pDriverObject] ;ebp+8h
push dword [eax+DRIVER_OBJECT.DeviceObject] ; eax+34h
call IoDeleteDevice
jmp drv_exit
;===================================================================
; data
;===================================================================
segment .Data align 4
DeviceName dw __utf16__('\Device\TEST_driver66'), 0h
SymbolicLinkName dw __utf16__('\DosDevices\TEST_driver66'), 0h
msg_drv db 'Driver : ok.', 0ah, 0h
IoCD_err db 'Driver : IoCreateDevice Failed.',0ah, 0h
IoCD_ok db 'Driver : IoCreateDevice Success.', 0ah, 0h
IoCSL_err db 'Driver : IoCreateSymbolicLink failed.', 0ah, 0h
IoCSL_ok db 'Driver : IoCreateSymbolicLink Success', 0ah, 0h
;===================================================================
; bss
;===================================================================
segment .bss align 4
PUNICODE_STRING resd 2
pDeviceObject resd 1
struc DRIVER_OBJECT
.Type: resw 1
.Size: resw 1
.DeviceObject resd 1
.Flags: resd 1
.DriverStart: resd 1
.DriverSize: resd 1
.DriverSection: resd 1
.DriverExtension: resd 1
.DriverName: resb 8
.HardwareDatabase: resd 1
.FastIoDispatch: resd 1
.DriverInit: resd 1
.DriverStartIo: resd 1
.DriverUnload: resd 1
.MajorFunction: resd IRP_MJ_MAXIMUM_FUNCTION + 1
endstruc |
Partager