-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathmy_rprn.py
202 lines (161 loc) · 5.81 KB
/
my_rprn.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
201
202
from impacket.dcerpc.v5.dtypes import DWORD, LPWSTR, ULONG
from impacket.dcerpc.v5.ndr import NDRCALL, NDRPOINTER, NDRSTRUCT, NDRUNION, NULL
from impacket.dcerpc.v5.rprn import DCERPCSessionError, PBYTE_ARRAY, STRING_HANDLE, checkNullString
from impacket.structure import Structure
APD_COPY_ALL_FILES = 0x00000004
APD_COPY_NEW_FILES = 0x00000008
APD_COPY_FROM_DIRECTORY = 0x00000010
APD_INSTALL_WARNED_DRIVER = 0x00008000
# 2.2.1.5.1 DRIVER_INFO_1
class DRIVER_INFO_1(NDRSTRUCT):
structure = (
('pName', STRING_HANDLE),
)
class PDRIVER_INFO_1(NDRPOINTER):
referent = (
('Data', DRIVER_INFO_1),
)
# 2.2.1.5.2 DRIVER_INFO_2
class DRIVER_INFO_2(NDRSTRUCT):
structure = (
('cVersion', DWORD),
('pName', LPWSTR),
('pEnvironment', LPWSTR),
('pDriverPath', LPWSTR),
('pDataFile', LPWSTR),
('pConfigFile', LPWSTR),
)
class PDRIVER_INFO_2(NDRPOINTER):
referent = (
('Data', DRIVER_INFO_2),
)
class DRIVER_INFO_UNION(NDRUNION):
commonHdr = (
('tag', ULONG),
)
union = {
1: ('pNotUsed', PDRIVER_INFO_1),
2: ('Level2', PDRIVER_INFO_2),
}
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/3a3f9cf7-8ec4-4921-b1f6-86cf8d139bc2
class DRIVER_CONTAINER(NDRSTRUCT):
structure = (
('Level', DWORD),
('DriverInfo', DRIVER_INFO_UNION),
)
class PDRIVER_CONTAINER(NDRPOINTER):
referent = (
("Level", DWORD),
("DriverInfo", DRIVER_INFO_UNION)
)
class RpcEnumPrinterDrivers(NDRCALL):
opnum = 10
structure = (
("pName", STRING_HANDLE),
("pEnvironment", LPWSTR),
("Level", DWORD),
("pDrivers", PBYTE_ARRAY),
("cbBuf", DWORD),
)
class RpcEnumPrinterDriversResponse(NDRCALL):
structure = (
('pDrivers', PBYTE_ARRAY),
('pcbNeeded', DWORD),
('pcReturned', DWORD),
('ErrorCode', ULONG),
)
class RpcAddPrinterDriverEx(NDRCALL):
opnum = 89
structure = (
("pName", STRING_HANDLE),
("pDriverContainer", DRIVER_CONTAINER),
("dwFileCopyFlags", DWORD),
)
class RpcAddPrinterDriverExResponse(NDRCALL):
structure = (
('ErrorCode', ULONG),
)
# https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/2825d22e-c5a5-47cd-a216-3e903fd6e030
class DRIVER_INFO_2_BLOB(Structure):
structure = (
('cVersion', '<L'),
('NameOffset', '<L'),
('EnvironmentOffset', '<L'),
('DriverPathOffset', '<L'),
('DataFileOffset', '<L'),
('ConfigFileOffset', '<L'),
)
def __init__(self, data=None):
Structure.__init__(self, data=data)
def fromString(self, data, offset=0):
Structure.fromString(self, data)
self['ConfigFileArray'] = self.rawData[
self['ConfigFileOffset'] + offset:self['DataFileOffset'] + offset].decode('utf-16-le')
self['DataFileArray'] = self.rawData[self['DataFileOffset'] + offset:self['DriverPathOffset'] + offset].decode(
'utf-16-le')
self['DriverPathArray'] = self.rawData[
self['DriverPathOffset'] + offset:self['EnvironmentOffset'] + offset].decode(
'utf-16-le')
self['EnvironmentArray'] = self.rawData[self['EnvironmentOffset'] + offset:self['NameOffset'] + offset].decode(
'utf-16-le')
# self['NameArray'] = self.rawData[self['NameOffset']+offset:len(self.rawData)].decode('utf-16-le')
class DRIVER_INFO_2_ARRAY(Structure):
def __init__(self, data=None, pcReturned=None):
Structure.__init__(self, data=data)
self['drivers'] = list()
remaining = data
if data is not None:
for i in range(pcReturned):
attr = DRIVER_INFO_2_BLOB(remaining)
self['drivers'].append(attr)
remaining = remaining[len(attr):]
def hRpcAddPrinterDriverEx(dce, pName, pDriverContainer, dwFileCopyFlags):
"""
RpcAddPrinterDriverEx installs a printer driver on the print server
Full Documentation: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/b96cc497-59e5-4510-ab04-5484993b259b
:param DCERPC_v5 dce: a connected DCE instance.
:param pName
:param pDriverContainer
:param dwFileCopyFlags
:return: raises DCERPCSessionError on error.
"""
request = RpcAddPrinterDriverEx()
request['pName'] = checkNullString(pName)
request['pDriverContainer'] = pDriverContainer
request['dwFileCopyFlags'] = dwFileCopyFlags
# return request
return dce.request(request)
def hRpcEnumPrinterDrivers(dce, pName, pEnvironment, Level):
"""
RpcEnumPrinterDrivers enumerates the printer drivers installed on a specified print server.
Full Documentation: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rprn/857d00ac-3682-4a0d-86ca-3d3c372e5e4a
:param DCERPC_v5 dce: a connected DCE instance.
:param pName
:param pEnvironment
:param Level
:return: raises DCERPCSessionError on error.
"""
# get value for cbBuf
request = RpcEnumPrinterDrivers()
request['pName'] = checkNullString(pName)
request['pEnvironment'] = pEnvironment
request['Level'] = Level
request['pDrivers'] = NULL
request['cbBuf'] = 0
try:
dce.request(request)
except DCERPCSessionError as e:
if str(e).find('ERROR_INSUFFICIENT_BUFFER') < 0:
raise
bytesNeeded = e.get_packet()['pcbNeeded']
else:
raise
# now do RpcEnumPrinterDrivers again
request = RpcEnumPrinterDrivers()
request['pName'] = checkNullString(pName)
request['pEnvironment'] = pEnvironment
request['Level'] = Level
request['pDrivers'] = b'a' * bytesNeeded
request['cbBuf'] = bytesNeeded
# return request
return dce.request(request)