Advertisement
Kostiggig

mybestcode

Jun 22nd, 2023
727
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.90 KB | None | 0 0
  1.  
  2. #include "framework.h"
  3. #include "FourthWindowsProgram.h"
  4.  
  5. #include <windows.h>
  6. #include <iostream>
  7. #include <conio.h>
  8. #include <fstream>
  9. #include <streambuf>
  10. #include <thread>
  11. #include <ctime>
  12. #include <sstream>
  13. #include <string>
  14.  
  15. #pragma warning(disable:4996)
  16.  
  17. #define APP_NAME L"FourthWindowsProgram"
  18. #define HARDWARE_UNIQUE_ID_LENGTH 7
  19. #define RELATIVE_PATH_TO_HARDWARE_UNIQUE_ID "\\hardwareId.txt"
  20.  
  21. using namespace std;
  22.  
  23. /* Call if you need to remove program from autoRun */
  24. void RemoveFromAutoRun() {
  25.     HKEY hkey = HKEY_CURRENT_USER;
  26.     RegOpenKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\Currentversion\\Run", &hkey);
  27.     RegDeleteValue(hkey, APP_NAME);
  28.     RegCloseKey(hkey);
  29. }
  30.  
  31. typedef void (*ProgramPathCallback)(string);
  32.  
  33. void getProgramAbsolutePath(ProgramPathCallback callback1, ProgramPathCallback callback2) {
  34.     char myPath[_MAX_PATH + 1];
  35.     wchar_t wtext[MAX_PATH];
  36.     mbstowcs(wtext, myPath, strlen(myPath) + 1);
  37.     LPWSTR ptr = wtext;
  38.     GetModuleFileName(NULL, wtext, _MAX_PATH);
  39.     wstring ws(wtext);
  40.     string programPath = string(ws.begin(), ws.end());
  41.    
  42.     callback1(programPath);
  43.     callback2(programPath);
  44.  
  45.     // to get rid of crash error: variable 'ws' is corrupted
  46.     while (1);
  47. }
  48.  
  49. void ExecuteMalwareCode() {
  50.     int var = 100;
  51.     while (1) {
  52.         ::Sleep(1000);
  53.         ofstream myFile;
  54.         myFile.open(L"C:\\Users\\Kostya\\Desktop\\test\\logger2.txt");
  55.         myFile << ++var;
  56.     }
  57. }
  58.  
  59. string getAbsolutePathProgramDir(string path) {
  60.     string lastFolder = "";
  61.     int endIndex = 0;
  62.     bool isCollecting = false;
  63.     for (int i = path.length(); i >= 0; i--) {
  64.         string currChar(1, path[i]);
  65.         if (currChar == "\\") break;
  66.         endIndex++;
  67.     }
  68.  
  69.     string copyPath = path;
  70.     return copyPath.substr(0, copyPath.length() - endIndex);;
  71. }
  72.  
  73. string getHardwareId(const int len) {
  74.     static const char alphanum[] =
  75.         "0123456789"
  76.         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  77.         "abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-:;/?";
  78.     string tmp_s;
  79.     tmp_s.reserve(len);
  80.  
  81.     for (int i = 0; i < len; ++i) {
  82.         tmp_s += alphanum[rand() % (sizeof(alphanum) - 1)];
  83.     }
  84.  
  85.     return tmp_s;
  86. }
  87.  
  88. string getHardwareIdFromFile(string path) {
  89.     string folderWithProgram = getAbsolutePathProgramDir(path);
  90.     string uniqueHardwareIdFilePath = folderWithProgram + RELATIVE_PATH_TO_HARDWARE_UNIQUE_ID;
  91.  
  92.     ifstream file(uniqueHardwareIdFilePath);
  93.     string id = "";
  94.     getline(file, id);
  95.     return id;
  96. }
  97.  
  98. void CreateFileWithUniqueId(string path) {
  99.     string hardwareId = getHardwareIdFromFile(path);
  100.     if (hardwareId.length() < HARDWARE_UNIQUE_ID_LENGTH) {
  101.         string folderWithProgram = getAbsolutePathProgramDir(path);
  102.         string uniqueHardwareIdFilePath = folderWithProgram + RELATIVE_PATH_TO_HARDWARE_UNIQUE_ID;
  103.         string hardwareId = getHardwareId(HARDWARE_UNIQUE_ID_LENGTH);
  104.  
  105.         ofstream myFile;
  106.         myFile.open(uniqueHardwareIdFilePath);
  107.         myFile << hardwareId;
  108.         myFile.close();
  109.     }
  110. }
  111.  
  112. void AddToAutoRunCallbackAndExecute(string path)
  113. {
  114.     wstring progPath(path.begin(), path.end());
  115.     HKEY hkey = NULL;
  116.     LONG createStatus = RegCreateKey(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &hkey);
  117.     LONG status = RegSetValueEx(hkey, APP_NAME, 0, REG_SZ, (BYTE*)progPath.c_str(), (progPath.size() + 1) * sizeof(wchar_t));
  118.     ExecuteMalwareCode();
  119. }
  120.  
  121. void AddToAutoRunAndExecute() {
  122.     getProgramAbsolutePath(&AddToAutoRunCallbackAndExecute, &CreateFileWithUniqueId);
  123. }
  124.  
  125. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  126.     _In_opt_ HINSTANCE hPrevInstance,
  127.     _In_ LPWSTR    lpCmdLine,
  128.     _In_ int       nCmdShow
  129. )
  130. {
  131.     UNREFERENCED_PARAMETER(hPrevInstance);
  132.     UNREFERENCED_PARAMETER(lpCmdLine);
  133.  
  134.     srand((unsigned)time(NULL) * getpid());
  135.     AddToAutoRunAndExecute();
  136.  
  137.     return 0;
  138. }
  139.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement