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

钩子与API截获

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <malloc.h>
  5. #include "util.h"
  6. #include "Filter.h"
  7. char g_dll_path[128];
  8. CFileFilter::CFileFilter()
  9. {
  10. m_pFilterInfo =NULL;
  11. m_nFilterInfoCount =0;
  12. FILE *fp;
  13. if(GetProfileString("HookAPI", "dll_path", "", g_dll_path, sizeof(g_dll_path)) ==0)
  14. return;
  15. char init_file[128];
  16. wsprintf(init_file, "%s\filterfile.ini", g_dll_path);
  17. //WriteLog("FilterInit:init_file=%s", init_file);
  18. if((fp =fopen(init_file, "r")) ==NULL)
  19. {
  20. WriteLog("open init file %s failed!", init_file);
  21. return;
  22. }
  23. char buf[300], *p, app[40], type[10], password[21];
  24. while(!feof(fp) && fgets(buf, sizeof(buf), fp))
  25. {
  26. if(strlen(buf) <5 || buf[0] =='#') continue;
  27. if(buf[strlen(buf)-1] =='r' || buf[strlen(buf)-1] =='n')
  28. buf[strlen(buf)-1] =0;
  29. if(buf[strlen(buf)-2] =='r' || buf[strlen(buf)-2] =='n')
  30. buf[strlen(buf)-2] =0;
  31. p =strtok(buf, ",");
  32. if(!p) continue;
  33. if(strlen(p) >= sizeof(type)) continue;
  34. strcpy(type, p);
  35. //WriteLog("type=%s", type);
  36. p =strtok(NULL, ",");
  37. if(!p) continue;
  38. if(strlen(p) >=sizeof(app)) continue;
  39. strcpy(app, p);
  40. strupr(app);
  41. //WriteLog("app=%s", app);
  42. //p =strtok(NULL, ",");
  43. //if(!p) continue;
  44. int f_permit =0;//*p-'0';
  45. password[0] =0;
  46. p =strtok(NULL, ",");
  47. if(p)
  48. {
  49. if(strlen(p) >=sizeof(password)) continue;
  50. strcpy(password, p);
  51. }
  52. if(m_pFilterInfo)
  53. m_pFilterInfo =(FILTER_INFO *)realloc(m_pFilterInfo, (m_nFilterInfoCount+1)*sizeof(FILTER_INFO));
  54. else
  55. m_pFilterInfo =(FILTER_INFO *)malloc((m_nFilterInfoCount+1)*sizeof(FILTER_INFO));
  56. if(!strcmpi(type, "execute"))
  57. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_EXECUTE;
  58. else if(!strcmpi(type, "open"))
  59. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_OPEN;
  60. else if(!strcmpi(type, "create"))
  61. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_CREATE;
  62. else if(!strcmpi(type, "write"))
  63. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_WRITE;
  64. else if(!strcmpi(type, "read"))
  65. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_READ;
  66. else if(!strcmpi(type, "copy"))
  67. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_COPY;
  68. else if(!strcmpi(type, "move"))
  69. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_MOVE;
  70. else if(!strcmpi(type, "chdir"))
  71. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_CHDIR;
  72. else if(!strcmpi(type, "opendir"))
  73. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_OPENDIR;
  74. else if(!strcmpi(type, "createdir"))
  75. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_CREATEDIR;
  76. else if(!strcmpi(type, "movedir"))
  77. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_MOVEDIR;
  78. else if(!strcmpi(type, "hideprocess"))
  79. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_HIDEPROCESS;
  80. else if(!strcmpi(type, "execrypt"))
  81. m_pFilterInfo[m_nFilterInfoCount].type =FILTER_EXECRYPT;
  82. else
  83. m_pFilterInfo[m_nFilterInfoCount].type =-1;
  84. strcpy(m_pFilterInfo[m_nFilterInfoCount].app, app);
  85. m_pFilterInfo[m_nFilterInfoCount].f_permit =f_permit;
  86. strcpy(m_pFilterInfo[m_nFilterInfoCount].password, password);
  87. m_nFilterInfoCount++;
  88. }
  89. //WriteLog("filercount=%d", m_nFilterInfoCount);
  90. }
  91. CFileFilter::~CFileFilter()
  92. {
  93. if(m_pFilterInfo)
  94. free(m_pFilterInfo);
  95. }
  96. int CFileFilter::FilterName(char *name, int type)
  97. {
  98. char name2[512];
  99. strcpy(name2, name);
  100. strupr(name2);
  101. for(int i=0; i<m_nFilterInfoCount; i++)
  102. {
  103. //WriteLog("type=%d, name2=%s, app=%s", type, name2, m_pFilterInfo[i].app);
  104. if(m_pFilterInfo[i].type !=type)
  105. continue;
  106. if(strstr(name2, m_pFilterInfo[i].app))
  107. {
  108. WriteLog("FilterName ok! %s", name);
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. int CFileFilter::FilterExecute(char *cmd)
  115. {
  116. return FilterName(cmd, FILTER_EXECUTE);
  117. }
  118. int CFileFilter::FilterExeCrypt(char *cmd, char *new_cmd)
  119. {
  120. char cmd2[512], password[9], file[128], file2[128], app[128], app2[128];
  121. strcpy(cmd2, cmd);
  122. strupr(cmd2);
  123. split_cmd(cmd2, app, file);
  124. for(int i=0; i<m_nFilterInfoCount; i++)
  125. {
  126. if(m_pFilterInfo[i].type !=FILTER_EXECRYPT)
  127. continue;
  128. if(strstr(file, m_pFilterInfo[i].app))
  129. {
  130. if(GetPassword(m_pFilterInfo[i].app, password) <0)
  131. return -1;
  132. if(MyDecryptFile(file, file2, password) <0)
  133. return -2;
  134. wsprintf(new_cmd, ""%s" "%s"", app, file2);
  135. return 1;
  136. }
  137. else if(strstr(app, m_pFilterInfo[i].app))
  138. {
  139. if(GetPassword(m_pFilterInfo[i].app, password) <0)
  140. return -1;
  141. if(MyDecryptFile(app, app2, password) <0)
  142. return -2;
  143. wsprintf(new_cmd, ""%s" "%s"", app2, file);
  144. return 1;
  145. }
  146. }
  147. return 0;
  148. }
  149. int CFileFilter::FilterOpenFile(char *fname)
  150. {
  151. return FilterName(fname, FILTER_OPEN);
  152. }
  153. int CFileFilter::FilterCreateFile(char *fname)
  154. {
  155. if(FilterName(fname, FILTER_OPEN)) return TRUE;
  156. return FilterName(fname, FILTER_CREATE);
  157. }
  158. int CFileFilter::FilterWriteFile(char *fname)
  159. {
  160. return FilterName(fname, FILTER_WRITE);
  161. }
  162. int CFileFilter::FilterReadFile(char *fname)
  163. {
  164. return FilterName(fname, FILTER_READ);
  165. }
  166. int CFileFilter::FilterCopyFile(char *fsrc, char *fdst)
  167. {
  168. return FilterName(fsrc, FILTER_COPY);
  169. }
  170. int CFileFilter::FilterMoveFile(char *fname, char *fname2)
  171. {
  172. return FilterName(fname, FILTER_MOVE);
  173. }
  174. int CFileFilter::FilterChDir(char *dir)
  175. {
  176. return FilterName(dir, FILTER_CHDIR);
  177. }
  178. int CFileFilter::FilterOpenDir(char *dir)
  179. {
  180. return FilterName(dir, FILTER_OPENDIR);
  181. }
  182. int CFileFilter::FilterCreateDir(char *dir)
  183. {
  184. return FilterName(dir, FILTER_CREATEDIR);
  185. }
  186. int CFileFilter::FilterDeleteFile(char *fname)
  187. {
  188. return FilterName(fname, FILTER_DELETE);
  189. }