Advertisement
vovan333

DProcess.h (DKOM class)

Nov 11th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include "Include.h"
  3.  
  4. using namespace std;
  5.  
  6. namespace asmjs
  7. {
  8.     class DProcess
  9.     {
  10.         HANDLE_TABLE_ENTRY* ExpLookupHandleTableEntry(HANDLE_TABLE* handleTable, HANDLE handle)
  11.         {
  12.             uint64 v2; // rdx@1
  13.             LONGLONG v3; // r8@2
  14.             uint64 result; // rax@4
  15.             uint64 v5;
  16.             uint64 a1 = (uint64)handleTable;
  17.             v2 = (uint64)handle & 0xFFFFFFFFFFFFFFFCui64;
  18.             if (v2 >= *(DWORD*)a1)
  19.             {
  20.                 result = 0i64;
  21.             }
  22.             else
  23.             {
  24.                 v3 = *(uint64*)(a1 + 8);
  25.                 if (*(uint64*)(a1 + 8) & 3)
  26.                 {
  27.                     if ((*(DWORD*)(a1 + 8) & 3) == 1)
  28.                     {
  29.                         v5 = ReadKernel<uint64>(v3 + 8 * (v2 >> 10) - 1);
  30.                         result = v5 + 4 * (v2 & 0x3FF);
  31.                     }
  32.                     else
  33.                     {
  34.                         v5 = ReadKernel<uint64>(ReadKernel<uint64>(v3 + 8 * (v2 >> 19) - 2) + 8 * ((v2 >> 10) & 0x1FF));
  35.                         result = v5 + 4 * (v2 & 0x3FF);
  36.                     }
  37.                 }
  38.                 else
  39.                 {
  40.                     result = v3 + 4 * v2;
  41.                 }
  42.             }
  43.             return (HANDLE_TABLE_ENTRY*)result;
  44.         }
  45.  
  46.         template <class T> T Read(uint64 dirbase, uint64 address)
  47.         {
  48.             T buff;
  49.             Pm->ReadVirtual(dirbase, address, (uint8_t*)&buff, sizeof(T));
  50.             return buff;
  51.         }
  52.  
  53.         template <class T> T ReadKernel(uint64 address)
  54.         {
  55.             return Read<T>(KernelDirbase, address);
  56.         }
  57.  
  58.         template <class T> void Write(uint64 dirbase, uint64 address, T& value)
  59.         {
  60.             Pm->WriteVirtual(dirbase, address, (uint8_t*)&value, sizeof(T));
  61.         }
  62.  
  63.         template <class T> void WriteKernel(uint64 address, T& value)
  64.         {
  65.             Write<T>(KernelDirbase, address, value);
  66.         }
  67.  
  68.         HANDLE_TABLE_ENTRY* GetHandleTableEntry(HANDLE handle)
  69.         {
  70.             auto pHandleTable = ReadKernel<HANDLE_TABLE*>(pEntry + oObjectTable);
  71.             HANDLE_TABLE handleTable = ReadKernel<HANDLE_TABLE>((uint64)pHandleTable);
  72.             return ExpLookupHandleTableEntry(&handleTable, handle);
  73.         }
  74.  
  75.         public:
  76.  
  77.         uint64 pEntry;
  78.         shared_ptr<PhysicalMemory> Pm;
  79.         uint64 KernelDirbase;
  80.  
  81.         uint64 oObjectTable = 0x418;
  82.         uint64 oProcessId = 0x2e0;
  83.         uint64 oProtection = 0x6ca;
  84.  
  85.         DProcess(shared_ptr<PhysicalMemory> physMem, uint64 processId) : Pm(physMem)
  86.         {
  87.             if (processId == null) processId = GetCurrentProcessId();
  88.             pEntry = Pm->GetEProcess(processId);
  89.             KernelDirbase = Pm->GetKernelDirBase();
  90.         }
  91.  
  92.         DProcess(shared_ptr<PhysicalMemory> physMem, string processName) : Pm(physMem)
  93.         {
  94.             uint64 id = GetProcessIdByName(processName);
  95.             if (!id) throw "Process doesn't exist.";
  96.             pEntry = Pm->GetEProcess(id);
  97.             KernelDirbase = Pm->GetKernelDirBase();
  98.         }
  99.  
  100.         static uint64 GetProcessIdByName(string name)
  101.         {
  102.             uint64 pid = false;
  103.             HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  104.             PROCESSENTRY32 entry;
  105.             entry.dwSize = sizeof(entry);
  106.             if (!Process32First(snap, &entry)) return false;
  107.             do
  108.             {
  109.                 if (LC(entry.szExeFile) == LC(W(name)))
  110.                 {
  111.                     pid = entry.th32ProcessID;
  112.                 }
  113.             } while (Process32Next(snap, &entry));
  114.             CloseHandle(snap);
  115.             return pid;
  116.         }
  117.  
  118.         uint64 GetId()
  119.         {
  120.             return ReadKernel<uint64>(pEntry + oProcessId);
  121.         }
  122.  
  123.         void SetId(uint64 id)
  124.         {
  125.             WriteKernel<uint64>(pEntry + oProcessId, id);
  126.         }
  127.  
  128.         WORD GetProtection()
  129.         {
  130.             return ReadKernel<WORD>(pEntry + oProtection);
  131.         }
  132.  
  133.         void SetProtection(WORD protection)
  134.         {
  135.             WriteKernel<WORD>(pEntry + oProtection, protection);
  136.         }
  137.  
  138.         void SetHandleAccess(HANDLE handle, uint64 access)
  139.         {
  140.             auto pEntry = GetHandleTableEntry(handle);
  141.             if (!pEntry) throw "Couldn't get handle entry";
  142.             HANDLE_TABLE_ENTRY entry = ReadKernel<HANDLE_TABLE_ENTRY>((uint64)pEntry);
  143.             entry.GrantedAccess = access;
  144.             WriteKernel<HANDLE_TABLE_ENTRY>((uint64)pEntry, entry);
  145.         }
  146.  
  147.         uint64 GetHandleAccess(HANDLE handle)
  148.         {
  149.             auto pEntry = GetHandleTableEntry(handle);
  150.             if (!pEntry) throw "Couldn't get handle entry";
  151.             HANDLE_TABLE_ENTRY entry = ReadKernel<HANDLE_TABLE_ENTRY>((uint64)pEntry);
  152.             return entry.GrantedAccess;
  153.         }
  154.     };
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement