狸猫换太子:动态替换WinCE的原生驱动!

//=====================================================================
//TITLE:
//    狸猫换太子:动态替换WinCE的原生驱动!
//AUTHOR:
//    norains
//DATE:
//    Friday 23-April-2010
//Environment:
//    Windows CE 5.0 + TCC7901
//=====================================================================

 

    大家应该都知道,WinCE系统的驱动是可以非常方便地动态加载和卸载的(如果对此不清楚,可以参考我之前写的两篇文章。《WinCE驱动的动态加载》:http://blog.csdn.net/norains/archive/2010/02/22/5316923.aspx《WinCE驱动的动态卸载》:http://blog.csdn.net/norains/archive/2010/04/22/5514351.aspx),如果我们善加利用,完全可以神不知鬼不觉地进行狸猫换太子--不必重新编译系统,就可以在应用程序完全不知情地情况下,将原生驱动偷梁换柱成我们自己的冒牌货!

 

    听起来很有意思,是吧?那么,让我们开始这次奇妙的旅程吧。

 

    能达到的目标才有动力,这次我们拿WinCE设备最常用的最基本的串口驱动开刀。我们要做的事情也很简单,如果通过Read读取回来的字符超过五个,那么我们就将最前面的五个字符替换为"abcde"。

 

    如果要达成这个目标,我们需要两个程序:
    1. 伪驱动DLL。它负责替换读取回来的数据
    2. 驱动加载程序。

 

    第二点比较简单,我们先来看看第一点。

  

    首先我们来了解一下WinCE驱动的基本情况。WinCE的驱动,说白了,就是一个dll文件。它和普通的dll唯一不同的是,它规定了导出函数的形式,比如XXX_Init,XXX_Deinit等等。其中的XXX,是不同驱动的前缀,就像串口驱动为COM_Init一样。更为严格的是,像COM_Init这样的函数,无论是返回值,还是形参的类型乃至于个数,都是规定死的,否则WinCE不会正常加载。
  
    而在这一框框条条之下,带给我们的是另外的一种机会:我们的伪驱动向上实现微软规定的接口,向下则调用原生的驱动。
  
  听起来是不是有点迷糊?没关系,我们以COM_Read函数举个例子。
  
  我们的伪驱动名为FakeDriver.dll,导出了一个函数叫COM_Read。我这里的平台是Telechips的TCC7901,它的驱动文件为tcc_serial.dll。接下来需要做的是,从tcc_serial.dll中获得其COM_Read的地址,以方便我们在伪驱动中调用。
  
  对于大家来说,对于下面调用dll函数的代码应该不会陌生:
    //显式加载DLL文件   g_hCoreDll = LoadLibrary(TEXT("tcc_serial.dll"));      ...      //获取DLL文件的COM_Read函数地址   typedef ULONG (WINAPI *DLL_COM_READ)(HANDLE pHead,PUCHAR pTargetBuffer,ULONG BufferLength);   DLL_COM_READ DLL_COM_Read = (DLL_COM_READ)GetProcAddress(g_hCoreDll, _T("COM_Read"));      ...      ULONG COM_Read(HANDLE pHead,PUCHAR pTargetBuffer,ULONG BufferLength)   {    //直接调用DLL的COM_Read函数    return DLL_COM_Read(pHead,pTargetBuffer,BufferLength);   }   
  
  没错,就是这么简单。我们的伪驱动,其实只是在原生驱动之上封装了一层,骨子里还是调用的原生驱动。只不过我们可以在这封装的一层中,可以做太多的事情了。对于本文前面的目标,我们的伪驱动的代码完整实现如下:
    #include "serpriv.h"      //------------------------------------------------------------   //导出函数的定义   typedef HANDLE (WINAPI *DLL_COM_INIT)(ULONG Identifier);   typedef BOOL (WINAPI *DLL_COM_DEINIT)(PHW_INDEP_INFO pSerialHead);   typedef BOOL (WINAPI *DLL_COM_IOCONTROL)(PHW_OPEN_INFO pOpenHead,DWORD dwCode, PBYTE pBufIn,DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,PDWORD pdwActualOut);   typedef void (WINAPI *DLL_COM_PRECLOSE)(PHW_OPEN_INFO pOpenHead);   typedef BOOL (WINAPI *DLL_COM_PREDEINIT)(PHW_INDEP_INFO pSerialHead);   typedef HANDLE (WINAPI *DLL_COM_OPEN)(HANDLE pHead, DWORD AccessCode,DWORD ShareMode);   typedef BOOL (WINAPI *DLL_COM_CLOSE)(PHW_OPEN_INFO pOpenHead);   typedef BOOL (WINAPI *DLL_COM_POWERDOWN)(HANDLE pHead);   typedef BOOL (WINAPI *DLL_COM_POWERUP)(HANDLE pHead);   typedef ULONG (WINAPI *DLL_COM_READ)(HANDLE pHead,PUCHAR pTargetBuffer,ULONG BufferLength);   typedef ULONG (WINAPI *DLL_COM_SEEK)(HANDLE pHead,LONG Position,DWORD Type);   typedef ULONG (WINAPI *DLL_COM_WRITE)(HANDLE pHead,PUCHAR pSourceBytes,ULONG NumberOfBytes);      //----------------------------------------------------------   //全局变量   DLL_COM_INIT DLL_COM_Init = NULL;   DLL_COM_DEINIT DLL_COM_Deinit = NULL;   DLL_COM_IOCONTROL DLL_COM_IOControl = NULL;   DLL_COM_PRECLOSE DLL_COM_PreClose = NULL;   DLL_COM_PREDEINIT DLL_COM_PreDeinit = NULL;   DLL_COM_OPEN DLL_COM_Open = NULL;   DLL_COM_CLOSE DLL_COM_Close = NULL;   DLL_COM_POWERDOWN DLL_COM_PowerDown = NULL;   DLL_COM_POWERUP DLL_COM_PowerUp = NULL;   DLL_COM_READ DLL_COM_Read = NULL;   DLL_COM_SEEK DLL_COM_Seek = NULL;   DLL_COM_WRITE DLL_COM_Write = NULL;      HINSTANCE g_hCoreDll = NULL;      //----------------------------------------------------------   //获取所有导出函数的地址   BOOL InitFuncAddr()   {    BOOL bRes = FALSE;          __try    {    g_hCoreDll = LoadLibrary(TEXT("tcc_serial.dll"));    if (g_hCoreDll == NULL)    {    __leave;    }       DLL_COM_Init = (DLL_COM_INIT)GetProcAddress(g_hCoreDll, _T("COM_Init"));    if(DLL_COM_Init == NULL)    {    __leave;    }       DLL_COM_Deinit = (DLL_COM_DEINIT)GetProcAddress(g_hCoreDll, _T("COM_Deinit"));    if(DLL_COM_Deinit == NULL)    {    __leave;    }       DLL_COM_IOControl = (DLL_COM_IOCONTROL)GetProcAddress(g_hCoreDll, _T("COM_IOControl"));    if(DLL_COM_IOControl == NULL)    {    __leave;    }       DLL_COM_PreClose = (DLL_COM_PRECLOSE)GetProcAddress(g_hCoreDll, _T("COM_PreClose"));    if(DLL_COM_PreClose == NULL)    {    __leave;    }       DLL_COM_PreDeinit = (DLL_COM_PREDEINIT)GetProcAddress(g_hCoreDll, _T("COM_PreDeinit"));    if(DLL_COM_PreDeinit == NULL)    {    __leave;    }       DLL_COM_Open = (DLL_COM_OPEN)GetProcAddress(g_hCoreDll, _T("COM_Open"));    if(DLL_COM_Open == NULL)    {    __leave;    }          DLL_COM_Close = (DLL_COM_CLOSE)GetProcAddress(g_hCoreDll, _T("COM_Close"));    if(DLL_COM_Close == NULL)    {    __leave;    }       DLL_COM_PowerDown = (DLL_COM_POWERDOWN)GetProcAddress(g_hCoreDll, _T("COM_PowerDown"));    if(DLL_COM_PowerDown == NULL)    {    __leave;    }       DLL_COM_PowerUp = (DLL_COM_POWERUP)GetProcAddress(g_hCoreDll, _T("COM_PowerUp"));    if(DLL_COM_PowerUp == NULL)    {    __leave;    }       DLL_COM_Read = (DLL_COM_READ)GetProcAddress(g_hCoreDll, _T("COM_Read"));    if(DLL_COM_Read == NULL)    {    __leave;    }       DLL_COM_Seek = (DLL_COM_SEEK)GetProcAddress(g_hCoreDll, _T("COM_Seek"));    if(DLL_COM_Seek == NULL)    {    __leave;    }       DLL_COM_Write = (DLL_COM_WRITE)GetProcAddress(g_hCoreDll, _T("COM_Write"));    if(DLL_COM_Write == NULL)    {    __leave;    }       bRes = TRUE;    }    __finally    {    if(bRes == FALSE)    {    if(g_hCoreDll != NULL)    {    FreeLibrary(g_hCoreDll);    }    }    }       return bRes;   }         BOOL WINAPI DllEntry(HANDLE hInstDll, DWORD dwReason, LPVOID lpvReserved)   {    switch ( dwReason )    {    case DLL_PROCESS_ATTACH:    break;    }    return TRUE;   }         HANDLE COM_Init(ULONG Identifier)   {    if(InitFuncAddr() != FALSE)    {    RETAILMSG(TRUE,(TEXT("InitFuncAddr Succeeded/r/n")));       return DLL_COM_Init(Identifier);    //return TRUE;    }    else    {    RETAILMSG(TRUE,(TEXT("InitFuncAddr Failed/r/n")));    return FALSE;    }   }         BOOL COM_Deinit(PHW_INDEP_INFO pSerialHead)   {       BOOL bRes = DLL_COM_Deinit(pSerialHead);       if(g_hCoreDll != NULL)    {    FreeLibrary(g_hCoreDll);    }       RETAILMSG(TRUE,(TEXT("COM_Deinit/r/n")));    return bRes;   }         BOOL COM_IOControl(PHW_OPEN_INFO pOpenHead,    DWORD dwCode, PBYTE pBufIn,    DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,    PDWORD pdwActualOut)   {    return DLL_COM_IOControl(pOpenHead,dwCode,pBufIn,dwLenIn,pBufOut,dwLenOut,pdwActualOut);   }            void COM_PreClose(PHW_OPEN_INFO pOpenHead)   {    DLL_COM_PreClose(pOpenHead);   }      BOOL COM_PreDeinit(PHW_INDEP_INFO pSerialHead)   {    return DLL_COM_PreDeinit(pSerialHead);   }      HANDLE COM_Open(HANDLE pHead, DWORD AccessCode,DWORD ShareMode)   {    return DLL_COM_Open(pHead,AccessCode,ShareMode);   }      BOOL COM_Close(PHW_OPEN_INFO pOpenHead)   {    return DLL_COM_Close(pOpenHead);   }      BOOL COM_PowerDown(HANDLE pHead)   {    return DLL_COM_PowerDown(pHead);   }      BOOL COM_PowerUp(HANDLE pHead)   {    return DLL_COM_PowerUp(pHead);   }      ULONG COM_Read(HANDLE pHead,PUCHAR pTargetBuffer,ULONG BufferLength)   {    ULONG ulRead = DLL_COM_Read(pHead,pTargetBuffer,BufferLength);       //将前面的五个字符替换为abced    if(ulRead > 5)    {    pTargetBuffer[0] = 'a';    pTargetBuffer[1] = 'b';    pTargetBuffer[2] = 'c';    pTargetBuffer[3] = 'd';    pTargetBuffer[4] = 'e';    }       return ulRead;   }      ULONG COM_Seek(HANDLE pHead,LONG Position,DWORD Type)   {    return DLL_COM_Seek(pHead,Position,Type);   }      ULONG COM_Write(HANDLE pHead,PUCHAR pSourceBytes,ULONG NumberOfBytes)   {    return DLL_COM_Write(pHead,pSourceBytes,NumberOfBytes);   }   
  
  代码中的DLL_COM_XXX函数其实全部都是原生驱动的导出函数,如果我们将COM_Read的替换代码注释掉,那么这个伪驱动的表现将和原生驱动一模一样,没有任何区别。
  
  伪驱动DLL已经完成,剩下的就是临门一脚。先看看TCC7901的COM1的注册表:
  狸猫换太子:动态替换WinCE的原生驱动!_第1张图片
  
  如图中所示,为了要让我们的伪驱动能够正常加载,需要将注册表的dll字段更改为我们伪驱动的文件名。
  
  所以,加载的应用程序就只有如下几行(代码中所提到的Unload函数,请参考本文开头的两篇文章):
  
  //卸载原来的COM1驱动   Unload(TEXT("COM1:"));      //更改注册表中DLL的文件名   CReg reg;   reg.Create(HKEY_LOCAL_MACHINE,TEXT("Drivers//BuiltIn//Serial1"));   reg.SetSZ(TEXT("Dll"),TEXT("FakeDriver.dll"));      //动态加载伪驱动   ActivateDeviceEx(TEXT("Drivers//BuiltIn//Serial1",NULL,0,pParam);   
  
  最后我们需要做的就是,将伪驱动FakeDriver.dll拷贝到windows文件夹,运行我们的加载程序,就可以用如下代码进行测试:
  
   HANDLE hCom = CreateFile(TEXT("COM1:"),GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);    std::vector<BYTE> vtBuf(MAX_PATH,0);    DWORD dwRead = 0;    BOOL bRes = ReadFile(hCom,&vtBuf[0],vtBuf.size(),&dwRead,NULL);    CloseHandle(hCom);
  
  如果不出意外的话,那么读取回来的数据前面五个字符将是我们所期望的abcde!
  
  
  本文所提到的伪驱动源代码可于此下载:http://download.csdn.net/source/2275520

你可能感兴趣的:(狸猫换太子:动态替换WinCE的原生驱动!)