-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcom_inject.h
595 lines (505 loc) · 18.8 KB
/
com_inject.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//
// Process Injection via COM
//
#pragma once
#ifndef UNICODE
#define UNICODE
#endif
#include <winsdkver.h>
#define WINVER 0x0601
#define _WIN32_WINNT 0x0601
#include <sdkddkver.h>
#define _WIN32_DCOM
#include <windows.h>
#include <shlwapi.h>
#include <hstring.h>
#include <tlhelp32.h>
#include <ctxtcall.h>
#include <objidl.h>
#include <winternl.h>
#include <objbase.h>
#include <comsvcs.h>
#include <shlobj.h>
#include <dbghelp.h>
#include <wbemidl.h>
#include <shlguid.h>
#include <cstdio>
#include <string>
#include <vector>
#include <deque>
#include <iostream>
#include <fstream>
#include <sstream>
#include <locale>
#include <codecvt>
//#include <filesystem>
#include <iomanip>
#ifdef _WIN64
#define __TARGET_ARCH__ "64"
#else
#define __TARGET_ARCH__ "32"
#endif
typedef struct tagSOleTlsData {
/* 0x0000 */ void* pvThreadBase;
/* 0x0008 */ void* pSmAllocator;
/* 0x0010 */ ULONG dwApartmentID;
/* 0x0014 */ ULONG dwFlags;
/* 0x0018 */ LONG TlsMapIndex;
/* 0x0020 */ void** ppTlsSlot;
/* 0x0028 */ ULONG cComInits;
/* 0x002c */ ULONG cOleInits;
/* 0x0030 */ ULONG cCalls;
/* 0x0038 */ void* pServerCall;
/* 0x0040 */ void* pCallObjectCache;
/* 0x0048 */ void* pContextStack;
/* 0x0050 */ void* pObjServer;
/* 0x0058 */ ULONG dwTIDCaller;
/* 0x0060 */ void* pCurrentCtxForNefariousReaders;
/* 0x0068 */ void* pCurrentContext;
} SOleTlsData;
typedef enum _PLM_TASKCOMPLETION_CATEGORY_FLAGS {
PT_TC_NONE = 0x0,
PT_TC_PBM = 0x1,
PT_TC_FILEOPENPICKER = 0x2,
PT_TC_SHARING = 0x4,
PT_TC_PRINTING = 0x8,
PT_TC_GENERIC = 0x10,
PT_TC_CAMERA_DCA = 0x20,
PT_TC_PRINTER_DCA = 0x40,
PT_TC_PLAYTO = 0x80,
PT_TC_FILESAVEPICKER = 0x100,
PT_TC_CONTACTPICKER = 0x200,
PT_TC_CACHEDFILEUPDATER_LOCAL = 0x400,
PT_TC_CACHEDFILEUPDATER_REMOTE = 0x800,
PT_TC_ERROR_REPORT = 0x2000,
PT_TC_DATA_PACKAGE = 0x4000,
PT_TC_CRASHDUMP = 0x10000,
PT_TC_STREAMEDFILE = 0x20000,
PT_TC_PBM_COMMUNICATION = 0x80000,
PT_TC_HOSTEDAPPLICATION = 0x100000,
PT_TC_MEDIA_CONTROLS_ACTIVE = 0x200000,
PT_TC_EMPTYHOST = 0x400000,
PT_TC_SCANNING = 0x800000,
PT_TC_ACTIONS = 0x1000000,
PT_TC_KERNEL_MODE = 0x20000000,
PT_TC_REALTIMECOMM = 0x40000000,
PT_TC_IGNORE_NAV_LEVEL_FOR_CS = 0x80000000
} PLM_TASKCOMPLETION_CATEGORY_FLAGS;
static const CLSID
CLSID_OSTaskCompletion = {
0x07fc2b94,
0x5285,
0x417e,
{ 0x8a, 0xc3, 0xc2, 0xce, 0x52, 0x40, 0xb0, 0xfa } };
static const IID
IID_IOSTaskCompletion = {
0xc7e40572,
0xc36a,
0x43ea,
{ 0x9a, 0x40, 0xf3, 0xb1, 0x68, 0xda, 0x55, 0x58 } };
static const IID
IID_ITaskCompletionCallback = {
0xe3a475cf,
0x34ea,
0x4e9a,
{ 0x9f, 0x3e, 0x48, 0xce, 0x5c, 0x6e, 0x4e, 0x57 } };
MIDL_INTERFACE("E3A475CF-34EA-4E9A-9F3E-48CE5C6E4E57")
ITaskCompletionCallback : public IUnknown{
STDMETHOD(Proc3)(int p0, int p1);
};
static const CLSID
CLSID_CoreShellComServerRegistrar = {
0x54e14197,
0x88b0,
0x442f,
{ 0xb9, 0xa3, 0x86, 0x83, 0x70, 0x61, 0xe2, 0xfb } };
static const IID
IID_ICoreShellComServerRegistrar = {
0x27eb33a5,
0x77f9,
0x4afe,
{ 0xae, 0x05, 0x6f, 0xdb, 0xbe, 0x72, 0x0e, 0xe7 } };
MIDL_INTERFACE("27EB33A5-77F9-4AFE-AE05-6FDBBE720EE7")
ICoreShellComServerRegistrar : public IUnknown{
STDMETHOD(RegisterCOMServer) (REFCLSID rclsid,
LPUNKNOWN pUnk,
PDWORD ServerTag);
STDMETHOD(UnregisterCOMServer) (DWORD ServerTag);
STDMETHOD(DuplicateHandle) (DWORD dwSourceProcessId,
HANDLE SourceHandle,
DWORD dwTargetProcessId,
LPHANDLE lpTargetHandle,
DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD dwOptions);
STDMETHOD(OpenProcess) (DWORD dwDesiredAccess,
BOOL bInheritHandle,
DWORD SourceProcessId,
DWORD TargetProcessId,
LPHANDLE lpTargetHandle);
STDMETHOD(GetAppIdFromProcessId) (DWORD dwProcessId,
HSTRING* AppId);
STDMETHOD(CoreQueryWindowService) (HWND hWindowHandle,
GUID* GuidInfo,
IUnknown** IUnknownInterface);
STDMETHOD(CoreQueryWindowServiceEx) (HWND hWindowHandle,
HWND hHandle,
GUID* GuidInfo,
IUnknown** IUnknownInterface);
STDMETHOD(GetUserContextForProcess) (DWORD dwProcessId,
LUID* ContextId);
STDMETHOD(BeginTaskCompletion) (DWORD dwProcessId,
ITaskCompletionCallback* pTaskCompletionCallback,
PLM_TASKCOMPLETION_CATEGORY_FLAGS Flags,
PDWORD TaskId);
STDMETHOD(EndTaskCompletion) (DWORD TaskId);
};
enum IPIDFlags {
IPIDF_CONNECTING = 0x1,
IPIDF_DISCONNECTED = 0x2,
IPIDF_SERVERENTRY = 0x4,
IPIDF_NOPING = 0x8,
IPIDF_COPY = 0x10,
IPIDF_VACANT = 0x80,
IPIDF_NONNDRSTUB = 0x100,
IPIDF_NONNDRPROXY = 0x200,
IPIDF_NOTIFYACT = 0x400,
IPIDF_TRIED_ASYNC = 0x800,
IPIDF_ASYNC_SERVER = 0x1000,
IPIDF_DEACTIVATED = 0x2000,
IPIDF_WEAKREFCACHE = 0x4000,
IPIDF_STRONGREFCACHE = 0x8000,
IPIDF_UNSECURECALLSALLOWED = 0x10000
};
typedef struct tagMInterfacePointer {
ULONG ulCntData;
BYTE abData[1];
} MInterfacePointer;
typedef MInterfacePointer* PMInterfacePointer;
typedef __int64 OXID;
typedef unsigned hyper OID;
typedef REFGUID REFIPID;
struct IPID {
WORD offset; // These are reversed because of little-endian
WORD page; // These are reversed because of little-endian
WORD pid;
WORD tid;
BYTE seq[8];
};
typedef struct tagSTDOBJREF {
ULONG flags;
ULONG cPublicRefs;
OXID oxid;
OID oid;
IPID ipid;
} STDOBJREF;
typedef struct tagREMQIRESULT {
HRESULT hResult;
STDOBJREF std;
} REMQIRESULT;
typedef struct tagREMINTERFACEREF {
IPID ipid;
ULONG cPublicRefs;
ULONG cPrivateRefs;
} REMINTERFACEREF;
typedef __int64 PTRMEM;
typedef struct tagXAptCallback {
PTRMEM pfnCallback; // what to execute. e.g. LoadLibraryA, EtwpCreateEtwThread
PTRMEM pParam; // parameter to callback.
PTRMEM pServerCtx; // combase!g_pMTAEmptyCtx
PTRMEM pUnk; // Not required
GUID iid; // Not required
int iMethod; // Not required
GUID guidProcessSecret; // combase!CProcessSecret::s_guidOle32Secret
} XAptCallback;
static const IID IID_IRundown = {
0x00000134,
0x0000,
0x0000,
{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} };
//
// Used on most recent builds of Windows.
//
MIDL_INTERFACE("00000134-0000-0000-C000-000000000046")
IRundown : public IUnknown{
STDMETHOD(RemQueryInterface) (REFIPID ripid,
ULONG cRefs,
USHORT cIids,
IID * iids,
REMQIRESULT * *ppQIResults);
STDMETHOD(RemAddRef) (USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[],
HRESULT* pResults);
STDMETHOD(RemRelease) (USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[]);
STDMETHOD(RemQueryInterface2) (REFIPID ripid,
USHORT cIids,
IID* piids,
HRESULT* phr,
MInterfacePointer** ppMIFs);
STDMETHOD(AcknowledgeMarshalingSets) (USHORT cMarshalingSets,
ULONG_PTR* pMarshalingSets);
STDMETHOD(RemChangeRef) (ULONG flags,
USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[]);
STDMETHOD(DoCallback) (XAptCallback* pParam);
STDMETHOD(DoNonreentrantCallback) (XAptCallback* pParam);
STDMETHOD(GetInterfaceNameFromIPID) (IPID* ipid,
HSTRING* Name);
STDMETHOD(RundownOid) (ULONG cOid,
OID aOid[],
BYTE aRundownStatus[]);
};
//
// Used on legacy systems (Vista, Windows 7, Windows 2008)
//
MIDL_INTERFACE("00000134-0000-0000-C000-000000000046")
IRundownLegacy : public IUnknown{
STDMETHOD(RemQueryInterface) (REFIPID ripid,
ULONG cRefs,
USHORT cIids,
IID * iids,
REMQIRESULT * *ppQIResults);
STDMETHOD(RemAddRef) (USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[],
HRESULT* pResults);
STDMETHOD(RemRelease) (USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[]);
STDMETHOD(RemQueryInterface2) (REFIPID ripid,
USHORT cIids,
IID* piids,
HRESULT* phr,
MInterfacePointer** ppMIFs);
STDMETHOD(RemChangeRef) (ULONG flags,
USHORT cInterfaceRefs,
REMINTERFACEREF InterfaceRefs[]);
STDMETHOD(DoCallback) (XAptCallback* pParam);
STDMETHOD(RundownOid) (ULONG cOid,
OID aOid[],
BYTE aRundownStatus[]);
};
static const IID
IID_IMarshalEnvoy = {
0x000001C8,
0x0000,
0x0000,
{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46} };
MIDL_INTERFACE("000001C8-0000-0000-C000-000000000046")
IMarshalEnvoy : public IUnknown{
// IMarshalEnvoy
STDMETHOD(GetEnvoyUnmarshalClass)(DWORD dwDestContext, CLSID * pclsid);
STDMETHOD(GetEnvoySizeMax) (DWORD dwDestContext, DWORD* pcb);
STDMETHOD(MarshalEnvoy) (IStream* pstm, DWORD dwDestContext);
STDMETHOD(UnmarshalEnvoy) (IStream* pstm, REFIID riid, void** ppv);
};
struct tagPageEntry {
tagPageEntry* pNext;
unsigned int dwFlag;
};
struct CInternalPageAllocator {
ULONG64 _cPages;
tagPageEntry** _pPageListStart;
tagPageEntry** _pPageListEnd;
UINT _dwFlags;
tagPageEntry _ListHead;
UINT _cEntries;
ULONG64 _cbPerEntry;
USHORT _cEntriesPerPage;
void* _pLock;
};
// CPageAllocator CIPIDTable::_palloc structure in combase.dll
struct CPageAllocator {
CInternalPageAllocator _pgalloc;
PVOID _hHeap;
ULONG64 _cbPerEntry;
INT _lNumEntries;
};
typedef struct tagIPIDEntry {
struct tagIPIDEntry* pNextIPID; // next IPIDEntry for same object
DWORD dwFlags; // flags (see IPIDFLAGS)
ULONG cStrongRefs; // strong reference count
ULONG cWeakRefs; // weak reference count
ULONG cPrivateRefs; // private reference count
void* pv; // real interface pointer
IUnknown* pStub; // proxy or stub pointer
void* pOXIDEntry; // ptr to OXIDEntry in OXID Table
IPID ipid; // interface pointer identifier
IID iid; // interface iid
void* pChnl; // channel pointer
void* pIRCEntry; // reference cache line
HSTRING* pInterfaceName;
struct tagIPIDEntry* pOIDFLink; // In use OID list
struct tagIPIDEntry* pOIDBLink;
} IPIDEntry;
struct tagCTXVERSION {
SHORT ThisVersion;
SHORT MinVersion;
};
struct tagCTXCOMMONHDR {
GUID ContextId;
DWORD Flags;
DWORD Reserved;
DWORD dwNumExtents;
DWORD cbExtents;
DWORD MshlFlags;
};
struct tagBYREFHDR {
DWORD Reserved;
DWORD ProcessId;
GUID guidProcessSecret;
PVOID pServerCtx; // CObjectContext
};
struct tagBYVALHDR {
ULONG Count;
BOOL Frozen;
} CTXBYVALHDR;
struct tagCONTEXTHEADER {
tagCTXVERSION Version;
tagCTXCOMMONHDR CmnHdr;
union {
tagBYVALHDR ByValHdr;
tagBYREFHDR ByRefHdr;
};
};
typedef struct tagSTRINGBINDING
{
unsigned short wTowerId;
unsigned short aNetworkAddr;
} STRINGBINDING;
#define COM_C_AUTHZ_NONE ( 0xffff )
typedef struct tagSECURITYBINDING
{
unsigned short wAuthnSvc;
unsigned short wAuthzSvc;
unsigned short aPrincName;
} SECURITYBINDING;
typedef struct tagDUALSTRINGARRAY
{
unsigned short wNumEntries;
unsigned short wSecurityOffset;
/* [size_is] */ unsigned short aStringArray[1];
} DUALSTRINGARRAY;
#define OBJREF_SIGNATURE ( 0x574f454d )
#define OBJREF_STANDARD ( 0x1 )
#define OBJREF_HANDLER ( 0x2 )
#define OBJREF_CUSTOM ( 0x4 )
#define OBJREF_EXTENDED ( 0x8 )
#define SORF_OXRES1 ( 0x1 )
#define SORF_OXRES2 ( 0x20 )
#define SORF_OXRES3 ( 0x40 )
#define SORF_OXRES4 ( 0x80 )
#define SORF_OXRES5 ( 0x100 )
#define SORF_OXRES6 ( 0x200 )
#define SORF_OXRES7 ( 0x400 )
#define SORF_OXRES8 ( 0x800 )
#define SORF_NULL ( 0 )
#define SORF_NOPING ( 0x1000 )
typedef struct tagDATAELEMENT
{
GUID dataID;
unsigned long cbSize;
unsigned long cbRounded;
/* [size_is] */ BYTE Data[1];
} DATAELEMENT;
typedef struct tagOBJREFDATA
{
unsigned long nElms;
/* [unique][size_is][size_is] */ DATAELEMENT** ppElmArray;
} OBJREFDATA;
typedef struct tagOBJREF
{
unsigned long signature;
unsigned long flags;
GUID iid;
/* [switch_type][switch_is] */ union
{
/* [case()] */ struct
{
STDOBJREF std;
DUALSTRINGARRAY saResAddr;
} u_standard;
/* [case()] */ struct
{
STDOBJREF std;
CLSID clsid;
DUALSTRINGARRAY saResAddr;
} u_handler;
/* [case()] */ struct
{
CLSID clsid;
unsigned long cbExtension;
unsigned long size;
/* [ref][size_is] */ byte* pData;
} u_custom;
/* [case()] */ struct
{
STDOBJREF std;
/* [unique] */ OBJREFDATA* pORData;
DUALSTRINGARRAY saResAddr;
} u_extended;
} u_objref;
} OBJREF;
//
// Holds information about the location of data required to invoke IRundown::DoCallback()
//
typedef struct _COM_CONTEXT {
DWORD pid; // Process ID
std::wstring name; // Process name
std::wstring path; // Full path of DLL or shellcode.
bool inject_pic; // Inject shellcode.
bool inject_dll; // Inject DLL into notepad.
bool list_ipid, verbose; // List IRundown instances or all COM.
bool use_objref; // If true, use CoGetObject() to bind with IRundown instance.
PBYTE base; // GetModuleHandle("combase"); or GetModuleHandle("ole32");
DWORD data; // VirtualAddress of .data segment
DWORD size; // VirtualSize
DWORD secret; // CProcessSecret::s_guidOle32Secret
DWORD server_ctx; // g_pMTAEmptyCtx
DWORD ipid_tbl; // CIPIDTable::_palloc
DWORD oxid; // offsetof(tagOXIDEntry, OXID)
} COM_CONTEXT, * PCOM_CONTEXT;
//
// Holds information about an IRundown interface in remote COM process.
//
typedef struct _IPID_ENTRY {
IID iid;
IPID ipid; // IPID to bind to
OXID oxid; // Object Exporter ID
OID oid;
} IPID_ENTRY, * PIPID_ENTRY;
//
// Holds information about an instance of IRundown in remote COM process and
// information required to invoke the DoCallback method.
//
typedef struct _RUNDOWN_CONTEXT {
PVOID pfnCallback; // what to execute
PVOID pParam; // parameter to callback function
PVOID pServerCtx; // required for DoCallback validation
GUID guidProcessSecret; // required for DoCallback validation
IPID ipid; // IPID to bind to
OXID oxid; // Object Exporter ID
OID oid; // Object Identifier
} RUNDOWN_CONTEXT, * PRUNDOWN_CONTEXT;
//
// Microsoft removed this prototype from oleacc.h, but the function still exists and works fine.
//
typedef HANDLE(WINAPI* GetProcessHandleFromHwnd_T)(
_In_ HWND hwnd
);
typedef struct _THREAD_BASIC_INFORMATION {
NTSTATUS ExitStatus;
PVOID TebBaseAddress;
CLIENT_ID ClientId;
KAFFINITY AffinityMask;
KPRIORITY Priority;
KPRIORITY BasePriority;
} THREAD_BASIC_INFORMATION, * PTHREAD_BASIC_INFORMATION;
NTSYSAPI
NTSTATUS
NTAPI
RtlAdjustPrivilege(
_In_ ULONG Privilege,
_In_ BOOLEAN Enable,
_In_ BOOLEAN Client,
_Out_ PBOOLEAN WasEnabled
);