home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / Chapt_04 / DDIWatcher / DDIWatcher.cpp next >
Encoding:
C/C++ Source or Header  |  2000-09-08  |  4.9 KB  |  181 lines

  1. //-----------------------------------------------------------------------------------//
  2. //              Windows Graphics Programming: Win32 GDI and DirectDraw               //
  3. //                             ISBN  0-13-086985-6                                   //
  4. //                                                                                   //
  5. //  Written            by  Yuan, Feng                             www.fengyuan.com   //
  6. //  Copyright (c) 2000 by  Hewlett-Packard Company                www.hp.com         //
  7. //  Published          by  Prentice Hall PTR, Prentice-Hall, Inc. www.phptr.com      //
  8. //                                                                                   //
  9. //  FileName   : ddiwatcher.cpp                                                         //
  10. //  Description: Testing program for DDI spy, Chapter 4                              //
  11. //  Version    : 1.00.001, Sep 8, 2000                                               //
  12. //-----------------------------------------------------------------------------------//
  13.  
  14. #define STRICT
  15. #define NOCRYPT
  16.  
  17. #include <windows.h>
  18. #include <winioctl.h>
  19. #include <assert.h>
  20. #include <tchar.h>
  21.  
  22. #include "..\..\include\device.h"
  23. #include "..\DDISpy\\DDISpy.h"
  24. #include "resource.h"
  25.  
  26. int MyMessageBox(HWND hWnd, const TCHAR * text, const TCHAR * caption, DWORD style)
  27. {
  28.     MSGBOXPARAMS param;
  29.  
  30.     memset(& param, 0, sizeof(param));
  31.     param.cbSize      = sizeof(param);
  32.     param.hwndOwner   = hWnd;
  33.     param.hInstance   = GetModuleHandle(NULL);
  34.     param.lpszText    = text;
  35.     param.lpszCaption = caption;
  36.     param.dwStyle     = style | MB_USERICON;
  37.     param.lpszIcon    = MAKEINTRESOURCE(IDI_SPY);
  38.  
  39.     return MessageBoxIndirect(¶m);
  40. }
  41.  
  42.  
  43. BOOL ConfirmHack(const unsigned ddientry[], int nEntry)
  44. {
  45.     TCHAR temp[8192];
  46.  
  47.     temp[0] = 0;
  48.  
  49.     for (int i=0; i<nEntry; i++)
  50.         if ( ddientry[i] !=0 )
  51.             wsprintf(temp + _tcslen(temp), _T("DDI[%d]=0x%x\r\n"), i, ddientry[i]);
  52.  
  53.     return MyMessageBox(NULL, temp, 
  54.                 _T("Your machine may bluescreen. Really want to hack into DDI?"), MB_OKCANCEL) == IDOK;
  55. }
  56.     
  57.  
  58. class KDDIWatcher : public KDevice
  59. {
  60. public:
  61.     KDDIWatcher(const TCHAR * DeviceName) : KDevice(DeviceName)
  62.     {
  63.     }
  64.  
  65.     bool Read(void * dst, const void * src, unsigned len);
  66. };
  67.  
  68.  
  69. bool KDDIWatcher::Read(void * dst, const void * src, unsigned len)
  70. {
  71.     unsigned      cmd[2] = { (unsigned) src, len };
  72.     unsigned long dwRead;
  73.    
  74.     return IoControl(DDISPY_READ, cmd, sizeof(cmd), dst, len, &dwRead) && (dwRead==len);
  75. }
  76.  
  77.  
  78. BOOL Hack(KDDIWatcher & scope)
  79. {
  80.     unsigned buf[8192/4];
  81.  
  82.     HDC  hDC  = GetDC(NULL);
  83.  
  84.     typedef unsigned (CALLBACK * Proc0) (void);
  85.  
  86.     Proc0 pGdiQueryTable = (Proc0) GetProcAddress(GetModuleHandle("GDI32.DLL"), "GdiQueryTable");
  87.  
  88.     unsigned pGDITable = 0;
  89.  
  90.     if ( pGdiQueryTable )
  91.         pGDITable = pGdiQueryTable();
  92.  
  93.     if ( (pGDITable==0) || (pGDITable % 4096) )
  94.     {
  95.         MyMessageBox(NULL, _T("Unable to locate GDI handle table"), _T("DDIWatcher"), MB_OK);
  96.         return FALSE;
  97.     }
  98.  
  99.     unsigned * addr = (unsigned *) (pGDITable + ((unsigned) hDC & 0xFFFF) * 16);
  100.     
  101.     addr = (unsigned *) addr[0];
  102.  
  103.     scope.Read(buf, addr, 40);
  104.  
  105. #ifdef NT4
  106.     unsigned pdev    = buf[5];
  107.     unsigned fntable = pdev + 0x3F4;
  108. #else
  109.     unsigned pdev    = buf[7];
  110.     unsigned fntable = pdev + 0xB8C;
  111. #endif
  112.  
  113.     scope.Read(buf, (void *) fntable, 89 * 4);
  114.  
  115.     if ( ! ConfirmHack(buf, 89) )
  116.         return FALSE;
  117.  
  118.     unsigned      cmd[2] = { fntable, 25 };
  119.     unsigned long dwRead;
  120.    
  121.     scope.IoControl(DDISPY_START, cmd, sizeof(cmd), buf, 100, &dwRead);
  122.  
  123.     TextOut(hDC, 10, 10, "Hello", 4);
  124.     Rectangle(hDC, 0, 0, 100, 100);
  125.     TextOut(hDC, -100, 10, "Why", 3);
  126.     MoveToEx(hDC, 10, 20, NULL);
  127.     LineTo(hDC, 100, 120);
  128.     SetPixel(hDC, 0, 0, RGB(0xFF, 0, 0));
  129.     GetPixel(hDC, 0, 0);
  130.     Ellipse(hDC, 0, 0, 100, 100);
  131.  
  132.     scope.IoControl(DDISPY_END, cmd, sizeof(cmd), buf, 8, &dwRead);
  133.  
  134.     memset(buf, 0xff, sizeof(buf));
  135.     cmd[1] = sizeof(buf);
  136.  
  137.     scope.IoControl(DDISPY_REPORT, cmd, sizeof(cmd), buf, sizeof(buf), &dwRead);
  138.  
  139.     ReleaseDC(NULL, hDC);
  140.  
  141.     MyMessageBox(NULL, (char *) buf, _T("DDI Watcher Report"), MB_OK);
  142.  
  143.     return TRUE;
  144. }
  145.  
  146.  
  147. void GetFullName(HINSTANCE hInstance, const TCHAR * module, TCHAR fullname[])
  148. {
  149.     GetModuleFileName(hInstance, fullname, MAX_PATH);
  150.  
  151.     TCHAR * pName = fullname;
  152.  
  153.     while ( _tcschr(pName, ':') || _tcschr(pName, '\\') )
  154.         if ( _tcschr(pName, ':') )
  155.             pName = _tcschr(pName, ':') + 1;
  156.         else
  157.             pName = _tcschr(pName, '\\') + 1;
  158.  
  159.     if ( pName )
  160.         _tcscpy(pName, module);
  161. }
  162.  
  163.  
  164. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int)
  165. {
  166.     KDDIWatcher scope("DDISpy");
  167.  
  168.     TCHAR fullname[MAX_PATH];
  169.     GetFullName(hInstance, "DDISpy.sys", fullname);
  170.  
  171.     if ( scope.Load(fullname)==ERROR_SUCCESS )
  172.     {
  173.         Hack(scope);
  174.         
  175.     //    scope.Close();    // It's not safe the unload the driver, leave it there in kernel
  176.     }
  177.     else
  178.         MyMessageBox(NULL, fullname, "Unable to load kernel mode driver", MB_OK);
  179.  
  180.     return 0;
  181. }