Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved. 

# Copyright (c) 2017 @MrAnde7son 

# 

# This software is provided under under a slightly modified version 

# of the Apache Software License. See the accompanying LICENSE file 

# for more information. 

# 

# Author: Itamar (@MrAnde7son) 

# 

# Description: 

# Initial [MS-EVEN6] Interface implementation 

# 

# Best way to learn how to use these calls is to grab the protocol standard 

# so you understand what the call does, and then read the test case located 

# at https://github.com/SecureAuthCorp/impacket/tree/master/tests/SMB_RPC 

# 

# Some calls have helper functions, which makes it even easier to use. 

# They are located at the end of this file. 

# Helper functions start with "h"<name of the call>. 

# There are test cases for them too. 

# 

from impacket import system_errors 

from impacket.dcerpc.v5.dtypes import WSTR, DWORD, LPWSTR, ULONG, LARGE_INTEGER, WORD, BYTE 

from impacket.dcerpc.v5.ndr import NDRCALL, NDRPOINTER, NDRUniConformantArray, NDRUniVaryingArray, NDRSTRUCT 

from impacket.dcerpc.v5.rpcrt import DCERPCException 

from impacket.uuid import uuidtup_to_bin 

 

MSRPC_UUID_EVEN6 = uuidtup_to_bin(('F6BEAFF7-1E19-4FBB-9F8F-B89E2018337C', '1.0')) 

 

class DCERPCSessionError(DCERPCException): 

def __init__(self, error_string=None, error_code=None, packet=None): 

DCERPCException.__init__(self, error_string, error_code, packet) 

 

def __str__(self): 

key = self.error_code 

if key in system_errors.ERROR_MESSAGES: 

error_msg_short = system_errors.ERROR_MESSAGES[key][0] 

error_msg_verbose = system_errors.ERROR_MESSAGES[key][1] 

return 'EVEN6 SessionError: code: 0x%x - %s - %s' % (self.error_code, error_msg_short, error_msg_verbose) 

else: 

return 'EVEN6 SessionError: unknown error code: 0x%x' % self.error_code 

 

################################################################################ 

# CONSTANTS 

################################################################################ 

 

# Evt Path Flags 

EvtQueryChannelName = 0x00000001 

EvtQueryFilePath = 0x00000002 

EvtReadOldestToNewest = 0x00000100 

EvtReadNewestToOldest = 0x00000200 

 

################################################################################ 

# STRUCTURES 

################################################################################ 

 

class CONTEXT_HANDLE_LOG_HANDLE(NDRSTRUCT): 

align = 1 

structure = ( 

('Data', '20s=""'), 

) 

 

class PCONTEXT_HANDLE_LOG_HANDLE(NDRPOINTER): 

referent = ( 

('Data', CONTEXT_HANDLE_LOG_HANDLE), 

) 

 

class CONTEXT_HANDLE_LOG_QUERY(NDRSTRUCT): 

align = 1 

structure = ( 

('Data', '20s=""'), 

) 

 

class PCONTEXT_HANDLE_LOG_QUERY(NDRPOINTER): 

referent = ( 

('Data', CONTEXT_HANDLE_LOG_QUERY), 

) 

 

class LPPCONTEXT_HANDLE_LOG_QUERY(NDRPOINTER): 

referent = ( 

('Data', PCONTEXT_HANDLE_LOG_QUERY), 

) 

 

class CONTEXT_HANDLE_OPERATION_CONTROL(NDRSTRUCT): 

align = 1 

structure = ( 

('Data', '20s=""'), 

) 

 

class PCONTEXT_HANDLE_OPERATION_CONTROL(NDRPOINTER): 

referent = ( 

('Data', CONTEXT_HANDLE_OPERATION_CONTROL), 

) 

 

# 2.2.11 EvtRpcQueryChannelInfo 

class EvtRpcQueryChannelInfo(NDRSTRUCT): 

structure = ( 

('Name', LPWSTR), 

('Status', DWORD), 

) 

 

