Advertisement
FlyFar

Inject DLL from kernel mode

Jun 5th, 2023 (edited)
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | Cybersecurity | 0 0
  1. How kernel mode injection works?
  2.  
  3.  
  4.  
  5. 1) Get the address of KdVersionBlock from KPCR. (__readfsdword)
  6.  
  7.  
  8.  
  9. 2) Get the address of MmLoadedUserImageList from KdVersionBlock.
  10.  
  11.  
  12.  
  13. 3) Get the base address of ntdll from MmLoadedUserImageList.
  14.  
  15.  
  16.  
  17. 4) Parse the export table of ntdll to locate LdrLoadDll.
  18.  
  19.  
  20.  
  21. 5) Find a thread to hijack. (ZwQuerySystemInformation)
  22.  
  23.  
  24.  
  25. 6) Open the target process. (PsLookupProcessByProcessId)
  26.  
  27.  
  28.  
  29. 7) Open the target thread. (PsLookupThreadByThreadId)
  30.  
  31.  
  32.  
  33. 8) Attach to target process's address space. (KeAttachProcess)
  34.  
  35.  
  36.  
  37. 8) Allocate memory in target process's address space. (ZwAllocateVirtualMemory)
  38.  
  39.  
  40.  
  41. 9) Copy the DLL name and APC routine into target process's address space. (memcpy,RtlInitUnicodeString)
  42.  
  43.  
  44.  
  45. 10) Set ApcState.UserApcPending to TRUE to force the target thread to execute the APC routine.
  46.  
  47.  
  48.  
  49. 11) Allocate an APC object from nonpaged pool. (ExAllocatePool)
  50.  
  51.  
  52.  
  53. 12) Initialize the APC and insert it to the target thread. (KeInitializeApc,KeInsertQueueApc)
  54.  
  55.  
  56.  
  57. 13) The target thread executes the APC routine in target process's address space. The APC routine calls LdrLoadDll to load the DLL.
  58.  
  59.  
  60.  
  61. 14) Wait for the APC routine to complete.
  62.  
  63.  
  64.  
  65. 15) Free the allocated memory. (ZwFreeVirtualMemory,ExFreePool)
  66.  
  67.  
  68.  
  69. 16) Detach from target process's address space. (KeDetachProcess)
  70.  
  71.  
  72.  
  73. 17) Dereference the target process and target thread. (ObDereferenceObject)
  74.  
  75.  
  76.  
  77. Usage:
  78.  
  79.  
  80.  
  81. To use the injector, run install.bat to install the driver, and then run kinject.exe from command prompt.
  82.  
  83.  
  84.  
  85. kinject [PID] [DLL name]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement