自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

網絡安全編程:內核驅動進程遍歷

安全
本文實現一個枚舉進程的函數。枚舉進程不能在用戶態(tài)下進行,需要到內核態(tài)下進行,這樣就必須使用驅動程序來完成。先用WinDbg完成一次手動的枚舉過程,再通過代碼來完成。

 內核驅動在安全方面占據重要的地位。本文實現一個枚舉進程的函數。枚舉進程不能在用戶態(tài)下進行,需要到內核態(tài)下進行,這樣就必須使用驅動程序來完成。先用WinDbg完成一次手動的枚舉過程,再通過代碼來完成。

1. 配置VMware和WinDbg進行驅動調試

使用WinDbg調試驅動程序或內核,需要雙機進行調試。所謂雙機,就是兩臺電腦。通常情況下,大部分人往往只有一臺電腦。那么,解決的方法就是安裝虛擬機,然后對虛擬機進行一些設置,也是可以通過WinDbg進行調試的。虛擬機選擇使用VMware,下面介紹如何對虛擬機進行配置。

安裝好VMware,并在VMware中安裝好操作系統(tǒng),然后對安裝好的虛擬機進行一些設置。通過此設置可以達到調試器與虛擬機的連接。單擊菜單“VM”→“Settings”命令,彈出“Virtual Machine Settings”對話框,如圖1所示。

圖1  “Virtual Machine Settings”對話框

單擊“Add”按鈕,打開“Add Hardware Wizard”(添加硬件向導)對話框,如圖2所示。

圖2  “Add Hardware Wizard”對話框1

在該對話框中選擇“Serial Port”選項,也就是串口,然后單擊“Next”按鈕,彈出“Add Hardware Wizard”對話框的第二個界面,如圖3所示。

圖3  “Add Hardware Wizard”對話框2

在該界面中選擇“Output to named pipe”單選按鈕,也就是命名管道。命名管道是Windows下進程通信的一種方法。選中該項后繼續(xù)單擊“Next”按鈕,進入下一個界面,也是設置的最后一個界面,如圖4所示。

圖4  “Add Hardware Wizard”對話框3

在這個界面中對命名管道進行設置,然后單擊“Finish”按鈕即可。至此,已經完成了一半的設置。接著,啟動虛擬機配置Windows的Boot.ini文件。Boot.ini文件原內容如下: 

  1. [boot loader]  
  2. timeout=30  
  3. default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS  
  4. [operating systems]  
  5. multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fa 
  6. stdetect /NoExecute=AlwaysOff 

將最后一行復制,然后放到最后面,并進行修改。修改后的內容如下: 

  1. [boot loader]  
  2. timeout=30  
  3. default=multi(0)disk(0)rdisk(0)partition(1)\WINDOWS  
  4. [operating systems]  
  5. multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fa  
  6. stdetect /NoExecute=AlwaysOff  
  7. multi(0)disk(0)rdisk(0)partition(1)\WINDOWS="Microsoft Windows XP Professional" /fa  
  8. stdetect /NoExecute=optin /debug /debugport=com1 /baudrate=115200 

去掉Boot.ini文件的只讀屬性,然后保存Boot.ini文件。在下次需要對驅動進行調試,或者對內核進行調試時,選擇啟動Debug模式的Windows。

這里只介紹了針對Windows XP系統(tǒng)的配置方法。關于其他版本系統(tǒng)的配置方法,請自行參考相關內容。

至此,所有的配置工作都做好了,但是使用WinDbg進行連接時,還是要有連接參數的。先在桌面上創(chuàng)建一個WinDbg的快捷方式,然后在WinDbg快捷方式上單擊右鍵,在彈出的快捷菜單中選擇“屬性”命令,彈出“屬性”對話框,將“目標”位置改為: 

  1. F:\WinDDK\7600.16385.0\Debuggers\windbg.exe -b -k com:port=\\.\pipe\com_1,baud115200,pipe 

這樣就可以用WinDbg連接虛擬機中調試狀態(tài)下的Windows XP了。

2. EPROCESS和手動遍歷進程

