BlackBone
Windows memory hacking library
 All Classes Functions
Thread.h
1 #pragma once
2 
3 #include "../../Config.h"
4 #include "../../Include/Winheaders.h"
5 #include "../../Include/NativeStructures.h"
6 #include "../../Include/Types.h"
7 
8 #include <memory>
9 
10 namespace blackbone
11 {
12 
13 #define DEFAULT_ACCESS_T THREAD_SUSPEND_RESUME | \
14  THREAD_GET_CONTEXT | \
15  THREAD_SET_CONTEXT | \
16  THREAD_QUERY_INFORMATION | \
17  THREAD_TERMINATE | \
18  SYNCHRONIZE
19 
20 #define MAXULONG64_2 ((uint64_t)~((uint64_t)0))
21 #define MAXULONG32_2 ((uint32_t)~((uint32_t)0))
22 
23 
24 // Breakpoint type
25 enum HWBPType
26 {
27  hwbp_access = 3, // Read or write
28  hwbp_write = 1, // Write only
29  hwbp_execute = 0, // Execute only
30 };
31 
32 enum HWBPLength
33 {
34  hwbp_1 = 0, // 1 byte
35  hwbp_2 = 1, // 2 bytes
36  hwbp_4 = 3, // 4 bytes
37  hwbp_8 = 2, // 8 bytes
38 };
39 
40 
44 struct regDR7
45 {
46  //
47  // Local/global enabled index
48  //
49  int l0 : 1;
50  int g0 : 1;
51  int l1 : 1;
52  int g1 : 1;
53  int l2 : 1;
54  int g2 : 1;
55  int l3 : 1;
56  int g3 : 1;
57 
58  int l_enable : 1; // Local breakpoints, obsolete for P6+
59  int g_enable : 1; // Global breakpoints, obsolete for P6+
60  int one : 1; // Reserved
61  int rtm : 1; // Restricted transactional memory
62  int ice : 1; // ICE debugger
63  int gd : 1; // General detect table
64  int tr1 : 1; // Trace 1
65  int tr2 : 1; // Trace2
66 
67  //
68  // Breakpoint type/length
69  //
70  int rw0 : 1;
71  int len0 : 1;
72  int rw1 : 1;
73  int len1 : 1;
74  int rw2 : 1;
75  int len2 : 1;
76  int rw3 : 1;
77  int len3 : 1;
78 
79  inline void setLocal( int idx, int val ) { idx == 0 ? l0 = val : (idx == 1 ? l1 = val : (idx == 2 ? l2 = val : l3 = val)); }
80  inline void setRW ( int idx, int val ) { idx == 0 ? rw0 = val : (idx == 1 ? rw1 = val : (idx == 2 ? rw2 = val : rw3 = val)); }
81  inline void setLen ( int idx, int val ) { idx == 0 ? len0 = val : (idx == 1 ? len1 = val : (idx == 2 ? len2 = val : len3 = val)); }
82 
83  inline bool empty() const { return (l0 | l1 | l2 | l3) ? false : true; }
84  inline int getFreeIndex() const { return !l0 ? 0 : (!l1 ? 1 : (!l2 ? 2 : (!l3 ? 3 : -1))); }
85 };
86 
87 
91 class Thread
92 {
93 public:
94  BLACKBONE_API Thread( DWORD id, class ProcessCore* hProcess, DWORD access = DEFAULT_ACCESS_T );
95  BLACKBONE_API Thread( HANDLE handle, class ProcessCore* hProcess );
96  BLACKBONE_API Thread( const Thread& other );
97  BLACKBONE_API ~Thread();
98 
103  BLACKBONE_API inline DWORD id() const { return _id; }
104 
109  BLACKBONE_API inline HANDLE handle() const { return _handle; }
110 
115  BLACKBONE_API inline bool valid() const { return (_handle != NULL && ExitCode() == STILL_ACTIVE); }
116 
122  BLACKBONE_API ptr_t teb( _TEB32* pteb = nullptr ) const;
123 
129  BLACKBONE_API ptr_t teb( _TEB64* pteb = nullptr ) const;
130 
135  BLACKBONE_API inline ptr_t teb() const { return teb( (TEB_T*)nullptr ); }
136 
141  BLACKBONE_API uint64_t startTime();
142 
147  BLACKBONE_API uint64_t execTime();
148 
153  BLACKBONE_API bool Suspend();
154 
159  BLACKBONE_API bool Resume();
160 
165  BLACKBONE_API bool Suspended();
166 
174  BLACKBONE_API bool GetContext( _CONTEXT32& ctx, DWORD flags = CONTEXT_ALL, bool dontSuspend = false );
175 
183  BLACKBONE_API bool GetContext( _CONTEXT64& ctx, DWORD flags = CONTEXT64_ALL, bool dontSuspend = false );
184 
191  BLACKBONE_API bool SetContext( _CONTEXT32& ctx, bool dontSuspend = false );
192 
199  BLACKBONE_API bool SetContext( _CONTEXT64& ctx, bool dontSuspend = false );
200 
206  BLACKBONE_API bool Terminate( DWORD code = 0 );
207 
213  BLACKBONE_API bool Join( int timeout = INFINITE );
214 
219  BLACKBONE_API DWORD ExitCode() const;
220 
228  BLACKBONE_API int AddHWBP( ptr_t addr, HWBPType type, HWBPLength length );
229 
235  BLACKBONE_API bool RemoveHWBP( int idx );
236 
242  BLACKBONE_API bool RemoveHWBP( ptr_t ptr );
243 
247  BLACKBONE_API void Close();
248 
249  BLACKBONE_API inline bool operator ==(const Thread& other) { return (_id == other._id); }
250 
251  BLACKBONE_API Thread& operator =(const Thread& other)
252  {
253  _id = other._id;
254  _core = other._core;
255  _handle = other._handle;
256 
257  // Transfer handle ownership
258  other._owner = false;
259 
260  return *this;
261  }
262 
263 private:
269  DWORD GetThreadIdT( HANDLE hThread );
270 
271 private:
272  class ProcessCore* _core; // Core routines
273 
274  DWORD _id = 0; // Thread ID
275  HANDLE _handle = NULL; // Thread handle
276  mutable bool _owner = true; // Class owns a handle
277 };
278 
279 }
BLACKBONE_API uint64_t execTime()
Get total execution time(user mode and kernel mode)
Definition: Thread.cpp:345
BLACKBONE_API void Close()
Close handle
Definition: Thread.cpp:359
BLACKBONE_API bool GetContext(_CONTEXT32 &ctx, DWORD flags=CONTEXT_ALL, bool dontSuspend=false)
Get WOW64 thread context
Definition: Thread.cpp:106
DR7 register bitfield
Definition: Thread.h:44
Definition: NativeStructures.h:82
BLACKBONE_API HANDLE handle() const
Get thread handle
Definition: Thread.h:109
BLACKBONE_API bool Suspended()
Check if thread is suspended
Definition: Thread.cpp:89
BLACKBONE_API ptr_t teb() const
Get TEB
Definition: Thread.h:135
BLACKBONE_API bool Join(int timeout=INFINITE)
Join thread
Definition: Thread.cpp:204
BLACKBONE_API bool Terminate(DWORD code=0)
Terminate thread
Definition: Thread.cpp:194
BLACKBONE_API bool Resume()
Resumes thread.
Definition: Thread.cpp:77
BLACKBONE_API DWORD ExitCode() const
Get thread exit code
Definition: Thread.cpp:319
BLACKBONE_API int AddHWBP(ptr_t addr, HWBPType type, HWBPLength length)
Add hardware breakpoint to thread
Definition: Thread.cpp:216
BLACKBONE_API bool valid() const
Check if thread exists
Definition: Thread.h:115
BLACKBONE_API bool Suspend()
Suspend thread
Definition: Thread.cpp:60
BLACKBONE_API bool RemoveHWBP(int idx)
Remove existing hardware breakpoint
Definition: Thread.cpp:257
Definition: ProcessCore.h:13
BLACKBONE_API DWORD id() const
Get thread ID
Definition: Thread.h:103
BLACKBONE_API uint64_t startTime()
Get thread creation time
Definition: Thread.cpp:331
BLACKBONE_API bool SetContext(_CONTEXT32 &ctx, bool dontSuspend=false)
Set WOW64 thread context
Definition: Thread.cpp:155
Thread management
Definition: Thread.h:91
Definition: NativeStructures.h:498
Definition: AsmHelper32.cpp:6
Definition: NativeStructures.h:468