/
native.c
7387 lines (6337 loc) · 192 KB
/
native.c
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
/*
* Process Hacker -
* native wrapper and support functions
*
* Copyright (C) 2009-2016 wj32
* Copyright (C) 2017 dmex
*
* This file is part of Process Hacker.
*
* Process Hacker is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Process Hacker is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ph.h>
#include <apiimport.h>
#include <kphuser.h>
#include <lsasup.h>
#include <mapimg.h>
#define PH_DEVICE_PREFIX_LENGTH 64
#define PH_DEVICE_MUP_PREFIX_MAX_COUNT 16
typedef BOOLEAN (NTAPI *PPHP_ENUM_PROCESS_MODULES_CALLBACK)(
_In_ HANDLE ProcessHandle,
_In_ PLDR_DATA_TABLE_ENTRY Entry,
_In_ PVOID AddressOfEntry,
_In_opt_ PVOID Context1,
_In_opt_ PVOID Context2
);
typedef BOOLEAN (NTAPI *PPHP_ENUM_PROCESS_MODULES32_CALLBACK)(
_In_ HANDLE ProcessHandle,
_In_ PLDR_DATA_TABLE_ENTRY32 Entry,
_In_ ULONG AddressOfEntry,
_In_opt_ PVOID Context1,
_In_opt_ PVOID Context2
);
static PH_INITONCE PhDevicePrefixesInitOnce = PH_INITONCE_INIT;
static UNICODE_STRING PhDevicePrefixes[26];
static PH_QUEUED_LOCK PhDevicePrefixesLock = PH_QUEUED_LOCK_INIT;
static PPH_STRING PhDeviceMupPrefixes[PH_DEVICE_MUP_PREFIX_MAX_COUNT] = { 0 };
static ULONG PhDeviceMupPrefixesCount = 0;
static PH_QUEUED_LOCK PhDeviceMupPrefixesLock = PH_QUEUED_LOCK_INIT;
static PH_INITONCE PhPredefineKeyInitOnce = PH_INITONCE_INIT;
static UNICODE_STRING PhPredefineKeyNames[PH_KEY_MAXIMUM_PREDEFINE] =
{
RTL_CONSTANT_STRING(L"\\Registry\\Machine"),
RTL_CONSTANT_STRING(L"\\Registry\\User"),
RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\Classes"),
{ 0, 0, NULL }
};
static HANDLE PhPredefineKeyHandles[PH_KEY_MAXIMUM_PREDEFINE] = { 0 };
/**
* Queries information about the token of the current process.
*/
PH_TOKEN_ATTRIBUTES PhGetOwnTokenAttributes(
VOID
)
{
static PH_INITONCE initOnce = PH_INITONCE_INIT;
static PH_TOKEN_ATTRIBUTES attributes = { 0 };
if (PhBeginInitOnce(&initOnce))
{
BOOLEAN elevated = TRUE;
TOKEN_ELEVATION_TYPE elevationType = TokenElevationTypeFull;
if (WindowsVersion >= WINDOWS_8)
{
attributes.TokenHandle = NtCurrentProcessToken();
}
else
{
HANDLE tokenHandle;
if (NT_SUCCESS(PhOpenProcessToken(NtCurrentProcess(), TOKEN_QUERY, &tokenHandle)))
attributes.TokenHandle = tokenHandle;
}
if (attributes.TokenHandle)
{
PTOKEN_USER tokenUser;
PhGetTokenIsElevated(attributes.TokenHandle, &elevated);
PhGetTokenElevationType(attributes.TokenHandle, &elevationType);
if (NT_SUCCESS(PhGetTokenUser(attributes.TokenHandle, &tokenUser)))
{
attributes.TokenSid = PhAllocateCopy(tokenUser->User.Sid, RtlLengthSid(tokenUser->User.Sid));
PhFree(tokenUser);
}
}
attributes.Elevated = elevated;
attributes.ElevationType = elevationType;
PhEndInitOnce(&initOnce);
}
return attributes;
}
/**
* Opens a process.
*
* \param ProcessHandle A variable which receives a handle to the process.
* \param DesiredAccess The desired access to the process.
* \param ProcessId The ID of the process.
*/
NTSTATUS PhOpenProcess(
_Out_ PHANDLE ProcessHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ HANDLE ProcessId
)
{
NTSTATUS status;
OBJECT_ATTRIBUTES objectAttributes;
CLIENT_ID clientId;
clientId.UniqueProcess = ProcessId;
clientId.UniqueThread = NULL;
#ifdef _DEBUG
if (ProcessId == NtCurrentProcessId())
{
*ProcessHandle = NtCurrentProcess();
return STATUS_SUCCESS;
}
#endif
if (KphIsVerified() && (DesiredAccess & KPH_PROCESS_READ_ACCESS) == DesiredAccess)
{
status = KphOpenProcess(
ProcessHandle,
DesiredAccess,
&clientId
);
}
else
{
InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
status = NtOpenProcess(
ProcessHandle,
DesiredAccess,
&objectAttributes,
&clientId
);
if (status == STATUS_ACCESS_DENIED && KphIsVerified())
{
status = KphOpenProcess(
ProcessHandle,
DesiredAccess,
&clientId
);
}
}
return status;
}
/** Limited API for untrusted/external code. */
NTSTATUS PhOpenProcessPublic(
_Out_ PHANDLE ProcessHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ HANDLE ProcessId
)
{
OBJECT_ATTRIBUTES objectAttributes;
CLIENT_ID clientId;
InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
clientId.UniqueProcess = ProcessId;
clientId.UniqueThread = NULL;
return NtOpenProcess(
ProcessHandle,
DesiredAccess,
&objectAttributes,
&clientId
);
}
/**
* Opens a thread.
*
* \param ThreadHandle A variable which receives a handle to the thread.
* \param DesiredAccess The desired access to the thread.
* \param ThreadId The ID of the thread.
*/
NTSTATUS PhOpenThread(
_Out_ PHANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ HANDLE ThreadId
)
{
NTSTATUS status;
OBJECT_ATTRIBUTES objectAttributes;
CLIENT_ID clientId;
clientId.UniqueProcess = NULL;
clientId.UniqueThread = ThreadId;
#ifdef _DEBUG
if (ThreadId == NtCurrentThreadId())
{
*ThreadHandle = NtCurrentThread();
return STATUS_SUCCESS;
}
#endif
if (KphIsVerified() && (DesiredAccess & KPH_THREAD_READ_ACCESS) == DesiredAccess)
{
status = KphOpenThread(
ThreadHandle,
DesiredAccess,
&clientId
);
}
else
{
InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
status = NtOpenThread(
ThreadHandle,
DesiredAccess,
&objectAttributes,
&clientId
);
if (status == STATUS_ACCESS_DENIED && KphIsVerified())
{
status = KphOpenThread(
ThreadHandle,
DesiredAccess,
&clientId
);
}
}
return status;
}
/** Limited API for untrusted/external code. */
NTSTATUS PhOpenThreadPublic(
_Out_ PHANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ HANDLE ThreadId
)
{
OBJECT_ATTRIBUTES objectAttributes;
CLIENT_ID clientId;
clientId.UniqueProcess = NULL;
clientId.UniqueThread = ThreadId;
InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
return NtOpenThread(
ThreadHandle,
DesiredAccess,
&objectAttributes,
&clientId
);
}
NTSTATUS PhOpenThreadProcess(
_In_ HANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_Out_ PHANDLE ProcessHandle
)
{
if (KphIsConnected())
{
return KphOpenThreadProcess(
ThreadHandle,
DesiredAccess,
ProcessHandle
);
}
else
{
NTSTATUS status;
THREAD_BASIC_INFORMATION basicInfo;
if (!NT_SUCCESS(status = PhGetThreadBasicInformation(
ThreadHandle,
&basicInfo
)))
return status;
return PhOpenProcess(
ProcessHandle,
DesiredAccess,
basicInfo.ClientId.UniqueProcess
);
}
}
/**
* Opens a process token.
*
* \param ProcessHandle A handle to a process.
* \param DesiredAccess The desired access to the token.
* \param TokenHandle A variable which receives a handle to the token.
*/
NTSTATUS PhOpenProcessToken(
_In_ HANDLE ProcessHandle,
_In_ ACCESS_MASK DesiredAccess,
_Out_ PHANDLE TokenHandle
)
{
NTSTATUS status;
if (KphIsVerified() && (DesiredAccess & KPH_TOKEN_READ_ACCESS) == DesiredAccess)
{
status = KphOpenProcessToken(
ProcessHandle,
DesiredAccess,
TokenHandle
);
}
else
{
status = NtOpenProcessToken(
ProcessHandle,
DesiredAccess,
TokenHandle
);
if (status == STATUS_ACCESS_DENIED && KphIsVerified())
{
status = KphOpenProcessToken(
ProcessHandle,
DesiredAccess,
TokenHandle
);
}
}
return status;
}
NTSTATUS PhGetObjectSecurity(
_In_ HANDLE Handle,
_In_ SECURITY_INFORMATION SecurityInformation,
_Out_ PSECURITY_DESCRIPTOR *SecurityDescriptor
)
{
NTSTATUS status;
ULONG bufferSize;
PVOID buffer;
bufferSize = 0x100;
buffer = PhAllocate(bufferSize);
// This is required (especially for File objects) because some drivers don't seem to handle
// QuerySecurity properly.
memset(buffer, 0, bufferSize);
status = NtQuerySecurityObject(
Handle,
SecurityInformation,
buffer,
bufferSize,
&bufferSize
);
if (status == STATUS_BUFFER_TOO_SMALL)
{
PhFree(buffer);
buffer = PhAllocate(bufferSize);
memset(buffer, 0, bufferSize);
status = NtQuerySecurityObject(
Handle,
SecurityInformation,
buffer,
bufferSize,
&bufferSize
);
}
if (!NT_SUCCESS(status))
{
PhFree(buffer);
return status;
}
*SecurityDescriptor = (PSECURITY_DESCRIPTOR)buffer;
return status;
}
NTSTATUS PhSetObjectSecurity(
_In_ HANDLE Handle,
_In_ SECURITY_INFORMATION SecurityInformation,
_In_ PSECURITY_DESCRIPTOR SecurityDescriptor
)
{
return NtSetSecurityObject(
Handle,
SecurityInformation,
SecurityDescriptor
);
}
/**
* Terminates a process.
*
* \param ProcessHandle A handle to a process. The handle must have PROCESS_TERMINATE access.
* \param ExitStatus A status value that indicates why the process is being terminated.
*/
NTSTATUS PhTerminateProcess(
_In_ HANDLE ProcessHandle,
_In_ NTSTATUS ExitStatus
)
{
NTSTATUS status;
if (KphIsVerified())
{
status = KphTerminateProcess(
ProcessHandle,
ExitStatus
);
if (status != STATUS_NOT_SUPPORTED)
return status;
}
return NtTerminateProcess(
ProcessHandle,
ExitStatus
);
}
/** Limited API for untrusted/external code. */
NTSTATUS PhTerminateProcessPublic(
_In_ HANDLE ProcessHandle,
_In_ NTSTATUS ExitStatus
)
{
return NtTerminateProcess(
ProcessHandle,
ExitStatus
);
}
/**
* Queries variable-sized information for a process. The function allocates a buffer to contain the
* information.
*
* \param ProcessHandle A handle to a process. The access required depends on the information class
* specified.
* \param ProcessInformationClass The information class to retrieve.
* \param Buffer A variable which receives a pointer to a buffer containing the information. You
* must free the buffer using PhFree() when you no longer need it.
*/
NTSTATUS PhpQueryProcessVariableSize(
_In_ HANDLE ProcessHandle,
_In_ PROCESSINFOCLASS ProcessInformationClass,
_Out_ PVOID *Buffer
)
{
NTSTATUS status;
PVOID buffer;
ULONG returnLength = 0;
status = NtQueryInformationProcess(
ProcessHandle,
ProcessInformationClass,
NULL,
0,
&returnLength
);
if (status != STATUS_BUFFER_OVERFLOW && status != STATUS_BUFFER_TOO_SMALL && status != STATUS_INFO_LENGTH_MISMATCH)
return status;
buffer = PhAllocate(returnLength);
status = NtQueryInformationProcess(
ProcessHandle,
ProcessInformationClass,
buffer,
returnLength,
&returnLength
);
if (NT_SUCCESS(status))
{
*Buffer = buffer;
}
else
{
PhFree(buffer);
}
return status;
}
/**
* Gets the file name of the process' image.
*
* \param ProcessHandle A handle to a process. The handle must have
* PROCESS_QUERY_LIMITED_INFORMATION access.
* \param FileName A variable which receives a pointer to a string containing the file name. You
* must free the string using PhDereferenceObject() when you no longer need it.
*/
NTSTATUS PhGetProcessImageFileName(
_In_ HANDLE ProcessHandle,
_Out_ PPH_STRING *FileName
)
{
NTSTATUS status;
PUNICODE_STRING fileName;
status = PhpQueryProcessVariableSize(
ProcessHandle,
ProcessImageFileName,
&fileName
);
if (!NT_SUCCESS(status))
return status;
*FileName = PhCreateStringFromUnicodeString(fileName);
PhFree(fileName);
return status;
}
/**
* Gets the Win32 file name of the process' image.
*
* \param ProcessHandle A handle to a process. The handle must have
* PROCESS_QUERY_LIMITED_INFORMATION access.
* \param FileName A variable which receives a pointer to a string containing the file name. You
* must free the string using PhDereferenceObject() when you no longer need it.
*
* \remarks This function is only available on Windows Vista and above.
*/
NTSTATUS PhGetProcessImageFileNameWin32(
_In_ HANDLE ProcessHandle,
_Out_ PPH_STRING *FileName
)
{
NTSTATUS status;
PUNICODE_STRING fileName;
status = PhpQueryProcessVariableSize(
ProcessHandle,
ProcessImageFileNameWin32,
&fileName
);
if (!NT_SUCCESS(status))
return status;
*FileName = PhCreateStringFromUnicodeString(fileName);
PhFree(fileName);
return status;
}
/**
* Gets a string stored in a process' parameters structure.
*
* \param ProcessHandle A handle to a process. The handle must have
* PROCESS_QUERY_LIMITED_INFORMATION and PROCESS_VM_READ access.
* \param Offset The string to retrieve.
* \param String A variable which receives a pointer to the requested string. You must free the
* string using PhDereferenceObject() when you no longer need it.
*
* \retval STATUS_INVALID_PARAMETER_2 An invalid value was specified in the Offset parameter.
*/
NTSTATUS PhGetProcessPebString(
_In_ HANDLE ProcessHandle,
_In_ PH_PEB_OFFSET Offset,
_Out_ PPH_STRING *String
)
{
NTSTATUS status;
PPH_STRING string;
ULONG offset;
#define PEB_OFFSET_CASE(Enum, Field) \
case Enum: offset = FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS, Field); break; \
case Enum | PhpoWow64: offset = FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS32, Field); break
switch (Offset)
{
PEB_OFFSET_CASE(PhpoCurrentDirectory, CurrentDirectory);
PEB_OFFSET_CASE(PhpoDllPath, DllPath);
PEB_OFFSET_CASE(PhpoImagePathName, ImagePathName);
PEB_OFFSET_CASE(PhpoCommandLine, CommandLine);
PEB_OFFSET_CASE(PhpoWindowTitle, WindowTitle);
PEB_OFFSET_CASE(PhpoDesktopInfo, DesktopInfo);
PEB_OFFSET_CASE(PhpoShellInfo, ShellInfo);
PEB_OFFSET_CASE(PhpoRuntimeData, RuntimeData);
default:
return STATUS_INVALID_PARAMETER_2;
}
if (!(Offset & PhpoWow64))
{
PROCESS_BASIC_INFORMATION basicInfo;
PVOID processParameters;
UNICODE_STRING unicodeString;
// Get the PEB address.
if (!NT_SUCCESS(status = PhGetProcessBasicInformation(ProcessHandle, &basicInfo)))
return status;
// Read the address of the process parameters.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(basicInfo.PebBaseAddress, FIELD_OFFSET(PEB, ProcessParameters)),
&processParameters,
sizeof(PVOID),
NULL
)))
return status;
// Read the string structure.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters, offset),
&unicodeString,
sizeof(UNICODE_STRING),
NULL
)))
return status;
string = PhCreateStringEx(NULL, unicodeString.Length);
// Read the string contents.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
unicodeString.Buffer,
string->Buffer,
string->Length,
NULL
)))
{
PhDereferenceObject(string);
return status;
}
}
else
{
PVOID peb32;
ULONG processParameters32;
UNICODE_STRING32 unicodeString32;
if (!NT_SUCCESS(status = PhGetProcessPeb32(ProcessHandle, &peb32)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(peb32, FIELD_OFFSET(PEB32, ProcessParameters)),
&processParameters32,
sizeof(ULONG),
NULL
)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters32, offset),
&unicodeString32,
sizeof(UNICODE_STRING32),
NULL
)))
return status;
string = PhCreateStringEx(NULL, unicodeString32.Length);
// Read the string contents.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
UlongToPtr(unicodeString32.Buffer),
string->Buffer,
string->Length,
NULL
)))
{
PhDereferenceObject(string);
return status;
}
}
*String = string;
return status;
}
/**
* Gets a process' command line.
*
* \param ProcessHandle A handle to a process. The handle must have
* PROCESS_QUERY_LIMITED_INFORMATION. Before Windows 8.1, the handle must also have PROCESS_VM_READ
* access.
* \param String A variable which receives a pointer to a string containing the command line. You
* must free the string using PhDereferenceObject() when you no longer need it.
*/
NTSTATUS PhGetProcessCommandLine(
_In_ HANDLE ProcessHandle,
_Out_ PPH_STRING *CommandLine
)
{
NTSTATUS status;
if (WindowsVersion >= WINDOWS_8_1)
{
PUNICODE_STRING commandLine;
status = PhpQueryProcessVariableSize(
ProcessHandle,
ProcessCommandLineInformation,
&commandLine
);
if (NT_SUCCESS(status))
{
*CommandLine = PhCreateStringFromUnicodeString(commandLine);
PhFree(commandLine);
return status;
}
}
return PhGetProcessPebString(ProcessHandle, PhpoCommandLine, CommandLine);
}
NTSTATUS PhGetProcessDesktopInfo(
_In_ HANDLE ProcessHandle,
_Out_ PPH_STRING *DesktopInfo
)
{
return PhGetProcessPebString(ProcessHandle, PhpoDesktopInfo, DesktopInfo);
}
/**
* Gets the window flags and window title of a process.
*
* \param ProcessHandle A handle to a process. The handle must have
* PROCESS_QUERY_LIMITED_INFORMATION. Before Windows 7 SP1, the handle must also have
* PROCESS_VM_READ access.
* \param WindowFlags A variable which receives the window flags.
* \param WindowTitle A variable which receives a pointer to the window title. You must free the
* string using PhDereferenceObject() when you no longer need it.
*/
NTSTATUS PhGetProcessWindowTitle(
_In_ HANDLE ProcessHandle,
_Out_ PULONG WindowFlags,
_Out_ PPH_STRING *WindowTitle
)
{
NTSTATUS status;
#ifdef _WIN64
BOOLEAN isWow64 = FALSE;
#endif
ULONG windowFlags;
PPROCESS_WINDOW_INFORMATION windowInfo;
status = PhpQueryProcessVariableSize(
ProcessHandle,
ProcessWindowInformation,
&windowInfo
);
if (NT_SUCCESS(status))
{
*WindowFlags = windowInfo->WindowFlags;
*WindowTitle = PhCreateStringEx(windowInfo->WindowTitle, windowInfo->WindowTitleLength);
PhFree(windowInfo);
return status;
}
#ifdef _WIN64
PhGetProcessIsWow64(ProcessHandle, &isWow64);
if (!isWow64)
#endif
{
PROCESS_BASIC_INFORMATION basicInfo;
PVOID processParameters;
// Get the PEB address.
if (!NT_SUCCESS(status = PhGetProcessBasicInformation(ProcessHandle, &basicInfo)))
return status;
// Read the address of the process parameters.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(basicInfo.PebBaseAddress, FIELD_OFFSET(PEB, ProcessParameters)),
&processParameters,
sizeof(PVOID),
NULL
)))
return status;
// Read the window flags.
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters, FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS, WindowFlags)),
&windowFlags,
sizeof(ULONG),
NULL
)))
return status;
}
#ifdef _WIN64
else
{
PVOID peb32;
ULONG processParameters32;
if (!NT_SUCCESS(status = PhGetProcessPeb32(ProcessHandle, &peb32)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(peb32, FIELD_OFFSET(PEB32, ProcessParameters)),
&processParameters32,
sizeof(ULONG),
NULL
)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters32, FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS32, WindowFlags)),
&windowFlags,
sizeof(ULONG),
NULL
)))
return status;
}
#endif
#ifdef _WIN64
status = PhGetProcessPebString(ProcessHandle, PhpoWindowTitle | (isWow64 ? PhpoWow64 : 0), WindowTitle);
#else
status = PhGetProcessPebString(ProcessHandle, PhpoWindowTitle, WindowTitle);
#endif
if (NT_SUCCESS(status))
*WindowFlags = windowFlags;
return status;
}
NTSTATUS PhGetProcessDepStatus(
_In_ HANDLE ProcessHandle,
_Out_ PULONG DepStatus
)
{
NTSTATUS status;
ULONG executeFlags;
ULONG depStatus;
if (!NT_SUCCESS(status = PhGetProcessExecuteFlags(
ProcessHandle,
&executeFlags
)))
return status;
// Check if execution of data pages is enabled.
if (executeFlags & MEM_EXECUTE_OPTION_ENABLE)
depStatus = 0;
else
depStatus = PH_PROCESS_DEP_ENABLED;
if (executeFlags & MEM_EXECUTE_OPTION_DISABLE_THUNK_EMULATION)
depStatus |= PH_PROCESS_DEP_ATL_THUNK_EMULATION_DISABLED;
if (executeFlags & MEM_EXECUTE_OPTION_PERMANENT)
depStatus |= PH_PROCESS_DEP_PERMANENT;
*DepStatus = depStatus;
return status;
}
/**
* Gets a process' environment block.
*
* \param ProcessHandle A handle to a process. The handle must have PROCESS_QUERY_INFORMATION and
* PROCESS_VM_READ access.
* \param Flags A combination of flags.
* \li \c PH_GET_PROCESS_ENVIRONMENT_WOW64 Retrieve the environment block from the WOW64 PEB.
* \param Environment A variable which will receive a pointer to the environment block copied from
* the process. You must free the block using PhFreePage() when you no longer need it.
* \param EnvironmentLength A variable which will receive the length of the environment block, in
* bytes.
*/
NTSTATUS PhGetProcessEnvironment(
_In_ HANDLE ProcessHandle,
_In_ ULONG Flags,
_Out_ PVOID *Environment,
_Out_ PULONG EnvironmentLength
)
{
NTSTATUS status;
PVOID environmentRemote;
MEMORY_BASIC_INFORMATION mbi;
PVOID environment;
ULONG environmentLength;
if (!(Flags & PH_GET_PROCESS_ENVIRONMENT_WOW64))
{
PROCESS_BASIC_INFORMATION basicInfo;
PVOID processParameters;
status = PhGetProcessBasicInformation(ProcessHandle, &basicInfo);
if (!NT_SUCCESS(status))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(basicInfo.PebBaseAddress, FIELD_OFFSET(PEB, ProcessParameters)),
&processParameters,
sizeof(PVOID),
NULL
)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters, FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS, Environment)),
&environmentRemote,
sizeof(PVOID),
NULL
)))
return status;
}
else
{
PVOID peb32;
ULONG processParameters32;
ULONG environmentRemote32;
status = PhGetProcessPeb32(ProcessHandle, &peb32);
if (!NT_SUCCESS(status))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(peb32, FIELD_OFFSET(PEB32, ProcessParameters)),
&processParameters32,
sizeof(ULONG),
NULL
)))
return status;
if (!NT_SUCCESS(status = NtReadVirtualMemory(
ProcessHandle,
PTR_ADD_OFFSET(processParameters32, FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS32, Environment)),
&environmentRemote32,
sizeof(ULONG),
NULL
)))
return status;
environmentRemote = UlongToPtr(environmentRemote32);
}
if (!NT_SUCCESS(status = NtQueryVirtualMemory(
ProcessHandle,
environmentRemote,
MemoryBasicInformation,
&mbi,
sizeof(MEMORY_BASIC_INFORMATION),
NULL
)))
return status;
environmentLength = (ULONG)(mbi.RegionSize -
((ULONG_PTR)environmentRemote - (ULONG_PTR)mbi.BaseAddress));
// Read in the entire region of memory.