删除
#define CTL_DELETE_PATH IRP_IOCTRL_CODE(5)
#define CTL_COPY_SPATH IRP_IOCTRL_CODE(6)
#define CTL_COPY_DPATH IRP_IOCTRL_CODE(7)
#define CTL_MOVE_SPATH IRP_IOCTRL_CODE(8)
#define CTL_MOVE_DPATH IRP_IOCTRL_CODE(9)
...
...
//删除文件的函数实现
NTSTATUS DeleteFileIoCall(char* filePath) {
NTSYSAPI NTSTATUS ZwDeleteFile(
POBJECT_ATTRIBUTES ObjectAttributes
);
//状态码
NTSTATUS ntSTATUS = STATUS_SUCCESS;
//文件句柄
HANDLE hFile = NULL;
//完成状态
IO_STATUS_BLOCK Iostatus = { 0 };
//对象属性
OBJECT_ATTRIBUTES ObjectAtt = { 0 };
//三环c:\a.txt
//0环:\\??\\c:\a.txt
//将三环路径转换成驱动使用的路径
ANSI_STRING asFilePath = { 0 };
UNICODE_STRING usFilePath = { 0 };
UNICODE_STRING usDriverFilePath = { 0 };
UNICODE_STRING usDrvPath = { 0 };
WCHAR wcBuffer[256];
ULONG wcbufferLen = 256 * sizeof(WCHAR);
RtlInitEmptyUnicodeString(&usDrvPath, &wcBuffer, wcbufferLen);
RtlInitUnicodeString(&usDriverFilePath, L"\\??\\");
RtlInitAnsiString(&asFilePath, filePath);
RtlAnsiStringToUnicodeString(&usFilePath, &asFilePath, TRUE);
RtlAppendUnicodeStringToString(&usDrvPath, &usDriverFilePath);
RtlAppendUnicodeStringToString(&usDrvPath, &usFilePath);
RtlFreeUnicodeString(&usFilePath);
InitializeObjectAttributes(&ObjectAtt, &usDrvPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
ntSTATUS = ZwDeleteFile(&ObjectAtt);
return ntSTATUS;
}
//复制文件的函数实现
NTSTATUS CopyFileIoCall(char *szFilePath) {//目的路径
//1.读取文件
ReadFileIoCall(g_FilePath);//全局路径,如果直接写就写回去了,我们得修改全局路径
RtlZeroMemory(g_FilePath, 0x256);
ULONG uDFillPathLength = strlen(szFilePath);
RtlCopyMemory(g_FilePath, szFilePath, uDFillPathLength);
//2.写入文件
WriteFileIoCall(g_Buffer);
}
//文件操作-移动文件
NTSTATUS MoveFileIoCall(char* szFilePath) {//目的路径
//1.读取文件
ReadFileIoCall(g_FilePath);//全局路径,如果直接写就写回去了,我们得修改全局路径
//源文件删除
DeleteFileIoCall(g_FilePath);//就多了一行删除文件
RtlZeroMemory(g_FilePath, 0x256);
ULONG uDFillPathLength = strlen(szFilePath);
RtlCopyMemory(g_FilePath, szFilePath, uDFillPathLength);
//2.写入文件
WriteFileIoCall(g_Buffer);
}
...
...
case CTL_DELETE_PATH: {
ntStatus = DeleteFileIoCall(pInputBuffer);
if (NT_SUCCESS(ntStatus))
{
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Delete Success!");
RtlCopyMemory(pOutputBuffer, "Delete Success!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
else {
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Delete Failed!");
RtlCopyMemory(pOutputBuffer, "Delete Failed!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
return STATUS_SUCCESS;
}
case CTL_COPY_SPATH: {
RtlZeroMemory(g_FilePath, 0x256);
RtlCopyMemory(g_FilePath, pInputBuffer, uInputLength);
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Success!");
RtlCopyMemory(pOutputBuffer, "Success!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
case CTL_COPY_DPATH: {
ntStatus = CopyFileIoCall(pInputBuffer);
if (NT_SUCCESS(ntStatus))
{
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Copy Success!");
RtlCopyMemory(pOutputBuffer, "Copy Success!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
else {
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Copy Failed!");
RtlCopyMemory(pOutputBuffer, "Copy Failed!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
return STATUS_SUCCESS;
}
case CTL_MOVE_SPATH: {
RtlZeroMemory(g_FilePath, 0x256);
RtlCopyMemory(g_FilePath, pInputBuffer, uInputLength);
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Success!");
RtlCopyMemory(pOutputBuffer, "Success!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
case CTL_MOVE_DPATH: {
ntStatus = MoveFileIoCall(pInputBuffer);
if (NT_SUCCESS(ntStatus))
{
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Move Success!");
RtlCopyMemory(pOutputBuffer, "Move Success!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
else {
RtlZeroMemory(pOutputBuffer, 1024);
ULONG uRetlength = strlen("Move Failed!");
RtlCopyMemory(pOutputBuffer, "Move Failed!", uRetlength);
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = uRetlength;
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
return STATUS_SUCCESS;
}
3环代码
#define CTL_DELETE_PATH IRP_IOCTRL_CODE(5)
#define CTL_COPY_SPATH IRP_IOCTRL_CODE(6)
#define CTL_COPY_DPATH IRP_IOCTRL_CODE(7)
#define CTL_MOVE_SPATH IRP_IOCTRL_CODE(8)
#define CTL_MOVE_DPATH IRP_IOCTRL_CODE(9)
...
...
case'4':
{
memset(Sfilepath, 0, sizeof(InputBuffer));
memset(Dfilepath, 0, sizeof(OutputBuffer));
memset(OutputBuffer, 0, sizeof(OutputBuffer));
printf("请输入源文件的路径:\n");
scanf("%s", Sfilepath);
printf("\n请输入目的文件路径:\n");
scanf("%s", Dfilepath);
DeviceIoControl(hDriver, CTL_COPY_SPATH, Sfilepath, sizeof(Sfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
if (!strcmp(OutputBuffer, "Success!"))
{
memset(OutputBuffer, 0, sizeof(OutputBuffer));
DeviceIoControl(hDriver, CTL_COPY_DPATH, Dfilepath, sizeof(Dfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
printf("%s\n", OutputBuffer);
system("pause");
}
break;
}
case '5': {
RetNumber = 0;
memset(InputBuffer, 0, sizeof(InputBuffer));
memset(OutputBuffer, 0, sizeof(OutputBuffer));
printf("请输入需要删除的文件路径:\n");
scanf("%s", InputBuffer);
DeviceIoControl(hDriver, CTL_DELETE_PATH, InputBuffer, sizeof(InputBuffer), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
printf("返回数据: % s\n", OutputBuffer);
system("pause");
break;
}
case'7': {
memset(Sfilepath, 0, sizeof(InputBuffer));
memset(Dfilepath, 0, sizeof(OutputBuffer));
memset(OutputBuffer, 0, sizeof(OutputBuffer));
printf("请输入源文件的路径:\n");
scanf("%s", Sfilepath);
printf("\n请输入目的文件路径:\n");
scanf("%s", Dfilepath);
DeviceIoControl(hDriver, CTL_MOVE_SPATH, Sfilepath, sizeof(Sfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
if (!strcmp(OutputBuffer, "Success!"))
{
memset(OutputBuffer, 0, sizeof(OutputBuffer));
DeviceIoControl(hDriver, CTL_MOVE_DPATH, Dfilepath, sizeof(Dfilepath), OutputBuffer, sizeof(OutputBuffer), &RetNumber, NULL);
printf("%s\n", OutputBuffer);
system("pause");
}
break;
}
复制文件

删除文件

移动文件
