mydll.cpp
上传用户:nbcables
上传日期:2007-01-11
资源大小:1243k
文件大小:4k
源码类别:

钩子与API截获

开发平台:

Visual C++

  1. // ------------------------------------- //
  2. // 您如果要使用本文件,请不要删除本说明  //
  3. // ------------------------------------- //
  4. //             HOOKAPI 开发例子          //
  5. //   Copyright 2002 编程沙龙 Paladin     //
  6. //       www.ProgramSalon.com            //
  7. // ------------------------------------- //
  8. #include "stdafx.h"
  9. #include <stdio.h>
  10. #include <shellapi.h>
  11. #include "mydll.h"
  12. #ifdef WIN95
  13. #pragma code_seg("_INIT")
  14. #pragma comment(linker,"/SECTION:.bss,RWS /SECTION:.data,RWS /SECTION:.rdata,RWS /SECTION:.text,RWS /SECTION:_INIT,RWS ")
  15. #pragma comment(linker,"/BASE:0xBFF70000")
  16. #endif
  17. BOOL APIENTRY DllMain( HANDLE hModule, 
  18.                        DWORD  ul_reason_for_call, 
  19.                        LPVOID lpReserved
  20.  )
  21. {
  22.     return TRUE;
  23. }
  24. void WriteLog(char *fmt, ...)
  25. {
  26. va_list args;
  27. char modname[200];
  28. char temp[5000];
  29. HANDLE hFile;
  30. GetModuleFileName(NULL, modname, sizeof(modname));
  31. if((hFile = CreateFile("c:\hookapi.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0)
  32. {
  33. return;
  34. }
  35. _llseek((HFILE)hFile, 0, SEEK_END);
  36. wsprintf(temp, "mydll.dll:%s:", modname);
  37. DWORD dw;
  38. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  39. va_start(args,fmt);
  40. vsprintf(temp, fmt, args);
  41. va_end(args);
  42. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  43. wsprintf(temp, "rn");
  44. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  45. _lclose((HFILE)hFile);
  46. }
  47. BOOL GetCurProcessBaseName(char *pBaseName, int BufSize)
  48. {
  49. BOOL bRet = FALSE;
  50. char szCurProcessPathName[MAX_PATH];
  51. memset(szCurProcessPathName, 0, MAX_PATH);
  52. memset(pBaseName, 0, BufSize);
  53. ::GetModuleFileName(NULL, szCurProcessPathName, MAX_PATH);
  54. char *p = strrchr(szCurProcessPathName, '\');
  55. if(p)
  56. {
  57. strcpy(pBaseName, p + 1);
  58. bRet = TRUE;
  59. }
  60. return bRet;
  61. }
  62. FARPROC WINAPI MyGetProcAddress(HMODULE hModule, LPCSTR lpProcName)
  63. {
  64. /*
  65. char szCurProcessBaseName[MAX_PATH];
  66. if(GetCurProcessBaseName(szCurProcessBaseName, MAX_PATH))
  67. {
  68. if(stricmp(szCurProcessBaseName, "explorer.exe") == 0)
  69. {
  70. WriteLog("You Use GetProcAddress, Func Name: %s", lpProcName);
  71. }
  72. }
  73. */
  74. return GetProcAddress(hModule, lpProcName);
  75. }
  76. HMODULE WINAPI MyLoadLibraryA(LPCSTR lpLibFileName)
  77. {
  78. /*
  79. char szCurProcessBaseName[MAX_PATH];
  80. if(GetCurProcessBaseName(szCurProcessBaseName, MAX_PATH))
  81. {
  82. if(stricmp(szCurProcessBaseName, "explorer.exe") == 0)
  83. {
  84. WriteLog("You Use LoadLibraryA, Dll Name: %s", lpLibFileName);
  85. }
  86. }
  87. */
  88. return LoadLibraryA(lpLibFileName);
  89. }
  90. HMODULE WINAPI MyLoadLibraryW(LPCWSTR lpLibFileName)
  91. {
  92. /*
  93. char fname[MAX_PATH];
  94. memset(fname, 0, MAX_PATH);
  95. ::WideCharToMultiByte( CP_ACP, 0, lpLibFileName, -1, fname, MAX_PATH, NULL, NULL);
  96. char szCurProcessBaseName[MAX_PATH];
  97. if(GetCurProcessBaseName(szCurProcessBaseName, MAX_PATH))
  98. {
  99. if(stricmp(szCurProcessBaseName, "explorer.exe") == 0)
  100. {
  101. // WriteLog("You Use LoadLibraryW, Dll Name: %s", fname);
  102. }
  103. }
  104. */
  105. return LoadLibraryW(lpLibFileName);
  106. }
  107. int WINAPI MySHFileOperation(LPSHFILEOPSTRUCTA lpFileOp)
  108. {
  109. //WriteLog("You Use SHFileOperation");
  110. // if(strstr(lpFileOp->pFrom, "Test.txt") != NULL)
  111. // return 0;
  112. // else
  113. return SHFileOperation(lpFileOp);
  114. }
  115. int WINAPI MySHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
  116. {
  117. WriteLog("You Use SHFileOperationA");
  118. if(lpFileOp ==NULL)
  119. return 0;
  120. if(strstr(lpFileOp->pFrom, "Test.txt") != NULL)
  121. return 0;
  122. else
  123. return SHFileOperationA(lpFileOp);
  124. }
  125. int WINAPI MySHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
  126. {
  127. WriteLog("You Use SHFileOperationW");
  128. if(lpFileOp ==NULL)
  129. return 0;
  130. char fname[MAX_PATH];
  131. memset(fname, 0, MAX_PATH);
  132. ::WideCharToMultiByte( CP_ACP, 0, lpFileOp->pFrom, -1, fname, MAX_PATH, NULL, NULL);
  133. if(strstr(fname, "Test.txt") != NULL)
  134. return 0;
  135. else
  136. return SHFileOperationW(lpFileOp);
  137. return 0;
  138. }
  139. MYAPIINFO myapi_info[] =
  140. {
  141. // {"KERNEL32.DLL", "GetProcAddress", 2, "MyGetProcAddress"},
  142. // {"KERNEL32.DLL", "LoadLibraryA", 1, "MyLoadLibraryA"},LoadLibraryA
  143. // {"KERNEL32.DLL", "LoadLibraryW", 1, "MyLoadLibraryW"},
  144. {"Shell32.DLL", "SHFileOperation", 1, "MySHFileOperation"},
  145. {"Shell32.DLL", "SHFileOperationA", 1, "MySHFileOperationA"},
  146. {"Shell32.DLL", "SHFileOperationW", 1, "MySHFileOperationW"},
  147. {NULL,NULL,NULL}
  148. };
  149. MYAPIINFO *GetMyAPIInfo()
  150. {
  151. return &myapi_info[0];
  152. }