本帖最后由 YFSafe 于 2023-7-9 11:54 编辑
驱动程序的代码:
[C++] 纯文本查看 复制代码 #include <ntifs.h>
#include <ntddk.h>
#include "SetPriorityCommon.h"
void DriverUnload(PDRIVER_OBJECT DriverObject);
NTSTATUS SetPriorityCreateClose(PDEVICE_OBJECT DeviceObject,PIRP Irp);
NTSTATUS SetPriorityDeviceControl(PDEVICE_OBJECT DeviceObject,PIRP Irp);
/*
extern "C" NTSTATUS
DriverEntry(_In_ PDRIVER_OBJECT DriverObject, _In_ PUNICODE_STRING RegistryPath) {
UNREFERENCED_PARAMETER(RegistryPath);
KdPrint(("PriorityBooster DriverEntry started\n"));
DriverObject->DriverUnload = DriverUnload;
DriverObject->MajorFunction[IRP_MJ_CREATE] = SetPriorityCreateClose;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = SetPriorityCreateClose;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SetPriorityDeviceControl;
UNICODE_STRING devName = RTL_CONSTANT_STRING(L"\\Device\\SetPriority");
PDEVICE_OBJECT DeviceObject;
NTSTATUS status = IoCreateDevice(
DriverObject,
0,
&devName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&DeviceObject);
if(!NT_SUCCESS(status))
{
DbgPrint("CreateDevice Failed!!!");
return status;
}
UNICODE_STRING symLink = RTL_CONSTANT_STRING(L"\\?\\SetPriority");
status = IoCreateSymbolicLink(&symLink,&devName);
if(!NT_SUCCESS(status))
{
DbgPrint("Create symLink Failed!!!");
IoDeleteDevice(DeviceObject);
return status;
}
KdPrint(("SetPriority DriverEntry completed successfully\n"));
return STATUS_SUCCESS;
}
void DriverUnload(PDRIVER_OBJECT DriverObject)
{
UNICODE_STRING symLink = RTL_CONSTANT_STRING(L"\\?\\SetPriority");
IoDeleteSymbolicLink(&symLink);
IoDeleteDevice(DriverObject->DeviceObject);
}
NTSTATUS SetPriorityCreateClose(PDEVICE_OBJECT DeviceObject,PIRP Irp)
{
Irp->IoStatus.Status = STATUS_SUCCESS;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
NTSTATUS SetPriorityDeviceControl(PDEVICE_OBJECT,PIRP Irp)
{
auto stack = IoGetCurrentIrpStackLocation(Irp);
auto status = STATUS_SUCCESS;
switch(stack->Parameters.DeviceIoControl.IoControlCode)
{
case IOCTL_SET_PRIORITY:
//TO DO:修改优先级
if(stack->Parameters.DeviceIoControl.InputBufferLength < sizeof(ThreadData))
{
status = STATUS_BUFFER_TOO_SMALL;
break;
}
auto data = (ThreadData*)stack->Parameters.DeviceIoControl.Type3InputBuffer;
if(data == NULL)
{
status = STATUS_INVALID_PARAMETER;
break;
}
if(data->Priority < 1 || data->Priority > 31)
{
status = STATUS_INVALID_PARAMETER;
break;
}
PETHREAD Thread;
status = PsLookupProcessByProcessId(ULongToHandle(data->ThreadId),&Thread);
if(!NT_SUCCESS(status))
break;
KeSetPriorityThread((PKTHREAD)Thread,data->Priority);
ObDereferenceObject(Thread);
break;
default:
status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
Irp->IoStatus.Status = status;
Irp->IoStatus.Information = 0;
IoCompleteRequest(Irp,IO_NO_INCREMENT);
return status;
}
SetPriorityCommon.h:
[C++] 纯文本查看 复制代码 #define SET_PRIORITY_DEVICE 0x8000
#define IOCTL_SET_PRIORITY CTL_CODE(SET_PRIORITY_DEVICE,0x800,METHOD_NEITHER,FILE_ANY_ACCESS)
struct ThreadData
{
ULONG ThreadId;
int Priority;
};
客户端程序:
[C++] 纯文本查看 复制代码 #include <windows.h>
#include <stdio.h>
#include "..\SetPriority\SetPriorityCommon.h"
using namespace std;
int PrintError(const char * msg)
{
printf("%s (error = %d)\n", msg, GetLastError());
return 114514;
}
int main(int argc,const char* argv[])
{
if(argc < 3)
{
printf("Usage: SetPriority <ThreadId> <Priority>\n");
return 0;
}
HANDLE hDevice = CreateFile(L"\\\\.\\SetPriority",GENERIC_WRITE,FILE_SHARE_WRITE,nullptr,OPEN_EXISTING,0,nullptr);
if(hDevice == NULL)
return PrintError("Failed to open device!!!");
ThreadData data;
data.ThreadId = atoi(argv[1]);
data.Priority = atoi(argv[2]);
DWORD dReturned;
BOOL status = DeviceIoControl(hDevice,
IOCTL_SET_PRIORITY,
&data,sizeof(data),
nullptr,0,
&dReturned,nullptr);
if(status)
printf("Thread Priority Change Success!!!");
else
PrintError("Priority Change Failed!!!");
CloseHandle(hDevice);
}
|