Windows中有一個非常大的與進程有關的結構體——EPROCESS。每個進程對應一個EPROCESS結構,但EPROCESS是一個系統(tǒng)未公開的結構體,在WDK中只能找到說明,而找不到其結構體的具體定義,因此需要通過WinDbg來查看。這次使用WinDbg和VMware進行調試。按照前面的方法,使WinDbg和VMware可以連接。當WinDbg出現調試界面時,在其命令處輸入dt _eprocess命令來查看該結構體,如圖5所示。

圖5  WinDbg顯示的部分EPROCESS結構體

從圖中可以看出,EPROCESS結構體顯示出非常多的內容,從WinDbg調試界面只能看到部分成員變量,而且偏移已經到了0x258,非常多??匆幌耊inDbg的全部內容。 

  1. kd> dt _eprocess  
  2. nt!_EPROCESS  
  3.  +0x000 Pcb : _KPROCESS // 進程控制塊  
  4.  +0x06c ProcessLock : _EX_PUSH_LOCK  
  5.  +0x070 CreateTime : _LARGE_INTEGER  
  6.  +0x078 ExitTime : _LARGE_INTEGER  
  7.  +0x080 RundownProtect : _EX_RUNDOWN_REF  
  8.  +0x084 UniqueProcessId : Ptr32 Void // 進程 ID  
  9.  +0x088 ActiveProcessLinks : _LIST_ENTRY // 活動進程鏈表  
  10.  +0x090 QuotaUsage : [3] Uint4B 
  11.  +0x09c QuotaPeak : [3] Uint4B  
  12.  +0x0a8 CommitCharge : Uint4B  
  13.  +0x0ac PeakVirtualSize : Uint4B  
  14.  +0x0b0 VirtualSize : Uint4B  
  15.  +0x0b4 SessionProcessLinks : _LIST_ENTRY  
  16.  +0x0bc DebugPort : Ptr32 Void  
  17.  +0x0c0 ExceptionPort : Ptr32 Void  
  18.  +0x0c4 ObjectTable : Ptr32 _HANDLE_TABLE  
  19.  +0x0c8 Token : _EX_FAST_REF  
  20.  +0x0cc WorkingSetLock : _FAST_MUTEX  
  21.  +0x0ec WorkingSetPage : Uint4B  
  22.  +0x0f0 AddressCreationLock : _FAST_MUTEX  
  23.  +0x110 HyperSpaceLock : Uint4B  
  24.  +0x114 ForkInProgress : Ptr32 _ETHREAD  
  25.  +0x118 HardwareTrigger : Uint4B  
  26.  +0x11c VadRoot : Ptr32 Void  
  27.  +0x120 VadHint : Ptr32 Void  
  28.  +0x124 CloneRoot : Ptr32 Void  
  29.  +0x128 NumberOfPrivatePages : Uint4B  
  30.  +0x12c NumberOfLockedPages : Uint4B  
  31.  +0x130 Win32Process : Ptr32 Void  
  32.  +0x134 Job : Ptr32 _EJOB  
  33.  +0x138 SectionObject : Ptr32 Void  
  34.  +0x13c SectionBaseAddress : Ptr32 Void  
  35.  +0x140 QuotaBlock : Ptr32 _EPROCESS_QUOTA_BLOCK  
  36.  +0x144 WorkingSetWatch : Ptr32 _PAGEFAULT_HISTORY  
  37.  +0x148 Win32WindowStation : Ptr32 Void 
  38.  +0x14c InheritedFromUniqueProcessId : Ptr32 Void  
  39.  +0x150 LdtInformation : Ptr32 Void  
  40.  +0x154 VadFreeHint : Ptr32 Void  
  41.  +0x158 VdmObjects : Ptr32 Void  
  42.  +0x15c DeviceMap : Ptr32 Void  
  43.  +0x160 PhysicalVadList : _LIST_ENTRY  
  44.  +0x168 PageDirectoryPte : _HARDWARE_PTE  
  45.  +0x168 Filler : Uint8B  
  46.  +0x170 Session : Ptr32 Void  
  47.  +0x174 ImageFileName : [16] UChar // 進程名  
  48.  +0x184 JobLinks : _LIST_ENTRY  
  49.  +0x18c LockedPagesList : Ptr32 Void  
  50.  +0x190 ThreadListHead : _LIST_ENTRY  
  51.  +0x198 SecurityPort : Ptr32 Void  
  52.  +0x19c PaeTop : Ptr32 Void  
  53.  +0x1a0 ActiveThreads : Uint4B  
  54.  +0x1a4 GrantedAccess : Uint4B  
  55.  +0x1a8 DefaultHardErrorProcessing : Uint4B  
  56.  +0x1ac LastThreadExitStatus : Int4B  
  57.  +0x1b0 Peb : Ptr32 _PEB // 進程環(huán)境塊 
  58.  +0x1b4 PrefetchTrace : _EX_FAST_REF  
  59.  +0x1b8 ReadOperationCount : _LARGE_INTEGER  
  60.  +0x1c0 WriteOperationCount : _LARGE_INTEGER  
  61.  +0x1c8 OtherOperationCount : _LARGE_INTEGER  
  62.  +0x1d0 ReadTransferCount : _LARGE_INTEGER  
  63.  +0x1d8 WriteTransferCount : _LARGE_INTEGER  
  64.  +0x1e0 OtherTransferCount : _LARGE_INTEGER  
  65.  +0x1e8 CommitChargeLimit : Uint4B  
  66.  +0x1ec CommitChargePeak : Uint4B  
  67.  +0x1f0 AweInfo : Ptr32 Void  
  68.  +0x1f4 SeAuditProcessCreationInfo : _SE_AUDIT_PROCESS_CREATION_INFO  
  69.  +0x1f8 Vm : _MMSUPPORT  
  70.  +0x238 LastFaultCount : Uint4B  
  71.  +0x23c ModifiedPageCount : Uint4B  
  72.  +0x240 NumberOfVads : Uint4B  
  73.  +0x244 JobStatus : Uint4B  
  74.  +0x248 Flags : Uint4B  
  75.  +0x248 CreateReported : Pos 0, 1 Bit  
  76.  +0x248 NoDebugInherit : Pos 1, 1 Bit  
  77.  +0x248 ProcessExiting : Pos 2, 1 Bit  
  78.  +0x248 ProcessDelete : Pos 3, 1 Bit  
  79.  +0x248 Wow64SplitPages : Pos 4, 1 Bit  
  80.  +0x248 VmDeleted : Pos 5, 1 Bit  
  81.  +0x248 OutswapEnabled : Pos 6, 1 Bit  
  82.  +0x248 Outswapped : Pos 7, 1 Bit  
  83.  +0x248 ForkFailed : Pos 8, 1 Bit  
  84.  +0x248 HasPhysicalVad : Pos 9, 1 Bit  
  85.  +0x248 AddressSpaceInitialized : Pos 10, 2 Bits  
  86.  +0x248 SetTimerResolution : Pos 12, 1 Bit  
  87.  +0x248 BreakOnTermination : Pos 13, 1 Bit  
  88.  +0x248 SessionCreationUnderway : Pos 14, 1 Bit  
  89.  +0x248 WriteWatch : Pos 15, 1 Bit  
  90.  +0x248 ProcessInSession : Pos 16, 1 Bit  
  91.  +0x248 OverrideAddressSpace : Pos 17, 1 Bit  
  92.  +0x248 HasAddressSpace : Pos 18, 1 Bit  
  93.  +0x248 LaunchPrefetched : Pos 19, 1 Bit  
  94.  +0x248 InjectInpageErrors : Pos 20, 1 Bit 
  95.  +0x248 VmTopDown : Pos 21, 1 Bit  
  96.  +0x248 Unused3 : Pos 22, 1 Bit  
  97.  +0x248 Unused4 : Pos 23, 1 Bit  
  98.  +0x248 VdmAllowed : Pos 24, 1 Bit  
  99.  +0x248 Unused : Pos 25, 5 Bits  
  100.  +0x248 Unused1 : Pos 30, 1 Bit  
  101.  +0x248 Unused2 : Pos 31, 1 Bit  
  102.  +0x24c ExitStatus : Int4B  
  103.  +0x250 NextPageColor : Uint2B  
  104.  +0x252 SubSystemMinorVersion : UChar  
  105.  +0x253 SubSystemMajorVersion : UChar  
  106.  +0x252 SubSystemVersion : Uint2B  
  107.  +0x254 PriorityClass : UChar  
  108.  +0x255 WorkingSetAcquiredUnsafe : UChar  
  109.  +0x258 Cookie : Uint4B 

