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

钩子与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 "mydll.h"
  11. #ifdef WIN95
  12. #pragma code_seg("_INIT")
  13. #pragma comment(linker,"/SECTION:.bss,RWS /SECTION:.data,RWS /SECTION:.rdata,RWS /SECTION:.text,RWS /SECTION:_INIT,RWS ")
  14. #pragma comment(linker,"/BASE:0xBFF70000")
  15. #endif
  16. BOOL APIENTRY DllMain( HANDLE hModule, 
  17.                        DWORD  ul_reason_for_call, 
  18.                        LPVOID lpReserved
  19.  )
  20. {
  21.     return TRUE;
  22. }
  23. void WriteLog(char *fmt,...)
  24. {
  25. va_list args;
  26. char modname[200];
  27. char temp[5000];
  28. HANDLE hFile;
  29. GetModuleFileName(NULL, modname, sizeof(modname));
  30. if((hFile =CreateFile("c:\hookapi.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)) <0)
  31. {
  32. return;
  33. }
  34. _llseek((HFILE)hFile, 0, SEEK_END);
  35. wsprintf(temp, "mydll.dll:%s:", modname);
  36. DWORD dw;
  37. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  38. va_start(args,fmt);
  39. vsprintf(temp, fmt, args);
  40. va_end(args);
  41. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  42. wsprintf(temp, "rn");
  43. WriteFile(hFile, temp, strlen(temp), &dw, NULL);
  44. _lclose((HFILE)hFile);
  45. }
  46. HANDLE WINAPI myCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
  47. {          
  48. char temp[200];
  49. GetModuleFileName(NULL, temp, sizeof(temp));
  50. WriteLog("%s, myCreateFileA:filename=%s", temp, lpFileName);
  51. //MessageBox(NULL, temp, "mydll", MB_OK);
  52. if(strstr(lpFileName, "aaa") !=NULL) return NULL;
  53. return CreateFileA(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  54. dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  55. }
  56. HANDLE WINAPI myCreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
  57. {
  58. //MessageBox(NULL, "myCreateFileW", "ok", MB_OK);
  59.                               
  60. char temp[200];
  61. GetModuleFileName(NULL, temp, sizeof(temp));
  62. char fname[128];
  63. WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, fname, 128,NULL,NULL); 
  64. WriteLog("%s, myCreateFileW:filename=%s", temp, fname);
  65. //MessageBox(NULL, temp, "mydll", MB_OK);
  66. if(strstr(fname, "aaa.txt") !=NULL)
  67. {
  68. WriteLog("CreateFileW aaa found!");
  69. return CreateFileA("c:\temp\bbb.txt", dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  70. dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  71. //SetLastError(ERROR_FILE_NOT_FOUND);
  72. //return NULL;
  73. }
  74. return CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes,
  75. dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
  76. }
  77. BOOL WINAPI myDeleteFileA(LPCSTR lpFileName)
  78. {
  79. char temp[200];
  80. GetModuleFileName(NULL, temp, sizeof(temp));
  81. WriteLog("%s,n myDeleteFileA:filename=%s", temp, lpFileName);
  82. if(strstr(temp, "aaa") !=NULL)
  83. {
  84. WriteLog("DeleteFileA aaa found!");
  85. SetLastError(ERROR_FILE_NOT_FOUND);
  86. return NULL;
  87. }
  88. return DeleteFileA(lpFileName);
  89. }
  90. BOOL WINAPI myDeleteFileW(LPCWSTR lpFileName)
  91. {
  92. char temp[200];
  93. GetModuleFileName(NULL, temp, sizeof(temp));
  94. char fname[128];
  95. WideCharToMultiByte( CP_ACP, 0, lpFileName, -1, fname, 128,NULL,NULL); 
  96. WriteLog("%s,n myDeleteFileW:filename=%s", temp, fname);
  97. if(strstr(fname, "aaa") !=NULL)
  98. {
  99. WriteLog("DeleteFileW aaa found!");
  100. SetLastError(ERROR_FILE_NOT_FOUND);
  101. return NULL;
  102. }
  103. return DeleteFileW(lpFileName);
  104. }
  105. BOOL WINAPI myReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
  106. LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
  107. {
  108. WriteLog("ReadFile:handle=%x", hFile);
  109. return ReadFile(hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped);
  110. }
  111. BOOL WINAPI myReadFileEx(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
  112. LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
  113. {
  114. WriteLog("ReadFileEx");
  115. return ReadFileEx(hFile, lpBuffer, nNumberOfBytesToRead,
  116. lpOverlapped, lpCompletionRoutine);
  117. }
  118. BOOL WINAPI myWriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
  119. LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
  120. {
  121. WriteLog("WriteFile");
  122. return WriteFile(hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten,
  123. lpOverlapped);
  124. }
  125. BOOL WINAPI myWriteFileEx(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite,
  126. LPOVERLAPPED lpOverlapped, LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
  127. {
  128. WriteLog("WriteFileEx");
  129. return WriteFileEx(hFile, lpBuffer, nNumberOfBytesToWrite, lpOverlapped, lpCompletionRoutine);
  130. }
  131. DWORD WINAPI myCreateProcessW(
  132. LPCWSTR lpApplicationName,
  133. LPWSTR lpCommandLine, 
  134. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  135. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  136. BOOL bInheritHandles,
  137. DWORD dwCreationFlags,
  138. LPVOID lpEnvironment,
  139. LPCWSTR lpCurrentDirectory,
  140. LPSTARTUPINFOW lpStartupInfo,
  141. LPPROCESS_INFORMATION lpProcessInformation
  142. )
  143. {
  144. char cmd[600];
  145. int len =WideCharToMultiByte( CP_ACP, 0, lpCommandLine, -1, cmd, sizeof(cmd),NULL,NULL); 
  146. cmd[len] =0;
  147. WriteLog("CreateProcessW :cmd=%s", cmd);
  148. BOOL ifsuccess = CreateProcessW(lpApplicationName,
  149. lpCommandLine, lpProcessAttributes,
  150. lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
  151. lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
  152. DWORD err =GetLastError();
  153. SetLastError(err);
  154. return (DWORD)ifsuccess;
  155. }
  156. DWORD WINAPI myCreateProcessA(
  157. LPCSTR lpApplicationName,
  158. LPSTR lpCommandLine, 
  159. LPSECURITY_ATTRIBUTES lpProcessAttributes,
  160. LPSECURITY_ATTRIBUTES lpThreadAttributes,
  161. BOOL bInheritHandles,
  162. DWORD dwCreationFlags,
  163. LPVOID lpEnvironment,
  164. LPCSTR lpCurrentDirectory,
  165. LPSTARTUPINFO lpStartupInfo,
  166. LPPROCESS_INFORMATION lpProcessInformation
  167. )
  168. {
  169. BOOL ifsuccess = CreateProcessA(lpApplicationName,
  170. lpCommandLine, lpProcessAttributes,
  171. lpThreadAttributes, bInheritHandles, dwCreationFlags, lpEnvironment,
  172. lpCurrentDirectory, lpStartupInfo, lpProcessInformation);
  173. DWORD err =GetLastError();
  174. WriteLog("CreateProcessA %s", lpCommandLine);
  175. SetLastError(err);
  176. return (DWORD)ifsuccess;
  177. }
  178. MYAPIINFO myapi_info[] =
  179. {
  180. {"KERNEL32.DLL", "CreateFileA", 7, "myCreateFileA"},
  181. {"KERNEL32.DLL", "CreateFileW", 7, "myCreateFileW"},
  182. {"KERNEL32.DLL", "DeleteFileA", 1, "myDeleteFileA"},
  183. {"KERNEL32.DLL", "DeleteFileW", 1, "myDeleteFileW"},
  184. {"KERNEL32.DLL", "ReadFile", 5, "myReadFile"},
  185. {"KERNEL32.DLL", "ReadFileEx", 5, "myReadFileEx"},
  186. {"KERNEL32.DLL", "WriteFile", 5, "myWriteFile"},
  187. {"KERNEL32.DLL", "WriteFileEx", 5, "myWriteFileEx"},
  188. {"KERNEL32.DLL", "CreateProcessW", 10, "myCreateProcessW"},
  189. {"KERNEL32.DLL", "CreateProcessA", 10, "myCreateProcessA"},
  190. {NULL,NULL,NULL}
  191. };
  192. MYAPIINFO *GetMyAPIInfo()
  193. {
  194. return &myapi_info[0];
  195. }