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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*++
Copyright (c) 2024 Microsoft Corporation All Rights Reserved
Module Name:
agent.c
Abstract:
Usermode agent that interacts with the kasantrigger.sys driver to
demonstrate how KASAN detects illegal memory accesses.
Environment:
Win32 console multi-threaded application.
--*/
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strsafe.h>
#include <sys\kasantrigger.h>
VOID __cdecl
main(
_In_ ULONG argc,
_In_reads_(argc) PCHAR argv[]
)
{
ULONG bytesReturned;
INT choice;
HANDLE hDevice;
ULONG index;
DWORD ioctlCode;
BOOLEAN isKasanEnabledOnDriver;
BOOL success;
UCHAR value;
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(argv);
printf(
" _ __ ______ \n"
"| | / / | _ \\ \n"
"| |/ / __ _ ___ __ _ _ __ | | | |___ _ __ ___ ___ \n"
"| \\ / _` / __|/ _` | '_ \\ | | | / _ \\ '_ ` _ \\ / _ \\ \n"
"| |\\ \\ (_| \\__ \\ (_| | | | | | |/ / __/ | | | | | (_) |\n"
"\\_| \\_/\\__,_|___/\\__,_|_| |_| |___/ \\___|_| |_| |_|\\___/ \n\n"
);
//
// Open the device.
//
hDevice = CreateFile("\\\\.\\KasanTrigger",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDevice == INVALID_HANDLE_VALUE) {
printf("CreateFile failed: %d. Did you forget to load kasantrigger.sys?\n",
GetLastError());
return;
}
//
// Check whether KASAN is enabled on the driver.
//
success = DeviceIoControl(hDevice,
(DWORD)IOCTL_KASANTRIGGER_INFO,
NULL,
0,
&isKasanEnabledOnDriver,
sizeof(isKasanEnabledOnDriver),
&bytesReturned,
NULL);
if (!success) {
printf("DeviceIoControl failed: %d\n\n", GetLastError());
return;
}
if (!isKasanEnabledOnDriver) {
printf("WARNING: KASAN is not enabled on kasantrigger.sys\n\n");
}
printf("Enter a number in the menu below to trigger the desired condition:\n");
printf("1. Trigger a stack out-of-bounds read\n");
printf("2. Trigger a global out-of-bounds read\n");
printf("3. Trigger a heap out-of-bounds read\n\n");
while (TRUE) {
printf("> ");
while ((choice = getchar()) == '\n');
switch (choice) {
case '1':
ioctlCode = (DWORD)IOCTL_KASANTRIGGER_OOBR_STACK;
break;
case '2':
ioctlCode = (DWORD)IOCTL_KASANTRIGGER_OOBR_GLOBAL;
break;
case '3':
ioctlCode = (DWORD)IOCTL_KASANTRIGGER_OOBR_HEAP;
break;
default:
printf("Incorrect input, try again\n");
continue;
}
printf("You have selected %c. Excellent choice. ", choice);
Sleep(1000);
printf("Triggering in 3...");
Sleep(1000);
printf(" 2...");
Sleep(1000);
printf(" 1...");
Sleep(1000);
printf("\n");
index = KASANTRIGGER_ARRAY_SIZE;
value = 0;
success = DeviceIoControl(hDevice,
ioctlCode,
&index,
sizeof(index),
&value,
sizeof(value),
&bytesReturned,
NULL);
if (!success) {
printf("DeviceIoControl failed: %d\n", GetLastError());
return;
}
//
// KASAN should have normally triggered a bugcheck, so this should be
// unreachable.
//
if (isKasanEnabledOnDriver) {
printf("Still alive. The bugcheck was not issued. This is not expected.\n");
} else {
printf("Still alive, because KASAN was not enabled on the driver. The illegal access went undetected.\n");
}
}
CloseHandle(hDevice);
}
|