ring0 call ring3 KeUserModeCallBack Китайцы Cool

Тема в разделе "WASM.NT.KERNEL", создана пользователем calidus, 13 май 2008.

  1. calidus

    calidus Member

    Публикаций:
    0
    Регистрация:
    27 дек 2005
    Сообщения:
    618
    Код (Text):
    1. .386
    2. .model flat, stdcall
    3. option casemap:none
    4.  
    5. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    6. ;                                  I N C L U D E   F I L E S                                        
    7. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    8.  
    9. include \\masm32\\include\\w2k\\ntstatus.inc
    10. include \\masm32\\include\\w2k\\ntddk.inc
    11. include \\masm32\\include\\w2k\\ntoskrnl.inc
    12. includelib \\masm32\\lib\\w2k\\ntoskrnl.lib
    13. include \\masm32\\Macros\\Strings.mac
    14. include useful.asm
    15. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    16. ;                                 R E A D O N L Y    D A T A                                        
    17. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    18.  
    19. .const
    20.  
    21. CCOUNTED_UNICODE_STRING "\\\\Device\\\\ring3code", g_usDeviceName, 4
    22. CCOUNTED_UNICODE_STRING "\\\\DosDevices\\\\ring3code", g_usSymbolicLinkName, 4
    23. Target db "Explorer.exe",0
    24. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    25. ;                              U N I N I T I A L I Z E D  D A T A                                  
    26. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    27.  
    28. .data?
    29. g_dwImageFileNameOffset DWORD   ?
    30. g_fbNotifyRoutineSet BOOL   ?
    31. szProcessName  CHAR IMAGE_FILE_PATH_LEN dup(?)
    32. notAttack  BOOL    ?
    33. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    34. ;                                         C O D E                                                  
    35. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    36.  
    37. .code
    38. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    39. ;                                   Myproc !!@!! I Just Code For Fan~~~ MayBe Error~~~                                            
    40. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    41. myproc:
    42. ;please using Api Reloc before  Call apiz
    43. ;even then u could restore kernel32.dll imagebase to ur code in ring0  bcz i got peb for u ~
    44. ;i was just coding something here to test my code can go to ring3~
    45. ;if it was call in ring0 it would crash down the system ~ but in ring3 all it would make a msgbox or
    46. ; an error for u to know code runs under ring3~      
    47.         invoke MessageBox, hDlg, $CTA0("Sure want to exit?"),      $CTA0("Exit Confirmation"), MB_YESNO + MB_ICONQUESTION + MB_DEFBUTTON1
    48.                         retn
    49. length_myproc =$ - myproc
    50. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    51. ;                                   DisableWriteProtect                                            
    52. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    53.  
    54. DisableWriteProtect proc
    55. Local uAttr:Dword
    56. push eax
    57. mov eax, cr0;
    58. mov uAttr, eax;
    59. and eax, 0FFFEFFFFh; // CR0 16 BIT = 0
    60. mov cr0, eax;
    61. pop eax
    62. mov eax,uAttr
    63. ret
    64. DisableWriteProtect Endp
    65.  
    66. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    67. ;                                   EnableWriteProtect                                            
    68. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    69.  
    70. EnableWriteProtect proc uOldAttr:dword
    71. push eax;
    72. mov eax, uOldAttr
    73. mov cr0, eax;
    74. pop eax;  
    75. ret 04h
    76. EnableWriteProtect endp
    77.  
    78.  
    79. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    80. ;                                   DispatchCreateClose                                            
    81. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    82.  
    83. DispatchCreateClose proc pDeviceObject:PDEVICE_OBJECT, pIrp:PIRP
    84.  
    85. mov ecx, pIrp
    86. mov (_IRP PTR [ecx]).IoStatus.Status, STATUS_SUCCESS
    87. and (_IRP PTR [ecx]).IoStatus.Information, 0
    88.  
    89. fastcall IofCompleteRequest, ecx, IO_NO_INCREMENT
    90.  
    91. mov eax, STATUS_SUCCESS
    92. ret
    93.  
    94. DispatchCreateClose endp
    95. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    96. ;                                       GetUserPEB                                          
    97. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    98. ; out: eax - *peb
    99. GetUserPEB proc near
    100. assume fs:nothing
    101. push ebx
    102. mov ebx, dword ptr fs:[124h]
    103. mov eax, dword ptr [ebx+134h]  ; gimme KTRAP_FRAME
    104.       ; no ktrap_frame if called from kernel mode
    105.       ; (from non user mode thread)
    106. test eax, eax
    107. jz GetUserPEB_End
    108.  
    109. mov eax, dword ptr [ebx+44h]
    110.         mov eax, dword ptr [eax+1b0h]  ; peb for non user mode threadz null too
    111.         test eax, eax
    112. GetUserPEB_end:
    113. pop ebx
    114.         retn
    115. GetUserPEB endp
    116. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    117. ;                                       CallToUserMode                                          
    118. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    119. CallToUserMode proc lpPeb:PVOID, dwAddr:DWORD, dwSize:DWORD
    120.   local kernel_callback_tableb:dword      
    121.   local ecx_on_return:dword
    122.   local edx_on_return:dword
    123.   local base_address:dword
    124.        ; mov ecx_on_retrun,ecx
    125.        ; mov edx_on_retrun,edx      
    126.         mov eax,lpPEB
    127. mov eax, dword ptr [eax+2ch]  ; *KernelCallbackTable
    128. mov kernel_callback_table, eax
    129. and base_address, 0
    130. push PAGE_READWRITE
    131. push MEM_COMMIT or MEM_TOP_DOWN or MEM_RESERVE
    132. lea eax, allocation_size
    133. mov allocation_size, (dwSize+1024)
    134. push eax        
    135. lea eax, base_address
    136. push 0
    137. push eax
    138. push -1
    139.         call NtAllocateVirtualMemory
    140. test eax, eax
    141.         jnz CallToUserMode_End
    142.         mov edx, base_address
    143.         mov edi, edx
    144.         push edi
    145.         push esi
    146.         mov esi, dwAddr; copy the to user-mode
    147.         push dwSize
    148.         pop ecx
    149.         rep movsb
    150.         pop esi
    151.         pop edi
    152.         mov eax, kernel_callback_table
    153. ;mov edx, base_address
    154. sub edx, eax
    155. shr edx, 2
    156. lea ecx,ecx_on_return
    157. push ecx
    158. lea eax, edx_on_return
    159. push eax
    160. push 0
    161. lea  eax, edx_on_return
    162. push eax  
    163. ;stack start
    164. ;where to start code user mode
    165. push edx  
    166.         call KeUserModeCallBack
    167. CallToUserMode_Free_Mem:
    168. push MEM_DECOMMIT
    169. mov eax, allocation_size
    170. push eax
    171. mov eax, base_address
    172. push eax
    173.         push -1
    174. call NtFreeVirtualMemory
    175. CallToUserMode_End:
    176.         leave
    177. ret
    178. CallToUserMode endp
    179. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    180. ;                                       AttackProcess                                          
    181. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    182. AttackProcess proc pEpr:PVOID
    183. local oldArr:DWORD
    184.   invoke KeAttachProcess,pEpr
    185.   .if eax == STATUS_SUCCESS
    186.           invoke DisableWriteProtect
    187.           mov oldArr,eax
    188.           invoke GetUserPEB
    189.           jz Attack_End_err:
    190.           invoke CallToUserMode, eax, addr myproc, length_myproc
    191.           invoke EnableWriteProcect,oldArr
    192.           invoke KeDetachProcess
    193. .endif
    194. Attack_End: mov eax, STATUS_SUCCESS
    195. Attack_End_err: ret
    196. AttackProcess endp
    197. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    198. ;                                       NotifyRoutine                                          
    199. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    200.  
    201. NotifyRoutine proc dwParentId:DWORD, dwProcessId:DWORD, bCreate:BOOL ; BOOLEAN
    202.  
    203. local peProcess:PVOID    ; PEPROCESS
    204. local fbDereference:BOOL
    205. local us:UNICODE_STRING
    206. local as:ANSI_STRING
    207.  
    208. push eax      ; reserve DWORD on stack
    209. invoke PsLookupProcessByProcessId, dwParentId, esp
    210. pop peProcess     ; -> EPROCESS
    211. .if eax == STATUS_SUCCESS
    212.   mov fbDereference, TRUE  ; PsLookupProcessByProcessId references process object
    213. .else
    214.    invoke PsLookupProcessByProcessId, dwProcessId, esp
    215.    pop peProcess     ; -> EPROCESS
    216.    .if eax == STATUS_SUCCESS
    217.               mov fbDereference, TRUE
    218.           .else
    219.               ret
    220.           .endif
    221. .endif
    222. mov eax, bCreate
    223. invoke GetImageFilePath, peProcess, addr us
    224. .if eax == STATUS_SUCCESS
    225.          lea eax,  szProcessName
    226.                 mov as.Buffer,   eax
    227.   mov as.MaximumLength, IMAGE_FILE_PATH_LEN
    228.   and as._Length,   0
    229.   invoke RtlUnicodeStringToAnsiString, addr as, addr us, FALSE
    230.   invoke ExFreePool, us.Buffer
    231.   lea eax,  szProcessName
    232.   invoke __strcmpi, eax, addr Target
    233.   .if eax
    234.       .if notAttacked
    235.                invoke AttackProcess,peProcess
    236.         .if eax == STATUS_SUCCESS
    237.                 mov notAttack,FALSE
    238.         .endif
    239.       .endif
    240.   .endif
    241.      
    242. .endif
    243.  
    244. .if fbDereference
    245.   fastcall ObfDereferenceObject, peProcess
    246. .endif
    247.  
    248. ret
    249.  
    250. NotifyRoutine endp
    251.  
    252. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    253. ;                                     DispatchControl                                              
    254. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    255.  
    256. DispatchControl proc uses esi edi pDeviceObject:PDEVICE_OBJECT, pIrp:PIRP
    257.  
    258. mov esi, pIrp
    259. assume esi:ptr _IRP
    260.  
    261. ; Initialize to failure.
    262.  
    263. mov [esi].IoStatus.Status, STATUS_UNSUCCESSFUL
    264. and [esi].IoStatus.Information, 0
    265.  
    266. IoGetCurrentIrpStackLocation esi
    267. mov edi, eax
    268. assume edi:ptr IO_STACK_LOCATION
    269. mov [esi].IoStatus.Status, STATUS_INVALID_DEVICE_REQUEST
    270. ; After IoCompleteRequest returns, the IRP pointer
    271. ; is no longer valid and cannot safely be dereferenced.
    272.  
    273. push [esi].IoStatus.Status
    274.  
    275. assume edi:nothing
    276. assume esi:nothing
    277.  
    278. fastcall IofCompleteRequest, esi, IO_NO_INCREMENT
    279.  
    280. pop eax   ; [esi].IoStatus.Status
    281. ret
    282.  
    283. DispatchControl endp
    284.  
    285. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    286. ;                                       DriverUnload                                                
    287. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    288.  
    289. DriverUnload proc pDriverObject:PDRIVER_OBJECT
    290.  
    291.         invoke PsSetCreateProcessNotifyRoutine, NotifyRoutine, TRUE
    292.                        
    293. invoke IoDeleteSymbolicLink, addr g_usSymbolicLinkName
    294.  
    295. mov eax, pDriverObject
    296. invoke IoDeleteDevice, (DRIVER_OBJECT PTR [eax]).DeviceObject
    297.  
    298. ret
    299.  
    300. DriverUnload endp
    301.  
    302. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    303. ;                              D I S C A R D A B L E   C O D E                                      
    304. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    305.  
    306. .code INIT
    307.  
    308. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    309. ;                                    GetImageFileNameOffset                                        
    310. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    311.  
    312. GetImageFileNameOffset proc uses esi ebx
    313.  
    314. ; Finds EPROCESS.ImageFileName field offset
    315.  
    316. ; W2K  EPROCESS.ImageFileName = 01FCh
    317. ; WXP  EPROCESS.ImageFileName = 0174h
    318. ; WNET  EPROCESS.ImageFileName = 0154h
    319.  
    320. ; Instead of hardcoding above offsets we just scan
    321. ; the EPROCESS structure of System process one page down.
    322. ; It\'s well-known trick.
    323.  
    324. invoke IoGetCurrentProcess
    325. mov esi, eax
    326.  
    327. xor ebx, ebx
    328. .while ebx < 1000h   ; one page more than enough.
    329.   ; Case insensitive compare.
    330.   lea eax, [esi+ebx]
    331.   invoke _strnicmp, eax, $CTA0("system"), 6
    332.   .break .if eax == 0
    333.   inc ebx
    334. .endw
    335.  
    336. .if eax == 0
    337.   ; Found.
    338.   mov eax, ebx
    339. .else
    340.   ; Not found.
    341.   xor eax, eax
    342. .endif
    343.  
    344. ret
    345.  
    346. GetImageFileNameOffset endp
    347.  
    348. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    349. ;                                       DriverEntry                                                
    350. ;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    351.  
    352. DriverEntry proc pDriverObject:PDRIVER_OBJECT, pusRegistryPath:PUNICODE_STRING
    353.  
    354. local status:NTSTATUS
    355. local pDeviceObject:PDEVICE_OBJECT
    356.  
    357. mov status, STATUS_DEVICE_CONFIGURATION_ERROR
    358.  
    359. invoke IoCreateDevice, pDriverObject, 0, addr g_usDeviceName,     FILE_DEVICE_UNKNOWN, 0, TRUE, addr pDeviceObject
    360. .if eax == STATUS_SUCCESS
    361.   invoke IoCreateSymbolicLink, addr g_usSymbolicLinkName, addr g_usDeviceName
    362.   .if eax == STATUS_SUCCESS
    363.    mov eax, pDriverObject
    364.    assume eax:ptr DRIVER_OBJECT
    365.    mov [eax].MajorFunction[IRP_MJ_CREATE*(sizeof PVOID)],   offset DispatchCreateClose
    366.    mov [eax].MajorFunction[IRP_MJ_CLOSE*(sizeof PVOID)],   offset DispatchCreateClose
    367.    mov [eax].MajorFunction[IRP_MJ_DEVICE_CONTROL*(sizeof PVOID)], offset DispatchControl
    368.    mov [eax].DriverUnload,           offset DriverUnload
    369.    assume eax:nothing
    370.    mov notAttack,FALSE
    371.    and g_fbNotifyRoutineSet, FALSE
    372.                         invoke PsSetCreateProcessNotifyRoutine, NotifyRoutine, FALSE
    373.                        
    374.    invoke GetImageFileNameOffset
    375.    mov g_dwImageFileNameOffset, eax   ; it can be not found and equal to 0, btw
    376.    mov status, STATUS_SUCCESS
    377.   .else
    378.    invoke IoDeleteDevice, pDeviceObject
    379.   .endif
    380. .endif
    381.  
    382. mov eax, status
    383. ret
    384.  
    385. DriverEntry endp      
    386.  
    387.  
    388. VOID
    389. KeInitializeApc (
    390.    IN PRKAPC Apc,
    391.    IN PRKTHREAD Thread,
    392.    IN KAPC_ENVIRONMENT Environment,
    393.    IN PKKERNEL_ROUTINE KernelRoutine,
    394.    IN PKRUNDOWN_ROUTINE RundownRoutine OPTIONAL,
    395.    IN PKNORMAL_ROUTINE NormalRoutine OPTIONAL,
    396.    IN KPROCESSOR_MODE ApcMode OPTIONAL
    397.    IN PVOID NormalContext OPTIONAL
    398.    )
    399.  
    400. ..............
    401. BOOLEAN
    402. KeInsertQueueApc (
    403.    IN PRKAPC Apc,//OurApc
    404.    IN PVOID SystemArgument1,//Ring3App-arg2
    405.    IN PVOID SystemArgument2,//Ring3App-arg3
    406.    IN KPRIORITY Increment//0
    407.    )
    408.  
    409. Ring3::    
    410. void Ring3App(ulong arg1,ulong arg2,ulong arg3);
    411.  
    412. kernel mode APC
    413. void MyApcRoutine(struct _KAPC *Apc,
    414.               PKNORMAL_ROUTINE norm_routine,
    415.               void *context,//arg1
    416.               void *SysArg1,//arg2
    417.               void *SysArg2)//arg3
    418. {
    419.    ExFreePool(Apc);
    420.    return;
    421. }
    422.  
    423.  
    424. //in kernel mode
    425. PKAPC OurApc;
    426. void SendApc(ulong addr,ulong arg1,ulong arg2,ulong arg3)
    427. {
    428.      PKTHREAD thread=KeGetCurrentThread();
    429.     OurApc=ExAllocatePool(NonPagedPool, sizeof(struct _KAPC));
    430.  
    431.    KeInitializeApc(OurApc, thread, 0,
    432.       (PKKERNEL_ROUTINE)&MyApcRoutine, 0,
    433.    (PKNORMAL_ROUTINE)addr, 1, (PVOID)arg1);
    434.    KeInsertQueueApc(OurApc, (PVOID)arg2, (PVOID)arg3, 0);
    435.    *((unsigned char *)thread+0x4a)=1   return ;
    436. }
    437. //in user mode
    438. void Ring3App(ulong arg1,ulong arg2,ulong arg3)
    439. {
    440. ....
    441. }
    442.    
    443. void SendQp(..)
    444. {
    445. ....
    446. SendBuf = BuildUpIrp(IRP_XXX_YYYY);
    447. SendBuf->BackAddr=(ULONG)Ring3App;
    448. ....
    449. ReturnBuf = SendIrp(hDevice,SendBuf,sizeof(SendBuf));
    450. ....
    451. ....