-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathRdpSettings.cpp
More file actions
1673 lines (1369 loc) · 55.8 KB
/
RdpSettings.cpp
File metadata and controls
1673 lines (1369 loc) · 55.8 KB
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <MsRdpEx/RdpSettings.h>
#include <MsRdpEx/Memory.h>
#include <MsRdpEx/RdpFile.h>
#include <MsRdpEx/RdpInstance.h>
#include <MsRdpEx/Environment.h>
#include <MsRdpEx/NameResolver.h>
#include <MsRdpEx/Detours.h>
#include <intrin.h>
#include "MsRdpEx.h"
#include "TSObjects.h"
#include "ComHelpers.h"
extern "C" const GUID IID_ITSPropertySet;
extern MsRdpEx_mstscax g_mstscax;
extern MsRdpEx_rdclientax g_rdclientax;
static bool g_TSPropertySet_Hooked = false;
static ITSPropertySet_SetBoolProperty Real_ITSPropertySet_SetBoolProperty = NULL;
static ITSPropertySet_GetBoolProperty Real_ITSPropertySet_GetBoolProperty = NULL;
static ITSPropertySet_SetIntProperty Real_ITSPropertySet_SetIntProperty = NULL;
static ITSPropertySet_GetIntProperty Real_ITSPropertySet_GetIntProperty = NULL;
static ITSPropertySet_SetStringProperty Real_ITSPropertySet_SetStringProperty = NULL;
static ITSPropertySet_GetStringProperty Real_ITSPropertySet_GetStringProperty = NULL;
static ITSPropertySet_SetSecureStringProperty Real_ITSPropertySet_SetSecureStringProperty = NULL;
static ITSPropertySet_GetSecureStringProperty Real_ITSPropertySet_GetSecureStringProperty = NULL;
static HRESULT Hook_ITSPropertySet_SetBoolProperty(ITSPropertySet* This, const char* propName, int propValue)
{
HRESULT hr;
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::SetBoolProperty(%s, %d)", propName, propValue);
if (MsRdpEx_StringIEquals(propName, "UsingSavedCreds")) {
// Workaround for "Always prompt for password upon connection" GPO":
// The RDP ActiveX sets the "UsingSavedCreds" to true if the password is set.
// This is obviously a problem for credential injection, so force it to false.
// https://theitbros.com/enable-saved-credentials-usage-rdp/
// https://twitter.com/awakecoding/status/1367953137210957826
// HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services\fPromptForPassword = 1
propValue = 0;
}
if (MsRdpEx_StringIEquals(propName, "ShellMarkRdpSecure")) {
// Workaround for RDP file signature requirement.
// Always mark RDP files as "signed" even when they're not
propValue = 1;
}
if (MsRdpEx_StringIEquals(propName, "ShowRedirectionWarningDialog")) {
// Workaround for RDP file signature requirement.
// Disable the redirection warning dialog to avoid some checks.
propValue = 0;
}
hr = Real_ITSPropertySet_SetBoolProperty(This, propName, propValue);
return hr;
}
static int g_InitializeKDCProxyClient = 0;
static HRESULT Hook_ITSPropertySet_GetBoolProperty(ITSPropertySet* This, const char* propName, int* propValue)
{
HRESULT hr;
hr = Real_ITSPropertySet_GetBoolProperty(This, propName, propValue);
// KDC proxy client hack: oh, the things we wouldn't do for Kerberos!
// CTscSslFilter::InitializeKDCProxyClient doesn't set KDCProxyName unless TSGTransportIsUsed is true
// CTsConnectionInfoDlg::GetExpandedInfoString crashes if we set TSGTransportIsUsed true when it's not
// CTscSslFilter::OnConnected checks IgnoreAuthenticationLevel, NegotiateSecurityLayer right before calling
// CTscSslFilter::InitializeKDCProxyClient, so use this to our advantage to filter out undesired call sites.
// We use a basic g_InitializeKDCProxyClient state machine, checking for caller DLLs, and hope for the best.
if (MsRdpEx_StringIEquals(propName, "IgnoreAuthenticationLevel") &&
MsRdpEx_IsAddressInRdpAxModule(_ReturnAddress())) {
if (g_InitializeKDCProxyClient == 0) {
g_InitializeKDCProxyClient = 1;
}
}
else if (MsRdpEx_StringIEquals(propName, "NegotiateSecurityLayer") &&
MsRdpEx_IsAddressInRdpAxModule(_ReturnAddress())) {
if (g_InitializeKDCProxyClient == 1) {
g_InitializeKDCProxyClient = 2;
}
}
else if (MsRdpEx_StringIEquals(propName, "TSGTransportIsUsed") &&
MsRdpEx_IsAddressInRdpAxModule(_ReturnAddress())) {
if (g_InitializeKDCProxyClient == 2) {
g_InitializeKDCProxyClient = 0;
*propValue = 1; // bypass if (TSGTransportIsUsed) { /* break Kerberos */ }
MsRdpEx_LogPrint(TRACE, "TSGTransportIsUsed is a lie!");
}
}
if (MsRdpEx_StringIEquals(propName, "UseNewOutput")) {
// Workaround to prevent disconnect error code 3334 with latest MSRDC
*propValue = 1;
}
if (MsRdpEx_StringIEquals(propName, "SkipAvdSignatureChecks")) {
// Workaround to force-disable AVD signature checks
*propValue = 1;
}
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::GetBoolProperty(%s, %d)", propName, *propValue);
return hr;
}
static HRESULT Hook_ITSPropertySet_SetIntProperty(ITSPropertySet* This, const char* propName, int propValue)
{
HRESULT hr;
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::SetIntProperty(%s, %d)", propName, propValue);
hr = Real_ITSPropertySet_SetIntProperty(This, propName, propValue);
return hr;
}
static HRESULT Hook_ITSPropertySet_GetIntProperty(ITSPropertySet* This, const char* propName, int* propValue)
{
HRESULT hr;
hr = Real_ITSPropertySet_GetIntProperty(This, propName, propValue);
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::GetIntProperty(%s, %d)", propName, *propValue);
return hr;
}
static HRESULT Hook_ITSPropertySet_SetStringProperty(ITSPropertySet* This, const char* propName, WCHAR* propValue)
{
HRESULT hr;
char* propValueA = _com_util::ConvertBSTRToString((BSTR) propValue);
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::SetStringProperty(%s, \"%s\")", propName, propValueA);
delete[] propValueA;
hr = Real_ITSPropertySet_SetStringProperty(This, propName, propValue);
return hr;
}
static HRESULT Hook_ITSPropertySet_GetStringProperty(ITSPropertySet* This, const char* propName, WCHAR** propValue)
{
HRESULT hr;
hr = Real_ITSPropertySet_GetStringProperty(This, propName, propValue);
if (SUCCEEDED(hr)) {
char* propValueA = _com_util::ConvertBSTRToString((BSTR)*propValue);
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::GetStringProperty(%s, \"%s\")", propName, propValueA ? propValueA : "");
delete[] propValueA;
}
else {
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::GetStringProperty(%s), hr = 0x%08X", propName, hr);
}
return hr;
}
static HRESULT Hook_ITSPropertySet_SetSecureStringProperty(ITSPropertySet* This, const char* propName, WCHAR* propValue)
{
HRESULT hr;
//char* propValueA = _com_util::ConvertBSTRToString((BSTR)propValue);
//MsRdpEx_LogPrint(TRACE, "ITSPropertySet::SetSecureStringProperty(%s, \"%s\")", propName, propValueA);
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::SetSecureStringProperty(%s, \"%s\")", propName, "*omitted*");
hr = Real_ITSPropertySet_SetSecureStringProperty(This, propName, propValue);
return hr;
}
static HRESULT Hook_ITSPropertySet_GetSecureStringProperty(ITSPropertySet* This, const char* propName, WCHAR** propValue, uint32_t* propLength)
{
HRESULT hr;
hr = Real_ITSPropertySet_GetSecureStringProperty(This, propName, propValue, propLength);
MsRdpEx_LogPrint(TRACE, "ITSPropertySet::GetSecureStringProperty(%s)", propName);
return hr;
}
static bool TSPropertySet_Hook(ITSPropertySet* pTSPropertySet, ITSPropertySetVtbl30* vtbl30, ITSPropertySetVtbl32* vtbl32)
{
LONG error;
if (!pTSPropertySet || (!vtbl30 && !vtbl32))
return false;
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
if (vtbl32)
{
Real_ITSPropertySet_SetBoolProperty = vtbl32->SetBoolProperty;
Real_ITSPropertySet_GetBoolProperty = vtbl32->GetBoolProperty;
Real_ITSPropertySet_SetIntProperty = vtbl32->SetIntProperty;
Real_ITSPropertySet_GetIntProperty = vtbl32->GetIntProperty;
Real_ITSPropertySet_SetStringProperty = vtbl32->SetStringProperty;
Real_ITSPropertySet_GetStringProperty = vtbl32->GetStringProperty;
Real_ITSPropertySet_SetSecureStringProperty = vtbl32->SetSecureStringProperty;
Real_ITSPropertySet_GetSecureStringProperty = vtbl32->GetSecureStringProperty;
}
else if (vtbl30)
{
Real_ITSPropertySet_SetBoolProperty = vtbl30->SetBoolProperty;
Real_ITSPropertySet_GetBoolProperty = vtbl30->GetBoolProperty;
Real_ITSPropertySet_SetIntProperty = vtbl30->SetIntProperty;
Real_ITSPropertySet_GetIntProperty = vtbl30->GetIntProperty;
Real_ITSPropertySet_SetStringProperty = vtbl30->SetStringProperty;
Real_ITSPropertySet_GetStringProperty = vtbl30->GetStringProperty;
Real_ITSPropertySet_SetSecureStringProperty = vtbl30->SetSecureStringProperty;
Real_ITSPropertySet_GetSecureStringProperty = vtbl30->GetSecureStringProperty;
}
DetourAttach((PVOID*)(&Real_ITSPropertySet_SetBoolProperty), Hook_ITSPropertySet_SetBoolProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_GetBoolProperty), Hook_ITSPropertySet_GetBoolProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_SetIntProperty), Hook_ITSPropertySet_SetIntProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_GetIntProperty), Hook_ITSPropertySet_GetIntProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_SetStringProperty), Hook_ITSPropertySet_SetStringProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_GetStringProperty), Hook_ITSPropertySet_GetStringProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_SetSecureStringProperty), Hook_ITSPropertySet_SetSecureStringProperty);
DetourAttach((PVOID*)(&Real_ITSPropertySet_GetSecureStringProperty), Hook_ITSPropertySet_GetSecureStringProperty);
error = DetourTransactionCommit();
return true;
}
static bool IsLikelyVoidMethodNoArgs(void* fn)
{
if (!fn)
return false;
MEMORY_BASIC_INFORMATION mbi = { 0 };
if (!VirtualQuery(fn, &mbi, sizeof(mbi)))
return false;
DWORD prot = mbi.Protect;
bool isExecutable =
(prot & PAGE_EXECUTE) ||
(prot & PAGE_EXECUTE_READ) ||
(prot & PAGE_EXECUTE_READWRITE) ||
(prot & PAGE_EXECUTE_WRITECOPY);
if (!isExecutable || mbi.State != MEM_COMMIT || (prot & PAGE_GUARD))
return false;
#if defined(_M_AMD64) || defined(__x86_64__)
uint8_t* code = (uint8_t*)fn;
size_t available = mbi.RegionSize - ((uintptr_t)fn - (uintptr_t)mbi.BaseAddress);
if (available < 8)
return false;
// Pattern 1: sub rsp, 0x28
if (code[0] == 0x48 && code[1] == 0x83 && code[2] == 0xEC)
return true;
// Pattern 2: push rbp; mov rbp, rsp
if (code[0] == 0x55 && code[1] == 0x48 && code[2] == 0x89 && code[3] == 0xE5)
return true;
// Pattern 3: add rcx, 0x50; jmp rel32 lock method stubs
if (code[0] == 0x48 && code[1] == 0x83 && code[2] == 0xC1 && code[3] == 0x50 &&
code[4] == 0xE9)
return true;
#elif defined(_M_IX86) || defined(__i386__)
uint8_t* code = (uint8_t*)fn;
if ((mbi.RegionSize - ((uintptr_t)fn - (uintptr_t)mbi.BaseAddress)) < 3)
return false;
// Typical prologue: push ebp; mov ebp, esp
if (code[0] == 0x55 && code[1] == 0x8B && code[2] == 0xEC)
return true;
// Alternate: sub esp, imm8
if (code[0] == 0x83 && code[1] == 0xEC)
return true;
#elif defined(_M_ARM64) || defined(__aarch64__)
uint32_t* ins = (uint32_t*)fn;
size_t available = mbi.RegionSize - ((uintptr_t)fn - (uintptr_t)mbi.BaseAddress);
if (available < 8)
return false;
uint32_t instr0 = ins[0];
uint32_t instr1 = ins[1];
// Pattern 1: stp x29, x30, [sp, #-16]! ; mov x29, sp
if (instr0 == 0xA9BF7BF0 && instr1 == 0x910003FD)
return true;
// Pattern 2: ret
if (instr0 == 0xD65F03C0)
return true;
// Pattern 3: add x0, x0, #imm ; b target
if ((instr0 & 0xFFC003FF) == 0x91000000 && // ADD x0, x0, #imm
(instr1 & 0xFC000000) == 0x14000000) // B <imm>
return true;
#endif
return false;
}
static int TSPropertySet_FindLockFunctionsInVtbl(void** vtbl)
{
const int blockSize = 4;
const int maxEntries = 30;
for (int i = 0; i <= maxEntries - blockSize; i++)
{
bool allMatch = true;
for (int j = 0; j < blockSize; j++) {
void* fn = vtbl[i + j];
if (!IsLikelyVoidMethodNoArgs(fn)) {
allMatch = false;
break;
}
}
if (allMatch)
return i; // found the start of a 4-function block
}
return -1; // not found
}
static int TSPropertySet_DetectVtblVersion(void** vtbl)
{
int vtblVersion = -1;
DWORD ctlVersion = 0;
if (MsRdpEx_IsAddressInRdclientAxModule(vtbl))
{
ctlVersion = g_rdclientax.tscCtlVer;
if (ctlVersion >= 5326) {
vtblVersion = 32;
}
else {
vtblVersion = 30;
}
}
else
{
ctlVersion = g_mstscax.tscCtlVer;
if (ctlVersion >= 27842) {
// First seen in Windows 11 Insider Preview Build 27842
vtblVersion = 32;
}
else {
vtblVersion = 30;
}
}
/**
* The vtable contains a block of 4 functions that take void and return void.
* Detect common assembly pattern matching that simple function prototype,
* and leverage this to find the offset where those functions begin:
*
* CTSPropertySet::EnterReadLock(void)
* CTSPropertySet::LeaveReadLock(void)
* CTSPropertySet::EnterWriteLock(void)
* CTSPropertySet::LeaveWriteLock(void)
*
* Once we know the offset, use it to detect which vtable we're dealing with
*/
int lockFunctionsOffset = TSPropertySet_FindLockFunctionsInVtbl(vtbl);
if (lockFunctionsOffset > 0) {
MsRdpEx_LogPrint(DEBUG, "Found TSPropertySet lock functions at vtable offset %d", lockFunctionsOffset);
if (lockFunctionsOffset == 20) {
vtblVersion = 32;
}
else if (lockFunctionsOffset == 18) {
vtblVersion = 30;
}
else {
MsRdpEx_LogPrint(WARN, "Unknown TSPropertySet lock functions vtable offset: %d", lockFunctionsOffset);
}
}
MsRdpEx_LogPrint(DEBUG, "TSPropertySet ctlVersion: %d vtblVersion: %d", ctlVersion, vtblVersion);
return vtblVersion;
}
class CMsRdpPropertySet : public IMsRdpExtendedSettings
{
public:
CMsRdpPropertySet(IUnknown* pUnknown)
{
m_refCount = 0;
m_pUnknown = pUnknown;
pUnknown->QueryInterface(IID_ITSPropertySet, (LPVOID*)&m_pTSPropertySet);
if (m_pTSPropertySet)
{
int vtblVersion = TSPropertySet_DetectVtblVersion(((void**)m_pTSPropertySet->vtbl));
if (vtblVersion == 32) {
m_vtbl32 = (ITSPropertySetVtbl32*)m_pTSPropertySet->vtbl;
m_vtbl30 = NULL;
}
else if (vtblVersion == 30) {
m_vtbl32 = NULL;
m_vtbl30 = (ITSPropertySetVtbl30*)m_pTSPropertySet->vtbl;
}
else {
MsRdpEx_LogPrint(ERROR, "Unknown TSPropertySet vtable version: %d", vtblVersion);
}
if (!g_TSPropertySet_Hooked) {
TSPropertySet_Hook(m_pTSPropertySet, m_vtbl30, m_vtbl32);
g_TSPropertySet_Hooked = true;
}
}
}
~CMsRdpPropertySet()
{
m_pUnknown->Release();
if (m_pTSPropertySet) {
m_pTSPropertySet->vtbl->Release(m_pTSPropertySet);
m_pTSPropertySet = NULL;
}
}
// IUnknown interface
public:
HRESULT STDMETHODCALLTYPE QueryInterface(
REFIID riid,
LPVOID* ppvObject
)
{
HRESULT hr = E_NOINTERFACE;
ULONG refCount = m_refCount;
char iid[MSRDPEX_GUID_STRING_SIZE];
MsRdpEx_GuidBinToStr((GUID*)&riid, iid, 0);
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::QueryInterface");
if (riid == IID_IUnknown)
{
*ppvObject = (LPVOID)((IUnknown*)this);
refCount = InterlockedIncrement(&m_refCount);
hr = S_OK;
}
else if ((riid == IID_IMsRdpExtendedSettings) && m_pTSPropertySet)
{
*ppvObject = (LPVOID)((IMsRdpExtendedSettings*)this);
refCount = InterlockedIncrement(&m_refCount);
hr = S_OK;
}
else
{
hr = m_pUnknown->QueryInterface(riid, ppvObject);
}
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::QueryInterface(%s) = 0x%08X, %d", iid, hr, refCount);
return hr;
}
ULONG STDMETHODCALLTYPE AddRef()
{
ULONG refCount = InterlockedIncrement(&m_refCount);
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::AddRef() = %d", refCount);
return refCount;
}
ULONG STDMETHODCALLTYPE Release()
{
ULONG refCount = InterlockedDecrement(&m_refCount);
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::Release() = %d", refCount);
if (refCount == 0)
{
delete this;
return 0;
}
return refCount;
}
// IMsRdpExtendedSettings
public:
HRESULT __stdcall put_Property(BSTR bstrPropertyName, VARIANT* pValue) {
char* propName = _com_util::ConvertBSTRToString(bstrPropertyName);
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::put_Property(%s, vt: %d)", propName, pValue->vt);
if (pValue->vt == VT_BOOL)
{
return SetVBoolProperty(propName, pValue->boolVal);
}
else if (pValue->vt == VT_I4)
{
return SetIntProperty(propName, pValue->intVal);
}
else if (pValue->vt == VT_UI4)
{
return SetIntProperty(propName, pValue->uintVal);
}
else if (pValue->vt == VT_BSTR)
{
return SetBStrProperty(propName, pValue->bstrVal);
}
delete[] propName;
return E_INVALIDARG;
}
HRESULT __stdcall get_Property(BSTR bstrPropertyName, VARIANT* pValue) {
HRESULT hr = E_INVALIDARG;
uint8_t propType = 0;
char* propName = _com_util::ConvertBSTRToString(bstrPropertyName);
MsRdpEx_LogPrint(DEBUG, "CMsRdpPropertySet::get_Property(%s)", propName);
VariantInit(pValue);
if (GetTSPropertyType(m_pTSPropertySet, propName, &propType)) {
if (propType == TSPROPERTY_TYPE_STRING) {
hr = GetBStrProperty(propName, &pValue->bstrVal);
if (hr == S_OK) {
pValue->vt = VT_BSTR;
}
}
else if (propType == TSPROPERTY_TYPE_BOOL) {
hr = GetVBoolProperty(propName, &pValue->boolVal);
if (hr == S_OK) {
pValue->vt = VT_BOOL;
}
}
}
delete[] propName;
return hr;
}
HRESULT __stdcall SetVBoolProperty(const char* propName, VARIANT_BOOL propValue) {
return m_pTSPropertySet->vtbl->SetBoolProperty(m_pTSPropertySet, propName, propValue);
}
HRESULT __stdcall SetIntProperty(const char* propName, uint32_t propValue) {
return m_pTSPropertySet->vtbl->SetIntProperty(m_pTSPropertySet, propName, propValue);
}
HRESULT __stdcall SetBStrProperty(const char* propName, BSTR propValue) {
return m_pTSPropertySet->vtbl->SetStringProperty(m_pTSPropertySet, propName, propValue);
}
HRESULT __stdcall SetSecureStringProperty(const char* propName, BSTR propValue) {
return m_pTSPropertySet->vtbl->SetSecureStringProperty(m_pTSPropertySet, propName, propValue);
}
HRESULT __stdcall GetVBoolProperty(const char* propName, VARIANT_BOOL* propValue) {
HRESULT hr = E_FAIL;
int iVal = 0;
if (m_vtbl32) {
hr = m_vtbl32->GetBoolProperty(m_pTSPropertySet, propName, &iVal);
} else if (m_vtbl30) {
hr = m_vtbl30->GetBoolProperty(m_pTSPropertySet, propName, &iVal);
}
*propValue = iVal ? VARIANT_TRUE : VARIANT_FALSE;
return hr;
}
HRESULT __stdcall GetBStrProperty(const char* propName, BSTR* propValue) {
HRESULT hr = E_FAIL;
WCHAR* wstrVal = NULL;
if (m_vtbl32) {
hr = m_vtbl32->GetStringProperty(m_pTSPropertySet, propName, &wstrVal);
} else if (m_vtbl30) {
hr = m_vtbl30->GetStringProperty(m_pTSPropertySet, propName, &wstrVal);
}
if (hr != S_OK)
return hr;
*propValue = SysAllocString(wstrVal);
return hr;
}
private:
ULONG m_refCount;
IUnknown* m_pUnknown;
ITSPropertySet* m_pTSPropertySet;
ITSPropertySetVtbl30* m_vtbl30 = NULL;
ITSPropertySetVtbl32* m_vtbl32 = NULL;
};
CMsRdpExtendedSettings::CMsRdpExtendedSettings(IUnknown* pUnknown, GUID* pSessionId)
{
HRESULT hr;
m_refCount = 0;
m_pUnknown = pUnknown;
MsRdpEx_GuidCopy(&m_sessionId, pSessionId);
strncpy_s(m_KeyboardHookToggleShortcutKey, "Space", sizeof(m_KeyboardHookToggleShortcutKey) - 1);
hr = pUnknown->QueryInterface(IID_IMsRdpClient7, (LPVOID*)&m_pMsRdpClient7);
if (SUCCEEDED(hr) && m_pMsRdpClient7)
m_pMsRdpClient7->AddRef();
hr = pUnknown->QueryInterface(IID_IMsRdpExtendedSettings, (LPVOID*)&m_pMsRdpExtendedSettings);
if (SUCCEEDED(hr) && m_pMsRdpExtendedSettings)
m_pMsRdpExtendedSettings->AddRef();
if (m_pMsRdpClient7)
{
m_pMsRdpClient7->get_TransportSettings2(&m_pMsRdpClientTransportSettings2);
if (m_pMsRdpClientTransportSettings2)
m_pMsRdpClientTransportSettings2->AddRef();
}
}
CMsRdpExtendedSettings::~CMsRdpExtendedSettings()
{
this->SetKdcProxyUrl(NULL);
this->SetRecordingPath(NULL);
this->SetRecordingPipeName(NULL);
if (m_pMsRdpExtendedSettings)
m_pMsRdpExtendedSettings->Release();
if (m_pMsRdpClientTransportSettings2)
m_pMsRdpClientTransportSettings2->Release();
if (m_pMsRdpClient7)
m_pMsRdpClient7->Release();
}
HRESULT STDMETHODCALLTYPE CMsRdpExtendedSettings::QueryInterface(
REFIID riid,
LPVOID* ppvObject
)
{
HRESULT hr = E_NOINTERFACE;
ULONG refCount = m_refCount;
char iid[MSRDPEX_GUID_STRING_SIZE];
MsRdpEx_GuidBinToStr((GUID*)&riid, iid, 0);
if (riid == IID_IUnknown)
{
*ppvObject = (LPVOID)((IUnknown*)this);
refCount = InterlockedIncrement(&m_refCount);
hr = S_OK;
}
else if ((riid == IID_IMsRdpExtendedSettings) && m_pMsRdpExtendedSettings)
{
*ppvObject = (LPVOID)((IMsRdpExtendedSettings*)this);
refCount = InterlockedIncrement(&m_refCount);
hr = S_OK;
}
else
{
hr = m_pUnknown->QueryInterface(riid, ppvObject);
}
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::QueryInterface(%s) = 0x%08X, %d", iid, hr, refCount);
return hr;
}
ULONG STDMETHODCALLTYPE CMsRdpExtendedSettings::AddRef()
{
ULONG refCount = InterlockedIncrement(&m_refCount);
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::AddRef() = %d", refCount);
return refCount;
}
ULONG STDMETHODCALLTYPE CMsRdpExtendedSettings::Release()
{
ULONG refCount = InterlockedDecrement(&m_refCount);
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::Release() = %d", refCount);
if (refCount == 0)
{
delete this;
return 0;
}
return refCount;
}
HRESULT __stdcall CMsRdpExtendedSettings::put_Property(BSTR bstrPropertyName, VARIANT* pValue) {
HRESULT hr = E_INVALIDARG;
char* propName = _com_util::ConvertBSTRToString(bstrPropertyName);
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::put_Property(%s)", propName);
if (MsRdpEx_StringEquals(propName, "KDCProxyURL"))
{
if (pValue->vt != VT_BSTR)
goto end;
char* propValue = _com_util::ConvertBSTRToString(pValue->bstrVal);
if (propValue) {
hr = this->SetKdcProxyUrl(propValue);
}
delete[] propValue;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "EnableMouseJiggler"))
{
if (pValue->vt != VT_BOOL)
goto end;
m_MouseJigglerEnabled = pValue->boolVal ? true : false;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "MouseJigglerInterval"))
{
if ((pValue->vt != VT_UI4) && (pValue->vt != VT_I4))
goto end;
m_MouseJigglerInterval = (uint32_t) pValue->uintVal;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "MouseJigglerMethod"))
{
if ((pValue->vt != VT_UI4) && (pValue->vt != VT_I4))
goto end;
m_MouseJigglerMethod = (uint32_t) pValue->uintVal;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "KeyboardHookToggleShortcutEnabled"))
{
if (pValue->vt != VT_BOOL)
goto end;
m_KeyboardHookToggleShortcutEnabled = pValue->boolVal ? true : false;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "KeyboardHookToggleShortcutKey"))
{
if (pValue->vt != VT_BSTR)
goto end;
char* propValueA = _com_util::ConvertBSTRToString((BSTR)pValue->bstrVal);
strncpy_s(m_KeyboardHookToggleShortcutKey, propValueA, sizeof(m_KeyboardHookToggleShortcutKey) - 1);
delete[] propValueA;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "OutputMirrorEnabled"))
{
if (pValue->vt != VT_BOOL)
goto end;
m_OutputMirrorEnabled = pValue->boolVal ? true : false;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "VideoRecordingEnabled"))
{
if (pValue->vt != VT_BOOL)
goto end;
m_VideoRecordingEnabled = pValue->boolVal ? true : false;
if (m_VideoRecordingEnabled)
m_OutputMirrorEnabled = true;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "VideoRecordingQuality"))
{
if ((pValue->vt != VT_UI4) && (pValue->vt != VT_I4))
goto end;
m_VideoRecordingQuality = (uint32_t)pValue->uintVal;
if (m_VideoRecordingQuality < 1)
m_VideoRecordingQuality = 1;
if (m_VideoRecordingQuality > 10)
m_VideoRecordingQuality = 10;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "RecordingPath"))
{
if (pValue->vt != VT_BSTR)
goto end;
char* propValue = _com_util::ConvertBSTRToString(pValue->bstrVal);
if (propValue) {
hr = this->SetRecordingPath(propValue);
}
free(propValue);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "RecordingPipeName"))
{
if (pValue->vt != VT_BSTR)
goto end;
char* propValue = _com_util::ConvertBSTRToString(pValue->bstrVal);
if (propValue) {
hr = this->SetRecordingPipeName(propValue);
}
free(propValue);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "DumpBitmapUpdates"))
{
if (pValue->vt != VT_BOOL)
goto end;
m_DumpBitmapUpdates = pValue->boolVal ? true : false;
if (m_DumpBitmapUpdates)
m_OutputMirrorEnabled = true;
hr = S_OK;
}
else
{
if (pValue->vt == VT_BSTR) {
char* propValueA = _com_util::ConvertBSTRToString((BSTR)pValue->bstrVal);
MsRdpEx_LogPrint(TRACE, "CMsRdpExtendedSettings::put_Property(%s, \"%s\")", propName, propValueA);
delete[] propValueA;
}
hr = m_pMsRdpExtendedSettings->put_Property(bstrPropertyName, pValue);
}
end:
delete[] propName;
return hr;
}
HRESULT __stdcall CMsRdpExtendedSettings::get_Property(BSTR bstrPropertyName, VARIANT* pValue) {
HRESULT hr = E_INVALIDARG;
char* propName = _com_util::ConvertBSTRToString(bstrPropertyName);
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::get_Property(%s)", propName);
VariantInit(pValue);
if (MsRdpEx_StringEquals(propName, "CoreProperties")) {
if (!m_CoreProps) {
return E_INVALIDARG;
}
pValue->vt = VT_UNKNOWN;
pValue->punkVal = NULL;
hr = m_CoreProps->QueryInterface(IID_IUnknown, (LPVOID*) &pValue->punkVal);
if (pValue->punkVal) {
pValue->punkVal->AddRef();
}
}
else if (MsRdpEx_StringEquals(propName, "BaseProperties")) {
if (!m_BaseProps) {
return E_INVALIDARG;
}
pValue->vt = VT_UNKNOWN;
pValue->punkVal = NULL;
hr = m_BaseProps->QueryInterface(IID_IUnknown, (LPVOID*)&pValue->punkVal);
if (pValue->punkVal) {
pValue->punkVal->AddRef();
}
}
else if (MsRdpEx_StringEquals(propName, "TransportProperties")) {
if (!m_TransportProps) {
return E_INVALIDARG;
}
pValue->vt = VT_UNKNOWN;
pValue->punkVal = NULL;
hr = m_TransportProps->QueryInterface(IID_IUnknown, (LPVOID*)&pValue->punkVal);
if (pValue->punkVal) {
pValue->punkVal->AddRef();
}
}
else if (MsRdpEx_StringEquals(propName, "KDCProxyURL")) {
pValue->vt = VT_BSTR;
const char* kdcProxyUrl = m_KdcProxyUrl ? m_KdcProxyUrl : "";
pValue->bstrVal = _com_util::ConvertStringToBSTR(kdcProxyUrl);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "EnableMouseJiggler")) {
pValue->vt = VT_BOOL;
pValue->boolVal = m_MouseJigglerEnabled ? VARIANT_TRUE : VARIANT_FALSE;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "MouseJigglerInterval")) {
pValue->vt = VT_I4;
pValue->intVal = (INT) m_MouseJigglerInterval;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "MouseJigglerMethod")) {
pValue->vt = VT_I4;
pValue->intVal = (INT) m_MouseJigglerMethod;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "KeyboardHookToggleShortcutEnabled")) {
pValue->vt = VT_BOOL;
pValue->boolVal = m_KeyboardHookToggleShortcutEnabled ? VARIANT_TRUE : VARIANT_FALSE;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "KeyboardHookToggleShortcutKey")) {
pValue->vt = VT_BSTR;
pValue->bstrVal = _com_util::ConvertStringToBSTR(m_KeyboardHookToggleShortcutKey);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "VideoRecordingEnabled")) {
pValue->vt = VT_BOOL;
pValue->boolVal = m_VideoRecordingEnabled ? VARIANT_TRUE : VARIANT_FALSE;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "VideoRecordingQuality")) {
pValue->vt = VT_I4;
pValue->intVal = (INT)m_VideoRecordingQuality;
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "RecordingPath")) {
pValue->vt = VT_BSTR;
const char* recordingPath = m_RecordingPath ? m_RecordingPath : "";
pValue->bstrVal = _com_util::ConvertStringToBSTR(recordingPath);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "RecordingPipeName")) {
pValue->vt = VT_BSTR;
const char* recordingPipeName = m_RecordingPipeName ? m_RecordingPipeName : "";
pValue->bstrVal = _com_util::ConvertStringToBSTR(recordingPipeName);
hr = S_OK;
}
else if (MsRdpEx_StringEquals(propName, "MsRdpEx_SessionId")) {
pValue->vt = VT_BSTR;
char sessionId[MSRDPEX_GUID_STRING_SIZE];
MsRdpEx_GuidBinToStr((GUID*)&m_sessionId, sessionId, 0);
pValue->bstrVal = _com_util::ConvertStringToBSTR(sessionId);
hr = S_OK;
}
else {
hr = m_pMsRdpExtendedSettings->get_Property(bstrPropertyName, pValue);
}
delete[] propName;
return hr;
}
// additional functions
HRESULT __stdcall CMsRdpExtendedSettings::put_CoreProperty(BSTR bstrPropertyName, VARIANT* pValue) {
char* propName = _com_util::ConvertBSTRToString(bstrPropertyName);
MsRdpEx_LogPrint(DEBUG, "CMsRdpExtendedSettings::put_CoreProperty(%s)", propName);
delete[] propName;
if (!m_CoreProps)
return E_INVALIDARG;