유저
#include <stdio.h>
#include <Windows.h>
#include <conio.h>
#define IOCTL_TEST CTL_CODE(FILE_DEVICE_UNKNOWN,0x4000,METHOD_NEITHER,FILE_ANY_ACCESS)
int main(void)
{
HANDLE dHandle;
WCHAR DeviceLink[] = L"\\\\.\\sanggamja";
DWORD dwRet;
char send[] = "10*100";
char buf[100] = { 0 };
dHandle = CreateFileW(
DeviceLink,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (dHandle == INVALID_HANDLE_VALUE)
{
printf("Get Device Handle Fail! : 0x%X \n", GetLastError());
getchar();
return 1;
}
if (!DeviceIoControl(dHandle, IOCTL_TEST, (LPVOID)buf, sizeof(buf), send, sizeof(send), &dwRet, 0))
{
printf("DeviceIOControl Fail! \n");
getchar();
CloseHandle(dHandle);
return 1;
}
printf("result = %s\n", buf);
getchar();
CloseHandle(dHandle);
return 0;
}
커널
#define _CRT_SECURE_NO_WARNINGS
#include <ntddk.h>
#include <string.h>
#include <stdio.h>
#define LINK_NAME L"\\DosDevices\\sanggamja"
#define DEVICE_NAME L"\\DEVICE\\test"
#define IOCTL_TEST CTL_CODE(FILE_DEVICE_UNKNOWN,0x4000,METHOD_NEITHER,FILE_ANY_ACCESS)
PDEVICE_OBJECT MyDevice;
UNICODE_STRING DeviceLink;
UNICODE_STRING DeviceName;
NTSTATUS MyIOControl(IN PDEVICE_OBJECT DeviceObject, IN PIRP irp)
{
PIO_STACK_LOCATION pStack;
NTSTATUS returnStatus = STATUS_SUCCESS;
ULONG ControlCode;
PCHAR Output;
PVOID Input;
ULONG val1;
ULONG val2;
ULONG val3;
CHAR Ind3 = {1};
int i;
int j = 0;
int num1;
int num2;
int op = 0;
char cal[100] = { 0, };
char Ind2[100] = { 0, };
char Ind1[100] = { 0, };
char plus;
char min;
char mul;
char divi;
char vi[] = { 0, };
pStack = IoGetCurrentIrpStackLocation(irp);
ControlCode = pStack->Parameters.DeviceIoControl.IoControlCode;
switch (ControlCode)
{
case IOCTL_TEST:
Input = irp->UserBuffer;
Output = pStack->Parameters.DeviceIoControl.Type3InputBuffer;
RtlCopyMemory(&cal, Input, strlen(irp->UserBuffer));
for (i = 0; i < strlen(cal)+1; i++)
{
memcpy(&plus, "+", 1);
memcpy(&min, "-", 1);
memcpy(&mul, "*", 1);
memcpy(&divi, "/", 1);
if (!(strncmp(&plus, &cal[i], 1)))
{
op = 1;
memcpy(&Ind1, &cal[0], i);
memcpy(&Ind2, &cal[i+1], strlen(cal)-(i+1));
}
else if (!(strncmp(&min, &cal[i], 1)))
{
op = 2;
memcpy(&Ind1, &cal[0], i);
memcpy(&Ind2, &cal[i + 1], strlen(cal) - (i + 1));
}
else if (!(strncmp(&mul, &cal[i], 1)))
{
op = 3;
memcpy(&Ind1, &cal[0], i);
memcpy(&Ind2, &cal[i + 1], strlen(cal) - (i + 1));
}
else if (!(strncmp(&divi, &cal[i], 1)))
{
op = 4;
memcpy(&Ind1, &cal[0], i);
memcpy(&Ind2, &cal[i + 1], strlen(cal) - (i + 1));
}
}
RtlCharToInteger(Ind1, 10, &val1);
RtlCharToInteger(Ind2, 10, &val2);
if (op == 1){ val3 = val1 + val2; }
else if (op == 2){ val3 = val1 - val2; }
else if (op == 3){ val3 = val1 * val2; }
else if (op == 4){ val3 = val1 / val2; }
sprintf(vi, "%lu", val3);
memcpy(Output, &vi, strlen(vi));
}
irp->IoStatus.Status = STATUS_SUCCESS;
irp->IoStatus.Information = sizeof(Output);
IoCompleteRequest(irp, IO_NO_INCREMENT);
return returnStatus;
}
NTSTATUS Create_Handler(IN PDEVICE_OBJECT DeviceObject, IN PIRP irp)
{
irp->IoStatus.Status = STATUS_SUCCESS;
IoCompleteRequest(irp, IO_NO_INCREMENT);
return STATUS_SUCCESS;
}
VOID OnUnload(IN PDRIVER_OBJECT DriverObject)
{
IoDeleteDevice(MyDevice);
IoDeleteSymbolicLink(&DeviceLink);
DbgPrint("OnUnload Call! \n");
}
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
NTSTATUS returnStatus = STATUS_SUCCESS;
RtlInitUnicodeString(&DeviceLink, LINK_NAME);
RtlInitUnicodeString(&DeviceName, DEVICE_NAME);
returnStatus = IoCreateDevice(
DriverObject,
0,
&DeviceName,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&MyDevice
);
if (!NT_SUCCESS(returnStatus))
{
DbgPrint("IoCreateDevice Fail! \n");
return returnStatus;
}
DbgPrint("Success IoCreateSymbilicLinck \n");
returnStatus = IoCreateSymbolicLink(&DeviceLink, &DeviceName);
if (!NT_SUCCESS(returnStatus))
{
return returnStatus;
}
DriverObject-> DriverUnload = OnUnload;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = MyIOControl;
DriverObject->MajorFunction[IRP_MJ_CREATE] = Create_Handler;
return returnStatus;
}
귀찮아서 뺄셈과 나눗셈 처리는 제대로 하지 않았지만 그 틀과 함수의 사용법에 유의하면 만들수 있을 것 이다.
'Window_Hacking' 카테고리의 다른 글
Windbg 명령어 (0) | 2018.09.11 |
---|