-
Notifications
You must be signed in to change notification settings - Fork 4
/
ProcessFullPath.c
138 lines (125 loc) · 3.78 KB
/
ProcessFullPath.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
#include<ntddk.h>
VOID UnloadDriver(PDRIVER_OBJECT pDriver);
VOID CreateProcessRoutineSpy(
IN HANDLE ParentID,
IN HANDLE ProcessID,
IN BOOLEAN Create
);
NTSTATUS GetCurrentProcessImageFullPath(PUNICODE_STRING ProcessImageName);
NTSTATUS DriverEntry(PDRIVER_OBJECT pDriver, PUNICODE_STRING Registry)
{
NTSTATUS status = STATUS_SUCCESS;
UNREFERENCED_PARAMETER(pDriver);
UNREFERENCED_PARAMETER(Registry);
KdPrint(("gxb Driver install success\n"));
status = PsSetCreateProcessNotifyRoutine(CreateProcessRoutineSpy, FALSE);
if (!NT_SUCCESS(status))
{
KdPrint(("PsSetCreateProcessNotifyRoutine faile status: %d \n", status));
}
pDriver->DriverUnload = UnloadDriver;
return status;
}
VOID UnloadDriver(PDRIVER_OBJECT pDriver)
{
UNREFERENCED_PARAMETER(pDriver);
NTSTATUS status;
status = PsSetCreateProcessNotifyRoutine(CreateProcessRoutineSpy,TRUE);
if (!NT_SUCCESS(status))
{
KdPrint(("uninstall PsSetCreateThreadNotifyRoutine is faile status : %d\n ",status));
}
return;
}
VOID CreateProcessRoutineSpy(
IN HANDLE ParentID,
IN HANDLE ProcessID,
IN BOOLEAN Create
)
{
UNREFERENCED_PARAMETER(ParentID);
UNREFERENCED_PARAMETER(ProcessID);
UNREFERENCED_PARAMETER(Create);
//HANDLE ProcessHandle;
NTSTATUS re;
WCHAR s[1024] = L"";
UNICODE_STRING ProcessImageName;
if (Create)
{
KdPrint(("Process Created.ParentID: (%d) Processid %d\n", ParentID, ProcessID));
}
else{
KdPrint(("Process Terminated.ParentID: (%d) Processid %d\n", ParentID, ProcessID));
}
ProcessImageName.Buffer = NULL;
ProcessImageName.Length =0;
ProcessImageName.MaximumLength = 0;
re = GetCurrentProcessImageFullPath(&ProcessImageName);
if (STATUS_BUFFER_OVERFLOW == re)
{
ProcessImageName.Buffer = ExAllocatePoolWithTag(NonPagedPool,
ProcessImageName.Length,
'gxb');
ProcessImageName.MaximumLength = ProcessImageName.Length;
re = GetCurrentProcessImageFullPath(&ProcessImageName);
}
KdPrint(("sssssssssssssss%wZ \n", ProcessImageName.Buffer));
}
/*
/第一次调用改函数,将会在PUNICODE_STRING的变量中存放路径的大小
/第二次进行实际的调用操作。
*/
NTSTATUS GetCurrentProcessImageFullPath(_Out_ PUNICODE_STRING ProcessImageName)
{
typedef NTSTATUS NTAPI NTQUERYINFORMATIONPROCESS(
_In_ HANDLE ProcessHandle,
_In_ PROCESSINFOCLASS ProcessInformationClass,
_Out_ PVOID ProcessInformation,
_In_ ULONG ProcessInformationLength,
_Out_opt_ PULONG ReturnLength
);
typedef NTQUERYINFORMATIONPROCESS FAR * LPNTQUERYINFORMATIONPROCESS;
NTSTATUS status=STATUS_SUCCESS;
ULONG returnedLength;
LPNTQUERYINFORMATIONPROCESS ZwQueryInformationProcess = NULL;
PAGED_CODE();
if (NULL == ZwQueryInformationProcess)
{
UNICODE_STRING routinName;
RtlInitUnicodeString(&routinName, L"ZwQueryInformationProcess");
ZwQueryInformationProcess = (LPNTQUERYINFORMATIONPROCESS)MmGetSystemRoutineAddress(&routinName);
if (NULL == ZwQueryInformationProcess)
KdPrint(("Cannot resolve ZwQueryInformationProcess\n"));
}
status = ZwQueryInformationProcess(NtCurrentProcess(),
ProcessImageFileName,
NULL,//buffer
0, //buffer size
&returnedLength
);
if (STATUS_INFO_LENGTH_MISMATCH != status)
return status;
//
// Is the passed-in buffer going to be big enough for us?
// This function returns a single contguous buffer model...
//
if (ProcessImageName->MaximumLength < returnedLength) {
ProcessImageName->Length =(USHORT) returnedLength;
KdPrint(("ProcessImageName's Buffer Is Toooo small %d \r\n", returnedLength));
return STATUS_BUFFER_OVERFLOW;
}
//
// If we get here, the buffer IS going to be big enough for us, so
// let's allocate some storage.
//
status = ZwQueryInformationProcess(NtCurrentProcess(),
ProcessImageFileName,
ProcessImageName->Buffer,
ProcessImageName->Length,
&returnedLength);
if (NT_SUCCESS(status))
{
KdPrint(("current n %wz\n", ProcessImageName->Buffer));
}
return status;
}