Advertisement
Guest User

Untitled

a guest
Apr 29th, 2014
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.50 KB | None | 0 0
  1. #include "ntdll.h"
  2. #include <stdio.h>
  3. namespace NT {
  4.     extern "C"{
  5.         NTSTATUS
  6.         NTAPI
  7.         CsrClientCallServer(
  8.         IN PVOID Message,
  9.         IN PVOID,
  10.         IN ULONG Opcode,
  11.         IN ULONG Size
  12.         );
  13.     }
  14. }
  15.  
  16. VOID InformCsrss(HANDLE hProcess, HANDLE hThread, ULONG pid, ULONG tid)
  17. {
  18.     struct CSRSS_MESSAGE
  19.     {
  20.         ULONG Unknown1;
  21.         ULONG Opcode;
  22.         ULONG Status;
  23.         ULONG Unknown2;
  24.     };
  25.     struct
  26.     {
  27.         NT::PORT_MESSAGE PortMessage;
  28.         CSRSS_MESSAGE CsrssMessage;
  29.         PROCESS_INFORMATION ProcessInformation;
  30.         NT::CLIENT_ID Debugger;
  31.         ULONG CreationFlags;
  32.         ULONG VdmInfo[2];
  33.     } csrmsg = {{0}, {0}, {hProcess, hThread, pid, tid}, {0}, 0, {0}};
  34.     NT::CsrClientCallServer(&csrmsg, 0, 0x10000, 0x24);
  35. }
  36. PWSTR CopyEnvironment(HANDLE hProcess)
  37. {
  38.     PWSTR env = GetEnvironmentStringsW();
  39.     ULONG n;
  40.     for (n = 0; env[n] != 0; n += wcslen(env + n) + 1); n *= sizeof *env;
  41.     ULONG m = n;
  42.     PVOID p = 0;
  43.     NT::ZwAllocateVirtualMemory(hProcess, &p, 0, &m,
  44.                                 MEM_COMMIT, PAGE_READWRITE);
  45.     NT::ZwWriteVirtualMemory(hProcess, p, env, n, 0);
  46.     return PWSTR(p);
  47. }
  48. VOID CreateProcessParameters(HANDLE hProcess, NT::PPEB Peb,
  49.                              NT::PUNICODE_STRING ImageFile)
  50. {
  51.     NT::PPROCESS_PARAMETERS pp;
  52.     NT::RtlCreateProcessParameters(&pp, ImageFile, 0, 0, 0, 0, 0, 0, 0, 0);
  53.     pp->Environment = CopyEnvironment(hProcess);
  54.     ULONG n = pp->Size;
  55.     PVOID p = 0;
  56.     NT::ZwAllocateVirtualMemory(hProcess, &p, 0, &n,
  57.                                 MEM_COMMIT, PAGE_READWRITE);
  58.     NT::ZwWriteVirtualMemory(hProcess, p, pp, pp->Size, 0);
  59.     NT::ZwWriteVirtualMemory(hProcess, PCHAR(Peb) + 0x10, &p, sizeof p, 0);
  60.     NT::RtlDestroyProcessParameters(pp);
  61. }
  62. int exec(NT::PUNICODE_STRING name)
  63. {
  64.     HANDLE hProcess, hThread, hSection, hFile;
  65.     NT::OBJECT_ATTRIBUTES oa = {sizeof oa, 0, name, OBJ_CASE_INSENSITIVE};
  66.     NT::IO_STATUS_BLOCK iosb;
  67.     NT::ZwOpenFile(&hFile, FILE_EXECUTE | SYNCHRONIZE, &oa, &iosb,
  68.                    FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT);
  69.     oa.ObjectName = 0;
  70.     NT::ZwCreateSection(&hSection, SECTION_ALL_ACCESS, &oa, 0,
  71.                         PAGE_EXECUTE, SEC_IMAGE, hFile);
  72.     NT::ZwClose(hFile);
  73.     NT::ZwCreateProcess(&hProcess, PROCESS_ALL_ACCESS, &oa,
  74.                         NtCurrentProcess(), TRUE, hSection, 0, 0);
  75.     NT::SECTION_IMAGE_INFORMATION sii;
  76.     NT::ZwQuerySection(hSection, NT::SectionImageInformation,
  77.                        &sii, sizeof sii, 0);
  78.     NT::ZwClose(hSection);
  79.     NT::USER_STACK stack = {0};
  80.     ULONG n = sii.StackReserve;
  81.     NT::ZwAllocateVirtualMemory(hProcess, &stack.ExpandableStackBottom, 0, &n,
  82.                                 MEM_RESERVE, PAGE_READWRITE);
  83.     stack.ExpandableStackBase = PCHAR(stack.ExpandableStackBottom)
  84.         + sii.StackReserve;
  85.     stack.ExpandableStackLimit = PCHAR(stack.ExpandableStackBase)
  86.         - sii.StackCommit;
  87.     n = sii.StackCommit + PAGE_SIZE;
  88.     PVOID p = PCHAR(stack.ExpandableStackBase) - n;
  89.     NT::ZwAllocateVirtualMemory(hProcess, &p, 0, &n,
  90.                                 MEM_COMMIT, PAGE_READWRITE);
  91.     ULONG x; n = PAGE_SIZE;
  92.     NT::ZwProtectVirtualMemory(hProcess, &p, &n,
  93.                                PAGE_READWRITE | PAGE_GUARD, &x);
  94.     NT::CONTEXT context = {CONTEXT_FULL};
  95.     context.SegGs = 0;
  96.     context.SegFs = 0x38;
  97.     context.SegEs = 0x20;
  98.     context.SegDs = 0x20;
  99.     context.SegSs = 0x20;
  100.     context.SegCs = 0x18;
  101.     context.EFlags = 0x3000;
  102.     context.Esp = ULONG(stack.ExpandableStackBase) - 4;
  103.     context.Eip = ULONG(sii.EntryPoint);
  104.     NT::CLIENT_ID cid;
  105.     NT::ZwCreateThread(&hThread, THREAD_ALL_ACCESS, &oa,
  106.                        hProcess, &cid, &context, &stack, TRUE);
  107.     NT::PROCESS_BASIC_INFORMATION pbi;
  108.     NT::ZwQueryInformationProcess(hProcess, NT::ProcessBasicInformation,
  109.                                   &pbi, sizeof pbi, 0);
  110.     CreateProcessParameters(hProcess, pbi.PebBaseAddress, name);
  111.     InformCsrss(hProcess, hThread,
  112.                 ULONG(cid.UniqueProcess), ULONG(cid.UniqueThread));
  113.     NT::ZwResumeThread(hThread, 0);
  114.     NT::ZwClose(hProcess);
  115.     NT::ZwClose(hThread);
  116.     return int(cid.UniqueProcess);
  117. }
  118.  
  119. #pragma comment(linker, "-entry:wmainCRTStartup")
  120. extern "C"
  121. int wmain(int argc, wchar_t *argv[])
  122. {
  123.     NT::UNICODE_STRING ImageFile;
  124.     NT::RtlInitUnicodeString(&ImageFile, argv[1]);
  125.     exec(&ImageFile);
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement