-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess.c
1270 lines (941 loc) · 33 KB
/
process.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
/*
* Copyright (c) 2004 Security Architects Corporation. All rights reserved.
*
* Module Name:
*
* process.c
*
* Abstract:
*
* This module defines various types used by process and thread hooking routines.
*
* Author:
*
* Eugene Tsyrklevich 23-Feb-2004
*
* Revision History:
*
* None.
*/
#include <NTDDK.h>
#include "process.h"
#include "driver.h"
#include "policy.h"
#include "hookproc.h"
#include "userland.h"
#include "procname.h"
#include "accessmask.h"
#include "learn.h"
#include "misc.h"
#include "i386.h"
#include "log.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text (INIT, InitProcessEntries)
#pragma alloc_text (PAGE, ProcessPostBootup)
#endif
fpZwCreateProcess OriginalNtCreateProcess = NULL;
fpZwCreateProcessEx OriginalNtCreateProcessEx = NULL;
fpZwOpenProcess OriginalNtOpenProcess = NULL;
fpZwCreateThread OriginalNtCreateThread = NULL;
fpZwOpenThread OriginalNtOpenThread = NULL;
BOOLEAN AllowProcessesWithPoliciesOnly = FALSE;
WCHAR OzoneInstallPath[MAX_PATH];
USHORT OzoneInstallPathSize = 0;
/* XXX this will not work on 64-bit architectures, due to assumption of size of ulong, pointers, etc */
typedef struct _CONTROL_AREA { // must be quadword sized.
ULONG data[9];
PFILE_OBJECT FilePointer;
} CONTROL_AREA, *PCONTROL_AREA;
typedef struct _SEGMENT {
struct _CONTROL_AREA *ControlArea;
} SEGMENT, *PSEGMENT;
typedef struct _SECTION {
ULONG_PTR data[5];
PSEGMENT Segment;
} SECTION, *PSECTION;
/*
* rand()
*
* Description:
* Returns a random number that is calculated by XOR'ing together often changing system values.
*
* Parameters:
* randval - An optional random value.
*
* Returns:
* A random ULONG value.
*/
ULONG
rand(ULONG randval)
{
LARGE_INTEGER perf, TickCount;
ULONG r;
KeQueryPerformanceCounter(&perf);
// KdPrint(("perf: %d %d\n", perf.LowPart, perf.HighPart));
KeQueryTickCount(&TickCount);
// KdPrint(("tick: %d %d\n", TickCount.LowPart, TickCount.HighPart));
// KdPrint(("int: %I64d\n", KeQueryInterruptTime()));
r = randval ^
perf.LowPart ^ perf.HighPart ^
TickCount.HighPart ^ TickCount.LowPart ^
(ULONG) KeQueryInterruptTime();
// KdPrint(("rand = %x\n", r));
return r;
}
/*
* PostProcessNtCreateProcess()
*
* Description:
* This function is called by the mediated NtCreateProcess() system service.
* It is responsible for saving the information about the newly created process in a
* global process hash table.
*
* Parameters:
* ProcessHandle - process object handle initialized by NtCreateProcess().
* SectionHandle - specifies a handle to an image section that grants SECTION_MAP_EXECUTE
* access. If this value is zero, the new process inherits the address space from the process
* referred to by InheritFromProcessHandle. In Windows 2000 the lowest bit specifies
* (when set) that the process should not be associated with the job of the
* InheritFromProcessHandle process.
*
* Returns:
* ULONG indicating an action to take (ACTION_DENY to disallow process createion, ACTION_PERMIT to allow).
*/
ULONG
PostProcessNtCreateProcess(PHANDLE ProcessHandle, HANDLE SectionHandle)
{
NTSTATUS status, rc;
PIMAGE_PID_ENTRY p, prev, NewProcess;
ULONG ProcessId, Size;
PULONG_PTR Base;
UNICODE_STRING usPath;
ANSI_STRING asPath;
KIRQL irql;
PROCESS_BASIC_INFORMATION ProcessBasicInfo;
CHAR ProcessPathUnresolved[MAX_PATH];
CHAR PROCESSNAME[MAX_PATH];
PCHAR FunctionName = "PostProcessNtCreateProcess";
if (ProcessHandle == NULL)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_WARNING, ("%s: ProcessHandle is NULL\n", FunctionName));
return ACTION_NONE;
}
/*
* Try to retrieve the image name from a section object
*/
if (!SectionHandle)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_WARNING, ("%s: SectionHandle is NULL\n", FunctionName));
return ACTION_NONE;
}
do
{
PSECTION SectionToMap;
PSEGMENT seg;
PCONTROL_AREA pca;
PFILE_OBJECT pfo;
status = ObReferenceObjectByHandle(
SectionHandle,
0,
0,
KernelMode,
(PSECTION *) &SectionToMap,
NULL
);
/* macro shortcut for bailing out of PostProcessNtCreateProcess do {} while loop in case of an error */
#define ABORT_PostProcessNtCreateProcess(msg) \
{ \
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("Error occurred in %s:", FunctionName)); \
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, (msg)); \
return ACTION_NONE; /* XXX */ \
}
if (! NT_SUCCESS(status))
ABORT_PostProcessNtCreateProcess(("ObReferenceObjectByHandle(SectionHandle) failed"));
if (SectionToMap == NULL)
ABORT_PostProcessNtCreateProcess(("SectionToMap is NULL"));
if ( (seg = ((PSECTION)SectionToMap)->Segment) == NULL)
ABORT_PostProcessNtCreateProcess(("Segment is NULL"));
if ( (pca = seg->ControlArea) == NULL)
ABORT_PostProcessNtCreateProcess(("ControlArea is NULL"));
if ( (pfo = pca->FilePointer) == NULL)
ABORT_PostProcessNtCreateProcess(("FilePointer is NULL"));
usPath = pfo->FileName;
if (usPath.Length == 0)
ABORT_PostProcessNtCreateProcess(("FileName length is 0"));
_snprintf(ProcessPathUnresolved, MAX_PATH, "%S", usPath.Buffer);
ProcessPathUnresolved[MAX_PATH - 1] = 0;
ObDereferenceObject(SectionToMap);
} while (0);
/*
* Now verify the executable name against the security policy
*/
VerifyExecutableName(ProcessPathUnresolved);
if (LearningMode == FALSE)
{
ACTION_TYPE Action;
VerifyUserReturnAddress();
FixupFilename(ProcessPathUnresolved, PROCESSNAME, MAX_PATH);
/*
* We manually inc/dec HookedRoutineRunning since POLICY_CHECK_OPTYPE_NAME() can call
* HOOK_ROUTINE_EXIT() which will decrement HookedRoutineRunning and then it will get
* decremented the second time in HookedNtCreateProcess()
*/
#if DBG
InterlockedIncrement(&HookedRoutineRunning);
#endif
POLICY_CHECK_OPTYPE_NAME(PROCESS, OP_PROC_EXECUTE);
#if DBG
InterlockedDecrement(&HookedRoutineRunning);
#endif
}
else
{
// learning mode
AddRule(RULE_PROCESS, ProcessPathUnresolved, OP_PROC_EXECUTE);
}
/*
* retrieve the Process ID
*/
status = ZwQueryInformationProcess(*ProcessHandle, ProcessBasicInformation, &ProcessBasicInfo, sizeof(ProcessBasicInfo), &Size);
if (! NT_SUCCESS(status))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: ZwQueryInformationProcess failed with status %x\n", status));
return ACTION_NONE;
}
ProcessId = ProcessBasicInfo.UniqueProcessId;
/*
* On win2k ProcessId is not available at this stage yet.
* ProcessId of 0 will get replaced by a real value in CreateProcessNotifyProc.
*/
/* once winlogon.exe executes, we consider the boot process to be complete */
if (BootingUp == TRUE)
{
PCHAR ProcessName;
ProcessName = strrchr(ProcessPathUnresolved, '\\');
if (ProcessName == NULL)
ProcessName = ProcessPathUnresolved;
else
++ProcessName; /* skip past the slash */
if (_strnicmp(ProcessName, "winlogon.exe", 12) == 0)
{
BootingUp = FALSE;
InitPostBootup();
}
}
/*
* Now create a new process entry and load the associated security policy (if any)
*/
NewProcess = CreateNewProcessEntry(ProcessId, CURRENT_PROCESS_PID, &usPath, TRUE);
if (ProcessInsertImagePidEntry(ProcessId, NewProcess) == FALSE)
{
ExFreePoolWithTag(NewProcess, _POOL_TAG);
return ACTION_NONE;
}
/*
* Now find and load appropriate security policy.
*
* Look for a per-user policy first. To do that, we send an SID resolve request
* to userland Ozone Agent Service.
*/
if (LearningMode == FALSE)
{
PSID_RESOLVE_REPLY pSidResolveReply = NULL;
PWSTR UserName = NULL;
if (IssueUserlandSidResolveRequest(NewProcess) != FALSE)
{
if (NewProcess->UserlandReply)
{
pSidResolveReply = (PSID_RESOLVE_REPLY) NewProcess->UserlandReply;
if (pSidResolveReply->UserNameLength)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: SID resolved to %S\n", pSidResolveReply->UserName));
UserName = pSidResolveReply->UserName;
}
else
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess(): SID resolve error\n"));
}
}
}
if (! FindAndLoadSecurityPolicy(&NewProcess->SecPolicy, usPath.Buffer, UserName))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_WARNING, ("%d PostProcessNtCreateProcess(): no policy, %d, %S\n", CURRENT_PROCESS_PID, ProcessId, usPath.Buffer));
//XXX have an option where only processes with existing policies (even if they are empty.. policy_default: allow) are allowed to run
//interactive session is excluded?!?!
if (AllowProcessesWithPoliciesOnly == TRUE)
{
ExFreePoolWithTag(NewProcess, _POOL_TAG);
return ACTION_DENY;
}
}
else
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_WARNING, ("%d PostProcessNtCreateProcess(): with policy, %d, %S\n", CURRENT_PROCESS_PID, ProcessId, usPath.Buffer));
}
if (NewProcess->UserlandReply)
{
ExFreePoolWithTag(NewProcess->UserlandReply, _POOL_TAG);
NewProcess->UserlandReply = NULL;
}
}
/*
* Stack Buffer Overflow Protection (Part 1).
*
* 1. Allocate/reserve a random (< 0x4000000) chunk of virtual memory starting at 0xFFFF.
* This causes the stack of the main thread to be moved by a random amount.
*
* (note1: first 64K of memory are allocated as a guard against NULL pointers dereferencing).
* (note2: main() is mapped at 0x4000000 and thus we cannot allocate anything that will cause the
* stack of the main thread to move past the 0x400000 boundary).
*
*
* Stack Buffer Overflow Protection (Part 2).
*
* 2. Allocate a random (> 4 Megs && < 10 Megs) chunk of virtual memory after the process code segment.
* This causes the heap and stacks non-main threads to be moved by a random amount.
*
* (note: as mentioned above, main() is mapped at 0x4000000, by reserving a large chunk of virtual
* we force Windows to find the first available address beyond the code segment and reserve
* a random amount of memory past it causing other thread stacks and heaps to shift).
*/
#define ONE_MEGABYTE (1024 * 1024)
if (IS_OVERFLOW_PROTECTION_ON(gSecPolicy) && IS_OVERFLOW_PROTECTION_ON(NewProcess->SecPolicy) && LearningMode == FALSE && BootingUp == FALSE)
{
//XXX verify that the image entry is actually at 4 meg (not true for most of system processes)
/*
* allocate up to 3 megs of virtual address space before the code segment,
* this affects main thread stack as well as some heaps
*/
#define FIRST_AVAILABLE_ADDRESS 0xFFFF
Size = PAGE_SIZE + (rand(ProcessId) % (3 * ONE_MEGABYTE));
Base = (PULONG_PTR) FIRST_AVAILABLE_ADDRESS;
status = ZwAllocateVirtualMemory(*ProcessHandle, &Base, 0L, &Size, MEM_RESERVE, PAGE_NOACCESS);
if (! NT_SUCCESS(status))
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: ZwAllocateVirtualMemory1(%x, %x) failed with status %x\n", Base, Size, status));
else
LOG(LOG_SS_PROCESS, LOG_PRIORITY_VERBOSE, ("PostProcessNtCreateProcess: size=%u base=%x status=%d\n", Size, Base, status));
/*
* allocate up to 10 megs of virtual address space after the code segment,
* this affects non-main thread stack as well as some heaps
*/
#define IMAGE_BASE (4 * ONE_MEGABYTE)
Size = IMAGE_BASE + (rand(ProcessId) % (10 * ONE_MEGABYTE));
Base = (PULONG_PTR) NULL;
status = ZwAllocateVirtualMemory(*ProcessHandle, &Base, 0L, &Size, MEM_RESERVE, PAGE_NOACCESS);
LOG(LOG_SS_PROCESS, LOG_PRIORITY_VERBOSE, ("PostProcessNtCreateProcess: size=%u base=%x status=%d\n", Size, Base, status));
if (! NT_SUCCESS(status))
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: ZwAllocateVirtualMemory2(%x, %x) failed with status %x\n", Base, Size, status));
/*
* allocate the entire KnownDll space
*/
//#if 0
// Size = (4 * ONE_MEGABYTE) + (rand(ProcessId) % (100 * ONE_MEGABYTE));
// Base = (PULONG_PTR) 0x71bf0000;
// Size = 0x7000000;//(125 * ONE_MEGABYTE);
// Base = (PULONG_PTR) 0x70000000;
#if HOOK_BOPROT
if (strstr(ProcessPathUnresolved, "stack.exe") != NULL)
{
Size = PAGE_SIZE;
// Base = (PULONG_PTR) 0x77d00000; //user32
Base = (PULONG_PTR) 0x77e30000; //kernel32 on win2k3
// Base = (PULONG_PTR) 0x77e80000; //kernel32 on win2k
status = ZwAllocateVirtualMemory(*ProcessHandle, &Base, 0L, &Size, MEM_RESERVE, PAGE_NOACCESS);
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: kernel32.dll size=%u base=%x status=%d\n", Size, Base, status));
if (! NT_SUCCESS(status))
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("PostProcessNtCreateProcess: ZwAllocateVirtualMemory1(%x, %x) failed with status %x\n", Base, Size, status));
}
else
NewProcess->FirstThread = FALSE;//XXX remove
#endif
}
return ACTION_PERMIT;
}
/*
* HookedNtCreateProcess()
*
* Description:
* This function mediates the NtCreateProcess() system service in order to keep track of all
* the newly created processes.
*
* NOTE: ZwCreateProcess creates a process object. [NAR]
*
* Parameters:
* Those of NtCreateProcess().
*
* Returns:
* NTSTATUS returned by NtCreateProcess().
*/
NTSTATUS
NTAPI
HookedNtCreateProcess
(
OUT PHANDLE ProcessHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN HANDLE InheritFromProcessHandle,
IN BOOLEAN InheritHandles,
IN HANDLE SectionHandle OPTIONAL,
IN HANDLE DebugPort OPTIONAL,
IN HANDLE ExceptionPort OPTIONAL
)
{
HOOK_ROUTINE_ENTER();
ASSERT(OriginalNtCreateProcess);
rc = OriginalNtCreateProcess(ProcessHandle, DesiredAccess, ObjectAttributes, InheritFromProcessHandle,
InheritHandles, SectionHandle, DebugPort, ExceptionPort);
if (NT_SUCCESS(rc))
{
ULONG ret = PostProcessNtCreateProcess(ProcessHandle, SectionHandle);
if (ret == ACTION_DENY || ret == STATUS_ACCESS_DENIED)
{
ZwClose(*ProcessHandle);
rc = STATUS_ACCESS_DENIED;
}
}
HOOK_ROUTINE_EXIT(rc);
}
/*
* HookedNtCreateProcessEx()
*
* Description:
* This function mediates the NtCreateProcessEx() system service in order to keep track of all
* the newly created processes.
*
* NOTE: ZwCreateProcessEx creates a process object. [NAR]
*
* Parameters:
* Those of NtCreateProcessEx().
*
* Returns:
* NTSTATUS returned by NtCreateProcessEx().
*/
NTSTATUS
NTAPI
HookedNtCreateProcessEx
(
OUT PHANDLE ProcessHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN HANDLE InheritFromProcessHandle,
IN ULONG Unknown1,
IN HANDLE SectionHandle OPTIONAL,
IN HANDLE DebugPort OPTIONAL,
IN HANDLE ExceptionPort OPTIONAL,
IN ULONG Unknown2
)
{
HOOK_ROUTINE_ENTER();
ASSERT(OriginalNtCreateProcessEx);
rc = OriginalNtCreateProcessEx(ProcessHandle, DesiredAccess, ObjectAttributes, InheritFromProcessHandle,
Unknown1, SectionHandle, DebugPort, ExceptionPort, Unknown2);
if (NT_SUCCESS(rc))
{
ULONG ret = PostProcessNtCreateProcess(ProcessHandle, SectionHandle);
if (ret == ACTION_DENY || ret == STATUS_ACCESS_DENIED)
{
ZwClose(*ProcessHandle);
rc = STATUS_ACCESS_DENIED;
}
}
HOOK_ROUTINE_EXIT(rc);
}
/*
* HookedNtOpenProcess()
*
* Description:
* This function mediates the NtOpenProcess() system service and disallows certain operations such as
* PROCESS_VM_WRITE and PROCESS_CREATE_THREAD.
*
* NOTE: ZwOpenProcess opens a process object. [NAR]
*
* Parameters:
* Those of NtOpenProcess().
*
* Returns:
* NTSTATUS returned by NtOpenProcess().
*/
NTSTATUS
NTAPI
HookedNtOpenProcess
(
OUT PHANDLE ProcessHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN PCLIENT_ID ClientId OPTIONAL
)
{
PCHAR FunctionName = "HookedNtOpenProcess";
CHAR PROCESSNAME[MAX_PATH];
//taskmgr uses PROCESS_TERMINATE to kill processes
//XXX IPD disallows PROCESS_CREATE_THREAD|PROCESS_VM_WRITE
/*
PROCESS_TERMINATE Terminate process
PROCESS_CREATE_THREAD Create threads in process
PROCESS_SET_SESSIONID Set process session id
PROCESS_VM_OPERATION Protect and lock memory of process
PROCESS_VM_READ Read memory of process
PROCESS_VM_WRITE Write memory of process
PROCESS_DUP_HANDLE Duplicate handles of process
PROCESS_CREATE_PROCESS Bequeath address space and handles to new process
PROCESS_SET_QUOTA Set process quotas
PROCESS_SET_INFORMATION Set information about process
PROCESS_QUERY_INFORMATION Query information about process
PROCESS_SET_PORT Set process exception or debug port
PROCESS_ALL_ACCESS All of the preceding
find out who uses which flags, i.e. VM_READ, etc.. filter out accordingly
*/
HOOK_ROUTINE_ENTER();
// if (! IS_BIT_SET(DesiredAccess, PROCESS_QUERY_INFORMATION) &&
// ! IS_BIT_SET(DesiredAccess, PROCESS_DUP_HANDLE) &&
// ! IS_BIT_SET(DesiredAccess, SYNCHRONIZE) )
if (! ARGUMENT_PRESENT(ClientId) || KeGetPreviousMode() != UserMode || KeGetCurrentIrql() != PASSIVE_LEVEL)
goto done;
__try
{
ProbeForRead(ClientId, sizeof(*ClientId), sizeof(PULONG_PTR));
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
NTSTATUS status = GetExceptionCode();
LOG(LOG_SS_MISC, LOG_PRIORITY_DEBUG, ("%s: caught an exception. address=%x, status = 0x%x\n", FunctionName, ClientId, status));
goto done;
}
if (PsGetCurrentProcessId() != ClientId->UniqueProcess &&
(ULONG) PsGetCurrentProcessId() != SystemProcessId &&
ClientId->UniqueProcess != 0)
{
PIMAGE_PID_ENTRY p;
/* can't access ClientId (pageable memory) while holding spinlonk */
ULONG RequestedProcessId = (ULONG) ClientId->UniqueProcess;
//XXX
/*
if (RequestedProcessId == UserAgentServicePid)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_VERBOSE, ("%s: FindImagePidEntry(%d) UserAgent %x\n", FunctionName, RequestedProcessId, DesiredAccess));
if (IS_BIT_SET(DesiredAccess, PROCESS_TERMINATE))
{
HOOK_ROUTINE_EXIT( STATUS_ACCESS_DENIED );
}
}
*/
p = FindImagePidEntry(RequestedProcessId, 0);
if (p == NULL)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%d %s: FindImagePidEntry(%d) failed\n", CURRENT_PROCESS_PID, FunctionName, RequestedProcessId));
goto done;
}
/*
if (DesiredAccess & PROCESS_CREATE_THREAD)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%d %s(%d): %x (PROCESS_CREATE_THREAD). (%S)\n", CURRENT_PROCESS_PID, FunctionName, RequestedProcessId, DesiredAccess, p->ImageName));
}
else if (DesiredAccess & PROCESS_VM_WRITE)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%d %s(%d): %x (PROCESS_VM_WRITE). (%S)\n", CURRENT_PROCESS_PID, FunctionName, RequestedProcessId, DesiredAccess, p->ImageName));
}
else
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_VERBOSE, ("%d %s(%d): %x. (%S)\n", CURRENT_PROCESS_PID, FunctionName, RequestedProcessId, DesiredAccess, p->ImageName));
}
*/
if (LearningMode == FALSE)
{
CHAR ProcessPathUnresolved[MAX_PATH];
_snprintf(ProcessPathUnresolved, MAX_PATH, "%S", p->ImageName);
ProcessPathUnresolved[ MAX_PATH - 1 ] = 0;
FixupFilename(ProcessPathUnresolved, PROCESSNAME, MAX_PATH);
POLICY_CHECK_OPTYPE_NAME(PROCESS, OP_PROC_OPEN);
}
else
{
// learning mode
_snprintf(PROCESSNAME, MAX_PATH, "%S", p->ImageName);
PROCESSNAME[ MAX_PATH - 1 ] = 0;
AddRule(RULE_PROCESS, PROCESSNAME, OP_PROC_OPEN);
}
}
if (LearningMode == FALSE && GetPathFromOA(ObjectAttributes, PROCESSNAME, MAX_PATH, RESOLVE_LINKS))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%d %s: %s SPECIAL CASE\n", (ULONG) PsGetCurrentProcessId(), FunctionName, PROCESSNAME));
}
done:
ASSERT(OriginalNtOpenProcess);
rc = OriginalNtOpenProcess(ProcessHandle, DesiredAccess, ObjectAttributes, ClientId);
HOOK_ROUTINE_EXIT(rc);
}
/*
* HookedNtCreateThread()
*
* Description:
* This function mediates the NtCreateThread() system service in order to randomize thread stack
* and inject userland dll into newly created main threads.
*
* NOTE: ZwCreateThread creates a thread in a process. [NAR]
*
* Parameters:
* Those of NtCreateThread().
*
* Returns:
* NTSTATUS returned by NtCreateThread().
*/
NTSTATUS
NTAPI
HookedNtCreateThread
(
OUT PHANDLE ThreadHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN HANDLE ProcessHandle,
OUT PCLIENT_ID ClientId,
IN PCONTEXT ThreadContext,
IN PUSER_STACK UserStack,
IN BOOLEAN CreateSuspended
)
{
PEPROCESS proc = NULL;
USHORT StackOffset = 0;
PCHAR FunctionName = "HookedNtCreateThread";
HOOK_ROUTINE_ENTER();
if (ARGUMENT_PRESENT(ThreadContext) && KeGetPreviousMode() == UserMode && LearningMode == FALSE && BootingUp == FALSE)
{
NTSTATUS status;
ULONG ProcessId;
PCHAR InstructionAddress;
ULONG Size;
PCHAR Base;
PROCESS_BASIC_INFORMATION ProcessBasicInfo;
ULONG ret;
PIMAGE_PID_ENTRY p;
VerifyUserReturnAddress();
/* verify userland parameter threadcontext */
__try
{
ProbeForRead(ThreadContext, sizeof(*ThreadContext), sizeof(ULONG));
ProbeForWrite(ThreadContext, sizeof(*ThreadContext), sizeof(ULONG));
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
NTSTATUS status = GetExceptionCode();
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%s: caught an exception. status = 0x%x\n", FunctionName, status));
goto done;
}
if (ThreadContext->Eax > SystemAddressStart)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%s: eax=%x > %x (SystemAddressStart)\n", FunctionName, ThreadContext->Eax, SystemAddressStart));
goto done;
}
/* retrieve the Process ID */
status = ZwQueryInformationProcess(ProcessHandle, ProcessBasicInformation, &ProcessBasicInfo, sizeof(ProcessBasicInfo), &Size);
if (! NT_SUCCESS(status))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%s: ZwQueryInformationProcess failed with status %x\n", FunctionName, status));
goto done;
}
ProcessId = ProcessBasicInfo.UniqueProcessId;
/*
* if ProcessId is 0 then the pid has not been assigned yet and we are the primary thread.
* in that case pass our pid (we are still running in the context of the parent process) as the ParentId
* to make sure we find the right process (since theoretically there can be more than one process with a
* pid of 0)
*/
p = FindImagePidEntry(ProcessId, ProcessId == 0 ? CURRENT_PROCESS_PID : 0);
if (p == NULL)
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%d %s: FindImagePidEntry(%d) failed\n", CURRENT_PROCESS_PID, FunctionName, ProcessId));
goto done;
}
/*
* Stack Buffer Overflow Protection (Part 3).
*
* Allocate/reserve a random (< 1024 bytes) part of a thread's stack space.
* This causes the least significant 10 bits to be randomized as well.
* (without this, the least significant 16 bits are always the same).
*/
/* save the stackoffset for now since we are holding a spinlock and cannot touch pageable memory */
//XXX we are not holding the spinlock here but are still accessing various p-> fields
if (IS_OVERFLOW_PROTECTION_ON(gSecPolicy) && IS_OVERFLOW_PROTECTION_ON(p->SecPolicy))
StackOffset = (USHORT) (16 + (rand(ThreadContext->Eax) % 63) * 16);
if (! IS_USERLAND_PROTECTION_ON(gSecPolicy) || ! IS_USERLAND_PROTECTION_ON(p->SecPolicy))
goto done;
/* Userland DLL needs to be loaded only once (by the first/main thread) */
if (p->FirstThread != TRUE)
goto done;
p->FirstThread = FALSE;
//XXX investigate MEM_WRITE_WATCH (supported on i386?)
/*
* Inject the userland DLL into the process.
*
* This is achieved by allocating 1 page of memory in the address space of a "victim" process.
* Then the LdrLoadDll(our_dll) code is written to the allocated page and victim's thread EIP
* is changed to point to our code.
* As soon as the thread executes, it will load our DLL and then jump to the original entry point.
*
* When not given explicit directions, the Microsoft linker creates each DLL with a
* preferred load address of 0x10000000. By loading our DLL first, we cause the rest of
* the application DLLs to load at a different address.
*/
/* allocate 1 page of commited rwx memory */
Size = PAGE_SIZE;
Base = NULL;
status = ZwAllocateVirtualMemory(ProcessHandle, &Base, 0L, &Size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
if (! NT_SUCCESS(status))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%s: ZwAllocateVirtualMemory(%x, %x) failed with status %x\n", FunctionName, Base, Size, status));
goto done;
}
status = ObReferenceObjectByHandle(ProcessHandle, PROCESS_ALL_ACCESS, 0, KernelMode, &proc, NULL);
if (! NT_SUCCESS(status))
{
LOG(LOG_SS_PROCESS, LOG_PRIORITY_DEBUG, ("%s: ObReferenceObjectByHandle(ProcessHandle) failed with status %x\n", FunctionName, status));
goto done;
}
try
{
ULONG CodeAddress, DllPathAddress;
ULONG ThreadEntryPoint = ThreadContext->Eax; /* thread entry point is stored in the EAX register */
PWSTR InjectDllName = L"ozoneusr.dll";
USHORT InjectDllNameSize = (wcslen(InjectDllName) + 1) * sizeof(WCHAR);
/*
* Execute the DLL inject in the memory context of a "victim" process
*/
KeAttachProcess(proc);
{
/* probe the memory, just in case */
ProbeForRead(Base, PAGE_SIZE, 1);
ProbeForWrite(Base, PAGE_SIZE, 1);
/*
* Memory Layout used for DLL injection
*
* Byte Value
*
* 0..4 Original EIP
* 4..8 LdrLoadDll() output handle
* 8..16 LdrLoadDll() DllName UNICODE_STRING structure
* 16..? DllName.Buffer WCHAR string
* ?..? DllPath WCHAR string
* .... assembler code (to call LdrLoadDll() and then jmp to the original EIP)
*/
#define BASE_PTR Base
#define ADDRESS_ORIGINAL_EIP (BASE_PTR + 0)
#define ADDRESS_DLL_HANDLE (BASE_PTR + 4)
#define ADDRESS_DLL_NAME (BASE_PTR + 8)
/* save the original thread entry point */
* (PULONG) ADDRESS_ORIGINAL_EIP = ThreadEntryPoint;
/* skip past eip and output handle (8 bytes) */
InstructionAddress = ADDRESS_DLL_NAME;
/*
* Create a UNICODE_STRING structure
*/
* ((PUSHORT) InstructionAddress)++ = InjectDllNameSize; /* UNICODE_STRING.Length */
* ((PUSHORT) InstructionAddress)++ = InjectDllNameSize; /* UNICODE_STRING.MaximumLength */
/* UNICODE_STRING.Buffer (points to unicode string directly following the buffer) */
* ((PULONG) InstructionAddress)++ = (ULONG) (InstructionAddress + sizeof(PWSTR));
/* the actual DllName.Buffer value */
wcscpy((PWSTR) InstructionAddress, InjectDllName);
InstructionAddress += InjectDllNameSize;