Tests: cat download ls powershell and upload

This commit is contained in:
maxdcb
2024-12-06 05:40:32 -05:00
parent c22b160846
commit 2e6c859e97
13 changed files with 375 additions and 56 deletions
+25 -3
View File
@@ -18,11 +18,14 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
bool testCat()
{
std::string testFile = "test1.txt";
std::string fileContent = "testCat";
std::ofstream outfile("test1.txt");
outfile << "testCat" << std::endl;
outfile.close();
@@ -31,7 +34,7 @@ bool testCat()
{
std::vector<std::string> splitedCmd;
splitedCmd.push_back("cat");
splitedCmd.push_back("test1.txt");
splitedCmd.push_back(testFile);
C2Message c2Message;
C2Message c2RetMessage;
@@ -42,7 +45,18 @@ bool testCat()
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
if (c2RetMessage.returnvalue().compare(0, fileContent.length(), fileContent) == 0)
{
}
else
{
std::cout << "[-] fileContent " << fileContent << std::endl;
std::cout << "[-] c2RetMessage.returnvalue() " << c2RetMessage.returnvalue() << std::endl;
return false;
}
}
{
std::vector<std::string> splitedCmd;
splitedCmd.push_back("cat");
@@ -57,8 +71,16 @@ bool testCat()
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
}
if (c2RetMessage.errorCode())
{
std::cout << "[+] c2RetMessage.errorCode() " << c2RetMessage.errorCode() << std::endl;
}
else
{
return false;
}
}
return true;
}
@@ -18,7 +18,7 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
@@ -26,6 +26,21 @@ bool testChangeDirectory()
{
std::unique_ptr<ChangeDirectory> changeDirectory = std::make_unique<ChangeDirectory>();
{
std::vector<std::string> splitedCmd;
splitedCmd.push_back("cd");
splitedCmd.push_back("sdghdfhdfgnjdgf");
C2Message c2Message;
C2Message c2RetMessage;
changeDirectory->init(splitedCmd, c2Message);
changeDirectory->process(c2Message, c2RetMessage);
std::string output = "\n\noutput:\n";
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
}
{
std::vector<std::string> splitedCmd;
splitedCmd.push_back("cd");
+32 -11
View File
@@ -56,12 +56,14 @@ std::string Download::getInfo()
int Download::init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
{
if (splitedCmd.size() == 3)
{
string inputFile = splitedCmd[1];
string outputFile = splitedCmd[2];
std::vector<std::string> quoteRegroupedCmd = regroupStrings(splitedCmd);
c2Message.set_instruction(splitedCmd[0]);
if (quoteRegroupedCmd.size() == 3)
{
string inputFile = quoteRegroupedCmd[1];
string outputFile = quoteRegroupedCmd[2];
c2Message.set_instruction(quoteRegroupedCmd[0]);
c2Message.set_inputfile(inputFile);
c2Message.set_outputfile(outputFile);
}
@@ -74,6 +76,7 @@ int Download::init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
return 0;
}
#define ERROR_OPEN_FILE 1
int Download::process(C2Message &c2Message, C2Message &c2RetMessage)
{
@@ -92,7 +95,7 @@ int Download::process(C2Message &c2Message, C2Message &c2RetMessage)
}
else
{
c2RetMessage.set_returnvalue("Failed: Couldn't open file.");
c2RetMessage.set_errorCode(ERROR_OPEN_FILE);
}
return 0;
@@ -101,12 +104,30 @@ int Download::process(C2Message &c2Message, C2Message &c2RetMessage)
int Download::followUp(const C2Message &c2RetMessage)
{
std::string outputFile = c2RetMessage.outputfile();
std::ofstream output(outputFile, std::ios::binary);
// check if there is an error
if(c2RetMessage.errorCode()==-1)
{
std::string outputFile = c2RetMessage.outputfile();
std::ofstream output(outputFile, std::ios::binary);
const std::string buffer = c2RetMessage.data();
output << buffer;
output.close();
const std::string buffer = c2RetMessage.data();
output << buffer;
output.close();
}
return 0;
}
int Download::errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg)
{
#ifdef BUILD_TEAMSERVER
int errorCode = c2RetMessage.errorCode();
if(errorCode>0)
{
if(errorCode==ERROR_OPEN_FILE)
errorMsg = "Failed: Couldn't open file";
}
#endif
return 0;
}
+1
View File
@@ -15,6 +15,7 @@ public:
int init(std::vector<std::string>& splitedCmd, C2Message& c2Message);
int process(C2Message& c2Message, C2Message& c2RetMessage);
int followUp(const C2Message &c2RetMessage);
int errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg);
private:
+105 -12
View File
@@ -18,22 +18,26 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
bool testDownload()
{
std::ofstream outfile("test2.txt");
outfile << "testDownload" << std::endl;
outfile.close();
std::unique_ptr<Download> download = std::make_unique<Download>();
{
std::string fileName = "test.txt";
std::string outputFileName = "testDownload.txt";
std::string fileContent = "tesDownload";
std::ofstream outfile(fileName);
outfile << fileContent;
outfile.close();
std::vector<std::string> splitedCmd;
splitedCmd.push_back("download");
splitedCmd.push_back("test2.txt");
splitedCmd.push_back("testDownload.txt");
splitedCmd.push_back(fileName);
splitedCmd.push_back(outputFileName);
C2Message c2Message;
C2Message c2RetMessage;
@@ -45,11 +49,100 @@ bool testDownload()
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
std::ifstream myfile(outputFileName);
if (myfile.is_open())
{
std::string buffer(std::istreambuf_iterator<char>(myfile), {});
if (buffer!=fileContent)
{
return false;
}
}
else
{
return false;
}
}
std::ifstream myfile("testDownload.txt");
if (myfile.is_open())
return true;
else
return false;
{
std::string fileName = "test with space.txt";
std::string outputFileName = "testDownload2.txt";
std::string fileContent = "tesDownload2";
std::ofstream outfile(fileName);
outfile << fileContent;
outfile.close();
std::vector<std::string> splitedCmd;
splitedCmd.push_back("download");
splitedCmd.push_back("\"test");
splitedCmd.push_back("with");
splitedCmd.push_back("space.txt\"");
splitedCmd.push_back(outputFileName);
C2Message c2Message;
C2Message c2RetMessage;
download->init(splitedCmd, c2Message);
download->process(c2Message, c2RetMessage);
download->followUp(c2RetMessage);
std::string output = "\n\noutput:\n";
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
std::ifstream myfile(outputFileName);
if (myfile.is_open())
{
std::string buffer(std::istreambuf_iterator<char>(myfile), {});
if (buffer!=fileContent)
{
return false;
}
}
else
{
return false;
}
}
{
std::string fileName = "sdgsdfhkjjhgzetreyixwvccn.txt";
std::string outputFileName = "notHere.txt";
std::vector<std::string> splitedCmd;
splitedCmd.push_back("download");
splitedCmd.push_back(fileName);
splitedCmd.push_back(outputFileName);
C2Message c2Message;
C2Message c2RetMessage;
download->init(splitedCmd, c2Message);
download->process(c2Message, c2RetMessage);
download->followUp(c2RetMessage);
std::string output = "\n\noutput:\n";
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
if (c2RetMessage.errorCode())
{
}
else
{
return false;
}
std::ifstream myfile;
myfile.open(outputFileName, std::ios::binary);
if(myfile)
{
return false;
}
}
}
@@ -18,7 +18,7 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
bool testListDirectory()
+49
View File
@@ -175,3 +175,52 @@ void static inline splitList(std::string list, const std::string& delimiter, std
}
splitedList.push_back(list);
}
static inline std::vector<std::string> regroupStrings(const std::vector<std::string>& input)
{
std::vector<std::string> result;
std::string currentGroup;
bool inQuotes = false;
for (const auto& word : input)
{
if (!inQuotes)
{
// Check if the current word starts a quoted group
if (!word.empty() && word.front() == '"')
{
inQuotes = true;
currentGroup = word; // Start the group
}
else
{
result.push_back(word); // Add non-quoted word directly
}
}
else
{
// Append the word to the current group
currentGroup += " " + word;
// Check if the current word ends the quoted group
if (!word.empty() && word.back() == '"')
{
inQuotes = false;
// remove quotes from the group
currentGroup.erase(std::remove(currentGroup.begin(), currentGroup.end(), '"'), currentGroup.end());
result.push_back(currentGroup); // Add the completed group
currentGroup.clear();
}
}
}
// Handle unclosed quotes (if any)
if (inQuotes)
{
result.push_back(currentGroup);
}
return result;
}
-2
View File
@@ -10,7 +10,5 @@ if(WITH_TESTS)
add_custom_command(TARGET testsPowershell POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
$<TARGET_FILE:testsPowershell> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testsPowershell>")
file(COPY ${CMAKE_SOURCE_DIR}/core/modules/Powershell/tests/HelloWorlModule.ps1 DESTINATION ${CMAKE_SOURCE_DIR}/Tests/)
add_test(NAME testsPowershell COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testsPowershell>")
endif()
@@ -1,7 +0,0 @@
# Define the PrintHelloWorld function
function PrintHelloWorld {
Write-Output "Hello, World!"
}
# Export the function to make it available to other scripts
Export-ModuleMember -Function PrintHelloWorld
+23 -2
View File
@@ -1,5 +1,7 @@
#include "../Powershell.hpp"
#include <fstream>
#ifdef __linux__
#elif _WIN32
#include <windows.h>
@@ -18,7 +20,7 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
@@ -72,10 +74,29 @@ bool testPowershell()
std::cout << output << std::endl;
}
{
std::string scriptFile = "HelloWorlModule.ps1";
std::string script = R"(
# Define the PrintHelloWorld function
function PrintHelloWorld {
Write-Output "Hello, World!"
}
# Export the function to make it available to other scripts
Export-ModuleMember -Function PrintHelloWorld
)";
std::ofstream outFile(scriptFile);
if (!outFile)
{
return false;
}
outFile << script;
outFile.close();
std::vector<std::string> splitedCmd;
splitedCmd.push_back("powershellt");
splitedCmd.push_back("-i");
splitedCmd.push_back("HelloWorlModule.ps1");
splitedCmd.push_back(scriptFile);
C2Message c2Message;
C2Message c2RetMessage;
+21 -5
View File
@@ -56,17 +56,19 @@ std::string Upload::getInfo()
int Upload::init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
{
if (splitedCmd.size() == 3)
std::vector<std::string> quoteRegroupedCmd = regroupStrings(splitedCmd);
if (quoteRegroupedCmd.size() == 3)
{
string inputFile = splitedCmd[1];
string outputFile = splitedCmd[2];
string inputFile = quoteRegroupedCmd[1];
string outputFile = quoteRegroupedCmd[2];
std::ifstream input(inputFile, std::ios::binary);
if( input )
{
std::string buffer(std::istreambuf_iterator<char>(input), {});
c2Message.set_instruction(splitedCmd[0]);
c2Message.set_instruction(quoteRegroupedCmd[0]);
c2Message.set_inputfile(inputFile);
c2Message.set_outputfile(outputFile);
c2Message.set_data(buffer.data(), buffer.size());
@@ -87,6 +89,7 @@ int Upload::init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
return 0;
}
#define ERROR_OPEN_FILE 1
int Upload::process(C2Message &c2Message, C2Message &c2RetMessage)
{
@@ -105,8 +108,21 @@ int Upload::process(C2Message &c2Message, C2Message &c2RetMessage)
}
else
{
c2RetMessage.set_returnvalue("Failed: Couldn't create file.");
c2RetMessage.set_errorCode(ERROR_OPEN_FILE);
}
return 0;
}
int Upload::errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg)
{
#ifdef BUILD_TEAMSERVER
int errorCode = c2RetMessage.errorCode();
if(errorCode>0)
{
if(errorCode==ERROR_OPEN_FILE)
errorMsg = "Failed: Couldn't open file";
}
#endif
return 0;
}
+1
View File
@@ -14,6 +14,7 @@ public:
int init(std::vector<std::string>& splitedCmd, C2Message& c2Message);
int process(C2Message& c2Message, C2Message& c2RetMessage);
int errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg);
private:
+101 -12
View File
@@ -18,21 +18,25 @@ int main()
else
std::cout << "[-] Failed" << std::endl;
return 0;
return !res;
}
bool testUpload()
{
std::ofstream outfile("test1.txt");
outfile << "testUpload" << std::endl;
outfile.close();
std::unique_ptr<Upload> upload = std::make_unique<Upload>();
{
std::string fileName = "testUpload.txt";
std::string outputFileName = "testUpload_.txt";
std::string fileContent = "testUpload";
std::ofstream outfile(fileName);
outfile << fileContent;
outfile.close();
std::vector<std::string> splitedCmd;
splitedCmd.push_back("upload");
splitedCmd.push_back("test1.txt");
splitedCmd.push_back("testUpload.txt");
splitedCmd.push_back(fileName);
splitedCmd.push_back(outputFileName);
C2Message c2Message;
C2Message c2RetMessage;
@@ -43,11 +47,96 @@ bool testUpload()
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
std::ifstream myfile(outputFileName);
if (myfile.is_open())
{
std::string buffer(std::istreambuf_iterator<char>(myfile), {});
if (buffer!=fileContent)
{
return false;
}
}
else
{
return false;
}
}
std::ifstream myfile("testUpload.txt");
if (myfile.is_open())
return true;
else
return false;
{
std::string fileName = "test Upload 2.txt";
std::string outputFileName = "test Upload 2_.txt";
std::string fileContent = "testUpload2";
std::ofstream outfile(fileName);
outfile << fileContent;
outfile.close();
std::vector<std::string> splitedCmd;
splitedCmd.push_back("");
splitedCmd.push_back("\"test"); splitedCmd.push_back("Upload"); splitedCmd.push_back("2.txt\"");
splitedCmd.push_back("\"test"); splitedCmd.push_back("Upload"); splitedCmd.push_back("2_.txt\"");
C2Message c2Message;
C2Message c2RetMessage;
upload->init(splitedCmd, c2Message);
upload->process(c2Message, c2RetMessage);
std::string output = "\n\noutput:\n";
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
std::ifstream myfile(outputFileName);
if (myfile.is_open())
{
std::string buffer(std::istreambuf_iterator<char>(myfile), {});
if (buffer!=fileContent)
{
return false;
}
}
else
{
return false;
}
}
{
std::string fileName = "sdgsdfhkjjhgzetreyixwvccn.txt";
std::string outputFileName = "notHere.txt";
std::vector<std::string> splitedCmd;
splitedCmd.push_back("upload");
splitedCmd.push_back(fileName);
splitedCmd.push_back(outputFileName);
C2Message c2Message;
C2Message c2RetMessage;
upload->init(splitedCmd, c2Message);
upload->process(c2Message, c2RetMessage);
std::string output = "\n\noutput:\n";
output += c2RetMessage.returnvalue();
output += "\n";
std::cout << output << std::endl;
if (c2RetMessage.errorCode())
{
}
else
{
return false;
}
std::ifstream myfile;
myfile.open(outputFileName, std::ios::binary);
if(myfile)
{
return false;
}
}
}