-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_tools.c
More file actions
161 lines (151 loc) · 4.25 KB
/
native_tools.c
File metadata and controls
161 lines (151 loc) · 4.25 KB
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
158
159
160
161
#include <ntddk.h>
#include <stdarg.h>
#include "utils.h"
void native_sleep(unsigned int ms)
{
LARGE_INTEGER Timeout = {.QuadPart = ms * -10000LL};
NtDelayExecution(FALSE, &Timeout);
}
int PrintString(char* fmt,...)
{
int Len;
char buffer[512];
ANSI_STRING AnsiString;
UNICODE_STRING UnicodeString;
va_list ap;
va_start(ap, fmt);
Len = my_vsprintf(buffer, fmt, ap);
va_end(ap);
RtlInitAnsiString(&AnsiString, buffer);
RtlAnsiStringToUnicodeString(&UnicodeString,
&AnsiString,
TRUE);
NtDisplayString(&UnicodeString);
RtlFreeUnicodeString(&UnicodeString);
return Len;
}
void PutChar(char c)
{
CHAR buffer[] = {c, 0};
UNICODE_STRING UnicodeString = {.Length = 2, .MaximumLength = 2, .Buffer = (PWCH)buffer};
NtDisplayString(&UnicodeString);
}
NTSTATUS OpenKeyboard(OUT PHANDLE pKeyboardHandle, IO_STATUS_BLOCK *pIoStatusBlock)
{
UNICODE_STRING KeyboardName;
OBJECT_ATTRIBUTES ObjectAttributes;
RtlInitUnicodeString(&KeyboardName, L"\\Device\\KeyboardClass0");
InitializeObjectAttributes(
&ObjectAttributes,
&KeyboardName,
OBJ_KERNEL_HANDLE,
NULL,
NULL
);
return NtCreateFile(
pKeyboardHandle,
SYNCHRONIZE | GENERIC_READ,
&ObjectAttributes,
pIoStatusBlock,
NULL,
0x80,
0,
FILE_OPEN,
1,
NULL,
0
);
}
NTSTATUS native_get_keyboard_scancode(HANDLE KeyboardHandle, IO_STATUS_BLOCK *pIoStatusBlock, HANDLE EventHandle, KEYBOARD_INPUT_DATA *pInputData)
{
NtClearEvent(EventHandle);
pIoStatusBlock->Status = 0;
LARGE_INTEGER ByteOffset = {0};
NTSTATUS Status = NtReadFile(
KeyboardHandle,
EventHandle,
NULL, NULL,
pIoStatusBlock,
pInputData,
sizeof(KEYBOARD_INPUT_DATA),
&ByteOffset,
NULL
);
if (!NT_SUCCESS(Status))
return Status;
//PrintString("NtReadFile succeeded: 0x%x\n", Status);
if (Status == STATUS_PENDING)
{
//PrintString("Read pending, waiting for event...\n");
NtWaitForSingleObject(EventHandle, FALSE, NULL);
}
//PrintString("Event signaled, Flags: 0x%x, MakeCode: 0x%x\n", pInputData->Flags, pInputData->MakeCode);
return 0;
}
NTSTATUS native_get_keyboard_char(HANDLE KeyboardHandle, IO_STATUS_BLOCK *pIoStatusBlock, HANDLE EventHandle, CHAR *c)
{
start:
KEYBOARD_INPUT_DATA InputData = {0};
NTSTATUS Status = native_get_keyboard_scancode(KeyboardHandle, pIoStatusBlock, EventHandle, &InputData);
if (!NT_SUCCESS(Status))
return Status;
if (InputData.Flags == 1)
goto start; // key release, ignore
*c = scancode_2_char(InputData.MakeCode);
if (*c == 0)
{
// special key or event
goto start;
}
return 0;
}
UINT8 is_shift;
NTSTATUS native_get_keyboard_str(HANDLE KeyboardHandle, IO_STATUS_BLOCK *pIoStatusBlock, HANDLE EventHandle, CHAR *buffer, UINT32 bufferSize)
{
buffer[bufferSize - 1] = '\0';
UINT32 index = 0;
while (index < bufferSize - 1)
{
KEYBOARD_INPUT_DATA InputData = {0};
NTSTATUS Status = native_get_keyboard_scancode(KeyboardHandle, pIoStatusBlock, EventHandle, &InputData);
if (!NT_SUCCESS(Status))
return Status;
if (InputData.Flags == 1) // key release
{
if (InputData.MakeCode == 0x2A) // Left Shift key
{
is_shift = 0;
}
continue;
}
if (InputData.MakeCode == 0x2A) // Left Shift key
{
is_shift = 1;
continue;
}
if (InputData.MakeCode == 0x1C) // Enter key
{
buffer[index] = '\0';
PutChar('\n');
return 0;
}
if (InputData.MakeCode == 0x0E) // Backspace key
{
if (index > 0)
{
index--;
PutChar('\b');
}
continue;
}
buffer[index] = scancode_2_char(InputData.MakeCode);
if (buffer[index] == 0)
{
// special key or event
continue;
}
PutChar(buffer[index]);
index++;
}
return 0;
}