Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Antonio Jesus Pelaez Priego
- Create a program which connects to a FTP server with a user and password.
- The server address and credentials should be received from the command line (argv).
- You have to enumerate all the files with the extension .txt from FTP server, you have to open each file
- in order to search for the lines witch starts with http, and ends in .exe
- You have to download all of these executables, and execute them. (CreateProcess, with the local path).
- */
- #include "stdafx.h"
- #include <windows.h>
- #include <wininet.h>
- #include <iostream>
- #include <string>
- #include <fstream>
- #include "atlbase.h"
- #include "atlstr.h"
- #include "comutil.h"
- #pragma comment( lib, "wininet" )
- using namespace std;
- int main(){
- HANDLE h = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
- HANDLE h1 = InternetConnect(h, _T("localhost"), 21, _T("so"), _T("123456"), INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
- if (!h1) {
- cout << "Error InternetConnect " << GetLastError() << endl;
- InternetCloseHandle(h);
- InternetCloseHandle(h1);
- return 1;
- }
- WIN32_FIND_DATA fd;
- HANDLE h2 = FtpFindFirstFile(h1, _T("*.txt"), &fd, NULL, INTERNET_FLAG_RELOAD);
- if (!h2) {
- cout << "Error FtpFindFirstFile " << GetLastError() << endl;
- return 1;
- }
- do {
- //wcout << fd.cFileName << endl;
- DWORD bytesRead = 0;
- char * buffer = new char[fd.nFileSizeLow];
- HANDLE h3 = FtpOpenFile(h1, fd.cFileName, GENERIC_READ, FTP_TRANSFER_TYPE_UNKNOWN, INTERNET_FLAG_RELOAD);
- if (!h3) {
- cout << "Error FtpOpenFile " << GetLastError() << endl;
- return 1;
- }
- while (InternetReadFile(h3, buffer, fd.nFileSizeLow, &bytesRead) && bytesRead != 0)
- {
- int size = fd.nFileSizeLow;
- //Gettings the http.....exe
- int numExes = 0;
- if (size > 8) {
- int pos1 = 0;
- for (int i = 0; (i <= size); i++) {
- if ((buffer[i] == '\n') || buffer[i] == EOF) {
- if ((buffer[pos1] == 'h') && (buffer[pos1 + 1] == 't') && (buffer[pos1 + 2] == 't') && (buffer[pos1 + 3] == 'p')) {
- if ((buffer[i - 2] == 'e') && (buffer[i - 3] == 'x') && (buffer[i - 4] == 'e') && (buffer[i - 5] == '.')) {
- string exeUrl = "";
- for (int j = pos1; j < i - 1; j++)
- exeUrl +=buffer[j];
- //Downloading the exe
- cout << exeUrl << endl;
- wstring stemp = std::wstring(exeUrl.begin(), exeUrl.end());
- LPCWSTR sw = stemp.c_str();
- URL_COMPONENTS url_c;
- url_c.dwStructSize = (DWORD)sizeof(URL_COMPONENTS);
- url_c.lpszExtraInfo = new TCHAR[1024];
- url_c.dwExtraInfoLength = (DWORD)1024;
- url_c.lpszHostName = new TCHAR[1024];
- url_c.dwHostNameLength = 1024;
- url_c.lpszPassword = new TCHAR[1024];
- url_c.dwPasswordLength = 1024;
- url_c.lpszScheme = new TCHAR[1024];
- url_c.dwSchemeLength = 1024;
- url_c.lpszUrlPath = new TCHAR[1024];
- url_c.dwUrlPathLength = 1024;
- url_c.lpszUserName = new TCHAR[1024];
- url_c.dwUserNameLength = 1024;
- if (!InternetCrackUrl(sw, 0, 0, &url_c)) {
- cout << "error InternetCrackUrl -- > " << GetLastError() << endl;
- }
- wcout << url_c.lpszHostName << endl;
- PCTSTR mimeTypes[] = { _T("text/*"),NULL };
- HANDLE hx = InternetOpen(_T("MyAgent"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
- if (!hx) {
- cout << "error hx -- > " << GetLastError() << endl;
- }
- HANDLE hx1 = InternetConnect(hx, url_c.lpszHostName, 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL);
- if (!hx1) {
- cout << "error hx1 -- > " << GetLastError() << endl;
- }
- HANDLE hx2 = HttpOpenRequest(hx1, _T("GET"), url_c.lpszUrlPath, NULL, NULL, mimeTypes, 0, 0);
- if (!hx2) {
- cout << "error hx2 -- > " << GetLastError() << endl;
- }
- wcout << url_c.lpszUrlPath << endl;
- if (!HttpSendRequest(hx2, NULL, 0, NULL, 0)) {
- cout << "error HttpSendRequest -- > " << GetLastError() << endl;
- }
- //*******************************************/////////////
- // Download the binary file
- fstream myfile;
- string name = "executable" + to_string(numExes) + ".exe" ;
- numExes++;
- myfile = fstream(name, ios::out | ios::binary);
- char *cBuffer = new char[12345678];
- DWORD dwBytesRead = 0;
- while (InternetReadFile(hx2, cBuffer, sizeof(cBuffer), &dwBytesRead) && dwBytesRead > 0)
- {
- myfile.write(cBuffer, dwBytesRead);
- }
- myfile.close();
- delete[] cBuffer;
- //Executing the .exe
- STARTUPINFO si;
- PROCESS_INFORMATION pi;
- ZeroMemory(&si, sizeof(si));
- si.cb = sizeof(si);
- ZeroMemory(&pi, sizeof(pi));
- USES_CONVERSION_EX;
- LPWSTR szCmdline = A2W_EX(name.c_str());
- if(!CreateProcess(NULL, szCmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)){
- cout << "error CreateProcess -- > " << GetLastError() << endl;
- }
- // Wait until child process exits.
- /****************************************************/
- WaitForSingleObject(pi.hProcess, INFINITE);
- CloseHandle(pi.hProcess);
- CloseHandle(pi.hThread);
- InternetCloseHandle(hx2);
- InternetCloseHandle(hx1);
- InternetCloseHandle(hx);
- }
- }
- pos1 = i + 1;
- }
- }
- }
- }
- delete[] buffer;
- InternetCloseHandle(h3);
- } while (InternetFindNextFile(h2, &fd));
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement