BlackBone
Windows memory hacking library
 All Classes Functions
Process.h
1 #pragma once
2 
3 #include "ProcessCore.h"
4 #include "ProcessMemory.h"
5 #include "ProcessModules.h"
6 #include "Threads/Threads.h"
7 #include "RPC/RemoteExec.h"
8 #include "RPC/RemoteHook.h"
9 #include "RPC/RemoteLocalHook.h"
10 #include "../ManualMap/Native/NtLoader.h"
11 #include "../ManualMap/MMap.h"
12 
13 #include "../Include/NativeStructures.h"
14 
15 #include <string>
16 #include <list>
17 
18 namespace blackbone
19 {
20 
24 struct ThreadInfo
25 {
26  uint32_t tid = 0;
27  uintptr_t startAddress = 0;
28  bool mainThread = false;
29 };
30 
35 {
36  uint32_t pid = 0;
37  std::wstring imageName;
38  std::vector<ThreadInfo> threads;
39 
40  bool operator < (const ProcessInfo& other)
41  {
42  return this->pid < other.pid;
43  }
44 };
45 
50 {
51  ptr_t size = 0;
52  uint32_t attrib = 0;
53 };
54 
58 struct HandleInfo
59 {
60  HANDLE handle = nullptr;
61  uint32_t access = 0;
62  uint32_t flags = 0;
63  ptr_t pObject = 0;
64 
65  std::wstring typeName;
66  std::wstring name;
67 
68  // Object-specific info
69  std::shared_ptr<SectionInfo> section;
70 };
71 
72 #define DEFAULT_ACCESS_P PROCESS_QUERY_INFORMATION | \
73  PROCESS_VM_READ | \
74  PROCESS_VM_WRITE | \
75  PROCESS_VM_OPERATION | \
76  PROCESS_CREATE_THREAD | \
77  PROCESS_SET_QUOTA | \
78  PROCESS_TERMINATE | \
79  PROCESS_DUP_HANDLE
80 class Process
81 {
82 public:
83  BLACKBONE_API Process();
84  BLACKBONE_API ~Process(void);
85 
92  BLACKBONE_API NTSTATUS Attach( DWORD pid, DWORD access = DEFAULT_ACCESS_P );
93 
99  BLACKBONE_API NTSTATUS Attach( HANDLE hProc );
100 
111  BLACKBONE_API NTSTATUS CreateAndAttach(
112  const std::wstring& path,
113  bool suspended = false,
114  bool forceInit = true,
115  const std::wstring& cmdLine = L"",
116  const wchar_t* currentDir = nullptr,
117  STARTUPINFOW* pStartup = nullptr
118  );
119 
124  BLACKBONE_API NTSTATUS Detach();
125 
130  BLACKBONE_API NTSTATUS EnsureInit();
131 
136  BLACKBONE_API inline DWORD pid() const { return _core.pid(); }
137 
142  BLACKBONE_API bool valid();
143 
149  BLACKBONE_API NTSTATUS Terminate( uint32_t code = 0 );
150 
156  BLACKBONE_API NTSTATUS EnumHandles( std::vector<HandleInfo>& handles );
157 
163  BLACKBONE_API static void EnumByName( const std::wstring& name, std::vector<DWORD>& found );
164 
173  BLACKBONE_API static NTSTATUS EnumByNameOrPID(
174  uint32_t pid,
175  const std::wstring& name,
176  std::vector<ProcessInfo>& found,
177  bool includeThreads = false
178  );
179 
180  //
181  // Subroutines
182  //
183  BLACKBONE_API inline ProcessCore& core() { return _core; } // Core routines and native
184  BLACKBONE_API inline ProcessMemory& memory() { return _memory; } // Memory manipulations
185  BLACKBONE_API inline ProcessModules& modules() { return _modules; } // Module management
186  BLACKBONE_API inline ProcessThreads& threads() { return _threads; } // Threads
187  BLACKBONE_API inline RemoteHook& hooks() { return _hooks; } // Hooking code remotely
188  BLACKBONE_API inline RemoteLocalHook& localHooks() { return _localHooks; } // Hooking code remotely
189  BLACKBONE_API inline RemoteExec& remote() { return _remote; } // Remote code execution
190  BLACKBONE_API inline MMap& mmap() { return _mmap; } // Manual module mapping
191  BLACKBONE_API inline NtLdr& nativeLdr() { return _nativeLdr; } // Native loader routines
192 
193 private:
199  NTSTATUS GrantPriviledge( const std::basic_string<TCHAR>& name );
200 
201  Process(const Process&) = delete;
202  Process& operator =(const Process&) = delete;
203 private:
204  ProcessCore _core; // Core routines and native subsystem
205  ProcessModules _modules; // Module management
206  ProcessMemory _memory; // Memory manipulations
207  ProcessThreads _threads; // Threads
208  RemoteHook _hooks; // Hooking code remotely
209  RemoteLocalHook _localHooks; // In-process remote hooks
210  RemoteExec _remote; // Remote code execution
211  MMap _mmap; // Manual module mapping
212  NtLdr _nativeLdr; // Native loader routines
213 };
214 
215 }
Section object information
Definition: Process.h:49
BLACKBONE_API DWORD pid() const
Get process ID
Definition: ProcessCore.h:37
BLACKBONE_API bool valid()
Checks if process still exists
Definition: Process.cpp:166
Process thread information
Definition: Process.h:24
static BLACKBONE_API void EnumByName(const std::wstring &name, std::vector< DWORD > &found)
Search for process by executable name
Definition: Process.cpp:352
static BLACKBONE_API NTSTATUS EnumByNameOrPID(uint32_t pid, const std::wstring &name, std::vector< ProcessInfo > &found, bool includeThreads=false)
Search for process by executable name or by process ID
Definition: Process.cpp:382
Definition: Process.h:80
BLACKBONE_API NTSTATUS Detach()
Detach form current process, if any
Definition: Process.cpp:134
Process information
Definition: Process.h:34
BLACKBONE_API NTSTATUS EnsureInit()
Ensure LdrInitializeProcess gets called
Definition: Process.cpp:152
Definition: ProcessCore.h:13
BLACKBONE_API NTSTATUS CreateAndAttach(const std::wstring &path, bool suspended=false, bool forceInit=true, const std::wstring &cmdLine=L"", const wchar_t *currentDir=nullptr, STARTUPINFOW *pStartup=nullptr)
Create new process and attach to it
Definition: Process.cpp:82
BLACKBONE_API DWORD pid() const
Get process ID
Definition: Process.h:136
BLACKBONE_API NTSTATUS Attach(DWORD pid, DWORD access=DEFAULT_ACCESS_P)
Attach to existing process
Definition: Process.cpp:39
Definition: AsmHelper32.cpp:6
Process handle information
Definition: Process.h:58
BLACKBONE_API NTSTATUS EnumHandles(std::vector< HandleInfo > &handles)
Enumerate all open handles
Definition: Process.cpp:192
BLACKBONE_API NTSTATUS Terminate(uint32_t code=0)
Terminate process
Definition: Process.cpp:181