drvZtsFw.bat
上传用户:lingfuwu
上传日期:2013-03-31
资源大小:13k
文件大小:9k
开发平台:

Asm

  1. ;@echo off
  2. ;goto make
  3. .386
  4. .model flat, stdcall
  5. option casemap:none
  6. include c:masm32includew2kntstatus.inc
  7. include c:masm32includew2kntddk.inc
  8. include c:masm32includew2kntoskrnl.inc
  9. include c:masm32includew2kipfirewall.inc
  10. includelib c:masm32libw2kntoskrnl.lib
  11. include c:masm32MacrosStrings.mac
  12. include ..common.inc
  13. _DispatchCreateClose proto :PDEVICE_OBJECT,:PIRP
  14. _DriverUnload proto :PDRIVER_OBJECT
  15. _DispatchControl proto :PDEVICE_OBJECT,:PIRP
  16. _SetFilterFunction proto :DWORD,:DWORD
  17. _IpFilterProc proto :DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD,:DWORD
  18. DEVICE_EXTENSION struct
  19. protocol db  ?
  20. srcIp dd  ?
  21. srcPort dw  ?
  22. dstIp dd  ?
  23. dstPort dw  ?
  24. bIn dd  ?
  25. handle dd  ?
  26. event PKEVENT <>
  27. DEVICE_EXTENSION ends
  28. .const
  29. CCOUNTED_UNICODE_STRING "\Device\devZtsFw", g_usDeviceName, 4
  30. CCOUNTED_UNICODE_STRING "\??\slZtsFw", g_usSymbolicLinkName, 4
  31. CCOUNTED_UNICODE_STRING "\BaseNamedObjects\FwHook_Event", g_usFwHookEvent, 4
  32. CCOUNTED_UNICODE_STRING "\Device\Ip", g_usIpFilterName, 4
  33. .data
  34. g_lpExtension dd  0
  35. g_bLoaded dd  0
  36. .data?
  37. g_Ports dw  MAX_PORTS+1 dup (?)
  38. .code
  39. DriverEntry proc uses esi, pDriverObject:PDRIVER_OBJECT, pusRegistryPath:PUNICODE_STRING
  40. LOCAL status : NTSTATUS
  41. LOCAL pDeviceObject : PDEVICE_OBJECT
  42. ; int 3
  43. mov status, STATUS_DEVICE_CONFIGURATION_ERROR
  44. invoke IoCreateDevice, pDriverObject, sizeof DEVICE_EXTENSION, addr g_usDeviceName, 
  45. FILE_DEVICE_UNKNOWN, 0, FALSE, addr pDeviceObject
  46. .if eax==STATUS_SUCCESS
  47. mov esi, pDeviceObject
  48. assume esi : ptr DEVICE_OBJECT
  49. mov esi, [esi].DeviceExtension
  50. mov g_lpExtension, esi
  51. invoke IoCreateSymbolicLink, addr g_usSymbolicLinkName, addr g_usDeviceName
  52. .if eax==STATUS_SUCCESS
  53. mov eax, pDriverObject
  54. assume eax:ptr DRIVER_OBJECT
  55. mov [eax].DriverUnload,    offset _DriverUnload
  56. mov [eax].MajorFunction[IRP_MJ_CREATE*(sizeof PVOID)],    offset _DispatchCreateClose
  57. mov [eax].MajorFunction[IRP_MJ_CLOSE*(sizeof PVOID)],    offset _DispatchCreateClose
  58. mov [eax].MajorFunction[IRP_MJ_DEVICE_CONTROL*(sizeof PVOID)], offset _DispatchControl
  59. assume eax:nothing
  60. assume esi : ptr DEVICE_EXTENSION
  61. lea eax, [esi].handle
  62. invoke IoCreateNotificationEvent, addr g_usFwHookEvent, eax
  63. mov [esi].event, eax
  64. invoke KeClearEvent, [esi].event
  65. mov status, STATUS_SUCCESS
  66. .else
  67. invoke IoDeleteDevice, pDeviceObject
  68. .endif
  69. .endif
  70. mov eax, status
  71. ret
  72. DriverEntry endp
  73. _DispatchControl proc uses esi edi ebx,pDeviceObject:PDEVICE_OBJECT, pIrp:PIRP
  74. LOCAL status : NTSTATUS
  75. LOCAL dwBytesReturned
  76. ; int 3
  77. and dwBytesReturned, 0
  78. mov status, STATUS_UNSUCCESSFUL
  79. mov esi, pIrp
  80. assume esi : ptr _IRP
  81. IoGetCurrentIrpStackLocation esi
  82. mov edi, eax
  83. assume edi : ptr IO_STACK_LOCATION
  84. mov eax, [edi].Parameters.DeviceIoControl.IoControlCode
  85. push edi
  86. .if eax==IOCTL_START_IP_HOOK
  87. .if g_bLoaded==0
  88. invoke _SetFilterFunction, offset _IpFilterProc, 1
  89. mov g_bLoaded, 1
  90. .endif
  91. mov status, eax
  92. .elseif eax==IOCTL_STOP_IP_HOOK
  93. .if g_bLoaded==1
  94. invoke _SetFilterFunction, offset _IpFilterProc, 0
  95. mov g_bLoaded, 0
  96. .endif
  97. mov status, eax
  98. .elseif eax==IOCTL_ADD_FILTER
  99. assume esi : ptr _IRP
  100. assume edi : ptr IO_STACK_LOCATION
  101. mov eax, MAX_PORTS
  102. imul eax, sizeof word
  103. mov ebx, [edi].Parameters.DeviceIoControl.InputBufferLength
  104. .if ebx <= eax
  105. mov edi, [esi].AssociatedIrp.SystemBuffer
  106. invoke memcpy, addr g_Ports, edi, ebx
  107. mov status, STATUS_SUCCESS
  108. .endif
  109. .elseif eax==IOCTL_GET_IP_INFO
  110. assume esi : ptr _IRP
  111. assume edi : ptr IO_STACK_LOCATION
  112. .if [edi].Parameters.DeviceIoControl.OutputBufferLength >= sizeof CALLBACK_IP_INFO
  113. mov edi, [esi].AssociatedIrp.SystemBuffer
  114. assume edi : ptr CALLBACK_IP_INFO
  115. mov esi, g_lpExtension
  116. assume esi : ptr DEVICE_EXTENSION
  117. mov al, [esi].protocol
  118. mov [edi].protocol , al
  119. mov eax, [esi].srcIp
  120. mov [edi].srcIp, eax
  121. mov ax, [esi].srcPort
  122. mov [edi].srcPort, ax
  123. mov eax, [esi].dstIp
  124. mov [edi].dstIp, eax
  125. mov ax, [esi].dstPort
  126. mov [edi].dstPort, ax
  127. mov eax, [esi].bIn
  128. mov [edi].bIn, eax
  129. mov status, STATUS_SUCCESS
  130. .else
  131. mov status, STATUS_BUFFER_TOO_SMALL
  132. .endif
  133. .endif
  134. pop edi
  135. assume edi : ptr IO_STACK_LOCATION
  136. mov esi, pIrp
  137. assume esi : ptr _IRP
  138. push status
  139. pop [esi].IoStatus.Status
  140. xor eax, eax
  141. .if status==STATUS_SUCCESS
  142. mov eax, [edi].Parameters.DeviceIoControl.OutputBufferLength
  143. .endif
  144. mov [esi].IoStatus.Information, eax
  145. assume esi : nothing
  146. assume edi : nothing
  147. invoke IoCompleteRequest, pIrp, IO_NO_INCREMENT
  148. mov eax, status
  149. ret
  150. _DispatchControl endp
  151. _DispatchCreateClose proc pDeviceObject:PDEVICE_OBJECT, pIrp:PIRP
  152. mov eax, pIrp
  153. assume eax:ptr _IRP
  154. mov [eax].IoStatus.Status, STATUS_SUCCESS
  155. and [eax].IoStatus.Information, 0
  156. assume eax:nothing
  157. invoke IoCompleteRequest, pIrp, IO_NO_INCREMENT
  158. mov eax, STATUS_SUCCESS
  159. ret
  160. _DispatchCreateClose endp
  161. _DriverUnload proc pDriverObject:PDRIVER_OBJECT
  162. .if g_bLoaded==1
  163. invoke _SetFilterFunction, offset _IpFilterProc, 0
  164. mov g_bLoaded, 0
  165. .endif
  166. invoke IoDeleteSymbolicLink, addr g_usSymbolicLinkName
  167. mov eax, pDriverObject
  168. invoke IoDeleteDevice, (DRIVER_OBJECT PTR [eax]).DeviceObject
  169. ret
  170. _DriverUnload endp
  171. _SetFilterFunction proc uses ebx esi edi, lpFunc:DWORD, bFlags:DWORD
  172. LOCAL status : NTSTATUS
  173. LOCAL ipFileObject : PFILE_OBJECT
  174. LOCAL ipDeviceObject : PDEVICE_OBJECT
  175. LOCAL fhi : IP_SET_FIREWALL_HOOK_INFO
  176. LOCAL ioStatus : IO_STATUS_BLOCK
  177. ; int 3
  178. invoke IoGetDeviceObjectPointer, addr g_usIpFilterName, STANDARD_RIGHTS_ALL, addr ipFileObject, addr ipDeviceObject
  179. .if eax==STATUS_SUCCESS
  180. mov eax, lpFunc
  181. mov fhi.FirewallPtr, eax
  182. mov fhi.Priority, 1
  183. mov eax, bFlags
  184. mov fhi._Add, al
  185. invoke IoBuildDeviceIoControlRequest, IOCTL_IP_SET_FIREWALL_HOOK,
  186. ipDeviceObject,
  187. addr fhi,
  188. sizeof IP_SET_FIREWALL_HOOK_INFO,
  189. 0, 0, 0, 0,
  190. addr ioStatus
  191. .if eax!=0
  192. invoke IoCallDriver, ipDeviceObject, eax
  193. .endif
  194. .if ipFileObject
  195. invoke ObDereferenceObject, ipFileObject
  196. .endif
  197. .endif
  198. ret
  199. _SetFilterFunction endp
  200. _IpFilterProc proc uses ebx esi edi, pData:DWORD, RecvInterfaceIndex:DWORD, pSendInterfaceIndex:DWORD, pDestinationType:DWORD, pContext:DWORD, ContextLength:DWORD, pRcvBuf:DWORD
  201. LOCAL dwSize
  202. LOCAL lpMem
  203. ; int 3
  204. mov dwSize, 0
  205. mov lpMem, 0
  206. mov esi, pData
  207. mov esi, dword ptr [esi]
  208. mov edi, esi
  209. assume esi : ptr IPRcvBuf
  210. mov eax, [esi].ipr_size
  211. mov dwSize, eax
  212. .while [esi].ipr_next
  213. mov esi, [esi].ipr_next
  214. mov eax, [esi].ipr_size
  215. add dwSize, eax
  216. .endw
  217. invoke ExAllocatePool, NonPagedPool, dwSize
  218. test eax, eax
  219. jz exit_0
  220. mov lpMem, eax
  221. mov esi, edi
  222. mov edi, lpMem
  223. mov ebx, [esi].ipr_size
  224. mov ecx, [esi].ipr_buffer
  225. invoke memcpy, edi, ecx, ebx
  226. add edi, ebx
  227. .while [esi].ipr_next
  228. mov esi, [esi].ipr_next
  229. mov ebx, [esi].ipr_size
  230. mov ecx, [esi].ipr_buffer
  231. invoke memcpy, edi, ecx, ebx
  232. add edi, ebx
  233. .endw
  234. mov esi, lpMem
  235. assume esi : ptr IPHeader
  236. mov al, [esi].protocol
  237. .if al==IPPROTO_TCP
  238. movzx eax, [esi].headerLength
  239. shl al, 2
  240. add esi, eax
  241. assume esi : ptr TCPHeader
  242. movzx ebx, [esi].sourcePort
  243. movzx ecx, [esi].destinationPort
  244. xor eax, eax
  245. mov edi, offset g_Ports
  246. mov ax, word ptr [edi]
  247. add edi, 2
  248. .while eax
  249. .if eax==ebx || eax==ecx
  250. mov edi, g_lpExtension
  251. assume edi : ptr DEVICE_EXTENSION
  252. mov ax, [esi].sourcePort
  253. mov [edi].srcPort, ax
  254. mov ax, [esi].destinationPort
  255. mov [edi].dstPort, ax
  256. xor eax, eax
  257. mov esi, pContext
  258. assume esi : ptr FIREWALL_CONTEXT_T
  259. .if esi
  260. mov eax, [esi].Direction
  261. .endif
  262. mov [edi].bIn, eax
  263. mov esi, lpMem
  264. assume esi : ptr IPHeader
  265. mov eax, [esi].source
  266. mov [edi].srcIp, eax
  267. mov eax, [esi].destination
  268. mov [edi].dstIp, eax
  269. mov al, [esi].protocol
  270. mov [edi].protocol, al
  271. invoke KeSetEvent, [edi].event, 0, FALSE
  272. invoke KeClearEvent, [edi].event
  273. mov eax, DROP
  274. ret
  275. .endif
  276. mov ax, word ptr [edi]
  277. add edi, 2
  278. .endw
  279. .endif
  280. .if lpMem
  281. invoke ExFreePool, lpMem
  282. .endif
  283. exit_0:
  284. mov eax, FORWARD
  285. ret
  286. _IpFilterProc endp
  287. end DriverEntry
  288. :make
  289. set path=%path%;c:masm32bin;D:Program FilesCompuwareDriverStudioSoftICE
  290. set drv=drvZtsFw
  291. ml /nologo /c /coff %drv%.bat
  292. link /nologo /driver /base:0x10000 /align:32 /out:%drv%.sys /subsystem:native %drv%.obj
  293. del %drv%.obj
  294. nmsym /translate:source,package,always %drv%.sys
  295. echo.
  296. pause