class EvtRpcQueryChannelInfoArray(NDRUniVaryingArray): 

item = EvtRpcQueryChannelInfo 

 

class LPEvtRpcQueryChannelInfoArray(NDRPOINTER): 

referent = ( 

('Data', EvtRpcQueryChannelInfoArray) 

) 

 

class RPC_INFO(NDRSTRUCT): 

structure = ( 

('Error', DWORD), 

('SubError', DWORD), 

('SubErrorParam', DWORD), 

) 

 

class PRPC_INFO(NDRPOINTER): 

referent = ( 

('Data', RPC_INFO) 

) 

 

class WSTR_ARRAY(NDRUniVaryingArray): 

item = WSTR 

 

class DWORD_ARRAY(NDRUniVaryingArray): 

item = DWORD 

 

class LPDWORD_ARRAY(NDRPOINTER): 

referent = ( 

('Data', DWORD_ARRAY) 

) 

 

class BYTE_ARRAY(NDRUniVaryingArray): 

item = 'c' 

 

class CBYTE_ARRAY(NDRUniVaryingArray): 

item = BYTE 

 

class CDWORD_ARRAY(NDRUniConformantArray): 

item = DWORD 

 

class LPBYTE_ARRAY(NDRPOINTER): 

referent = ( 

('Data', CBYTE_ARRAY) 

) 

 

class ULONG_ARRAY(NDRUniVaryingArray): 

item = ULONG 

 

# 2.3.1 EVENT_DESCRIPTOR 

class EVENT_DESCRIPTOR(NDRSTRUCT): 

structure = ( 

('Id', WORD), 

('Version', BYTE), 

('Channel', BYTE), 

('LevelSeverity', BYTE), 

('Opcode', BYTE), 

('Task', WORD), 

('Keyword', ULONG), 

) 

 

class BOOKMARK(NDRSTRUCT): 

structure = ( 

('BookmarkSize', DWORD), 

('HeaderSize', '<L=0x18'), 

('ChannelSize', DWORD), 

('CurrentChannel', DWORD), 

('ReadDirection', DWORD), 

('RecordIdsOffset', DWORD), 

('LogRecordNumbers', ULONG_ARRAY), 

) 

 

 

#2.2.17 RESULT_SET 

class RESULT_SET(NDRSTRUCT): 

structure = ( 

('TotalSize', DWORD), 

('HeaderSize', DWORD), 

('EventOffset', DWORD), 

('BookmarkOffset', DWORD), 

('BinXmlSize', DWORD), 

('EventData', BYTE_ARRAY), 

#('NumberOfSubqueryIDs', '<L=0'), 

#('SubqueryIDs', BYTE_ARRAY), 

#('BookMarkData', BOOKMARK), 

) 

 

################################################################################ 

# RPC CALLS 

################################################################################ 

 

class EvtRpcRegisterLogQuery(NDRCALL): 

opnum = 5 

structure = ( 

('Path', LPWSTR), 

('Query', WSTR), 

('Flags', DWORD), 

) 

 

class EvtRpcRegisterLogQueryResponse(NDRCALL): 

structure = ( 

('Handle', CONTEXT_HANDLE_LOG_QUERY), 

('OpControl', CONTEXT_HANDLE_OPERATION_CONTROL), 

('QueryChannelInfoSize', DWORD), 

('QueryChannelInfo', EvtRpcQueryChannelInfoArray), 

('Error', RPC_INFO), 

) 

 

class EvtRpcQueryNext(NDRCALL): 

opnum = 11 

structure = ( 

('LogQuery', CONTEXT_HANDLE_LOG_QUERY), 

('NumRequestedRecords', DWORD), 

('TimeOutEnd', DWORD), 

('Flags', DWORD), 

) 

 

class EvtRpcQueryNextResponse(NDRCALL): 

structure = ( 

('NumActualRecords', DWORD), 

('EventDataIndices', DWORD_ARRAY), 

('EventDataSizes', DWORD_ARRAY), 

('ResultBufferSize', DWORD), 

('ResultBuffer', BYTE_ARRAY), 

('ErrorCode', ULONG), 

) 

 