上面就是EPROCESS結構體的全部。對于遍歷進程列表來說,有用的只有幾個內容,首先是偏移0x84處的進程ID,然后是偏移0x88處的進程鏈表,最后一個是偏移0x174的進程名。下面手動進行一次遍歷。

在WinDbg的命令輸入提示處輸入! Process 0 0命令,得到進程的列表,如圖6所示。

圖6  進程信息

PROCESS后面給出的值就是當前進程中EPROCESS的地址,選擇explorer.exe進程給出的地址0xff364708來解析EPROCESS。輸入命令dt _eprocess ff364708,輸出如下: 

  1. kd> dt _eprocess ff364708  
  2. nt!_EPROCESS  
  3.  +0x000 Pcb : _KPROCESS  
  4.  +0x06c ProcessLock : _EX_PUSH_LOCK  
  5.  +0x070 CreateTime : _LARGE_INTEGER 0x1cb6af5`91d56cea  
  6.  +0x078 ExitTime : _LARGE_INTEGER 0x0  
  7.  +0x080 RundownProtect : _EX_RUNDOWN_REF  
  8.  +0x084 UniqueProcessId : 0x00000600  
  9.  +0x088 ActiveProcessLinks : _LIST_ENTRY [ 0xff2b44b0 - 0xff3640a8 ]  
  10.  <部分省略>  
  11.  +0x174 ImageFileName : [16] "explorer.exe"  
  12.  <部分省略>  
  13.  +0x1b0 Peb : 0x7ffde000 _PEB  
  14.  <后面省略> 

可以看到,按照EPROCESS結構體解析ff364708地址,輸出了需要的內容。接著,通過ActiveProcessLinks獲取下一個進程的信息。輸入命令dd ff364708 + 0x88,輸出如下: 

  1. kd> dd ff364708 + 0x88  
  2. ff364790 ff2b44b0 ff3640a8 00002940 00021944  
  3. ff3647a0 00000a92 00003940 00024cb4 00000bf8  
  4. ff3647b0 00000a92 05e04000 0563a000 ff2b44dc  
  5. ff3647c0 ff3640d4 00000000 e15b6eb8 e1ce2640  
  6. ff3647d0 e166f389 00000001 f39a5440 00000000  
  7. ff3647e0 00040001 00000000 ff3647e8 ff3647e8  
  8. ff3647f0 0000003d 000059ca 00000001 f39a5440  
  9. ff364800 00000000 00040001 00000000 ff36480c 

ff364790地址處保存了下一個EPROCESS結構體ActiveProcessLinks的地址。要得到下一個EPROCESS的地址,必須減去0x88才行。輸入命令dt _eprocess (ff2b44b0 – 0x88),輸出如下: 

  1. kd> dt _eprocess (ff2b44b0 - 0x88)  
  2. nt!_EPROCESS  
  3.  +0x000 Pcb : _KPROCESS  
  4.  +0x06c ProcessLock : _EX_PUSH_LOCK  
  5.  +0x070 CreateTime : _LARGE_INTEGER 0x1cb6af5`95026ecc  
  6.  +0x078 ExitTime : _LARGE_INTEGER 0x0  
  7.  +0x080 RundownProtect : _EX_RUNDOWN_REF  
  8.  +0x084 UniqueProcessId : 0x000006b8  
  9.  +0x088 ActiveProcessLinks : _LIST_ENTRY [ 0xff2b7580 - 0xff364790 ]  
  10.  <后面省略>  
  11.  +0x174 ImageFileName : [16] "VMwareTray.exe"  
  12.  <后面省略> 

