diff --git a/modules/Cat/tests/testsCat.cpp b/modules/Cat/tests/testsCat.cpp index 4558afa..eb21120 100644 --- a/modules/Cat/tests/testsCat.cpp +++ b/modules/Cat/tests/testsCat.cpp @@ -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 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 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; } diff --git a/modules/ChangeDirectory/tests/testsChangeDirectory.cpp b/modules/ChangeDirectory/tests/testsChangeDirectory.cpp index b5b5d69..3f59c7d 100644 --- a/modules/ChangeDirectory/tests/testsChangeDirectory.cpp +++ b/modules/ChangeDirectory/tests/testsChangeDirectory.cpp @@ -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 = std::make_unique(); + { + std::vector 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 splitedCmd; splitedCmd.push_back("cd"); diff --git a/modules/Download/Download.cpp b/modules/Download/Download.cpp index d1fac86..9bd4d6b 100644 --- a/modules/Download/Download.cpp +++ b/modules/Download/Download.cpp @@ -56,12 +56,14 @@ std::string Download::getInfo() int Download::init(std::vector &splitedCmd, C2Message &c2Message) { - if (splitedCmd.size() == 3) - { - string inputFile = splitedCmd[1]; - string outputFile = splitedCmd[2]; + std::vector 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 &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; +} diff --git a/modules/Download/Download.hpp b/modules/Download/Download.hpp index 2448f20..48c0d87 100644 --- a/modules/Download/Download.hpp +++ b/modules/Download/Download.hpp @@ -15,6 +15,7 @@ public: int init(std::vector& splitedCmd, C2Message& c2Message); int process(C2Message& c2Message, C2Message& c2RetMessage); int followUp(const C2Message &c2RetMessage); + int errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg); private: diff --git a/modules/Download/tests/testsDownload.cpp b/modules/Download/tests/testsDownload.cpp index 3de52ca..cd779c4 100644 --- a/modules/Download/tests/testsDownload.cpp +++ b/modules/Download/tests/testsDownload.cpp @@ -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 = std::make_unique(); { + std::string fileName = "test.txt"; + std::string outputFileName = "testDownload.txt"; + std::string fileContent = "tesDownload"; + + std::ofstream outfile(fileName); + outfile << fileContent; + outfile.close(); + std::vector 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(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 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(myfile), {}); + + if (buffer!=fileContent) + { + return false; + } + } + else + { + return false; + } + } + + { + std::string fileName = "sdgsdfhkjjhgzetreyixwvccn.txt"; + std::string outputFileName = "notHere.txt"; + + std::vector 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; + } + } } diff --git a/modules/ListDirectory/tests/testsListDirectory.cpp b/modules/ListDirectory/tests/testsListDirectory.cpp index e004cc6..151af04 100644 --- a/modules/ListDirectory/tests/testsListDirectory.cpp +++ b/modules/ListDirectory/tests/testsListDirectory.cpp @@ -18,7 +18,7 @@ int main() else std::cout << "[-] Failed" << std::endl; - return 0; + return !res; } bool testListDirectory() diff --git a/modules/ModuleCmd/Common.hpp b/modules/ModuleCmd/Common.hpp index 3f04be9..6c1c51b 100644 --- a/modules/ModuleCmd/Common.hpp +++ b/modules/ModuleCmd/Common.hpp @@ -175,3 +175,52 @@ void static inline splitList(std::string list, const std::string& delimiter, std } splitedList.push_back(list); } + + +static inline std::vector regroupStrings(const std::vector& input) +{ + std::vector 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; +} \ No newline at end of file diff --git a/modules/Powershell/CMakeLists.txt b/modules/Powershell/CMakeLists.txt index 4ee37d0..4c2a3d5 100644 --- a/modules/Powershell/CMakeLists.txt +++ b/modules/Powershell/CMakeLists.txt @@ -10,7 +10,5 @@ if(WITH_TESTS) add_custom_command(TARGET testsPowershell POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ "${CMAKE_SOURCE_DIR}/Tests/$") - 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/$") endif() \ No newline at end of file diff --git a/modules/Powershell/tests/HelloWorlModule.ps1 b/modules/Powershell/tests/HelloWorlModule.ps1 deleted file mode 100644 index c95c745..0000000 --- a/modules/Powershell/tests/HelloWorlModule.ps1 +++ /dev/null @@ -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 diff --git a/modules/Powershell/tests/testsPowershell.cpp b/modules/Powershell/tests/testsPowershell.cpp index 049cf71..ba9485e 100644 --- a/modules/Powershell/tests/testsPowershell.cpp +++ b/modules/Powershell/tests/testsPowershell.cpp @@ -1,5 +1,7 @@ #include "../Powershell.hpp" +#include + #ifdef __linux__ #elif _WIN32 #include @@ -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 splitedCmd; splitedCmd.push_back("powershellt"); splitedCmd.push_back("-i"); - splitedCmd.push_back("HelloWorlModule.ps1"); + splitedCmd.push_back(scriptFile); C2Message c2Message; C2Message c2RetMessage; diff --git a/modules/Upload/Upload.cpp b/modules/Upload/Upload.cpp index acee5ef..f1e225a 100644 --- a/modules/Upload/Upload.cpp +++ b/modules/Upload/Upload.cpp @@ -56,17 +56,19 @@ std::string Upload::getInfo() int Upload::init(std::vector &splitedCmd, C2Message &c2Message) { - if (splitedCmd.size() == 3) + std::vector 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(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 &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; +} \ No newline at end of file diff --git a/modules/Upload/Upload.hpp b/modules/Upload/Upload.hpp index 4900d37..845cbfc 100644 --- a/modules/Upload/Upload.hpp +++ b/modules/Upload/Upload.hpp @@ -14,6 +14,7 @@ public: int init(std::vector& splitedCmd, C2Message& c2Message); int process(C2Message& c2Message, C2Message& c2RetMessage); + int errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg); private: diff --git a/modules/Upload/tests/testsUpload.cpp b/modules/Upload/tests/testsUpload.cpp index e63bee7..9fb1ac7 100644 --- a/modules/Upload/tests/testsUpload.cpp +++ b/modules/Upload/tests/testsUpload.cpp @@ -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 = std::make_unique(); { + std::string fileName = "testUpload.txt"; + std::string outputFileName = "testUpload_.txt"; + std::string fileContent = "testUpload"; + + std::ofstream outfile(fileName); + outfile << fileContent; + outfile.close(); + std::vector 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(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 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(myfile), {}); + + if (buffer!=fileContent) + { + return false; + } + } + else + { + return false; + } + } + + { + std::string fileName = "sdgsdfhkjjhgzetreyixwvccn.txt"; + std::string outputFileName = "notHere.txt"; + + std::vector 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; + } + } }