获取加载后就自动删除的驱动SYS文件Word格式文档下载.docx
《获取加载后就自动删除的驱动SYS文件Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《获取加载后就自动删除的驱动SYS文件Word格式文档下载.docx(15页珍藏版)》请在冰点文库上搜索。
刚在路上突然想到,下面的开关中断是不是应该KeAquireSpinLock一下,因为如果是多核的话,其他的核在读取地址的时候,万一正在修改的话会不会出现BSOD?
?
看了某驱动用的是InterLockedExChange来锁住CPU的地址线,应该要加吧。
1#include<
ntifs.h>
2
3externULONG_PTRKeServiceDescriptorTable;
4
5typedefNTSTATUS(__stdcall*ZWLOADDRIVER)(INPUNICODE_STRINGDriverServiceName);
6
7ZWLOADDRIVEROriginalZwLoadDriver;
8
9DRIVER_INITIALIZEDriverEntry;
10DRIVER_UNLOADDriverUnload;
11
12NTSTATUSZwCopyFile(PUNICODE_STRINGDestinationFileName,PUNICODE_STRINGSourceFileName)
13{
14NTSTATUSstatus;
15HANDLESourceFileHandle=NULL;
16HANDLEDestinationFileHandle=NULL;
17OBJECT_ATTRIBUTESObjectAttributes;
18IO_STATUS_BLOCKIoStatusBlock;
19FILE_STANDARD_INFORMATIONFileInfo;
20ULONGAllocationSize;
21PVOIDFileBuffer=NULL;
22BOOLEANbAllocateInVirtualMemory=FALSE;
23
24InitializeObjectAttributes(&
ObjectAttributes,
25SourceFileName,
26OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,
27NULL,
28NULL);
29status=IoCreateFile(&
SourceFileHandle,
30GENERIC_READ|SYNCHRONIZE,
31&
32&
IoStatusBlock,
33NULL,
34FILE_ATTRIBUTE_NORMAL,
35FILE_SHARE_READ,
36FILE_OPEN,
37FILE_SYNCHRONOUS_IO_NONALERT,
38NULL,
390,
40CreateFileTypeNone,
410,
42IO_NO_PARAMETER_CHECKING);
43if(!
NT_SUCCESS(status))
44{
45DbgPrint("
IoCreateFile(%wZ)failed,eid=0x%08x\n"
SourceFileName,status);
46gotocleanup;
47}
48
49//DbgPrint("
Open%wZsuccess!
SourceFileName);
50
51status=ZwQueryInformationFile(
52SourceFileHandle,
53&
54(PVOID)&
FileInfo,
55sizeof(FileInfo),
56FileStandardInformation);
57if(!
58{
59DbgPrint("
ZwQueryFileInformation(%wZ)failed,eid=0x%08x\n"
60gotocleanup;
61}
62
63//DbgPrint("
ZwQueryInformationFilesuccess!
64
65AllocationSize=FileInfo.AllocationSize.LowPart;
66
67FileBuffer=ExAllocatePoolWithTag(PagedPool,AllocationSize,'
CODE'
68if(!
FileBuffer)
69{
70status=ZwAllocateVirtualMemory((HANDLE)(-1),
71(PVOID)&
FileBuffer,
720,
73&
AllocationSize,
74MEM_COMMIT,
75PAGE_READWRITE);
76if(!
77{
78DbgPrint("
CannotAllocateSuchLargeBuffer!
79gotocleanup;
80}
81bAllocateInVirtualMemory=TRUE;
82}
83
84status=ZwReadFile(SourceFileHandle,
85NULL,
86NULL,
87NULL,
88&
89FileBuffer,
90AllocationSize,
91NULL,
92NULL);
93
94if(!
95{
96DbgPrint("
ZwReadFile(%wZ)failed,eid=0x%08x\n"
97gotocleanup;
98}
99
100InitializeObjectAttributes(&
101DestinationFileName,
102OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,
103NULL,
104NULL);
105status=IoCreateFile(&
DestinationFileHandle,
106GENERIC_READ|GENERIC_WRITE,
107&
108&
109NULL,
110FILE_ATTRIBUTE_NORMAL,
111FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
112FILE_OVERWRITE_IF,
113FILE_SYNCHRONOUS_IO_NONALERT,
114NULL,
1150,
116CreateFileTypeNone,
117NULL,
118IO_NO_PARAMETER_CHECKING);
119if(!
120{
121DbgPrint("
DestinationFileName,status);
122gotocleanup;
123}
124
125status=ZwWriteFile(DestinationFileHandle,
126NULL,
127NULL,
128NULL,
129&
130FileBuffer,
131AllocationSize,
132NULL,
133NULL);
134
135if(!
136DbgPrint("
ZwWriteFile(%wZ)failed,eid=0x%08x\n"
137
138cleanup:
139if(bAllocateInVirtualMemory)
140ZwFreeVirtualMemory((HANDLE)(-1),(PVOID)&
FileBuffer,&
AllocationSize,MEM_RELEASE);
141elseif(FileBuffer)
142ExFreePoolWithTag(FileBuffer,'
143if(SourceFileHandle)
144ZwClose(SourceFileHandle);
145if(DestinationFileHandle)
146ZwClose(DestinationFileHandle);
147
148returnstatus;
149}
150
151NTSTATUSFilterZwLoadDriver(INPUNICODE_STRINGDriverServiceName)
152{
153NTSTATUSstatus;
154HANDLEServiceKeyHandle;
155OBJECT_ATTRIBUTESObjectAttribute;
156UNICODE_STRINGusValueKey;
157UNICODE_STRINGusDestinationFileName;
158UNICODE_STRINGusSourceFileName;
159ULONGcbNeeded;
160PKEY_VALUE_PARTIAL_INFORMATIONInfo;
161WCHARszDestinationFileName[260]=L"
\\?
\\C:
\\"
;
162WCHAR*EndPointer;
163
164//DbgPrint("
SSDTHOOKZwLoadDriversuccess!
165
166InitializeObjectAttributes(&
ObjectAttribute,DriverServiceName,OBJ_CASE_INSENSITIVE|OBJ_KERNEL_HANDLE,NULL,NULL);
167status=ZwOpenKey(&
ServiceKeyHandle,KEY_READ,&
ObjectAttribute);
168if(!
169{
170DbgPrint("
ZwOpenKeyfailed,eid=0x%08x!
status);
171returnstatus;
172}
173
174//DbgPrint("
ZwOpenKey(%wZ)success!
DriverServiceName);
175
176RtlInitUnicodeString(&
usValueKey,L"
ImagePath"
177ZwQueryValueKey(ServiceKeyHandle,&
usValueKey,KeyValuePartialInformation,NULL,0,&
cbNeeded);
178//DbgPrint("
cbNeeded=%d\n"
cbNeeded);
179
180Info=ExAllocatePoolWithTag(PagedPool,cbNeeded,'
181status=ZwQueryValueKey(ServiceKeyHandle,&
usValueKey,KeyValuePartialInformation,(PVOID)Info,cbNeeded,&
182if(!
183{
184DbgPrint("
ZwQueryValueKeyfailed,eid=0x%08x\n"
185ExFreePoolWithTag(Info,'
186returnstatus;
187}
188
189ZwClose(ServiceKeyHandle);
190
191//DbgPrint("
Type=%d,ImagePath=%ws,DataLength=%d\n"
Info->
Type,Info->
Data,Info->
DataLength);
//includeUNICODE_NULL_TERMINATOR
192
193RtlInitUnicodeString(&
usSourceFileName,(PCWSTR)(Info->
Data));
194
195EndPointer=(WCHAR*)(Info->
Data+Info->
196
197while(*EndPointer!
='
\\'
)
198EndPointer--;
199memcpy(szDestinationFileName+wcslen(szDestinationFileName),EndPointer+1,((ULONG)(Info->
Data)+Info->
DataLength-(ULONG)EndPointer));
200
201RtlInitUnicodeString(&
usDestinationFileName,szDestinationFileName);
202
203//DbgPrint("
Destination=%wZ\n"
&
usDestinationFileName);
204
205ExFreePoolWithTag(Info,'
206
207status=ZwCopyFile(&
usDestinationFileName,&
usSourceFileName);
208if(!
209{
210DbgPrint("
ZwCopyFilefailed,eid=%d!
211returnstatus;
212}
213
214DbgPrint("
CopyFile%ws--->
%wssuccess!
(&
usSourceFileName)->
Buffer+wcslen(L"
),(&
usDestinationFileName)->
));
215
216DbgPrint("
MissionComplete,Congratulations!
217
218return(OriginalZwLoadDriver)(DriverServiceName);
219}
220
221VOIDSSDT_HOOK_ZwLoadDriver()
222{
223__asm
224{
225cli
226pusheax
227moveax,CR0
228andeax,0FFFEFFFFh
229movCR0,eax
230popeax
231}
232
233_asm{
234movecx,dwordptr[ZwLoadDriver];
235movedx,[ecx+1];
236moveax,dwordptr[KeServiceDescriptorTable];
237movesi,[eax];
238movedx,[esi+edx*4];
239movdwordptr[OriginalZwLoadDriver],edx
240movecx,[ecx+1]
241moveax,[eax]
242movdwordptr[eax+ecx*4],offsetFilterZwLoadDriver;
243}
244
245__asm
246{
247pusheax
248moveax,CR0
249oreax,NOT0FFFEFFFFh
250movCR0,eax
251popeax
252sti
253}
254}
255
256VOIDSSDT_UNHOOK_ZwLoadDriver()
257{
258__asm
259{
260cli
261pusheax
262moveax,CR0
263andeax,0FFFEFFFFh
264movCR0,eax
265popeax
266}
267
268_asm{
269
270movecx,dwordptr[ZwLoadDriver];
271movedx,[ecx+1];
272moveax,dwordptr[KeServiceDescriptorTable];
273movesi,[eax];
274movebx,dwordptr[OriginalZwLoadDriver];
275mov[esi+edx*4],ebx;
276}
277
278__asm
279{
280pusheax
281moveax,CR0
282oreax,NOT0FFFEFFFFh
283movCR0,eax
284popeax
285sti
286}
287}
288
289NTSTATUSDriverEntry(PDRIVER_OBJECTDriverObject,PUNICODE_STRINGRegPath)
290{
291SSDT_HOOK_ZwLoadDriver();
292
293DriverObject->
DriverUnload=DriverUnload;
294
295returnSTATUS_SUCCESS;
296}
297
298VOIDDriverUnload(PDRIVER_OBJECTDriverObject)
299{
300SSDT_UNHOOK_ZwLoadDriver();
301}