將輸出結果和圖6中的結果對比,explorer.exe的下一個進程為VMwareTray.exe??梢姳闅v方法是正確的。

3. 編程實現進程遍歷

上面介紹的手動遍歷過程就是指導用戶如何編寫代碼的,只要能夠掌握上面的手動遍歷過程,那么代碼的編寫也就不是問題了。下面直接看代碼: 

  1. NTSTATUS DriverEntry(  
  2.   PDRIVER_OBJECT pDriverObject,  
  3.   PUNICODE_STRING pRegistryPath)  
  4.  
  5.   PEPROCESS pEprocess = NULL 
  6.   PEPROCESS pFirstEprocess = NULL 
  7.   ULONG ulProcessName = 0 
  8.   ULONG ulProcessId = 0 
  9.   pDriverObject->DriverUnloadDriverUnload = DriverUnload;  
  10.   pEprocess = PsGetCurrentProcess();  
  11.   if ( pEprocess == 0 )  
  12.   {  
  13.     KdPrint(("PsGetcurrentProcess Error ! \r\n"));  
  14.     return STATUS_SUCCESS;  
  15.   }  
  16.   pFirstEprocess = pEprocess 
  17.   while ( pEprocess != NULL )  
  18.   {  
  19.     ulProcessName = (ULONG)pEprocess + 0x174;  
  20.     ulProcessId = *(ULONG *)((ULONG)pEprocess + 0x84);  
  21.     KdPrint(("ProcessName = %s, ProcessId = %d \r\n", ulProcessName, ulProcessId));  
  22.     pEprocess = (ULONG)( *(ULONG *)((ULONG)pEprocess + 0x88) - 0x88);  
  23.     if ( pEprocess == pFirstEprocess || (*(LONG *)((LONG)pEprocess + 0x84)) < 0 )  
  24.     {  
  25.       break ;  
  26.     }  
  27.   }  
  28.   return STATUS_SUCCESS;  
  29. }  

代碼中用到了一個函數,就是PsGetCurrentProcess()。這個函數是用來獲取當前進程的EPROCESS指針的,其定義如下: 

  1. PEPROCESS PsGetCurrentProcess(VOID); 

通過PsGetCurrentProcess()函數獲得的是system進程的EPROCESS,大多數內核模式系統(tǒng)線程都在system進程中。除了這個函數沒有接觸過以外,剩下的部分就是對EPROCESS結構體的操作,這里不做過多的介紹。實現進程內DLL文件的隱藏方法是將指定DLL在DLL鏈表中“脫鏈”。為了隱藏進程,同樣可以將指定進程的EPROCESS結構體在進程鏈表中“脫鏈”,以達到隱藏的目的。 

 

責任編輯:龐桂玉 來源: 計算機與網絡安全
相關推薦

2021-02-23 10:20:07

網絡安全進程代碼

2021-02-21 18:19:43

網絡安全網絡安全編程創(chuàng)建進程

2021-03-01 11:38:15

網絡安全進程代碼

2021-03-19 10:23:45

網絡安全內核文件

2021-03-03 12:20:42

網絡安全DLL編程

2021-01-22 10:58:16

網絡安全進程間碼如

2021-01-26 13:45:03

網絡安全Winsock編程

2021-03-05 13:46:56

網絡安全遠程線程

2016-10-10 00:18:27

2021-06-18 09:55:09

網絡安全目錄監(jiān)控

2021-06-15 11:16:24

網絡安全U盤軟件

2021-04-19 10:26:41

網絡安全PE文件

2021-02-04 10:50:11

網絡安全非阻塞模Winsock編程

2021-05-12 14:57:13

網絡安全密碼代碼

2021-05-24 11:55:55

網絡安全Windows鉤子函數

2021-03-01 11:20:13

網絡安全多線程代碼

2011-03-17 13:32:45

2021-01-18 10:35:18

網絡安全Windows代碼

2017-09-22 16:16:49

2021-04-26 10:32:38

網絡安全PE編程工具
點贊
收藏

51CTO技術棧公眾號