-
Notifications
You must be signed in to change notification settings - Fork 4
/
MsIo64.c
289 lines (269 loc) · 8.97 KB
/
MsIo64.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
NTSTATUS entry(_DRIVER_OBJECT *DriverObject,_UNICODE_STRING *RegistryPath)
{
NTSTATUS NVar1;
_DEVICE_OBJECT *local_38;
_UNICODE_STRING local_30;
_UNICODE_STRING local_20 [2];
if (((DAT_00013108 == 0) || (DAT_00013108 == 0x2b992ddfa232)) &&
(DAT_00013108 = (_DAT_fffff78000000320 ^ 0x13108) & 0xffffffffffff, DAT_00013108 == 0)) {
DAT_00013108 = 0x2b992ddfa232;
}
_DAT_00013100 = ~DAT_00013108;
local_38 = (_DEVICE_OBJECT *)0x0;
DbgPrint("Entering DriverEntry");
RtlInitUnicodeString(&local_30,L"\\Device\\MsIo");
NVar1 = IoCreateDevice(DriverObject,0,&local_30,0x8010,0,'\0',&local_38);
if (NVar1 < 0) {
DbgPrint("ERROR: IoCreateDevice failed");
}
else {
*(code **)&DriverObject->DispatchDeviceIOControl = ioctl;
*(code **)&DriverObject->DispatchClose = ioctl;
*(code **)&DriverObject->DispatchCreate = ioctl;
*(code **)&DriverObject->DriverUnload = DriverUnload;
RtlInitUnicodeString(local_20,L"\\DosDevices\\MsIo");
NVar1 = IoCreateSymbolicLink(local_20,&local_30);
if (NVar1 < 0) {
DbgPrint("ERROR: IoCreateSymbolicLink failed");
IoDeleteDevice(local_38);
}
}
DbgPrint("Leaving DriverEntry");
return NVar1;
}
void DriverUnload(longlong param_1)
{
NTSTATUS NVar1;
_UNICODE_STRING local_18;
DbgPrint("Entering MsIoUnload");
RtlInitUnicodeString(&local_18,L"\\DosDevices\\MsIo");
NVar1 = IoDeleteSymbolicLink(&local_18);
if (-1 < NVar1) {
IoDeleteDevice(*(_DEVICE_OBJECT **)(param_1 + 8));
DbgPrint("Leaving MsIoUnload");
return;
}
DbgPrint("ERROR: IoDeleteSymbolicLink");
DbgPrint("Leaving MsIoUnload");
return;
}
NTSTATUS ioctl(_DEVICE_OBJECT *DeviceObject,_IRP *Irp)
{
UCHAR MajorFunction;
uint InputBufferLength;
ULONG UVar1;
NTSTATUS NVar2;
_IO_STACK_LOCATION *CurrentStackLocation;
undefined8 *SystemBuffer;
byte bVar3;
ushort uVar4;
undefined4 uVar5;
ulonglong InputBufferLength0;
ulonglong uVar6;
char *pcVar7;
undefined2 local_res10;
undefined4 local_res12;
char local_res16;
longlong local_48;
ulonglong local_40;
undefined8 local_38;
longlong local_30;
longlong local_28 [3];
DbgPrint("---Entry MsIoDispatch---");
CurrentStackLocation = Irp->CurrentStackLocation;
SystemBuffer = (undefined8 *)Irp->SystemBuffer;
(Irp->IoStatus).Status = 0;
(Irp->IoStatus).Information = 0;
MajorFunction = CurrentStackLocation->MajorFunction;
InputBufferLength = *(uint *)&CurrentStackLocation->InputBufferLength;
InputBufferLength0 = (ulonglong)InputBufferLength;
if (MajorFunction == '\0') {
pcVar7 = "IRP_MJ_CREATE";
}
else {
if (MajorFunction != '\x02') {
if (MajorFunction == '\x0e') {
DbgPrint("IRP_MJ_DEVICE_CONTROL");
UVar1 = CurrentStackLocation->IoControlCode;
if (UVar1 == 0x80102040) {
DbgPrint("IOCTL_MSIO_MAPPHYSTOLIN");
if (InputBufferLength != 0) {
CopyMemoryBlock(&local_48,SystemBuffer,InputBufferLength0);
uVar6 = MapArbitraryPhysicalMemory(local_40,local_48,&local_30,&local_38,local_28);
if (-1 < (int)uVar6) {
CopyMemoryBlock(SystemBuffer,&local_48,InputBufferLength0);
(Irp->IoStatus).Information = InputBufferLength0;
}
(Irp->IoStatus).Status = (int)uVar6;
goto LAB_0001167f;
}
}
else {
if (UVar1 == 0x80102044) {
DbgPrint("IOCTL_MSIO_UNMAPPHYSADDR");
if (InputBufferLength != 0) {
CopyMemoryBlock(&local_48,SystemBuffer,InputBufferLength0);
InputBufferLength0 = UnmapPhysicalMemory(local_38,local_30,local_28[0]);
(Irp->IoStatus).Status = (LONG)InputBufferLength0;
goto LAB_0001167f;
}
}
else {
if (UVar1 == 0x80102050) {
DbgPrint("IOCTL_MSIO_READPORT");
if (InputBufferLength != 0) {
CopyMemoryBlock((undefined8 *)&local_res10,SystemBuffer,InputBufferLength0);
if (local_res16 == '\x01') {
bVar3 = in(local_res10);
*(uint *)SystemBuffer = (uint)bVar3;
(Irp->IoStatus).Information = 4;
}
else {
if (local_res16 == '\x02') {
uVar4 = in(local_res10);
*(uint *)SystemBuffer = (uint)uVar4;
(Irp->IoStatus).Information = 4;
}
else {
if (local_res16 == '\x04') {
uVar5 = in(local_res10);
*(undefined4 *)SystemBuffer = uVar5;
(Irp->IoStatus).Information = 4;
}
else {
*(undefined4 *)SystemBuffer = local_res12;
(Irp->IoStatus).Information = 4;
}
}
}
goto LAB_0001167f;
}
}
else {
if (UVar1 == 0x80102054) {
DbgPrint("IOCTL_MSIO_WRITEPORT");
if (InputBufferLength != 0) {
CopyMemoryBlock((undefined8 *)&local_res10,SystemBuffer,InputBufferLength0);
if (local_res16 == '\x01') {
out(local_res10,(undefined)local_res12);
}
else {
if (local_res16 == '\x02') {
out(local_res10,(undefined2)local_res12);
}
else {
if (local_res16 == '\x04') {
out(local_res10,local_res12);
}
}
}
goto LAB_0001167f;
}
}
else {
DbgPrint("ERROR: Unknown IRP_MJ_DEVICE_CONTROL");
}
}
}
}
(Irp->IoStatus).Status = -0x3ffffff3;
}
goto LAB_0001167f;
}
pcVar7 = "IRP_MJ_CLOSE";
}
DbgPrint(pcVar7);
LAB_0001167f:
NVar2 = (Irp->IoStatus).Status;
IofCompleteRequest(Irp,'\0');
DbgPrint("Leaving MsIoDispatch");
return NVar2;
}
ulonglong MapArbitraryPhysicalMemory
(ulonglong param_1,longlong param_2,longlong *param_3,undefined8 *param_4,
undefined8 *param_5)
{
undefined8 *puVar1;
UCHAR UVar2;
UCHAR UVar3;
uint uVar4;
ULONGLONG local_res10 [2];
ULONG local_res20 [2];
PVOID local_98;
longlong local_90;
ulonglong local_88;
ulonglong local_80;
_UNICODE_STRING local_78;
_OBJECT_ATTRIBUTES local_68;
local_98 = (PVOID)0x0;
local_res10[0] = param_2;
DbgPrint("Entering MapPhysicalMemoryToLinearSpace");
RtlInitUnicodeString(&local_78,L"\\Device\\PhysicalMemory");
puVar1 = param_5;
local_68.ObjectName = &local_78;
*param_4 = 0;
*param_5 = 0;
local_68.Length = 0x30;
local_68.RootDirectory = (void *)0x0;
local_68.Attributes = 0x40;
local_68.SecurityDescriptor = (void *)0x0;
local_68.SecurityQualityOfService = (void *)0x0;
uVar4 = ZwOpenSection((HANDLE *)param_4,0xf001f,&local_68);
if ((int)uVar4 < 0) {
DbgPrint("ERROR: ZwOpenSection failed");
}
else {
uVar4 = ObReferenceObjectByHandle(*param_4,0xf001f,0,0,puVar1,0);
if ((int)uVar4 < 0) {
DbgPrint("ERROR: ObReferenceObjectByHandle failed");
}
else {
local_88 = param_1 & 0xffffffff;
local_90 = local_88 + local_res10[0];
local_res20[0] = 0;
UVar2 = HalTranslateBusAddress(Isa,0,local_88,local_res20,&local_88);
local_res20[0] = 0;
UVar3 = HalTranslateBusAddress(Isa,0,local_90,local_res20,&local_90);
if ((UVar2 == '\0') || (UVar3 == '\0')) {
DbgPrint("ERROR: HalTranslateBusAddress failed");
}
else {
local_res10[0] = local_90 - local_88;
local_80 = local_88;
uVar4 = ZwMapViewOfSection((HANDLE)*param_4,(HANDLE)0xffffffffffffffff,&local_98,0,
local_res10[0],&local_80,local_res10,1,0,0x204);
if (uVar4 == 0xc0000018) {
uVar4 = ZwMapViewOfSection((HANDLE)*param_4,(HANDLE)0xffffffffffffffff,&local_98,0,
local_res10[0],&local_80,local_res10,1,0,4);
}
if ((int)uVar4 < 0) {
DbgPrint("ERROR: ZwMapViewOfSection failed");
}
else {
local_98 = (PVOID)((longlong)local_98 + (local_88 - local_80));
*(PVOID *)param_3 = local_98;
}
}
}
}
if ((int)uVar4 < 0) {
ZwClose((HANDLE)*param_4);
}
DbgPrint("Leaving MapPhysicalMemoryToLinearSpace");
return (ulonglong)uVar4;
}
ulonglong UnmapPhysicalMemory(HANDLE param_1,PVOID param_2,longlong param_3)
{
uint uVar1;
DbgPrint("Entering UnmapPhysicalMemory");
uVar1 = ZwUnmapViewOfSection((HANDLE)0xffffffffffffffff,param_2);
if ((int)uVar1 < 0) {
DbgPrint("ERROR: UnmapViewOfSection failed");
}
if (param_3 != 0) {
ObfDereferenceObject(param_3);
}
ZwClose(param_1);
DbgPrint("Leaving UnmapPhysicalMemory");
return (ulonglong)uVar1;
}