找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4266|回复: 0

[开源] 简洁有力的纯win32的SSDT List

[复制链接]

96

主题

158

回帖

4

精华

核心会员

积分
6513
发表于 2012-5-18 16:10:51 | 显示全部楼层 |阅读模式
本帖最后由 diddom 于 2012-5-23 01:45 编辑

很棒的入门款式

纯win32的唷,有ListView且双色item

Hook地方还用红色字标示

ListS_SSDT_win32.rar (22.33 KB, 下载次数: 6)

  1. // SystemTool.cpp : Defines the entry point for the application.
  2. //

  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include <stdio.h>
  6. #include <windows.h>   
  7. #include <winnt.h>   
  8. #include <WindowsX.h>   
  9. #include <commctrl.h>   

  10. #pragma comment(lib,"comctl32")

  11. typedef ULONG NTSTATUS;
  12. #define MAX_LOADSTRING 100
  13. #define SystemModuleInformation 11   
  14. #define ibaseDD *(PDWORD)&ibase   
  15.       
  16. HINSTANCE g_hInst;   
  17. HWND hWinMain;
  18. HWND hList;
  19. #define ID_LISTVIEW 1001
  20.   
  21. #define RVATOVA(base,offset) ((PVOID)((DWORD)(base)+(DWORD)(offset)))   
  22. #define ibaseDD *(PDWORD)&ibase   
  23. #define STATUS_INFO_LENGTH_MISMATCH ((NTSTATUS)0xC0000004L)   
  24. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)   
  25.       
  26. typedef struct
  27. {   
  28.         WORD    offset:12;   
  29.         WORD    type:4;   
  30. } IMAGE_FIXUP_ENTRY, *PIMAGE_FIXUP_ENTRY;   
  31.       
  32.       
  33. typedef
  34. ULONG
  35. (WINAPI *ZWQUERYSYSTEMINFORMATION)
  36. (   
  37.         DWORD    SystemInformationClass,   
  38.         PVOID    SystemInformation,   
  39.         ULONG    SystemInformationLength,   
  40.         PULONG   ReturnLength
  41. );   

  42. ZWQUERYSYSTEMINFORMATION ZwQuerySystemInformation = NULL;   
  43.       
  44. typedef enum _SYSDBG_COMMAND {   
  45.         SysDbgReadVirtualMemory = 8,   
  46.         SysDbgWriteVirtualMemory = 9,   
  47. } SYSDBG_COMMAND, *PSYSDBG_COMMAND;   
  48.       
  49. typedef struct _MEMORY_CHUNKS
  50. {   
  51.         ULONG Address;
  52.         PVOID Data;
  53.         ULONG Length;
  54. } MEMORY_CHUNKS, *PMEMORY_CHUNKS;   
  55.       
  56. typedef
  57. NTSTATUS
  58. (NTAPI * ZWSYSTEMDEBUGCONTROL)
  59. (
  60.         SYSDBG_COMMAND ControlCode,   
  61.         PVOID InputBuffer,   
  62.         ULONG InputBufferLength,   
  63.         PVOID OutputBuffer,   
  64.         ULONG OutputBufferLength,   
  65.         PULONG ReturnLength
  66. );   
  67.       
  68. ZWSYSTEMDEBUGCONTROL ZwSystemDebugControl = NULL;   
  69.       
  70. typedef struct _SYSTEM_MODULE_INFORMATION { //Information Class 11   
  71.         ULONG    Reserved[2];   
  72.         PVOID    Base;   
  73.         ULONG    Size;   
  74.         ULONG    Flags;   
  75.         USHORT   Index;   
  76.         USHORT   Unknown;   
  77.         USHORT   LoadCount;   
  78.         USHORT   ModuleNameOffset;   
  79.         CHAR     ImageName[256];   
  80. } SYSTEM_MODULE_INFORMATION,*PSYSTEM_MODULE_INFORMATION;   
  81.       
  82.       
  83. typedef struct   
  84. {   
  85.         CHAR fname[100];   
  86.         ULONG address1;   
  87.         ULONG address2;   
  88. } SSDT_LIST_ENTRY;   
  89.       
  90. SSDT_LIST_ENTRY ssdt_list[500];

  91. //ssdt_list = new SSDT_LIST_ENTRY[500];

  92. // Global Variables:
  93. HINSTANCE hInst;                                                                // current instance
  94. TCHAR szTitle[MAX_LOADSTRING];                                        // The title bar text
  95. TCHAR szWindowClass[MAX_LOADSTRING];                        // The title bar text

  96. // Foward declarations of functions included in this code module:
  97. ATOM                                MyRegisterClass(HINSTANCE hInstance);
  98. BOOL                                InitInstance(HINSTANCE, int);
  99. LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
  100. LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);

  101. BOOL LocateNtdllEntry();
  102. BOOL DebugPrivilege(TCHAR *PName,BOOL bEnable);
  103. void InitListView();
  104. void GetSSDT();
  105. void FindExport();

  106. int APIENTRY WinMain(HINSTANCE hInstance,
  107.                      HINSTANCE hPrevInstance,
  108.                      LPSTR     lpCmdLine,
  109.                      int       nCmdShow)
  110. {
  111.         // TODO: Place code here.
  112.         MSG msg;
  113.         HACCEL hAccelTable;

  114.         // Initialize global strings
  115.         LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
  116.         LoadString(hInstance, IDC_SYSTEMTOOL, szWindowClass, MAX_LOADSTRING);
  117.         MyRegisterClass(hInstance);

  118.         // Perform application initialization:
  119.         if (!InitInstance (hInstance, nCmdShow))
  120.         {
  121.                 return FALSE;
  122.         }

  123.         hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SYSTEMTOOL);

  124.         // Main message loop:
  125.         while (GetMessage(&msg, NULL, 0, 0))
  126.         {
  127.                 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  128.                 {
  129.                         TranslateMessage(&msg);
  130.                         DispatchMessage(&msg);
  131.                 }
  132.         }

  133.         //delete [] ssdt_list;
  134.         return msg.wParam;
  135. }



  136. //
  137. //  FUNCTION: MyRegisterClass()
  138. //
  139. //  PURPOSE: Registers the window class.
  140. //
  141. //  COMMENTS:
  142. //
  143. //    This function and its usage is only necessary if you want this code
  144. //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
  145. //    function that was added to Windows 95. It is important to call this function
  146. //    so that the application will get 'well formed' small icons associated
  147. //    with it.
  148. //
  149. ATOM MyRegisterClass(HINSTANCE hInstance)
  150. {
  151.         WNDCLASSEX wcex;

  152.         wcex.cbSize = sizeof(WNDCLASSEX);

  153.         wcex.style                        = CS_HREDRAW | CS_VREDRAW;
  154.         wcex.lpfnWndProc        = (WNDPROC)WndProc;
  155.         wcex.cbClsExtra                = 0;
  156.         wcex.cbWndExtra                = 0;
  157.         wcex.hInstance                = hInstance;
  158.         wcex.hIcon                        = LoadIcon(hInstance, (LPCTSTR)IDI_SYSTEMTOOL);
  159.         wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
  160.         wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
  161.         wcex.lpszMenuName        = (LPCSTR)IDC_SYSTEMTOOL;
  162.         wcex.lpszClassName        = szWindowClass;
  163.         wcex.hIconSm                = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

  164.         return RegisterClassEx(&wcex);
  165. }

  166. //
  167. //   FUNCTION: InitInstance(HANDLE, int)
  168. //
  169. //   PURPOSE: Saves instance handle and creates main window
  170. //
  171. //   COMMENTS:
  172. //
  173. //        In this function, we save the instance handle in a global variable and
  174. //        create and display the main program window.
  175. //
  176. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  177. {
  178.         HWND hWnd;

  179.         hInst = hInstance; // Store instance handle in our global variable

  180.         hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
  181.                                                 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
  182.                                                 NULL, NULL, hInstance, NULL);

  183.         if (!hWnd)
  184.         {
  185.                 return FALSE;
  186.         }

  187.         hWinMain = hWnd;

  188.         ShowWindow(hWnd, nCmdShow);
  189.         UpdateWindow(hWnd);
  190.         return TRUE;
  191. }

  192. //
  193. //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
  194. //
  195. //  PURPOSE:  Processes messages for the main window.
  196. //
  197. //  WM_COMMAND        - process the application menu
  198. //  WM_PAINT        - Paint the main window
  199. //  WM_DESTROY        - post a quit message and return
  200. //
  201. //
  202. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  203. {
  204.         int wmId, wmEvent;
  205.         PAINTSTRUCT ps;
  206.         HDC hdc;
  207.         TCHAR szHello[MAX_LOADSTRING];
  208.         LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

  209.         switch (message)
  210.         {
  211.                 case WM_NOTIFY:   
  212.             switch (LOWORD(wParam))   
  213.             {   
  214.             case ID_LISTVIEW:
  215.                                 //LPNMHDR pnm = (LPNMHDR)lParam;   
  216.                                 //if(pnm->hwndFrom != m_hWnd)   
  217.                                         //SendMessage(pnm->hwndFrom,msg,wParam,lParam);

  218.                                 //LPNMHDR pnm = (LPNMHDR) lParam;   
  219.                                 //if(pnm->code == NM_CUSTOMDRAW)
  220.                                 if(((LPNMHDR)lParam)->code == NM_CUSTOMDRAW)
  221.                 {                    
  222.                                         //LPNMLVCUSTOMDRAW lpLVCD = (LPNMLVCUSTOMDRAW) lParam;
  223.                                         //switch(lpLVCD->nmcd.dwDrawStage)
  224.                                         switch(((LPNMLVCUSTOMDRAW)lParam)->nmcd.dwDrawStage)
  225.                     {   
  226.                     case CDDS_PREPAINT:   
  227.                         return CDRF_NOTIFYITEMDRAW;

  228.                     case CDDS_ITEMPREPAINT:   
  229.                         {   //設置不同顏色
  230.                             LPNMLVCUSTOMDRAW customDraw = (LPNMLVCUSTOMDRAW) lParam;
  231.                             customDraw->clrTextBk=customDraw->nmcd.dwItemSpec%2?RGB(250, 250, 250):RGB(0xFF, 0xFF, 0xFF);   
  232.                                                         if (ssdt_list[customDraw->nmcd.dwItemSpec].address1 != ssdt_list[customDraw->nmcd.dwItemSpec].address2) customDraw->clrText = RGB(255, 0, 0) ;   
  233.                             else customDraw->clrText = RGB(0, 0, 0) ;   
  234.                             return CDRF_NEWFONT;   
  235.                         }   
  236.                         break;   
  237.                     default:   
  238.                         return CDRF_DODEFAULT;   
  239.                     }   
  240.                 }

  241.                                 if(((LPNMHDR)lParam)->code == WM_ERASEBKGND)   
  242.                 {
  243.                                         int ii;
  244.                                         ii = 0;
  245.                                 }

  246.                 break;

  247.                                

  248.             }   
  249.             break;   

  250.                 case WM_COMMAND:
  251.                         wmId    = LOWORD(wParam);
  252.                         wmEvent = HIWORD(wParam);
  253.                         // Parse the menu selections:
  254.                         switch (wmId)
  255.                         {
  256.                                 case IDM_ABOUT:
  257.                                    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
  258.                                    break;
  259.                                 case IDM_EXIT:
  260.                                    DestroyWindow(hWnd);
  261.                                    break;
  262.                                 default:
  263.                                    return DefWindowProc(hWnd, message, wParam, lParam);
  264.                         }
  265.                         break;
  266.                
  267.                 case WM_ERASEBKGND:
  268.                         //return TRUE;
  269.                         break;
  270.                
  271.                 case WM_SIZE:
  272.             MoveWindow(hList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
  273.                         break;

  274.                 case WM_CREATE:   
  275.             hWinMain = hWnd;   
  276.             InitListView();   
  277.             LocateNtdllEntry();
  278.             FindExport();
  279.             DebugPrivilege(SE_DEBUG_NAME,TRUE);   
  280.             GetSSDT();   
  281.             return 0;   
  282.                         break;

  283.                 case WM_PAINT:
  284.                         hdc = BeginPaint(hWnd, &ps);
  285.                         // TODO: Add any drawing code here...
  286.                         RECT rt;
  287.                         GetClientRect(hWnd, &rt);
  288.                         DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
  289.                         EndPaint(hWnd, &ps);
  290.                         break;

  291.                 case WM_DESTROY:
  292.                         PostQuitMessage(0);
  293.                         break;

  294.                 default:
  295.                         return DefWindowProc(hWnd, message, wParam, lParam);
  296.    }
  297.    return 0;
  298. }

  299. // Mesage handler for about box.
  300. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  301. {
  302.         switch (message)
  303.         {
  304.                 case WM_INITDIALOG:
  305.                                 return TRUE;

  306.                 case WM_COMMAND:
  307.                         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  308.                         {
  309.                                 EndDialog(hDlg, LOWORD(wParam));
  310.                                 return TRUE;
  311.                         }
  312.                         break;
  313.         }
  314.     return FALSE;
  315. }


  316. //////////////////////////////////////////////////////////////////////////
  317. //
  318. //  
  319. BOOL LocateNtdllEntry()   
  320. {   
  321.     HMODULE ntdll_dll   = NULL;   
  322.    
  323.     if (!(ntdll_dll = GetModuleHandle("ntdll.dll"))) return FALSE;

  324.     if (!( ZwQuerySystemInformation = (ZWQUERYSYSTEMINFORMATION)GetProcAddress(ntdll_dll, "ZwQuerySystemInformation" )))   
  325.         return FALSE;

  326.     if (!( ZwSystemDebugControl = (ZWSYSTEMDEBUGCONTROL)GetProcAddress(ntdll_dll, "ZwSystemDebugControl" )))   
  327.         return FALSE;   
  328.       
  329.     return TRUE;   
  330. }

  331.    
  332. //////////////////////////////////////////////////////////////////////////
  333. //
  334. //
  335. BOOL DebugPrivilege(TCHAR *PName,BOOL bEnable)   
  336. {   
  337.     BOOL              fOk = FALSE;   
  338.     HANDLE            hToken;   
  339.     TOKEN_PRIVILEGES  tp;   
  340.    
  341.     if(OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES,&hToken))   
  342.     {         
  343.         tp.PrivilegeCount           = 1;   
  344.         tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;   
  345.         LookupPrivilegeValue(NULL,PName,&tp.Privileges[0].Luid);   
  346.         AdjustTokenPrivileges(hToken,FALSE,&tp,sizeof(TOKEN_PRIVILEGES),NULL,NULL);   
  347.         fOk=(GetLastError() == ERROR_SUCCESS);         
  348.         CloseHandle(hToken);   
  349.     }   
  350.     return fOk;   
  351. }   


  352. //////////////////////////////////////////////////////////////////////////
  353. //
  354. //
  355. DWORD GetHeaders(PCHAR ibase,   
  356.                  PIMAGE_FILE_HEADER *pfh,   
  357.                  PIMAGE_OPTIONAL_HEADER *poh,   
  358.                  PIMAGE_SECTION_HEADER *psh)   
  359.    
  360. {   
  361.     PIMAGE_DOS_HEADER mzhead = (PIMAGE_DOS_HEADER)ibase;   
  362.    
  363.     if ((mzhead->e_magic != IMAGE_DOS_SIGNATURE) || (ibaseDD[mzhead->e_lfanew] != IMAGE_NT_SIGNATURE))   
  364.         return FALSE;   
  365.    
  366.     *pfh = (PIMAGE_FILE_HEADER)&ibase[mzhead->e_lfanew];

  367.     if (((PIMAGE_NT_HEADERS)*pfh)->Signature != IMAGE_NT_SIGNATURE)   
  368.         return FALSE;

  369.     *pfh = (PIMAGE_FILE_HEADER)((PBYTE)*pfh + sizeof(IMAGE_NT_SIGNATURE));   
  370.    
  371.     *poh = (PIMAGE_OPTIONAL_HEADER)((PBYTE)*pfh + sizeof(IMAGE_FILE_HEADER));

  372.     if ((*poh)->Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC)   
  373.         return FALSE;
  374.    
  375.     *psh = (PIMAGE_SECTION_HEADER)((PBYTE)*poh + sizeof(IMAGE_OPTIONAL_HEADER));   
  376.        
  377.         return TRUE;

  378. }   
  379.    
  380.    
  381. //////////////////////////////////////////////////////////////////////////
  382. //
  383. //
  384. void FindExport()
  385. {   
  386.     PIMAGE_FILE_HEADER                        pfh;   
  387.     PIMAGE_OPTIONAL_HEADER                poh;   
  388.     PIMAGE_SECTION_HEADER                psh;   
  389.     PIMAGE_EXPORT_DIRECTORY                ped;

  390.     DWORD*        arrayOfFunctionNames;   
  391.     DWORD*        arrayOfFunctionAddresses;   
  392.     WORD*        arrayOfFunctionOrdinals;   
  393.     DWORD        functionOrdinal;
  394.         DWORD        functionAddress;   

  395.     HMODULE hNtdll = GetModuleHandle(TEXT("ntdll.dll"));

  396.     GetHeaders((PCHAR)hNtdll, &pfh, &poh, &psh);

  397.     if (poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress)   
  398.     {   
  399.         ped = (PIMAGE_EXPORT_DIRECTORY)(poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress + (BYTE*)hNtdll);   
  400.         
  401.                 arrayOfFunctionNames = (DWORD*)(ped->AddressOfNames + (BYTE*)hNtdll);

  402.         arrayOfFunctionAddresses = (DWORD*)((BYTE*)hNtdll + ped->AddressOfFunctions);

  403.         arrayOfFunctionNames = (DWORD*)((BYTE*)hNtdll + ped->AddressOfNames);

  404.         arrayOfFunctionOrdinals = (WORD*)((BYTE*)hNtdll + ped->AddressOfNameOrdinals);   
  405.                   
  406.         for (int i=0; i < ped->NumberOfNames; i++)   
  407.         {   
  408.             char* fun_name = (char*)((BYTE*)hNtdll + arrayOfFunctionNames[i]);
  409.                        
  410.             functionOrdinal = arrayOfFunctionOrdinals[i] + ped->Base - 1;
  411.                        
  412.             functionAddress = (DWORD)((BYTE*)hNtdll + arrayOfFunctionAddresses[functionOrdinal]);
  413.                        
  414.             if (fun_name[0] == 'N' && fun_name[1] == 't')   
  415.             {   
  416.                 WORD number = *((WORD*)(functionAddress + 1));
  417.                                
  418.                 if (number>ped->NumberOfNames) continue;
  419.                                
  420.                 lstrcpy(ssdt_list[number].fname, fun_name);   
  421.             }   
  422.         }   
  423.     }   
  424. }   


  425. //////////////////////////////////////////////////////////////////////////
  426. //
  427. //
  428. DWORD FindKiServiceTable(HMODULE hModule, DWORD dwKSDT)   
  429. {   
  430.     PIMAGE_FILE_HEADER                        pfh;   
  431.     PIMAGE_OPTIONAL_HEADER                poh;   
  432.     PIMAGE_SECTION_HEADER                psh;   
  433.     PIMAGE_BASE_RELOCATION                pbr;   
  434.     PIMAGE_FIXUP_ENTRY                        pfe;   
  435.       
  436.     DWORD    dwFixups = 0, i, dwPointerRva, dwPointsToRva, dwKiServiceTable;   
  437.     BOOL     bFirstChunk;   
  438.       
  439.     GetHeaders((PCHAR)hModule, &pfh, &poh, &psh);   
  440.       
  441.     // loop thru relocs to speed up the search   
  442.     if ((poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress) &&   
  443.         (!((pfh->Characteristics)&IMAGE_FILE_RELOCS_STRIPPED)))
  444.         {   
  445.            
  446.         pbr = (PIMAGE_BASE_RELOCATION) RVATOVA(poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress, hModule);   
  447.            
  448.         bFirstChunk = TRUE;
  449.                
  450.         // 1st IMAGE_BASE_RELOCATION.VirtualAddress of ntoskrnl is 0   
  451.         while (bFirstChunk || pbr->VirtualAddress)
  452.                 {   
  453.             bFirstChunk = FALSE;   
  454.                
  455.             pfe=(PIMAGE_FIXUP_ENTRY)((DWORD)pbr+sizeof(IMAGE_BASE_RELOCATION));   
  456.                
  457.             for (i=0; i < (pbr->SizeOfBlock - sizeof(IMAGE_BASE_RELOCATION)) >> 1; i++, pfe++)
  458.                         {   
  459.                 if (pfe->type == IMAGE_REL_BASED_HIGHLOW)
  460.                                 {   
  461.                     dwFixups++;

  462.                     dwPointerRva = pbr->VirtualAddress + pfe->offset;

  463.                     // DONT_RESOLVE_DLL_REFERENCES flag means relocs aren't fixed   
  464.                     dwPointsToRva = *(PDWORD)((DWORD)hModule+dwPointerRva)-(DWORD)poh->ImageBase;   
  465.                        
  466.                     // does this reloc point to KeServiceDescriptorTable.Base?   
  467.                     if (dwPointsToRva == dwKSDT)
  468.                                         {   
  469.                         // check for mov [mem32],imm32. we are trying to find   
  470.                         // "mov ds:_KeServiceDescriptorTable.Base, offset _KiServiceTable"   
  471.                         // from the KiInitSystem.   
  472.                         if (*(PWORD)((DWORD)hModule + dwPointerRva-2) == 0x05c7)
  473.                                                 {   
  474.                             // should check for a reloc presence on KiServiceTable here   
  475.                             // but forget it   
  476.                             dwKiServiceTable = *(PDWORD)((DWORD)hModule + dwPointerRva + 4) - poh->ImageBase;   
  477.                             return dwKiServiceTable;   
  478.                         }   
  479.                     }   
  480.                        
  481.                 }   
  482.                         // should never get here   
  483.             }

  484.             *(PDWORD)&pbr += pbr->SizeOfBlock;   
  485.         }   
  486.     }      
  487.       
  488.     return 0;   
  489. }   
  490.    
  491. DWORD    dwKSDT;              // rva of KeServiceDescriptorTable   
  492. DWORD    dwKiServiceTable;    // rva of KiServiceTable   
  493. DWORD    dwKernelBase, dwServices = 0;   

  494. //////////////////////////////////////////////////////////////////////////
  495. //
  496. //  
  497. void GetSSDT()   
  498. {        
  499.     HMODULE                                        hKernel;   
  500.     PCHAR                                        pKernelName;   
  501.     PDWORD                                        pService;   
  502.     PIMAGE_FILE_HEADER                pfh;   
  503.     PIMAGE_OPTIONAL_HEADER  poh;   
  504.     PIMAGE_SECTION_HEADER   psh;
  505.     ULONG n;   
  506.       

  507.     // get system modules - ntoskrnl is always first there   
  508.     ZwQuerySystemInformation(SystemModuleInformation, &n, 0, &n);
  509.        
  510.     PULONG p = new ULONG[n];

  511.     ZwQuerySystemInformation(SystemModuleInformation, p, n * sizeof(*p), 0);
  512.        
  513.     PSYSTEM_MODULE_INFORMATION module = PSYSTEM_MODULE_INFORMATION(p+1);   
  514.    
  515.     // imagebase   
  516.     dwKernelBase = (DWORD)module->Base;
  517.        
  518.         USHORT ofs = module->ModuleNameOffset;

  519.         char* imgName = (char*)module->ImageName;

  520.         pKernelName = ofs + imgName;
  521.     // filename - it may be renamed in the boot.ini   
  522.     pKernelName = module->ModuleNameOffset + module->ImageName;   
  523.       
  524.     // map ntoskrnl - hopefully it has relocs   
  525.     hKernel = LoadLibraryEx(pKernelName, 0, DONT_RESOLVE_DLL_REFERENCES);
  526.        
  527.     if(!hKernel) {   
  528.         return;   
  529.     }   
  530.       
  531.     // our own export walker is useless here - we have GetProcAddress :)   
  532.     if (!(dwKSDT = (DWORD)GetProcAddress(hKernel, "KeServiceDescriptorTable"))) {   
  533.         return;   
  534.     }   
  535.       
  536.     // get KeServiceDescriptorTable rva   
  537.     dwKSDT -= (DWORD)hKernel;

  538.     // find KiServiceTable   
  539.     if (!(dwKiServiceTable = FindKiServiceTable(hKernel, dwKSDT))) {
  540.         return;   
  541.     }   
  542.       
  543.     // let's dump KiServiceTable contents   
  544.       
  545.     // MAY FAIL!!!   
  546.     // should get right ServiceLimit here, but this is trivial in the kernel mode   
  547.     GetHeaders((PCHAR)hKernel, &pfh, &poh, &psh);

  548.     dwServices = 0;   
  549.    
  550.     for (pService = (PDWORD)((DWORD)hKernel+dwKiServiceTable);
  551.                 *pService - poh->ImageBase < poh->SizeOfImage;   
  552.                 pService++, dwServices++)   
  553.     {   
  554.         ssdt_list[dwServices].address1 = *pService - poh->ImageBase + dwKernelBase;   
  555.     }

  556.     FreeLibrary(hKernel);

  557.     MEMORY_CHUNKS QueryBuff;

  558.     DWORD *address2 = new DWORD[dwServices];

  559.     QueryBuff.Address = dwKernelBase+dwKiServiceTable;

  560.     QueryBuff.Data = address2;

  561.     QueryBuff.Length = sizeof(DWORD)*dwServices;
  562.        
  563.     DWORD ReturnLength;

  564.     ZwSystemDebugControl(
  565.                 SysDbgReadVirtualMemory,   
  566.         &QueryBuff, sizeof(MEMORY_CHUNKS), NULL,   
  567.         0, &ReturnLength);   
  568.       
  569.     LV_ITEM lvi;

  570.     lvi.mask = LVIF_TEXT;

  571.     char tmp[10];

  572.     ListView_DeleteAllItems(hList);

  573.     for (int j=0; j < dwServices; j++)   
  574.     {   
  575.         lvi.iItem = j;   
  576.         lvi.iSubItem = 0;   
  577.         lvi.pszText = tmp;

  578.         wsprintf(tmp, "0x%02X", j);

  579.         ListView_InsertItem(hList, &lvi);

  580.         ListView_SetItemText(hList, j, 1, ssdt_list[j].fname);

  581.         wsprintf(tmp, "0x%08X", ssdt_list[j].address1);

  582.         ListView_SetItemText(hList, j, 2, tmp);

  583.         wsprintf(tmp,"0x%08X", address2[j]);

  584.         ssdt_list[j].address2 = address2[j];

  585.         ListView_SetItemText(hList, j, 3, tmp);  

  586.         for(int i=0; i < *p; i++)
  587.         {   
  588.             if (ssdt_list[j].address2 > (DWORD)module[i].Base&&ssdt_list[j].address2 < (DWORD)module[i].Base+module[i].Size)   
  589.             {   
  590.                 ListView_SetItemText(hList, j, 4, module[i].ImageName);   
  591.                 break;   
  592.             }   
  593.         }   
  594.     }   
  595.       
  596.     delete [] p;   
  597.     delete [] address2;   
  598. }


  599. void InitListView()   
  600. {
  601.         INITCOMMONCONTROLSEX iex;
  602.         iex.dwSize = sizeof(iex);   
  603.         iex.dwICC=ICC_LISTVIEW_CLASSES;   
  604.         InitCommonControlsEx(&iex);

  605.     hList = CreateWindowEx(WS_EX_CLIENTEDGE, WC_LISTVIEW, NULL,   
  606.         WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SINGLESEL | LVS_SHAREIMAGELISTS,   
  607.         0, 0, 0, 0, hWinMain, (HMENU)ID_LISTVIEW, g_hInst, NULL);

  608.     SendMessage(hList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP | LVS_EX_GRIDLINES);   

  609.         LV_COLUMN lvc;

  610.     lvc.mask = LVCF_TEXT | LVCF_WIDTH ;   
  611.     lvc.fmt = LVCFMT_RIGHT;   

  612.         lvc.pszText = "No.";
  613.         lvc.cx = 50;   
  614.         SendMessage(hList, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc);   
  615.            
  616.         lvc.mask |= LVCF_FMT;
  617.         lvc.fmt = LVCFMT_LEFT;
  618.            
  619.         lvc.pszText = "Function Name";
  620.         lvc.cx = 350;   
  621.         SendMessage(hList, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc);

  622.         lvc.pszText = "OrgAddr";
  623.         lvc.cx = 100;   
  624.         SendMessage(hList, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc);   

  625.         lvc.pszText = "HookAddr";   
  626.         lvc.cx = 100;   
  627.         SendMessage(hList, LVM_INSERTCOLUMN, 3, (LPARAM)&lvc);   

  628.         lvc.pszText = "Module";
  629.         lvc.cx = 200;
  630.         SendMessage(hList, LVM_INSERTCOLUMN, 4, (LPARAM)&lvc);
  631. }   
  632.       
复制代码
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表