Files
maxDcb-C2Core/modules/ModuleCmd/CommonCommand.hpp
T
Maxime DE CAUMIA BAILLENX 0a454e8ecd Inital commit
2023-04-26 08:28:46 +02:00

244 lines
5.4 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <random>
#include "ModuleCmd.hpp"
const std::string HelpCmd = "help";
const std::string SleepCmd = "sleep";
const std::string EndCmd = "end";
const std::string ListenerCmd = "listener";
const std::string LoadC2Module = "loadModule";
const std::string UnloadC2Module = "unloadModule";
const std::string ModulesDirectoryFromTeamServer = "../Modules/";
const std::string StartCmd = "start";
const std::string StopCmd = "stop";
const std::string CmdStatusSuccess = "Success";
const std::string CmdStatusFail = "Fail";
class CommonCommands
{
public:
CommonCommands()
{
m_commonCommands.push_back(SleepCmd);
m_commonCommands.push_back(EndCmd);
m_commonCommands.push_back(ListenerCmd);
m_commonCommands.push_back(LoadC2Module);
m_commonCommands.push_back(UnloadC2Module);
}
int getNumberOfCommand()
{
return m_commonCommands.size();
}
std::string getCommand(int idx)
{
if(idx<m_commonCommands.size())
return m_commonCommands[idx];
else
return "";
}
std::string getHelp(std::string cmd)
{
std::string output;
if(cmd==SleepCmd)
{
output = "sleep: \n";
output += "Set the sleep time in sec for the beacon.\n";
output += "exemple:\n";
output += " - sleep 1\n";
}
else if(cmd==EndCmd)
{
output = "end: \n";
output += "Stop the beacon.\n";
output += "exemple:\n";
output += " - end\n";
}
else if(cmd==ListenerCmd)
{
output = "listener: \n";
output += "Start a tcp listener on the beacon.\n";
output += "exemple:\n";
output += " - listener 0.0.0.0 4444\n";
//output += "TODO add start / stop instruction \n";
}
else if(cmd==LoadC2Module)
{
output = "loadModule: \n";
output += "Load module DLL file on the memory of the beacon, giving the beacon this capability.\n";
output += "Load the DLL from the given path, if it's not found try the default ../Modules/ path.";
output += "exemple:\n";
output += " - loadModule /tools/PrintWorkingDirectory.dll \n";
}
else if(cmd==UnloadC2Module)
{
output = "unloadModule: \n";
output += "Unload module DLL loaded by loadModule.\n";
output += "exemple:\n";
output += " - unloadModule assemblyExec \n";
}
return output;
}
// if an error ocurre:
// set_returnvalue(errorMsg) && return -1
int init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
{
std::string instruction = splitedCmd[0];
if(instruction==SleepCmd)
{
if(splitedCmd.size()==2)
{
int sleepTimeSec=5;
try
{
sleepTimeSec = atoi(splitedCmd[1].c_str());
}
catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument: " << ia.what() << '\n';
return -1;
}
c2Message.set_instruction(instruction);
c2Message.set_cmd(std::to_string(sleepTimeSec));
}
else
{
std::string errorMsg = getHelp(instruction);
c2Message.set_returnvalue(errorMsg);
return -1;
}
}
else if(instruction==EndCmd)
{
c2Message.set_instruction(instruction);
c2Message.set_cmd("");
}
else if(instruction==ListenerCmd)
{
if(splitedCmd.size()>=3)
{
if(splitedCmd[1]==StartCmd)
{
if(splitedCmd.size()>=4)
{
std::string host = splitedCmd[2];
int port=4444;
try
{
port = std::atoi(splitedCmd[3].c_str());
}
catch (const std::invalid_argument& ia)
{
std::cerr << "Invalid argument: " << ia.what() << '\n';
return -1;
}
std::string cmd = splitedCmd[1];
cmd+=" ";
cmd+=host;
cmd+=" ";
cmd+=std::to_string(port);
c2Message.set_instruction(instruction);
c2Message.set_cmd(cmd);
}
else
{
std::string errorMsg = "listener start: not enough arguments";
c2Message.set_returnvalue(errorMsg);
return -1;
}
}
else if(splitedCmd[1]==StopCmd)
{
std::string hash = splitedCmd[2];
std::string cmd = splitedCmd[1];
cmd+=" ";
cmd+=hash;
c2Message.set_instruction(instruction);
c2Message.set_cmd(cmd);
}
}
else
{
std::string errorMsg = getHelp(instruction);
c2Message.set_returnvalue(errorMsg);
return -1;
}
}
else if(instruction==LoadC2Module)
{
if (splitedCmd.size() == 2)
{
std::string inputFile = splitedCmd[1];
std::ifstream input;
input.open(inputFile, std::ios::binary);
if(!input)
{
std::string newInputFile = ModulesDirectoryFromTeamServer;
newInputFile+=inputFile;
input.open(newInputFile, std::ios::binary);
}
if( input )
{
std::string buffer(std::istreambuf_iterator<char>(input), {});
c2Message.set_instruction(splitedCmd[0]);
c2Message.set_inputfile(inputFile);
c2Message.set_data(buffer.data(), buffer.size());
}
else
{
c2Message.set_returnvalue("Failed: Couldn't open file.");
return -1;
}
}
else
{
std::string errorMsg = getHelp(instruction);
c2Message.set_returnvalue(errorMsg);
return -1;
}
}
else if(instruction==UnloadC2Module)
{
if (splitedCmd.size() == 2)
{
std::string moduleName = splitedCmd[1];
c2Message.set_instruction(splitedCmd[0]);
c2Message.set_cmd(moduleName);
}
else
{
std::string errorMsg = getHelp(instruction);
c2Message.set_returnvalue(errorMsg);
return -1;
}
}
return 0;
}
private:
std::vector<std::string> m_commonCommands;
};