class EvtRpcQuerySeek(NDRCALL): 

opnum = 12 

structure = ( 

('LogQuery', CONTEXT_HANDLE_LOG_QUERY), 

('Pos', LARGE_INTEGER), 

('BookmarkXML', LPWSTR), 

('Flags', DWORD), 

) 

 

class EvtRpcQuerySeekResponse(NDRCALL): 

structure = ( 

('Error', RPC_INFO), 

) 

 

class EvtRpcClose(NDRCALL): 

opnum = 13 

structure = ( 

("Handle", CONTEXT_HANDLE_LOG_HANDLE), 

) 

 

class EvtRpcCloseResponse(NDRCALL): 

structure = ( 

("Handle", PCONTEXT_HANDLE_LOG_HANDLE), 

('ErrorCode', ULONG), 

) 

 

class EvtRpcOpenLogHandle(NDRCALL): 

opnum = 17 

structure = ( 

('Channel', WSTR), 

('Flags', DWORD), 

) 

 

class EvtRpcOpenLogHandleResponse(NDRCALL): 

structure = ( 

('Handle', PCONTEXT_HANDLE_LOG_HANDLE), 

('Error', RPC_INFO), 

) 

 

class EvtRpcGetChannelList(NDRCALL): 

opnum = 19 

structure = ( 

('Flags', DWORD), 

) 

 

class EvtRpcGetChannelListResponse(NDRCALL): 

structure = ( 

('NumChannelPaths', DWORD), 

('ChannelPaths', WSTR_ARRAY), 

('ErrorCode', ULONG), 

) 

 

################################################################################ 

# OPNUMs and their corresponding structures 

################################################################################ 

 

OPNUMS = { 

5 : (EvtRpcRegisterLogQuery, EvtRpcRegisterLogQueryResponse), 

11 : (EvtRpcQueryNext, EvtRpcQueryNextResponse), 

12 : (EvtRpcQuerySeek, EvtRpcQuerySeekResponse), 

13 : (EvtRpcClose, EvtRpcCloseResponse), 

17 : (EvtRpcOpenLogHandle, EvtRpcOpenLogHandle), 

19 : (EvtRpcGetChannelList, EvtRpcGetChannelListResponse), 

} 

 

################################################################################ 

# HELPER FUNCTIONS 

################################################################################ 

 

def hEvtRpcRegisterLogQuery(dce, path, flags, query='*\x00'): 

request = EvtRpcRegisterLogQuery() 

 

request['Path'] = path 

request['Query'] = query 

request['Flags'] = flags 

resp = dce.request(request) 

return resp 

 

def hEvtRpcQueryNext(dce, handle, numRequestedRecords, timeOutEnd=1000): 

request = EvtRpcQueryNext() 

 

request['LogQuery'] = handle 

request['NumRequestedRecords'] = numRequestedRecords 

request['TimeOutEnd'] = timeOutEnd 

request['Flags'] = 0 

status = system_errors.ERROR_MORE_DATA 

resp = dce.request(request) 

while status == system_errors.ERROR_MORE_DATA: 

try: 

resp = dce.request(request) 

except DCERPCException as e: 

if str(e).find('ERROR_NO_MORE_ITEMS') < 0: 

raise 

elif str(e).find('ERROR_TIMEOUT') < 0: 

raise 

resp = e.get_packet() 

return resp 

 

def hEvtRpcClose(dce, handle): 

request = EvtRpcClose() 

request['Handle'] = handle 

resp = dce.request(request) 

return resp 

 

def hEvtRpcOpenLogHandle(dce, channel, flags): 

request = EvtRpcOpenLogHandle() 

 

request['Channel'] = channel 

request['Flags'] = flags 

return dce.request(request) 

 

def hEvtRpcGetChannelList(dce): 

request = EvtRpcGetChannelList() 

 

request['Flags'] = 0 

resp = dce.request(request) 

return resp