Advertisement
Guest User

Untitled

a guest
Mar 29th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SDKDDKVer.h>
  2. #include <cstdio>
  3. #include <windows.h>
  4. #include <cstddef>
  5. #include <Psapi.h>
  6. #include <winternl.h>
  7. #pragma comment(lib, "ntdll.lib")
  8.  
  9. #define TEMPLATE
  10.  
  11. typedef
  12. HMODULE
  13. (WINAPI *PFN_LOAD_LIBRARY_A)(
  14.     _In_ LPCSTR lpLibFileName
  15.     );
  16.  
  17. typedef
  18. DWORD
  19. (WINAPI *PFN_GET_LAST_ERROR)(
  20.     VOID
  21.     );
  22.  
  23. PFN_LOAD_LIBRARY_A g_LoadLibrary = NULL; //LoadLibraryA
  24. PFN_GET_LAST_ERROR g_GetLastError = NULL; //GetLastError
  25.  
  26. void Init1()
  27. {
  28.     HMODULE hLibrary = LoadLibraryA("kernel32.dll");
  29.     g_LoadLibrary = (PFN_LOAD_LIBRARY_A)GetProcAddress(hLibrary, "LoadLibraryA");
  30.     g_GetLastError = (PFN_GET_LAST_ERROR)GetProcAddress(hLibrary, "GetLastError");
  31. }
  32.  
  33. typedef struct _MY_SHELLCODE {
  34.     PFN_LOAD_LIBRARY_A pfn_LoadLibrary;
  35.     PFN_GET_LAST_ERROR pfn_GetLastError;
  36.     CHAR libraryName[MAX_PATH];
  37.     UCHAR shellCode[100]; //100 bytes should be enough
  38. } MY_SHELLCODE, *PMY_SHELLCODE;
  39.  
  40. const UCHAR myShellcode32[] = {
  41.     0x55,           //push ebp
  42.     0x8b, 0xec,     //mov ebp, esp
  43.     0x56, 0x8b, 0x75, 0x08, 0x8d, 0x46, 0x08,
  44.     0x50, 0x8b, 0x06, 0xff, 0xd0, 0x85, 0xc0, 0x75, 0x0a, 0x8b,
  45.     0x46, 0x04, 0xff, 0xd0, 0x5e, 0x5d, 0xc2, 0x04, 0x00, 0x33,
  46.     0xc0, 0x5e, 0x5d, 0xc2, 0x04, 0x00
  47. };
  48.  
  49. const UCHAR myShellcode64[] = {
  50.     0x40, 0x53, 0x48, 0x83, 0xec, 0x20, 0x48,
  51.     0x8b, 0xd9, 0x48, 0x83, 0xc1, 0x10, 0xff,
  52.     0x13, 0x48, 0x85, 0xc0, 0x75, 0x0c, 0x48,
  53.     0x8b, 0x43, 0x08, 0x48, 0x83, 0xc4, 0x20,
  54.     0x5b, 0x48, 0xff, 0xe0, 0x33, 0xc0, 0x48,
  55.     0x83, 0xc4, 0x20, 0x5b, 0xc3
  56.     //40 53 48 83 ec 20 48 8b d9 48 83 c1 10 ff 13 48 85 c0 75 0c 48 8b 43 08 48 83 c4 20 5b 48 ff e0 33 c0 48 83 c4 20 5b c3
  57. };
  58.  
  59. DWORD WINAPI MyThreadProc(PMY_SHELLCODE shellCode)
  60. {
  61.     const auto hLibrary = shellCode->pfn_LoadLibrary(shellCode->libraryName);
  62.     if (nullptr == hLibrary)
  63.     {
  64. #ifndef TEMPLATE
  65.         printf("Failed to load DLL, error %lu \n", GetLastError());
  66. #endif
  67.         return shellCode->pfn_GetLastError();
  68.     }
  69. #ifndef TEMPLATE
  70.     else
  71.     {
  72.         printf("DLL was loaded at %p \n", hLibrary);
  73.     }
  74. #endif
  75.     return 0;
  76. }
  77.  
  78. void Test1()
  79. {
  80.     DWORD ThreadId;
  81.     const auto LibraryName = "MyDll.dll";
  82.     const auto size = strlen(LibraryName) + 1;
  83.  
  84.     MY_SHELLCODE shellcode;
  85.     shellcode.pfn_LoadLibrary = g_LoadLibrary;
  86.     shellcode.pfn_GetLastError = g_GetLastError;
  87.     memcpy(shellcode.libraryName, LibraryName, size);
  88.  
  89.     const auto hThread = CreateThread(
  90.         nullptr,
  91.         0,
  92.         reinterpret_cast<LPTHREAD_START_ROUTINE>(MyThreadProc),
  93.         &shellcode,
  94.         0,
  95.         &ThreadId);
  96.     if (nullptr == hThread)
  97.     {
  98.         printf("CreateThread failed with error %lu \n", GetLastError());
  99.         return;
  100.     }
  101.     WaitForSingleObject(hThread, INFINITE);
  102.  
  103.     DWORD ExitCode;
  104.     if (GetExitCodeThread(hThread, &ExitCode))
  105.     {
  106.         printf("Thread exited with %d \n", ExitCode);
  107.     }
  108.     else
  109.     {
  110.         printf("GetExitCodeThread failed with error %lu \n", GetLastError());
  111.     }
  112.  
  113.     CloseHandle(hThread);
  114. }
  115.  
  116. void Test2()
  117. {
  118.     STARTUPINFO StartupInfo;
  119.     memset(&StartupInfo, 0, sizeof(STARTUPINFO));
  120.     StartupInfo.cb = sizeof(STARTUPINFO);
  121.     PROCESS_INFORMATION ProcessInformation;
  122.     auto Ret = CreateProcess(
  123.         L"C:\\Windows\\system32\\notepad.exe",
  124.         nullptr,
  125.         nullptr,
  126.         nullptr,
  127.         false,
  128.         CREATE_SUSPENDED,
  129.         nullptr,
  130.         nullptr,
  131.         &StartupInfo,
  132.         &ProcessInformation
  133.     );
  134.     if (FALSE == Ret)
  135.     {
  136.         printf("CreateProcess failed with error %lu \n", GetLastError());
  137.         return;
  138.     }
  139.     // here we need to do an infinite loop in notepad's entry point  EB FE - jump to itself @ jmp @
  140.  
  141.     // process environment block - is undocumented and changing from version to version of Windows
  142.  
  143.     /*
  144.     Module(0) has entrypoint
  145.     hThread
  146.  
  147.     GetThreadContext -> context with rip
  148.     NtQueryInformationThread -> ThreadQuerySetWin32Address
  149.     undocumented.ntinternals.com
  150.  
  151.  
  152.     a save two bytes
  153.     b patch entrypoint (WriteProcessMemory)
  154.     c resume thread
  155.     d patch imports
  156.     e unpatch entrypoint
  157.  
  158.  
  159.  
  160.     next :
  161.     a catch direct x (sample direct x)
  162.     b task manager (add a column, подменить оконную функцию)
  163.     c explorer comctl32.dll - proxy image list 0 - 15
  164.     */
  165.     PVOID StartAddress;
  166.     ULONG ReturnLength;
  167.     const auto status = NtQueryInformationThread(
  168.         ProcessInformation.hThread,
  169.         (THREADINFOCLASS)9,
  170.         &StartAddress,
  171.         sizeof(StartAddress),
  172.         &ReturnLength
  173.     );
  174.     printf("NtQueryInformationThread returns 0x%08x and %p \n", status, StartAddress);
  175.  
  176.     WORD ep_word;
  177.     SIZE_T ep_bytes_read;
  178.     auto ep_read_result = ReadProcessMemory(
  179.         ProcessInformation.hProcess,
  180.         StartAddress,
  181.         &ep_word,
  182.         sizeof(ep_word),
  183.         &ep_bytes_read
  184.     );
  185.  
  186.     if (!ep_read_result) {
  187.         printf("ReadProcessMemory failed with error %lu \n", GetLastError());
  188.         return;
  189.     }
  190.     if (ep_bytes_read != sizeof(ep_word)) {
  191.         printf("Wrong amount of entrypoint bytes is read. Read %u bytes", ep_bytes_read);
  192.         return;
  193.     }
  194.    
  195.     WORD infinite_jump = 0xFEEB;
  196.     SIZE_T ep_bytes_written;
  197.     auto  ep_write_result = WriteProcessMemory(
  198.         ProcessInformation.hProcess,
  199.         StartAddress,
  200.         &infinite_jump,
  201.         sizeof(infinite_jump),
  202.         &ep_bytes_written
  203.     );
  204.     if (!ep_write_result) {
  205.         printf("WriteProcessMemory failed with error %lu \n", GetLastError());
  206.         return;
  207.     }
  208.     if (ep_bytes_written != sizeof(infinite_jump)) {
  209.         printf("Wrong amount of entrypoint bytes is written. Written %u bytes \n", ep_bytes_written);
  210.         return;
  211.     }
  212.  
  213.     //  CONTEXT Context;
  214.     //  if (!GetThreadContext(
  215.     //      ProcessInformation.hThread,
  216.     //      &Context
  217.     //  )) {
  218.     //      printf("GetThreadContext failed %lu \n", GetLastError());
  219.     //      return;
  220.     //  }
  221.     //
  222.     //#ifdef _AMD64_
  223.     //  printf("Address %p \n", Context.Rip);
  224.     //#else
  225.     //  printf("Address %p \n", Context.Eip);
  226.     //
  227.     //#endif
  228.  
  229.     ResumeThread(ProcessInformation.hThread);
  230.     HMODULE Modules[1000];
  231.     DWORD cbneeded;
  232.     DWORD cbneeded_before(0);
  233.     while (true) {
  234.         if (!EnumProcessModulesEx(
  235.             ProcessInformation.hProcess,
  236.             Modules,
  237.             sizeof(Modules),
  238.             &cbneeded,
  239.             LIST_MODULES_ALL
  240.         )) {
  241.             auto err = GetLastError();
  242.             if (ERROR_PARTIAL_COPY == err) {
  243.                 Sleep(100);
  244.                 continue;
  245.             }
  246.             printf("EnumProcessModules failed %lu\n", GetLastError());
  247.             return;
  248.         }
  249.         if (cbneeded > cbneeded_before) {
  250.             cbneeded_before = cbneeded;
  251.             Sleep(100);
  252.             continue;
  253.         }
  254.         break;
  255.     }
  256.  
  257.     if (!(cbneeded / sizeof(HMODULE))) {
  258.         printf("Zero modules returned \n");
  259.     }
  260.  
  261.     // do it with PE parsing
  262.     for (int i = 0; i < cbneeded / sizeof(HMODULE); ++i) {
  263.         printf("%d = %p\n", i, Modules[i]);
  264.         WCHAR filename[MAX_PATH];
  265.         if (GetModuleFileNameEx(
  266.             ProcessInformation.hProcess,
  267.             Modules[i],
  268.             filename,
  269.             MAX_PATH
  270.         )) {
  271.             printf("\t%d = %S\n", i, filename);
  272.         }
  273.     }
  274.  
  275.     const auto LibraryName = "MyDll.dll";
  276.     const auto size = strlen(LibraryName) + 1; //null=terminated character
  277.                                                //(wcslen()+1)*sizeof(wchar_t)
  278.     MY_SHELLCODE shellcode;
  279.     shellcode.pfn_LoadLibrary = g_LoadLibrary;
  280.     shellcode.pfn_GetLastError = g_GetLastError;
  281.     memcpy(shellcode.libraryName, LibraryName, size);
  282. #ifdef _AMD64_
  283.     memcpy(shellcode.shellCode, myShellcode64, sizeof(myShellcode64));
  284. #else
  285.     memcpy(shellcode.shellCode, myShellcode32, sizeof(myShellcode32));
  286. #endif
  287.  
  288.     const auto Size = sizeof(shellcode);
  289.  
  290.     const auto ShellcodeRemote = VirtualAllocEx(
  291.         ProcessInformation.hProcess,
  292.         nullptr,
  293.         Size,
  294.         MEM_COMMIT,
  295.         PAGE_EXECUTE_READWRITE);
  296.     if (nullptr == ShellcodeRemote)
  297.     {
  298.         printf("VirtualAllocEx failed with error %lu \n", GetLastError());
  299.         return;
  300.     }
  301.     SIZE_T SizeWritten = 0;
  302.     Ret = WriteProcessMemory(
  303.         ProcessInformation.hProcess,
  304.         ShellcodeRemote,
  305.         &shellcode,
  306.         Size,
  307.         &SizeWritten
  308.     );
  309.     if (!Ret)
  310.     {
  311.         printf("WriteProcessMemory failed with error %lu \n", GetLastError());
  312.         return;
  313.     }
  314.     else if (Size != SizeWritten)
  315.     {
  316.         printf("WriteProcessMemory %lu != %lu \n", Size, SizeWritten);
  317.         return;
  318.     }
  319.  
  320.     char* ShellcodeRemoteCode =
  321.         ((char*)ShellcodeRemote) +
  322.         offsetof(MY_SHELLCODE, shellCode);
  323.  
  324.     DWORD ThreadId;
  325.     const auto hThread = CreateRemoteThread(
  326.         ProcessInformation.hProcess,
  327.         nullptr,
  328.         0,
  329.         reinterpret_cast<LPTHREAD_START_ROUTINE>(ShellcodeRemoteCode),
  330.         ShellcodeRemote,
  331.         0,
  332.         &ThreadId);
  333.     if (nullptr == hThread)
  334.     {
  335.         printf("CreateThread failed with error %lu \n", GetLastError());
  336.         return;
  337.     }
  338.     WaitForSingleObject(hThread, INFINITE);
  339.  
  340.     DWORD ExitCode;
  341.     if (GetExitCodeThread(hThread, &ExitCode))
  342.     {
  343.         printf("Thread exited with 0x%p \n", (void*)ExitCode);
  344.     }
  345.     else
  346.     {
  347.         printf("GetExitCodeThread failed with error %d \n", GetLastError());
  348.     }
  349.  
  350.     CloseHandle(hThread);
  351.     CloseHandle(ProcessInformation.hProcess);
  352.     CloseHandle(ProcessInformation.hThread);
  353. }
  354.  
  355. int main(int argc, char* argv[])
  356. {
  357.     Init1();
  358.     //Test1();
  359.     Test2();
  360.     return 0;
  361. }
  362.  
  363.  
  364.  
  365. // DLL
  366.  
  367.  
  368. #include <Windows.h>
  369.  
  370. BOOL APIENTRY DllMain(HMODULE hModule,
  371.     DWORD  ul_reason_for_call,
  372.     LPVOID lpReserved
  373. )
  374. {
  375.     switch (ul_reason_for_call)
  376.     {
  377.     case DLL_PROCESS_ATTACH:
  378.         MessageBox(
  379.             NULL,
  380.             L"Attached to process!",
  381.             L"testDLL",
  382.             MB_OK
  383.         );
  384.         break;
  385.     case DLL_THREAD_ATTACH:
  386.     case DLL_THREAD_DETACH:
  387.         break;
  388.     case DLL_PROCESS_DETACH:
  389.         MessageBox(
  390.             NULL,
  391.             L"Detached from process!",
  392.             L"testDLL",
  393.             MB_OK
  394.         );
  395.         break;
  396.     }
  397.     return TRUE;
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement