-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathWinIoCtlDecoder.py
200 lines (179 loc) · 6.84 KB
/
WinIoCtlDecoder.py
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
#!/usr/bin/python
"""Decodes a given 32-Bit Windows Device I/O control code
Author: Satoshi Tanda
Description:
Decodes Windows Device I/O control code into DeviceType, FunctionCode,
AccessType and MethodType.
Usage:
1. Select an interesting IOCTL code in the disassemble window.
2. Hit Ctrl+Alt+D or select Edit/Plugins/Windows IOCTL code decoder
or
Call winio_decode function directly from the Python CLI window.
Python>winio_decode(0x220086)
Example:
Python>winio_decode(0x220086)
winio_decode(0x00220086)
Device : FILE_DEVICE_UNKNOWN (0x22)
Function : 0x21
Method : METHOD_OUT_DIRECT (2)
Access : FILE_ANY_ACCESS (0)
"""
import sys
import idc
import idaapi
def winio_decode(ioctl_code):
"""Decodes IOCTL code and print it."""
access_names = [
"FILE_ANY_ACCESS",
"FILE_READ_ACCESS",
"FILE_WRITE_ACCESS",
"FILE_READ_ACCESS | FILE_WRITE_ACCESS",
]
method_names = [
"METHOD_BUFFERED",
"METHOD_IN_DIRECT",
"METHOD_OUT_DIRECT",
"METHOD_NEITHER",
]
device_name_unknown = "<UNKNOWN>"
device_names = [
device_name_unknown, # 0x00000000
"FILE_DEVICE_BEEP", # 0x00000001
"FILE_DEVICE_CD_ROM", # 0x00000002
"FILE_DEVICE_CD_ROM_FILE_SYSTEM", # 0x00000003
"FILE_DEVICE_CONTROLLER", # 0x00000004
"FILE_DEVICE_DATALINK", # 0x00000005
"FILE_DEVICE_DFS", # 0x00000006
"FILE_DEVICE_DISK", # 0x00000007
"FILE_DEVICE_DISK_FILE_SYSTEM", # 0x00000008
"FILE_DEVICE_FILE_SYSTEM", # 0x00000009
"FILE_DEVICE_INPORT_PORT", # 0x0000000a
"FILE_DEVICE_KEYBOARD", # 0x0000000b
"FILE_DEVICE_MAILSLOT", # 0x0000000c
"FILE_DEVICE_MIDI_IN", # 0x0000000d
"FILE_DEVICE_MIDI_OUT", # 0x0000000e
"FILE_DEVICE_MOUSE", # 0x0000000f
"FILE_DEVICE_MULTI_UNC_PROVIDER", # 0x00000010
"FILE_DEVICE_NAMED_PIPE", # 0x00000011
"FILE_DEVICE_NETWORK", # 0x00000012
"FILE_DEVICE_NETWORK_BROWSER", # 0x00000013
"FILE_DEVICE_NETWORK_FILE_SYSTEM", # 0x00000014
"FILE_DEVICE_NULL", # 0x00000015
"FILE_DEVICE_PARALLEL_PORT", # 0x00000016
"FILE_DEVICE_PHYSICAL_NETCARD", # 0x00000017
"FILE_DEVICE_PRINTER", # 0x00000018
"FILE_DEVICE_SCANNER", # 0x00000019
"FILE_DEVICE_SERIAL_MOUSE_PORT", # 0x0000001a
"FILE_DEVICE_SERIAL_PORT", # 0x0000001b
"FILE_DEVICE_SCREEN", # 0x0000001c
"FILE_DEVICE_SOUND", # 0x0000001d
"FILE_DEVICE_STREAMS", # 0x0000001e
"FILE_DEVICE_TAPE", # 0x0000001f
"FILE_DEVICE_TAPE_FILE_SYSTEM", # 0x00000020
"FILE_DEVICE_TRANSPORT", # 0x00000021
"FILE_DEVICE_UNKNOWN", # 0x00000022
"FILE_DEVICE_VIDEO", # 0x00000023
"FILE_DEVICE_VIRTUAL_DISK", # 0x00000024
"FILE_DEVICE_WAVE_IN", # 0x00000025
"FILE_DEVICE_WAVE_OUT", # 0x00000026
"FILE_DEVICE_8042_PORT", # 0x00000027
"FILE_DEVICE_NETWORK_REDIRECTOR", # 0x00000028
"FILE_DEVICE_BATTERY", # 0x00000029
"FILE_DEVICE_BUS_EXTENDER", # 0x0000002a
"FILE_DEVICE_MODEM", # 0x0000002b
"FILE_DEVICE_VDM", # 0x0000002c
"FILE_DEVICE_MASS_STORAGE", # 0x0000002d
"FILE_DEVICE_SMB", # 0x0000002e
"FILE_DEVICE_KS", # 0x0000002f
"FILE_DEVICE_CHANGER", # 0x00000030
"FILE_DEVICE_SMARTCARD", # 0x00000031
"FILE_DEVICE_ACPI", # 0x00000032
"FILE_DEVICE_DVD", # 0x00000033
"FILE_DEVICE_FULLSCREEN_VIDEO", # 0x00000034
"FILE_DEVICE_DFS_FILE_SYSTEM", # 0x00000035
"FILE_DEVICE_DFS_VOLUME", # 0x00000036
"FILE_DEVICE_SERENUM", # 0x00000037
"FILE_DEVICE_TERMSRV", # 0x00000038
"FILE_DEVICE_KSEC", # 0x00000039
"FILE_DEVICE_FIPS", # 0x0000003A
"FILE_DEVICE_INFINIBAND", # 0x0000003B
device_name_unknown, # 0x0000003C
device_name_unknown, # 0x0000003D
"FILE_DEVICE_VMBUS", # 0x0000003E
"FILE_DEVICE_CRYPT_PROVIDER", # 0x0000003F
"FILE_DEVICE_WPD", # 0x00000040
"FILE_DEVICE_BLUETOOTH", # 0x00000041
"FILE_DEVICE_MT_COMPOSITE", # 0x00000042
"FILE_DEVICE_MT_TRANSPORT", # 0x00000043
"FILE_DEVICE_BIOMETRIC", # 0x00000044
"FILE_DEVICE_PMI", # 0x00000045
"FILE_DEVICE_EHSTOR", # 00000046
"FILE_DEVICE_DEVAPI", # 00000047
"FILE_DEVICE_GPIO", # 00000048
"FILE_DEVICE_USBEX", # 00000049
"FILE_DEVICE_CONSOLE", # 00000050
"FILE_DEVICE_NFP", # 00000051
"FILE_DEVICE_SYSENV", # 00000052
"FILE_DEVICE_VIRTUAL_BLOCK", # 00000053
"FILE_DEVICE_POINT_OF_SERVICE", # 00000054
"FILE_DEVICE_STORAGE_REPLICATION", # 00000055
"FILE_DEVICE_TRUST_ENV", # 00000056
"FILE_DEVICE_UCM", # 00000057
"FILE_DEVICE_UCMTCPCI", # 00000058
"FILE_DEVICE_PERSISTENT_MEMORY", # 00000059
"FILE_DEVICE_NVDIMM", # 0000005a
"FILE_DEVICE_HOLOGRAPHIC", # 0000005b
"FILE_DEVICE_SDFXHCI", # 0000005c
"FILE_DEVICE_UCMUCSI", # 0000005d
"FILE_DEVICE_PRM", # 0000005e
"FILE_DEVICE_EVENT_COLLECTOR", # 0000005f
"FILE_DEVICE_USB4", # 00000060
"FILE_DEVICE_SOUNDWIRE", # 00000061
]
device_names2 = [
{"name": "MOUNTMGRCONTROLTYPE", "code": 0x0000006D},
]
device = (ioctl_code >> 16) & 0xFFFF
access = (ioctl_code >> 14) & 3
function = (ioctl_code >> 2) & 0xFFF
method = ioctl_code & 3
if device >= len(device_names):
device_name = device_name_unknown
for dev in device_names2:
if device == dev["code"]:
device_name = dev["name"]
break
else:
device_name = device_names[device]
print(f"winio_decode(0x{ioctl_code:08X})")
print(f"Device : {device_name} ({device:X})")
print(f"Function : 0x{function:X}")
print(f"Method : {method_names[method]} ({method})")
print(f"Access : {access_names[access]} ({access})")
class WinIoCtlPlugin(idaapi.plugin_t):
"""Class for IDA Pro plugin."""
flags = idaapi.PLUGIN_UNL
comment = (
"Decodes Windows Device I/O control code into "
+ "DeviceType, FunctionCode, AccessType and MethodType."
)
help = ""
wanted_name = "Windows IOCTL code decoder"
wanted_hotkey = "Ctrl-Alt-D"
def init(self):
return idaapi.PLUGIN_OK
def run(self, _=0):
if idc.get_operand_type(idc.get_screen_ea(), 1) != 5: # Immediate
return
value = idc.get_operand_value(idc.get_screen_ea(), 1) & 0xFFFFFFFF
winio_decode(value)
def term(self):
pass
def PLUGIN_ENTRY():
return WinIoCtlPlugin()
def main():
if len(sys.argv) != 2:
return
winio_decode(int(sys.argv[1], 16))
if __name__ == "__main__":
main()