AutoRun.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // AutoRun.cpp : Defines the class behaviors for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "AutoRun.h"
  5. #include "AutoRunDlg.h"
  6. #include <initguid.h>
  7. #include <afxsock.h>//<Winsock2.h>
  8. #include <wincrypt.h>
  9. #include <tlhelp32.h>
  10. #include "DBConfig.h"
  11. #include "Lzari.h"
  12. #define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
  13. #define KEYLENGTH 0x00800000
  14. #define ENCRYPT_ALGORITHM CALG_RC4
  15. #define ENCRYPT_BLOCK_SIZE 8
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. CDatabase g_db;
  22. extern CDatabase *g_curdb;
  23. CString g_mainpath;
  24. CString g_server;
  25. CString g_server2;
  26. CString g_localip;
  27. CAutoRunDlg *g_pMainWnd = NULL;
  28. BOOL g_bSendOK = 0;
  29. SENDHEAD g_sendhead;
  30. CStringArray g_serverarray;
  31. #pragma comment(lib, "Shlwapi.lib")
  32. BOOL isInnerIP(DWORD a_ip)
  33. {
  34. BOOL bValid = 0;
  35. if ((a_ip >> 24 == 0xa) || (a_ip >> 16 == 0xc0a8) || (a_ip >> 22 == 0x2b0))
  36. {
  37. bValid = 1;
  38. }
  39. return bValid;
  40. }
  41. void MyGetIPByName(CString &name)
  42. {
  43. CString strIP = name;
  44. name.Empty();
  45. DWORD dwServerIP = 0;
  46. HOSTENT *host = gethostbyname(strIP);
  47. struct in_addr addr;
  48. if (host != NULL)
  49. {
  50. for (int i = 0; host->h_addr_list[i] != NULL; i++)
  51. {
  52. memset(&addr, 0, sizeof(addr));
  53. memcpy(&addr.S_un.S_addr, host->h_addr_list[i], host->h_length);
  54. dwServerIP = ntohl(addr.S_un.S_addr);
  55. BYTE* pIP = (BYTE*)&dwServerIP;
  56. name.Format(_T("%d.%d.%d.%d"), pIP[3], pIP[2], pIP[1], pIP[0]);
  57. g_serverarray.Add(name);
  58. }
  59. }
  60. while (g_serverarray.GetSize() > 1)
  61. {
  62. CString name;
  63. BOOL bFind = 0;
  64. for (int i = 0; i < g_serverarray.GetSize(); i++)
  65. {
  66. DWORD dwServerIP = inet_addr(g_serverarray.ElementAt(i));
  67. dwServerIP = htonl(dwServerIP);
  68. if (isInnerIP(dwServerIP) == 0)
  69. {
  70. g_serverarray.RemoveAt(i);
  71. bFind = 1;
  72. break;
  73. }
  74. }
  75. if (bFind == 0)
  76. {
  77. break;
  78. }
  79. }
  80. if (g_serverarray.GetSize())
  81. name = g_serverarray.ElementAt(0);
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CAutoRunApp
  85. BEGIN_MESSAGE_MAP(CAutoRunApp, CWinApp)
  86. //{{AFX_MSG_MAP(CAutoRunApp)
  87. // NOTE - the ClassWizard will add and remove mapping macros here.
  88. // DO NOT EDIT what you see in these blocks of generated code!
  89. //}}AFX_MSG
  90. ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  91. END_MESSAGE_MAP()
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CAutoRunApp construction
  94. CAutoRunApp::CAutoRunApp()
  95. {
  96. // TODO: add construction code here,
  97. // Place all significant initialization in InitInstance
  98. }
  99. /////////////////////////////////////////////////////////////////////////////
  100. // The one and only CAutoRunApp object
  101. CAutoRunApp theApp;
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CAutoRunApp initialization
  104. DWORD FindAppProcessID(CString path)
  105. {
  106. try
  107. {
  108. HANDLE handle = ::CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0);
  109. PROCESSENTRY32 Info;
  110. Info.dwSize = sizeof(PROCESSENTRY32);
  111. path.MakeLower();
  112. if (::Process32First(handle, &Info))
  113. {
  114. do
  115. {
  116. CString ss = Info.szExeFile;
  117. ss.MakeLower();
  118. // if(ss.Find (path)!=-1 || (!ss.CompareNoCase(path)) )
  119. if (ss == path)
  120. {
  121. ::CloseHandle(handle);
  122. return Info.th32ProcessID;
  123. }
  124. } while (::Process32Next(handle, &Info));
  125. ::CloseHandle(handle);
  126. }
  127. return -1;
  128. }
  129. catch (...)
  130. {
  131. }
  132. }
  133. BOOL CAutoRunApp::InitInstance()
  134. {
  135. HANDLE hObject = CreateMutex(NULL, FALSE, _T("xdCAutoRunAppXiao"));
  136. if (GetLastError() == ERROR_ALREADY_EXISTS)
  137. {
  138. return false;
  139. }
  140. int nResult = Transport_Init();
  141. if (TRANSPORT_OK != nResult)
  142. {
  143. return false;
  144. }
  145. Global::GetIniInfo();
  146. char server[50];
  147. DWORD leng = 50;
  148. ::GetComputerName(server, &leng);
  149. g_server2 = server;
  150. MyGetIPByName(g_server2);
  151. if (g_server2.GetLength() < 3)
  152. return false;
  153. // if(g_server2.Left (3)!="192" && g_server2.Left (3)!="127")
  154. // return false;
  155. // Standard initialization
  156. // If you are not using these features and wish to reduce the size
  157. // of your final executable, you should remove from the following
  158. // the specific initialization routines you do not need.
  159. CAutoRunDlg dlg;
  160. m_pMainWnd = &dlg;
  161. int nResponse = dlg.DoModal();
  162. if (nResponse == IDOK)
  163. {
  164. // TODO: Place code here to handle when the dialog is
  165. // dismissed with OK
  166. }
  167. else if (nResponse == IDCANCEL)
  168. {
  169. // TODO: Place code here to handle when the dialog is
  170. // dismissed with Cancel
  171. }
  172. // Since the dialog has been closed, return FALSE so that we exit the
  173. // application, rather than start the application's message pump.
  174. return FALSE;
  175. }
  176. int CAutoRunApp::ExitInstance()
  177. {
  178. return CWinApp::ExitInstance();
  179. }
  180. BOOL EncryptFile2(BYTE *buffer, DWORD leng, PCHAR szPassword)
  181. {
  182. try
  183. {
  184. HCRYPTPROV hCryptProv;
  185. HCRYPTKEY hKey;
  186. HCRYPTHASH hHash;
  187. PBYTE pbBuffer;
  188. DWORD dwBlockLen;
  189. DWORD dwBufferLen;
  190. DWORD dwCount;
  191. //以下获得一个CSP句柄
  192. if (CryptAcquireContext(
  193. &hCryptProv,
  194. NULL, //NULL表示使用默认密钥容器,默认密钥容器名为用户登陆名
  195. NULL,
  196. PROV_RSA_FULL,
  197. 0))
  198. {
  199. printf("A cryptographic provider has been acquired. \n");
  200. }
  201. else//密钥容器不存在
  202. {
  203. if (CryptAcquireContext(
  204. &hCryptProv,
  205. NULL,
  206. NULL,
  207. PROV_RSA_FULL,
  208. CRYPT_NEWKEYSET))//创建密钥容器
  209. {
  210. //创建密钥容器成功,并得到CSP句柄
  211. printf("A new key container has been created.\n");
  212. }
  213. else
  214. {
  215. return 0;
  216. }
  217. }
  218. //--------------------------------------------------------------------
  219. // 创建一个会话密钥(session key)
  220. // 会话密钥也叫对称密钥,用于对称加密算法。
  221. // (注: 一个Session是指从调用函数CryptAcquireContext到调用函数
  222. // CryptReleaseContext 期间的阶段。)
  223. //--------------------------------------------------------------------
  224. // Create a hash object.
  225. if (CryptCreateHash(
  226. hCryptProv,
  227. CALG_MD5,
  228. 0,
  229. 0,
  230. &hHash))
  231. {
  232. printf("A hash object has been created. \n");
  233. }
  234. else
  235. {
  236. return 0;
  237. }
  238. //--------------------------------------------------------------------
  239. // 用输入的密码产生一个散列
  240. if (CryptHashData(
  241. hHash,
  242. (BYTE *)szPassword,
  243. strlen(szPassword),
  244. 0))
  245. {
  246. printf("The password has been added to the hash. \n");
  247. }
  248. else
  249. {
  250. return 0;
  251. }
  252. //--------------------------------------------------------------------
  253. // 通过散列生成会话密钥(session key)
  254. if (CryptDeriveKey(
  255. hCryptProv,
  256. ENCRYPT_ALGORITHM,
  257. hHash,
  258. KEYLENGTH,
  259. &hKey))
  260. {
  261. printf("An encryption key is derived from the password hash. \n");
  262. }
  263. else
  264. {
  265. return 0;
  266. }
  267. //--------------------------------------------------------------------
  268. // Destroy the hash object.
  269. CryptDestroyHash(hHash);
  270. hHash = NULL;
  271. //--------------------------------------------------------------------
  272. // The session key is now ready.
  273. //--------------------------------------------------------------------
  274. // 因为加密算法是按ENCRYPT_BLOCK_SIZE 大小的块加密的,所以被加密的
  275. // 数据长度必须是ENCRYPT_BLOCK_SIZE 的整数倍。下面计算一次加密的
  276. // 数据长度。
  277. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
  278. //--------------------------------------------------------------------
  279. // Determine the block size. If a block cipher is used,
  280. // it must have room for an extra block.
  281. if (ENCRYPT_BLOCK_SIZE > 1)
  282. dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE;
  283. else
  284. dwBufferLen = dwBlockLen;
  285. dwCount = dwBufferLen;
  286. //--------------------------------------------------------------------
  287. // In a do loop, encrypt the source file and write to the source file.
  288. int count;
  289. // if(leng%dwBlockLen==0)
  290. count = leng / dwBufferLen;
  291. // else
  292. // count=leng/dwBlockLen+1;
  293. for (int i = 0; i < count; i++)
  294. {
  295. pbBuffer = buffer + i*dwBufferLen;
  296. // 加密数据
  297. if (!CryptEncrypt(
  298. hKey, //密钥
  299. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  300. 0, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  301. //这里通过判断是否到文件尾来决定是否为最后一块。
  302. 0, //保留
  303. pbBuffer, //输入被加密数据,输出加密后的数据
  304. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  305. dwBufferLen)) //pbBuffer的大小。
  306. {
  307. return 0;
  308. }
  309. }
  310. if (leng%dwBlockLen)
  311. {
  312. pbBuffer = buffer + i*dwBufferLen;
  313. dwCount = leng - i*dwBufferLen;
  314. if (!CryptEncrypt(
  315. hKey, //密钥
  316. 0, //如果数据同时进行散列和加密,这里传入一个散列对象
  317. TRUE, //如果是最后一个被加密的块,输入TRUE.如果不是输入FALSE.
  318. //这里通过判断是否到文件尾来决定是否为最后一块。
  319. 0, //保留
  320. pbBuffer, //输入被加密数据,输出加密后的数据
  321. &dwCount, //输入被加密数据实际长度,输出加密后数据长度
  322. dwBufferLen)) //pbBuffer的大小。
  323. {
  324. return 0;
  325. }
  326. }
  327. //--------------------------------------------------------------------
  328. // Destroy session key.
  329. if (hKey)
  330. CryptDestroyKey(hKey);
  331. //--------------------------------------------------------------------
  332. // Destroy hash object.
  333. if (hHash)
  334. CryptDestroyHash(hHash);
  335. //--------------------------------------------------------------------
  336. // Release provider handle.
  337. if (hCryptProv)
  338. CryptReleaseContext(hCryptProv, 0);
  339. return(TRUE);
  340. }
  341. catch (...)
  342. {
  343. }
  344. }
  345. extern CArray<CStringArray, CStringArray>g_List1array;
  346. extern DWORD g_nLeng;
  347. extern BYTE *g_pData;
  348. extern CString g_str;
  349. void DataToArray(CArray<CStringArray, CStringArray>*List1array)
  350. {
  351. List1array->RemoveAll();
  352. if (g_nLeng == 0)return;
  353. if (g_sendhead.code[0])
  354. {
  355. BYTE *lpszOut = NULL;
  356. int nOutSize = 0;
  357. LZARI Lzari;
  358. Lzari.UnCompress(g_pData, g_nLeng, lpszOut, nOutSize);
  359. CMemFile memfile;
  360. memfile.Attach(lpszOut, nOutSize);
  361. Lzari.Release();
  362. CArchive ar(&memfile, CArchive::load);
  363. List1array->SetSize(g_sendhead.count[0]);
  364. for (int ii = 0; ii < List1array->GetSize(); ii++)
  365. {
  366. List1array->ElementAt(ii).Serialize(ar);
  367. }
  368. ar.Close();
  369. memfile.Detach();
  370. }
  371. else
  372. {
  373. CMemFile memfile;
  374. memfile.Attach(g_pData, g_nLeng);
  375. CArchive ar(&memfile, CArchive::load);
  376. List1array->SetSize(g_sendhead.count[0]);
  377. for (int ii = 0; ii < List1array->GetSize(); ii++)
  378. {
  379. List1array->ElementAt(ii).Serialize(ar);
  380. }
  381. ar.Close();
  382. memfile.Detach();
  383. }
  384. }
  385. void DataToArray(CArray<CStringArray, CStringArray>*List1array, CArray<CStringArray, CStringArray>*List2array)
  386. {
  387. CArray<CStringArray, CStringArray>*parray[2] = { List1array, List2array };
  388. DWORD bytereads = 0;
  389. for (int i = 0; i < 2; i++)
  390. {
  391. parray[i]->RemoveAll(); if (g_sendhead.length[i] == 0)continue;
  392. if (g_sendhead.code[i])
  393. {
  394. BYTE *lpszOut = NULL;
  395. int nOutSize = 0;
  396. LZARI Lzari;
  397. Lzari.UnCompress(g_pData + bytereads, g_sendhead.length[i], lpszOut, nOutSize);
  398. CMemFile memfile;
  399. memfile.Attach(lpszOut, nOutSize);
  400. Lzari.Release();
  401. bytereads += g_sendhead.length[i];
  402. CArchive ar(&memfile, CArchive::load);
  403. parray[i]->SetSize(g_sendhead.count[i]);
  404. for (int ii = 0; ii < parray[i]->GetSize(); ii++)
  405. {
  406. parray[i]->ElementAt(ii).Serialize(ar);
  407. }
  408. ar.Close();
  409. memfile.Detach();
  410. }
  411. else
  412. {
  413. CMemFile memfile;
  414. memfile.Attach(g_pData + bytereads, g_sendhead.length[i]);
  415. bytereads += g_sendhead.length[i];
  416. CArchive ar(&memfile, CArchive::load);
  417. parray[i]->SetSize(g_sendhead.count[i]);
  418. for (int ii = 0; ii < parray[i]->GetSize(); ii++)
  419. {
  420. parray[i]->ElementAt(ii).Serialize(ar);
  421. }
  422. ar.Close();
  423. memfile.Detach();
  424. }
  425. }
  426. }