Skip to content

Commit a499948

Browse files
committed
Add MkDir module and tests
1 parent f5d2860 commit a499948

File tree

5 files changed

+200
-0
lines changed

5 files changed

+200
-0
lines changed

modules/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_subdirectory(SpawnAs)
2222
add_subdirectory(StealToken)
2323
add_subdirectory(Upload)
2424
add_subdirectory(Cat)
25+
add_subdirectory(MkDir)
2526
add_subdirectory(Tree)
2627
add_subdirectory(WmiExec)
2728
add_subdirectory(KeyLogger)

modules/MkDir/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include_directories(../)
2+
add_library(MkDir SHARED MkDir.cpp)
3+
set_property(TARGET MkDir PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded")
4+
target_link_libraries(MkDir )
5+
add_custom_command(TARGET MkDir POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
6+
$<TARGET_FILE:MkDir> "${CMAKE_SOURCE_DIR}/Release/Modules/$<TARGET_FILE_NAME:MkDir>")
7+
8+
if(WITH_TESTS)
9+
add_executable(testsMkDir tests/testsMkDir.cpp MkDir.cpp)
10+
target_link_libraries(testsMkDir )
11+
add_custom_command(TARGET testsMkDir POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy
12+
$<TARGET_FILE:testsMkDir> "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testsMkDir>")
13+
add_test(NAME testsMkDir COMMAND "${CMAKE_SOURCE_DIR}/Tests/$<TARGET_FILE_NAME:testsMkDir>")
14+
endif()

modules/MkDir/MkDir.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "MkDir.hpp"
2+
3+
#include <filesystem>
4+
#include <cstring>
5+
6+
#include "Common.hpp"
7+
8+
using namespace std;
9+
namespace fs = std::filesystem;
10+
11+
constexpr std::string_view moduleName = "mkDir";
12+
constexpr unsigned long long moduleHash = djb2(moduleName);
13+
14+
#ifdef _WIN32
15+
__declspec(dllexport) MkDir* MkDirConstructor()
16+
{
17+
return new MkDir();
18+
}
19+
#else
20+
__attribute__((visibility("default"))) MkDir* MkDirConstructor()
21+
{
22+
return new MkDir();
23+
}
24+
#endif
25+
26+
MkDir::MkDir()
27+
#ifdef BUILD_TEAMSERVER
28+
: ModuleCmd(std::string(moduleName), moduleHash)
29+
#else
30+
: ModuleCmd("", moduleHash)
31+
#endif
32+
{
33+
}
34+
35+
MkDir::~MkDir()
36+
{
37+
}
38+
39+
std::string MkDir::getInfo()
40+
{
41+
std::string info;
42+
#ifdef BUILD_TEAMSERVER
43+
info += "mkDir Module:\n";
44+
info += "Create a new directory on the target system.\n";
45+
info += "\nUsage:\n";
46+
info += " - mkDir <path>\n";
47+
#endif
48+
return info;
49+
}
50+
51+
int MkDir::init(std::vector<std::string> &splitedCmd, C2Message &c2Message)
52+
{
53+
#if defined(BUILD_TEAMSERVER) || defined(BUILD_TESTS)
54+
if (splitedCmd.size() >= 2)
55+
{
56+
std::string path;
57+
for (size_t i = 1; i < splitedCmd.size(); ++i)
58+
{
59+
if (!path.empty())
60+
path += " ";
61+
path += splitedCmd[i];
62+
}
63+
c2Message.set_instruction(splitedCmd[0]);
64+
c2Message.set_cmd(path);
65+
}
66+
else
67+
{
68+
c2Message.set_returnvalue(getInfo());
69+
return -1;
70+
}
71+
#endif
72+
return 0;
73+
}
74+
75+
#define ERROR_CREATE_DIRECTORY 1
76+
77+
int MkDir::process(C2Message &c2Message, C2Message &c2RetMessage)
78+
{
79+
std::string path = c2Message.cmd();
80+
c2RetMessage.set_instruction(c2RetMessage.instruction());
81+
c2RetMessage.set_cmd(path);
82+
std::error_code ec;
83+
bool created = fs::create_directories(path, ec);
84+
if (!ec)
85+
{
86+
c2RetMessage.set_returnvalue(created ? "Directory created." : "Already exists.");
87+
}
88+
else
89+
{
90+
c2RetMessage.set_errorCode(ERROR_CREATE_DIRECTORY);
91+
}
92+
return 0;
93+
}
94+
95+
int MkDir::errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg)
96+
{
97+
#ifdef BUILD_TEAMSERVER
98+
int errorCode = c2RetMessage.errorCode();
99+
if (errorCode > 0)
100+
{
101+
if (errorCode == ERROR_CREATE_DIRECTORY)
102+
errorMsg = "Failed: Could not create directory";
103+
}
104+
#endif
105+
return 0;
106+
}
107+

modules/MkDir/MkDir.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include "ModuleCmd.hpp"
4+
5+
class MkDir : public ModuleCmd
6+
{
7+
public:
8+
MkDir();
9+
~MkDir();
10+
11+
std::string getInfo();
12+
13+
int init(std::vector<std::string>& splitedCmd, C2Message& c2Message);
14+
int process(C2Message& c2Message, C2Message& c2RetMessage);
15+
int errorCodeToMsg(const C2Message &c2RetMessage, std::string& errorMsg);
16+
int osCompatibility()
17+
{
18+
return OS_LINUX | OS_WINDOWS;
19+
}
20+
21+
private:
22+
};
23+
24+
#ifdef _WIN32
25+
extern "C" __declspec(dllexport) MkDir * MkDirConstructor();
26+
#else
27+
extern "C" __attribute__((visibility("default"))) MkDir * MkDirConstructor();
28+
#endif
29+

modules/MkDir/tests/testsMkDir.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "../MkDir.hpp"
2+
#include <filesystem>
3+
4+
bool testMkDir();
5+
6+
int main()
7+
{
8+
bool res;
9+
std::cout << "[+] testMkDir" << std::endl;
10+
res = testMkDir();
11+
if (res)
12+
std::cout << "[+] Sucess" << std::endl;
13+
else
14+
std::cout << "[-] Failed" << std::endl;
15+
16+
return !res;
17+
}
18+
19+
bool testMkDir()
20+
{
21+
namespace fs = std::filesystem;
22+
fs::path temp = fs::temp_directory_path() / "c2core_mkdir_test";
23+
fs::remove_all(temp);
24+
bool ok = true;
25+
26+
{
27+
MkDir mk;
28+
std::vector<std::string> cmd = {"mkDir", temp.string()};
29+
C2Message msg, ret;
30+
mk.init(cmd, msg);
31+
msg.set_cmd(temp.string());
32+
mk.process(msg, ret);
33+
ok &= ret.returnvalue().find("Directory created") != std::string::npos;
34+
ok &= fs::exists(temp);
35+
}
36+
37+
{
38+
MkDir mk;
39+
std::vector<std::string> cmd = {"mkDir", temp.string()};
40+
C2Message msg, ret;
41+
mk.init(cmd, msg);
42+
msg.set_cmd(temp.string());
43+
mk.process(msg, ret);
44+
ok &= ret.returnvalue().find("Already exists") != std::string::npos;
45+
}
46+
47+
fs::remove_all(temp);
48+
return ok;
49+
}

0 commit comments

Comments
 (0)