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

钩子与API截获

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include <malloc.h>
  3. #include "util.h"
  4. #include "const.h"
  5. #include "cryptfilter.h"
  6. char crypt_dll_file[128];
  7. CryptFilter::~CryptFilter()
  8. {
  9. if(hModCrypt)
  10. {
  11. FreeLibrary((HINSTANCE)hModCrypt);
  12. hModCrypt =NULL;
  13. }
  14. m_crypt_info_count =0;
  15. if(m_pCryptInfo)
  16. {
  17. free(m_pCryptInfo);
  18. m_pCryptInfo =NULL;
  19. }
  20. }
  21. CryptFilter::CryptFilter()
  22. {
  23. char init_file[128];
  24. char temp[100], temp1[128];
  25. hModCrypt =NULL;
  26. myCryptData =NULL;
  27. myDecryptData =NULL;
  28. m_pCryptInfo =NULL;
  29. m_crypt_info_count =NULL;
  30. if(g_szDllPath[0] ==0)
  31. return;
  32. wsprintf(init_file, "%s\crypt.ini", g_szDllPath);
  33. if(GetPrivateProfileString("SETUP", "dll", "", temp1, sizeof(temp1), init_file) >0)
  34. wsprintf(crypt_dll_file, "%s\%s", g_szDllPath, temp1);
  35. m_crypt_info_count =0;
  36. while(1)
  37. {
  38. wsprintf(temp, "APP%d", m_crypt_info_count+1);
  39. if(GetPrivateProfileString("SETUP", temp, "", temp1, sizeof(temp1), init_file) <1)
  40. break;
  41. if(m_pCryptInfo ==NULL) m_pCryptInfo =(CRYPT_INFO *)malloc(sizeof(CRYPT_INFO));
  42. else m_pCryptInfo =(CRYPT_INFO *)realloc(m_pCryptInfo, sizeof(CRYPT_INFO)*(m_crypt_info_count+1));
  43. if(m_pCryptInfo ==NULL) return;
  44. memset(&m_pCryptInfo[m_crypt_info_count], 0, sizeof(CRYPT_INFO));
  45. strupr(temp1);
  46. strcpy(m_pCryptInfo[m_crypt_info_count].app, temp1);
  47. wsprintf(temp, "IP%d", m_crypt_info_count+1);
  48. GetPrivateProfileString("SETUP", temp, "", m_pCryptInfo[m_crypt_info_count].ip, 16, init_file);
  49. wsprintf(temp, "LOCAL_PORT_TYPE%d", m_crypt_info_count+1);
  50. m_pCryptInfo[m_crypt_info_count].server_port =GetPrivateProfileInt("SETUP", temp, 0, init_file);
  51. GetPrivateProfileString("SETUP", temp, "", m_pCryptInfo[m_crypt_info_count].local_port_type, 2, init_file);
  52. //WriteLog("crypt:%d", m_crypt_info_count);
  53. m_crypt_info_count++;
  54. }
  55. if(crypt_dll_file[0] ==0) return;
  56. if((hModCrypt =LoadLibrary(crypt_dll_file)) !=NULL)
  57. {
  58. myCryptData =(CRYPTDATA)GetProcAddress((HMODULE)hModCrypt, "CryptData");
  59. myDecryptData =(DECRYPTDATA)GetProcAddress((HMODULE)hModCrypt, "DecryptData");
  60. }
  61. else WriteLog("LoadLibrary crypt dll %s failed!", crypt_dll_file);
  62. }
  63. // 检查是否需要加解密
  64. BOOL CryptFilter::CryptCheck(char *remote_ip, int local_port, int remote_port)
  65. {
  66. int i;
  67. int port;
  68. //WriteLog("CryptCheck...");
  69. for(i =0; i<m_crypt_info_count; i++)
  70. {
  71. //WriteLog("g_szAppBaseName:%s, app:%s, ip:%s, port:%d, local_port_type:%s", g_szAppBaseName, m_pCryptInfo[i].app, m_pCryptInfo[i].ip, m_pCryptInfo[i].server_port,m_pCryptInfo[i].local_port_type);
  72. if(strcmp(m_pCryptInfo[i].app, g_szAppBaseName)==0 || strcmp(m_pCryptInfo[i].app, "ALL") ==0)
  73. {
  74. if(m_pCryptInfo[i].ip[0] ==0 || strcmp(m_pCryptInfo[i].ip, remote_ip) ==0)
  75. {
  76. if(m_pCryptInfo[i].server_port ==0) return true;
  77. if(m_pCryptInfo[i].local_port_type[0] =='S' || m_pCryptInfo[i].local_port_type[0] =='s')
  78. port =local_port;
  79. else port =remote_port;
  80. if(m_pCryptInfo[i].server_port ==port)
  81. return true;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. // CryptData
  88. // return: 0 - data not changed, 1 - data crypted, <0 - failed
  89. int CryptFilter::CryptData(char *data, int data_len, char *data_crypted, int *pdata_crypted_len)
  90. {
  91. int ret =0;
  92. if(myCryptData)
  93. {
  94. ret = myCryptData(data, data_len, data_crypted, pdata_crypted_len);
  95. WriteLog("Dll crypt, ret=%d", ret);
  96. }
  97. else
  98. {
  99. for(int i =0; i<data_len; i++)
  100. data_crypted[i] ^=data[i];
  101. *pdata_crypted_len =data_len;
  102. ret =data_len;
  103. WriteLog("Local Crypt, ret=%d", ret);
  104. }
  105. return ret;
  106. }
  107. // DecryptData
  108. // return: 0 - data not changed, 1 - data decrypted, <0 - failed
  109. int CryptFilter::DecryptData(char *data, int data_len, char *data_decrypted, int *pdata_decrypted_len)
  110. {
  111. int ret =0;
  112. if(myDecryptData)
  113. {
  114. ret = myDecryptData(data, data_len, data_decrypted, pdata_decrypted_len);
  115. WriteLog("Dll Decrypt, ret=%d", ret);
  116. }
  117. else
  118. {
  119. for(int i =0; i<data_len; i++)
  120. data_decrypted[i] ^=data[i];
  121. *pdata_decrypted_len =data_len;
  122. ret =data_len;
  123. WriteLog("Local Decrypt, ret=%d", ret);
  124. }
  125. return ret;
  126. }