Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <openssl/rsa.h>
- #include <openssl/pem.h>
- #include <openssl/fernet.h>
- #include <openssl/sha.h>
- #include <openssl/rand.h>
- #include <openssl/evp.h>
- #include <openssl/bio.h>
- #include <openssl/buffer.h>
- #include <openssl/md5.h>
- #include <openssl/err.h>
- #include <zip.h>
- using namespace std;
- // Function to display help menu with examples
- void display_help() {
- cout << "Usage: secure_tool [option]\n"
- << "Options:\n"
- << " -h, --help Display this help menu\n"
- << " -u, --upload FILE Upload a file for processing\n"
- << "Examples:\n"
- << " secure_tool -u input.txt\n";
- }
- // Generate or load RSA keys
- pair<RSA*, RSA*> generate_rsa_keypair(int key_size = 4096) {
- RSA *private_key = RSA_generate_key(key_size, RSA_F4, nullptr, nullptr);
- RSA *public_key = RSAPublicKey_dup(private_key);
- return make_pair(private_key, public_key);
- }
- // Save RSA key to a file
- void save_rsa_key(RSA *key, const char *filename, const char *passphrase = nullptr) {
- BIO *bio = BIO_new_file(filename, "wb");
- if (!bio) {
- throw runtime_error("Error opening file for writing: " + string(filename));
- }
- unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> evp_key(EVP_PKEY_new(), EVP_PKEY_free);
- EVP_PKEY_set1_RSA(evp_key.get(), key);
- if (passphrase) {
- if (!PEM_write_bio_PKCS8PrivateKey(bio, evp_key.get(), EVP_aes_256_cbc(), nullptr, 0, nullptr, passphrase)) {
- throw runtime_error("Error writing private key to file");
- }
- } else {
- if (!PEM_write_bio_RSAPrivateKey(bio, key, nullptr, nullptr, 0, nullptr, nullptr)) {
- throw runtime_error("Error writing private key to file");
- }
- }
- }
- // Load RSA key from a file
- RSA* load_rsa_key(const char *filename, const char *passphrase = nullptr) {
- BIO *bio = BIO_new_file(filename, "rb");
- if (!bio) {
- throw runtime_error("Error opening file for reading: " + string(filename));
- }
- RSA *key = nullptr;
- unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> evp_key(PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, const_cast<char*>(passphrase)), EVP_PKEY_free);
- if (evp_key) {
- key = EVP_PKEY_get1_RSA(evp_key.get());
- }
- BIO_free(bio);
- return key;
- }
- // Encrypt a file using Fernet symmetric encryption
- void encrypt_file(const char *file_path, const char *output_path, const string &fernet_key) {
- FILE *file = fopen(file_path, "rb");
- FILE *output_file = fopen(output_path, "wb");
- if (!file || !output_file) {
- throw runtime_error("Error opening files for encryption");
- }
- unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
- unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
- FERNET_CTX *ctx = FERNET_init(fernet_key.c_str());
- if (!ctx) {
- throw runtime_error("Error initializing Fernet context");
- }
- FERNET_encrypt_file(ctx, file, output_file);
- FERNET_cleanup(ctx);
- }
- // Decrypt a file using Fernet symmetric encryption
- void decrypt_file(const char *file_path, const char *output_path, const string &fernet_key) {
- FILE *file = fopen(file_path, "rb");
- FILE *output_file = fopen(output_path, "wb");
- if (!file || !output_file) {
- throw runtime_error("Error opening files for decryption");
- }
- unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
- unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
- FERNET_CTX *ctx = FERNET_init(fernet_key.c_str());
- if (!ctx) {
- throw runtime_error("Error initializing Fernet context");
- }
- FERNET_decrypt_file(ctx, file, output_file);
- FERNET_cleanup(ctx);
- }
- // Sign a file with the RSA private key
- void sign_file(const char *file_path, RSA *private_key) {
- FILE *file = fopen(file_path, "rb");
- if (!file) {
- throw runtime_error("Error opening file for signing");
- }
- unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
- fseek(file, 0, SEEK_END);
- long file_size = ftell(file```cpp
- );
- fseek(file, 0, SEEK_SET);
- unique_ptr<unsigned char[]> data(new unsigned char[file_size]);
- fread(data.get(), 1, file_size, file);
- fclose(file);
- unique_ptr<unsigned char[]> signature(new unsigned char[RSA_size(private_key)]);
- unsigned int signature_len;
- if (RSA_sign(NID_sha256, data.get(), file_size, signature.get(), &signature_len, private_key) != 1) {
- throw runtime_error("Error signing file");
- }
- FILE *signature_file = fopen((string(file_path) + ".sig").c_str(), "wb");
- if (!signature_file) {
- throw runtime_error("Error opening file for signature");
- }
- unique_ptr<FILE, decltype(&fclose)> signature_file_guard(signature_file, fclose);
- fwrite(signature.get(), 1, signature_len, signature_file);
- }
- // Verify the digital signature of a file
- bool verify_signature(const char *file_path, RSA *public_key) {
- FILE *file = fopen(file_path, "rb");
- if (!file) {
- throw runtime_error("Error opening file for signature verification");
- }
- unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
- fseek(file, 0, SEEK_END);
- long file_size = ftell(file);
- fseek(file, 0, SEEK_SET);
- unique_ptr<unsigned char[]> data(new unsigned char[file_size]);
- fread(data.get(), 1, file_size, file);
- fclose(file);
- FILE *signature_file = fopen((string(file_path) + ".sig").c_str(), "rb");
- if (!signature_file) {
- throw runtime_error("Error opening signature file");
- }
- unique_ptr<FILE, decltype(&fclose)> signature_file_guard(signature_file, fclose);
- fseek(signature_file, 0, SEEK_END);
- long signature_size = ftell(signature_file);
- fseek(signature_file, 0, SEEK_SET);
- unique_ptr<unsigned char[]> signature(new unsigned char[signature_size]);
- fread(signature.get(), 1, signature_size, signature_file);
- int result = RSA_verify(NID_sha256, data.get(), file_size, signature.get(), signature_size, public_key);
- return result == 1;
- }
- // Calculate the hash of a file
- string calculate_hash(const char *file_path) {
- FILE *file = fopen(file_path, "rb");
- if (!file) {
- throw runtime_error("Error opening file for hash calculation");
- }
- unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
- SHA256_CTX sha256_ctx;
- SHA256_Init(&sha256_ctx);
- unsigned char buffer[65536];
- size_t bytes_read;
- while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
- SHA256_Update(&sha256_ctx, buffer, bytes_read);
- }
- unsigned char hash[SHA256_DIGEST_LENGTH];
- SHA256_Final(hash, &sha256_ctx);
- stringstream ss;
- for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
- ss << hex << setw(2) << setfill('0') << static_cast<int>(hash[i]);
- }
- return ss.str();
- }
- // Encrypt a Fernet key with RSA public key
- string encrypt_fernet_key(const string &fernet_key, RSA *public_key) {
- size_t encrypted_key_len = RSA_size(public_key);
- unique_ptr<unsigned char[]> encrypted_key(new unsigned char[encrypted_key_len]);
- int result = RSA_public_encrypt(fernet_key.size(), reinterpret_cast<const unsigned char *>(fernet_key.c_str()), encrypted_key.get(), public_key, RSA_PKCS1_OAEP_PADDING);
- if (result <= 0) {
- throw runtime_error("Error encrypting Fernet key");
- }
- return string(reinterpret_cast<const char *>(encrypted_key.get()), result);
- }
- // Decrypt a Fernet key with RSA private key
- string decrypt_fernet_key(const string &encrypted_key, RSA *private_key) {
- size_t decrypted_key_len = RSA_size(private_key);
- unique_ptr<unsigned char[]> decrypted_key(new unsigned char[decrypted_key_len]);
- int result = RSA_private_decrypt(encrypted_key.size(), reinterpret_cast<const unsigned char *>(encrypted_key.c_str()), decrypted_key.get(), private_key, RSA_PKCS1_OAEP_PADDING);
- if (result <= 0) {
- throw runtime_error("Error decrypting Fernet key");
- }
- return string(reinterpret_cast<const char *>(decrypted_key.get()), result);
- }
- // Encrypt multiple files into a single archive
- void encrypt_multiple_files(const vector<string> &file_paths, const char *output_archive, const string &fernet_key, const char *payload = nullptr) {
- zip_t *archive = zip_open(output_archive, ZIP_CREATE | ZIP_TRUNCATE, nullptr);
- if (!archive) {
- throw runtime_error("Error creating archive");
- }
- unique_ptr<zip_t, decltype(&zip_close)> archive_guard(archive, zip_close);
- for (const auto &file_path : file_paths) {
- zip_source_t *source = zip_source_file(archive, file_path.c_str(), 0, 0);
- if (!source) {
- throw runtime_error("Error creating source for file: " + string(file_path));
- }
- zip_file_add(archive, file_path.c_str(), source, ZIP_FL_OVERWRITE);
- }
- if (payload) {
- zip_source_t *payload_source = zip_source_buffer(archive, payload, strlen(payload), 0);
- if (!payload_source) {
- throw runtime_error("Error creating source for payload");
- }
- zip_file_add(archive, "payload.txt", payload_source, ZIP_FL_OVERWRITE);
- }
- zip_close(archive);
- encrypt_file(output_archive, (string(output_archive) + ".enc").c_str(), fernet_key.c_str());
- remove(output_archive);
- }
- // Decrypt a single archive containing multiple files
- void decrypt_multiple_files(const char *archive_path, const char *output_dir, const string &fernet_key) {
- decrypt_file(archive_path, (string(archive_path) + ".dec").c_str(), fernet_key.c_str());
- zip_t *archive = zip_open((string(archive_path) + ".dec").c_str(), 0, nullptr);
- if (!archive) {
- throw runtime_error("Error opening decrypted archive");
- }
- unique_ptr<zip_t, decltype(&zip_close)> archive_guard(archive, zip_close);
- for (zip_int64_t i = 0; i < zip_get_num_entries(archive, 0); ++i) {
- zip_stat_t stat;
- zip_stat_index(archive, i, 0, &stat);
- zip_file_t *file = zip_fopen_index(archive, i, 0);
- if (!file) {
- throw runtime_error("Error opening file in decrypted archive: " + string(stat.name));
- }
- string output_path = string(output_dir) + "/" + stat.name;
- FILE *output_file = fopen(output_path.c_str(), "wb");
- if (!output_file) {
- throw runtime_error("Error opening output file: " + output_path);
- }
- unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
- char buffer[65536];
- zip_fread```cpp
- (file, buffer, sizeof(buffer));
- fwrite(buffer, 1, zip_fread(file, buffer, sizeof(buffer)), output_file);
- }
- }
- // Function to bind an EXE to media
- void bind_exe_to_media(const char *media_file, const char *exe_file, const char *output_media_file) {
- FILE *media = fopen(media_file, "rb");
- FILE *exe = fopen(exe_file, "rb");
- FILE *output_media = fopen(output_media_file, "wb");
- if (!media || !exe || !output_media) {
- throw runtime_error("Error opening files for binding");
- }
- unique_ptr<FILE, decltype(&fclose)> media_guard(media, fclose);
- unique_ptr<FILE, decltype(&fclose)> exe_guard(exe, fclose);
- unique_ptr<FILE, decltype(&fclose)> output_media_guard(output_media, fclose);
- fseek(media, 0, SEEK_END);
- long media_size = ftell(media);
- fseek(media, 0, SEEK_SET);
- fseek(exe, 0, SEEK_END);
- long exe_size = ftell(exe);
- fseek(exe, 0, SEEK_SET);
- unique_ptr<unsigned char[]> media_data(new unsigned char[media_size]);
- fread(media_data.get(), 1, media_size, media);
- unique_ptr<unsigned char[]> exe_data(new unsigned char[exe_size]);
- fread(exe_data.get(), 1, exe_size, exe);
- fwrite(media_data.get(), 1, media_size, output_media);
- fwrite(exe_data.get(), 1, exe_size, output_media);
- }
- // Function to extract the EXE from bound media
- void extract_exe_from_media(const char *bound_media_file, const char *output_exe_file) {
- FILE *media = fopen(bound_media_file, "rb");
- FILE *output_exe = fopen(output_exe_file, "wb");
- if (!media || !output_exe) {
- throw runtime_error("Error opening files for extraction");
- }
- unique_ptr<FILE, decltype(&fclose)> media_guard(media, fclose);
- unique_ptr<FILE, decltype(&fclose)> output_exe_guard(output_exe, fclose);
- fseek(media, 0, SEEK_END);
- long media_size = ftell(media);
- fseek(media, 0, SEEK_SET);
- unique_ptr<unsigned char[]> media_data(new unsigned char[media_size]);
- fread(media_data.get(), 1, media_size, media);
- const char *exe_magic_header = "MZ";
- const char *exe_start = strstr(reinterpret_cast<const char *>(media_data.get()), exe_magic_header);
- if (exe_start) {
- fwrite(exe_start, 1, media_size - (exe_start - reinterpret_cast<const char *>(media_data.get())), output_exe);
- } else {
- cout << "No EXE data found in the bound media." << endl;
- }
- }
- // Main function
- int main(int argc, char *argv[]) {
- try {
- if (argc != 3) {
- cerr << "Invalid number of arguments." << endl;
- display_help();
- return 1;
- }
- string option(argv[1]);
- if (option == "-h" || option == "--help") {
- display_help();
- return 0;
- } else if (option == "-u" || option == "--upload") {
- string file_path(argv[2]);
- // Perform file upload processing
- // ...
- } else {
- cerr << "Invalid option: " << option << endl;
- display_help();
- return 1;
- }
- } catch (const exception &e) {
- cerr << "An error occurred: " << e.what() << endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement