Coverage for /root/GitHubProjects/impacket/impacket/dcerpc/v5/dhcpm.py: 60%

# Coverage for /root/GitHubProjects/impacket/impacket/dcerpc/v5/dhcpm.py : 60%

## 482 statements

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# SECUREAUTH LABS. Copyright 2018 SecureAuth Corporation. All rights reserved.

2#

3# This software is provided under under a slightly modified version

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

5# for more information.

6#

7# Author: Alberto Solino (@agsolino)

8#

9# Description:

10# [MS-DHCPM] Interface implementation

11#

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

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

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

15#

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

17# They are located at the end of this file.

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

19# There are test cases for them too.

20#

21 from __future__ import division

22 from __future__ import print_function

23 from impacket import system_errors

24 from impacket.dcerpc.v5.dtypes import LPWSTR, ULONG, NULL, DWORD, BOOL, BYTE, LPDWORD, WORD

25 from impacket.dcerpc.v5.ndr import NDRCALL, NDRUniConformantArray, NDRPOINTER, NDRSTRUCT, NDRENUM, NDRUNION

26 from impacket.dcerpc.v5.rpcrt import DCERPCException

27 from impacket.dcerpc.v5.enum import Enum

28 from impacket.uuid import uuidtup_to_bin

29

30 MSRPC_UUID_DHCPSRV = uuidtup_to_bin(('6BFFD098-A112-3610-9833-46C3F874532D', '1.0'))

31 MSRPC_UUID_DHCPSRV2 = uuidtup_to_bin(('5B821720-F63B-11D0-AAD2-00C04FC324DB', '1.0'))

32

33

34 class DCERPCSessionError(DCERPCException):

35 ERROR_MESSAGES = {

36 0x00004E2D: ("ERROR_DHCP_JET_ERROR", "An error occurred while accessing the DHCP server database."),

37 0x00004E25: ("ERROR_DHCP_SUBNET_NOT_PRESENT", "The specified IPv4 subnet does not exist."),

38 0x00004E54: ("ERROR_DHCP_SUBNET_EXISTS", "The IPv4 scope parameters are incorrect. Either the IPv4 scope already"

39 " exists, corresponding to the SubnetAddress and SubnetMask members of "

40 "the structure DHCP_SUBNET_INFO (section 2.2.1.2.8), or there is a "

41 "range overlap of IPv4 addresses between those associated with the "

42 "SubnetAddress and SubnetMask fields of the new IPv4 scope and the "

43 "subnet address and mask of an already existing IPv4 scope"),

44

45 }

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

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

48

49 def __str__(self):

50 key = self.error_code

51 if key in system_errors.ERROR_MESSAGES:

52 error_msg_short = system_errors.ERROR_MESSAGES[key][0]

53 error_msg_verbose = system_errors.ERROR_MESSAGES[key][1]

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

55 elif key in self.ERROR_MESSAGES:

56 error_msg_short = self.ERROR_MESSAGES[key][0]

57 error_msg_verbose = self.ERROR_MESSAGES[key][1]

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

59 else:

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

61

62################################################################################

63# CONSTANTS

64################################################################################

65 DHCP_SRV_HANDLE = LPWSTR

66 DHCP_IP_ADDRESS = DWORD

67 DHCP_IP_MASK = DWORD

68 DHCP_OPTION_ID = DWORD

69

70# DHCP enumeratiom flags

71 DHCP_FLAGS_OPTION_DEFAULT = 0x00000000

72 DHCP_FLAGS_OPTION_IS_VENDOR = 0x00000003

73

74# Errors

75 ERROR_DHCP_JET_ERROR = 0x00004E2D

76 ERROR_DHCP_SUBNET_NOT_PRESENT = 0x00004E25

77 ERROR_DHCP_SUBNET_EXISTS = 0x00004E54

78################################################################################

79# STRUCTURES

80################################################################################

81# 2.2.1.1.3 DHCP_SEARCH_INFO_TYPE

82 class DHCP_SEARCH_INFO_TYPE(NDRENUM):

83 class enumItems(Enum):

84 DhcpClientIpAddress = 0

85 DhcpClientHardwareAddress = 1

86 DhcpClientName = 2

87

88# 2.2.1.1.11 QuarantineStatus

89 class QuarantineStatus(NDRENUM):

90 class enumItems(Enum):

91 NOQUARANTINE = 0

92 RESTRICTEDACCESS = 1

93 DROPPACKET = 2

94 PROBATION = 3

95 EXEMPT = 4

96 DEFAULTQUARSETTING = 5

97 NOQUARINFO = 6

98

99# 2.2.1.2.7 DHCP_HOST_INFO

100 class DHCP_HOST_INFO(NDRSTRUCT):

101 structure = (

102 ('IpAddress', DHCP_IP_ADDRESS),

103 ('NetBiosName', LPWSTR),

104 ('HostName', LPWSTR),

105 )

106

107# 2.2.1.2.9 DHCP_BINARY_DATA

108 class BYTE_ARRAY(NDRUniConformantArray):

109 item = 'c'

110

111 class PBYTE_ARRAY(NDRPOINTER):

112 referent = (

113 ('Data', BYTE_ARRAY),

114 )

115

116 class DHCP_BINARY_DATA(NDRSTRUCT):

117 structure = (

118 ('DataLength', DWORD),

119 ('Data_', PBYTE_ARRAY),

120 )

121

122 DHCP_CLIENT_UID = DHCP_BINARY_DATA

123

124# 2.2.1.2.11 DATE_TIME

125 class DATE_TIME(NDRSTRUCT):

126 structure = (

127 ('dwLowDateTime', DWORD),

128 ('dwHighDateTime', DWORD),

129 )

130

131# 2.2.1.2.19 DHCP_CLIENT_INFO_VQ

132 class DHCP_CLIENT_INFO_VQ(NDRSTRUCT):

133 structure = (

134 ('ClientIpAddress', DHCP_IP_ADDRESS),

135 ('SubnetMask', DHCP_IP_MASK),

136 ('ClientHardwareAddress', DHCP_CLIENT_UID),

137 ('ClientName', LPWSTR),

138 ('ClientComment', LPWSTR),

139 ('ClientLeaseExpires', DATE_TIME),

140 ('OwnerHost', DHCP_HOST_INFO),

141 ('bClientType', BYTE),

142 ('AddressState', BYTE),

143 ('Status', QuarantineStatus),

144 ('ProbationEnds', DATE_TIME),

145 ('QuarantineCapable', BOOL),

146 )

147

148 class DHCP_CLIENT_SEARCH_UNION(NDRUNION):

149 union = {

150 DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress: ('ClientIpAddress', DHCP_IP_ADDRESS),

151 DHCP_SEARCH_INFO_TYPE.DhcpClientHardwareAddress: ('ClientHardwareAddress', DHCP_CLIENT_UID),

152 DHCP_SEARCH_INFO_TYPE.DhcpClientName: ('ClientName', LPWSTR),

153 }

154

155 class DHCP_SEARCH_INFO(NDRSTRUCT):

156 structure = (

157 ('SearchType', DHCP_SEARCH_INFO_TYPE),

158 ('SearchInfo', DHCP_CLIENT_SEARCH_UNION),

159 )

160

161# 2.2.1.2.14 DHCP_CLIENT_INFO_V4

162 class DHCP_CLIENT_INFO_V4(NDRSTRUCT):

163 structure = (

164 ('ClientIpAddress', DHCP_IP_ADDRESS),

165 ('SubnetMask', DHCP_IP_MASK),

166 ('ClientHardwareAddress', DHCP_CLIENT_UID),

167 ('ClientName', LPWSTR),

168 ('ClientComment', LPWSTR),

169 ('ClientLeaseExpires', DATE_TIME),

170 ('OwnerHost', DHCP_HOST_INFO),

171 ('bClientType', BYTE),

172 )

173

174 class DHCP_CLIENT_INFO_V5(NDRSTRUCT):

175 structure = (

176 ('ClientIpAddress', DHCP_IP_ADDRESS),

177 ('SubnetMask', DHCP_IP_MASK),

178 ('ClientHardwareAddress', DHCP_CLIENT_UID),

179 ('ClientName', LPWSTR),

180 ('ClientComment', LPWSTR),

181 ('ClientLeaseExpires', DATE_TIME),

182 ('OwnerHost', DHCP_HOST_INFO),

183 ('bClientType', BYTE),

184 ('AddressState', BYTE),

185 )

186

187 class LPDHCP_CLIENT_INFO_V4(NDRPOINTER):

188 referent = (

189 ('Data', DHCP_CLIENT_INFO_V4),

190 )

191

192 class LPDHCP_CLIENT_INFO_V5(NDRPOINTER):

193 referent = (

194 ('Data', DHCP_CLIENT_INFO_V5),

195 )

196

197# 2.2.1.2.115 DHCP_CLIENT_INFO_PB

198 class DHCP_CLIENT_INFO_PB(NDRSTRUCT):

199 structure = (

200 ('ClientIpAddress', DHCP_IP_ADDRESS),

201 ('SubnetMask', DHCP_IP_MASK),

202 ('ClientHardwareAddress', DHCP_CLIENT_UID),

203 ('ClientName', LPWSTR),

204 ('ClientComment', LPWSTR),

205 ('ClientLeaseExpires', DATE_TIME),

206 ('OwnerHost', DHCP_HOST_INFO),

207 ('bClientType', BYTE),

208 ('AddressState', BYTE),

209 ('Status', QuarantineStatus),

210 ('ProbationEnds', DATE_TIME),

211 ('QuarantineCapable', BOOL),

212 ('FilterStatus', DWORD),

213 ('PolicyName', LPWSTR),

214 )

215

216 class LPDHCP_CLIENT_INFO_PB(NDRPOINTER):

217 referent = (

218 ('Data', DHCP_CLIENT_INFO_PB),

219 )

220

221 class LPDHCP_CLIENT_INFO_VQ(NDRPOINTER):

222 referent = (

223 ('Data', DHCP_CLIENT_INFO_VQ),

224 )

225

226 class DHCP_CLIENT_INFO_VQ_ARRAY(NDRUniConformantArray):

227 item = LPDHCP_CLIENT_INFO_VQ

228

229 class LPDHCP_CLIENT_INFO_VQ_ARRAY(NDRPOINTER):

230 referent = (

231 ('Data', DHCP_CLIENT_INFO_VQ_ARRAY),

232 )

233

234 class DHCP_CLIENT_INFO_ARRAY_VQ(NDRSTRUCT):

235 structure = (

236 ('NumElements', DWORD),

237 ('Clients', LPDHCP_CLIENT_INFO_VQ_ARRAY),

238 )

239

240 class LPDHCP_CLIENT_INFO_ARRAY_VQ(NDRPOINTER):

241 referent = (

242 ('Data', DHCP_CLIENT_INFO_ARRAY_VQ),

243 )

244

245 class DHCP_CLIENT_INFO_V4_ARRAY(NDRUniConformantArray):

246 item = LPDHCP_CLIENT_INFO_V4

247

248 class DHCP_CLIENT_INFO_V5_ARRAY(NDRUniConformantArray):

249 item = LPDHCP_CLIENT_INFO_V5

250

251 class LPDHCP_CLIENT_INFO_V4_ARRAY(NDRPOINTER):

252 referent = (

253 ('Data', DHCP_CLIENT_INFO_V4_ARRAY),

254 )

255

256 class LPDHCP_CLIENT_INFO_V5_ARRAY(NDRPOINTER):

257 referent = (

258 ('Data', DHCP_CLIENT_INFO_V5_ARRAY),

259 )

260

261 class DHCP_CLIENT_INFO_ARRAY_V4(NDRSTRUCT):

262 structure = (

263 ('NumElements', DWORD),

264 ('Clients', LPDHCP_CLIENT_INFO_V4_ARRAY),

265 )

266

267 class DHCP_CLIENT_INFO_ARRAY_V5(NDRSTRUCT):

268 structure = (

269 ('NumElements', DWORD),

270 ('Clients', LPDHCP_CLIENT_INFO_V4_ARRAY),

271 )

272

273 class LPDHCP_CLIENT_INFO_ARRAY_V5(NDRPOINTER):

274 referent = (

275 ('Data', DHCP_CLIENT_INFO_ARRAY_V5),

276 )

277

278 class LPDHCP_CLIENT_INFO_ARRAY_V4(NDRPOINTER):

279 referent = (

280 ('Data', DHCP_CLIENT_INFO_ARRAY_V4),

281 )

282

283 class DHCP_IP_ADDRESS_ARRAY(NDRUniConformantArray):

284 item = DHCP_IP_ADDRESS

285

286 class LPDHCP_IP_ADDRESS_ARRAY(NDRPOINTER):

287 referent = (

288 ('Data', DHCP_IP_ADDRESS_ARRAY),

289 )

290

291 class DHCP_IP_ARRAY(NDRSTRUCT):

292 structure = (

293 ('NumElements', DWORD),

294 ('Elements', LPDHCP_IP_ADDRESS_ARRAY),

295 )

296

297 class DHCP_SUBNET_STATE(NDRENUM):

298 class enumItems(Enum):

299 DhcpSubnetEnabled = 0

300 DhcpSubnetDisabled = 1

301 DhcpSubnetEnabledSwitched = 2

302 DhcpSubnetDisabledSwitched = 3

303 DhcpSubnetInvalidState = 4

304

305 class DHCP_SUBNET_INFO(NDRSTRUCT):

306 structure = (

307 ('SubnetAddress', DHCP_IP_ADDRESS),

308 ('SubnetMask', DHCP_IP_MASK),

309 ('SubnetName', LPWSTR),

310 ('SubnetComment', LPWSTR),

311 ('PrimaryHost', DHCP_HOST_INFO),

312 ('SubnetState', DHCP_SUBNET_STATE),

313 )

314

315 class LPDHCP_SUBNET_INFO(NDRPOINTER):

316 referent = (

317 ('Data', DHCP_SUBNET_INFO),

318 )

319

320 class DHCP_OPTION_SCOPE_TYPE(NDRENUM):

321 class enumItems(Enum):

322 DhcpDefaultOptions = 0

323 DhcpGlobalOptions = 1

324 DhcpSubnetOptions = 2

325 DhcpReservedOptions = 3

326 DhcpMScopeOptions = 4

327

328 class DHCP_RESERVED_SCOPE(NDRSTRUCT):

329 structure = (

330 ('ReservedIpAddress', DHCP_IP_ADDRESS),

331 ('ReservedIpSubnetAddress', DHCP_IP_ADDRESS),

332 )

333

334 class DHCP_OPTION_SCOPE_UNION(NDRUNION):

335 union = {

336 DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions : (),

337 DHCP_OPTION_SCOPE_TYPE.DhcpGlobalOptions : (),

338 DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions : ('SubnetScopeInfo', DHCP_IP_ADDRESS),

339 DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions : ('ReservedScopeInfo', DHCP_RESERVED_SCOPE),

340 DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions : ('MScopeInfo', LPWSTR),

341 }

342

343 class DHCP_OPTION_SCOPE_INFO(NDRSTRUCT):

344 structure = (

345 ('ScopeType', DHCP_OPTION_SCOPE_TYPE),

346 ('ScopeInfo', DHCP_OPTION_SCOPE_UNION),

347 )

348

349 class LPDHCP_OPTION_SCOPE_INFO(NDRPOINTER):

350 referent = (

351 ('Data', DHCP_OPTION_SCOPE_INFO)

352 )

353

354 class DWORD_DWORD(NDRSTRUCT):

355 structure = (

356 ('DWord1', DWORD),

357 ('DWord2', DWORD),

358 )

359

360 class DHCP_BOOTP_IP_RANGE(NDRSTRUCT):

361 structure = (

362 ('StartAddress', DHCP_IP_ADDRESS),

363 ('EndAddress', DHCP_IP_ADDRESS),

364 ('BootpAllocated', ULONG),

365 ('MaxBootpAllowed', DHCP_IP_ADDRESS),

366 ('MaxBootpAllowed', ULONG ),

367 )

368

369 class DHCP_IP_RESERVATION_V4(NDRSTRUCT):

370 structure = (

371 ('ReservedIpAddress', DHCP_IP_ADDRESS),

372 ('ReservedForClient', DHCP_CLIENT_UID),

373 ('bAllowedClientTypes', BYTE),

374 )

375

376 class DHCP_IP_RANGE(NDRSTRUCT):

377 structure = (

378 ('StartAddress', DHCP_IP_ADDRESS),

379 ('EndAddress', DHCP_IP_ADDRESS),

380 )

381

382 class DHCP_IP_CLUSTER(NDRSTRUCT):

383 structure = (

384 ('ClusterAddress', DHCP_IP_ADDRESS),

385 ('ClusterMask', DWORD),

386 )

387

388 class DHCP_SUBNET_ELEMENT_TYPE(NDRENUM):

389 class enumItems(Enum):

390 DhcpIpRanges = 0

391 DhcpSecondaryHosts = 1

392 DhcpReservedIps = 2

393 DhcpExcludedIpRanges = 3

394 DhcpIpUsedClusters = 4

395 DhcpIpRangesDhcpOnly = 5

396 DhcpIpRangesDhcpBootp = 6

397 DhcpIpRangesBootpOnly = 7

398

399 class DHCP_SUBNET_ELEMENT_UNION_V5(NDRUNION):

400 union = {

401 DHCP_SUBNET_ELEMENT_TYPE.DhcpIpRanges : ('IpRange', DHCP_BOOTP_IP_RANGE),

402 DHCP_SUBNET_ELEMENT_TYPE.DhcpSecondaryHosts : ('SecondaryHost', DHCP_HOST_INFO),

403 DHCP_SUBNET_ELEMENT_TYPE.DhcpReservedIps : ('ReservedIp', DHCP_IP_RESERVATION_V4),

404 DHCP_SUBNET_ELEMENT_TYPE.DhcpExcludedIpRanges : ('ExcludeIpRange', DHCP_IP_RANGE),

405 DHCP_SUBNET_ELEMENT_TYPE.DhcpIpUsedClusters : ('IpUsedCluster', DHCP_IP_CLUSTER),

406 }

407

408 class DHCP_SUBNET_ELEMENT_DATA_V5(NDRSTRUCT):

409 structure = (

410 ('ElementType', DHCP_SUBNET_ELEMENT_TYPE),

411 ('Element', DHCP_SUBNET_ELEMENT_UNION_V5),

412 )

413

414 class LPDHCP_SUBNET_ELEMENT_DATA_V5(NDRUniConformantArray):

415 item = DHCP_SUBNET_ELEMENT_DATA_V5

416

417 class DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5(NDRSTRUCT):

418 structure = (

419 ('NumElements', DWORD),

420 ('Elements', LPDHCP_SUBNET_ELEMENT_DATA_V5),

421 )

422

423 class LPDHCP_SUBNET_ELEMENT_INFO_ARRAY_V5(NDRPOINTER):

424 referent = (

425 ('Data', DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5)

426 )

427

428 class DHCP_OPTION_DATA_TYPE(NDRENUM):

429 class enumItems(Enum):

430 DhcpByteOption = 0

431 DhcpWordOption = 1

432 DhcpDWordOption = 2

433 DhcpDWordDWordOption = 3

434 DhcpIpAddressOption = 4

435 DhcpStringDataOption = 5

436 DhcpBinaryDataOption = 6

437 DhcpEncapsulatedDataOption = 7

438 DhcpIpv6AddressOption = 8

439

440 class DHCP_OPTION_ELEMENT_UNION(NDRUNION):

441 commonHdr = (

442 ('tag', DHCP_OPTION_DATA_TYPE),

443 )

444 union = {

445 DHCP_OPTION_DATA_TYPE.DhcpByteOption : ('ByteOption', BYTE),

446 DHCP_OPTION_DATA_TYPE.DhcpWordOption : ('WordOption', WORD),

447 DHCP_OPTION_DATA_TYPE.DhcpDWordOption : ('DWordOption', DWORD),

448 DHCP_OPTION_DATA_TYPE.DhcpDWordDWordOption : ('DWordDWordOption', DWORD_DWORD),

449 DHCP_OPTION_DATA_TYPE.DhcpIpAddressOption : ('IpAddressOption', DHCP_IP_ADDRESS),

450 DHCP_OPTION_DATA_TYPE.DhcpStringDataOption : ('StringDataOption', LPWSTR),

451 DHCP_OPTION_DATA_TYPE.DhcpBinaryDataOption : ('BinaryDataOption', DHCP_BINARY_DATA),

452 DHCP_OPTION_DATA_TYPE.DhcpEncapsulatedDataOption: ('EncapsulatedDataOption', DHCP_BINARY_DATA),

453 DHCP_OPTION_DATA_TYPE.DhcpIpv6AddressOption : ('Ipv6AddressDataOption', LPWSTR),

454 }

455

456 class DHCP_OPTION_DATA_ELEMENT(NDRSTRUCT):

457 structure = (

458 ('OptionType', DHCP_OPTION_DATA_TYPE),

459 ('Element', DHCP_OPTION_ELEMENT_UNION),

460 )

461

462 class DHCP_OPTION_DATA_ELEMENT_ARRAY2(NDRUniConformantArray):

463 item = DHCP_OPTION_DATA_ELEMENT

464

465 class LPDHCP_OPTION_DATA_ELEMENT(NDRPOINTER):

466 referent = (

467 ('Data', DHCP_OPTION_DATA_ELEMENT_ARRAY2),

468 )

469

470 class DHCP_OPTION_DATA(NDRSTRUCT):

471 structure = (

472 ('NumElements', DWORD),

473 ('Elements', LPDHCP_OPTION_DATA_ELEMENT),

474 )

475

476 class DHCP_OPTION_VALUE(NDRSTRUCT):

477 structure = (

478 ('OptionID', DHCP_OPTION_ID),

479 ('Value', DHCP_OPTION_DATA),

480 )

481

482 class PDHCP_OPTION_VALUE(NDRPOINTER):

483 referent = (

484 ('Data', DHCP_OPTION_VALUE),

485 )

486

487 class DHCP_OPTION_VALUE_ARRAY2(NDRUniConformantArray):

488 item = DHCP_OPTION_VALUE

489

490 class LPDHCP_OPTION_VALUE(NDRPOINTER):

491 referent = (

492 ('Data', DHCP_OPTION_VALUE_ARRAY2),

493 )

494

495 class DHCP_OPTION_VALUE_ARRAY(NDRSTRUCT):

496 structure = (

497 ('NumElements', DWORD),

498 ('Values', LPDHCP_OPTION_VALUE),

499 )

500

501 class LPDHCP_OPTION_VALUE_ARRAY(NDRPOINTER):

502 referent = (

503 ('Data', DHCP_OPTION_VALUE_ARRAY),

504 )

505

506 class DHCP_ALL_OPTION_VALUES(NDRSTRUCT):

507 structure = (

508 ('ClassName', LPWSTR),

509 ('VendorName', LPWSTR),

510 ('IsVendor', BOOL),

511 ('OptionsArray', LPDHCP_OPTION_VALUE_ARRAY),

512 )

513

514 class OPTION_VALUES_ARRAY(NDRUniConformantArray):

515 item = DHCP_ALL_OPTION_VALUES

516

517 class LPOPTION_VALUES_ARRAY(NDRPOINTER):

518 referent = (

519 ('Data', OPTION_VALUES_ARRAY),

520 )

521

522 class DHCP_ALL_OPTIONS_VALUES(NDRSTRUCT):

523 structure = (

524 ('Flags', DWORD),

525 ('NumElements', DWORD),

526 ('Options', LPOPTION_VALUES_ARRAY),

527 )

528

529 class LPDHCP_ALL_OPTION_VALUES(NDRPOINTER):

530 referent = (

531 ('Data', DHCP_ALL_OPTIONS_VALUES),

532 )

533

534################################################################################

535# RPC CALLS

536################################################################################

537# Interface dhcpsrv

538 class DhcpGetSubnetInfo(NDRCALL):

539 opnum = 2

540 structure = (

541 ('ServerIpAddress', DHCP_SRV_HANDLE),

542 ('SubnetAddress', DHCP_IP_ADDRESS),

543 )

544

545 class DhcpGetSubnetInfoResponse(NDRCALL):

546 structure = (

547 ('SubnetInfo', LPDHCP_SUBNET_INFO),

548 ('ErrorCode', ULONG),

549 )

550

551 class DhcpEnumSubnets(NDRCALL):

552 opnum = 3

553 structure = (

554 ('ServerIpAddress', DHCP_SRV_HANDLE),

555 ('ResumeHandle', LPDWORD),

556 ('PreferredMaximum', DWORD),

557 )

558

559 class DhcpEnumSubnetsResponse(NDRCALL):

560 structure = (

561 ('ResumeHandle', LPDWORD),

562 ('EnumInfo', DHCP_IP_ARRAY),

563 ('EnumRead', DWORD),

564 ('EnumTotal', DWORD),

565 ('ErrorCode', ULONG),

566 )

567

568 class DhcpGetOptionValue(NDRCALL):

569 opnum = 13

570 structure = (

571 ('ServerIpAddress', DHCP_SRV_HANDLE),

572 ('OptionID', DHCP_OPTION_ID),

573 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

574 )

575

576 class DhcpGetOptionValueResponse(NDRCALL):

577 structure = (

578 ('OptionValue', PDHCP_OPTION_VALUE),

579 ('ErrorCode', ULONG),

580 )

581

582 class DhcpEnumOptionValues(NDRCALL):

583 opnum = 14

584 structure = (

585 ('ServerIpAddress', DHCP_SRV_HANDLE),

586 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

587 ('ResumeHandle', LPDWORD),

588 ('PreferredMaximum', DWORD),

589 )

590

591 class DhcpEnumOptionValuesResponse(NDRCALL):

592 structure = (

593 ('ResumeHandle', DWORD),

594 ('OptionValues', LPDHCP_OPTION_VALUE_ARRAY),

595 ('OptionsRead', DWORD),

596 ('OptionsTotal', DWORD),

597 ('ErrorCode', ULONG),

598 )

599

600 class DhcpGetClientInfoV4(NDRCALL):

601 opnum = 34

602 structure = (

603 ('ServerIpAddress', DHCP_SRV_HANDLE),

604 ('SearchInfo', DHCP_SEARCH_INFO),

605 )

606

607 class DhcpGetClientInfoV4Response(NDRCALL):

608 structure = (

609 ('ClientInfo', LPDHCP_CLIENT_INFO_V4),

610 ('ErrorCode', ULONG),

611 )

612

613 class DhcpEnumSubnetClientsV4(NDRCALL):

614 opnum = 35

615 structure = (

616 ('ServerIpAddress', DHCP_SRV_HANDLE),

617 ('SubnetAddress', DHCP_IP_ADDRESS),

618 ('ResumeHandle', DWORD),

619 ('PreferredMaximum', DWORD),

620 )

621

622 class DhcpEnumSubnetClientsV4Response(NDRCALL):

623 structure = (

624 ('ResumeHandle', LPDWORD),

625 ('ClientInfo', LPDHCP_CLIENT_INFO_ARRAY_V4),

626 ('ClientsRead', DWORD),

627 ('ClientsTotal', DWORD),

628 ('ErrorCode', ULONG),

629 )

630

631# Interface dhcpsrv2

632

633 class DhcpEnumSubnetClientsV5(NDRCALL):

634 opnum = 0

635 structure = (

636 ('ServerIpAddress', DHCP_SRV_HANDLE),

637 ('SubnetAddress', DHCP_IP_ADDRESS),

638 ('ResumeHandle', LPDWORD),

639 ('PreferredMaximum', DWORD),

640 )

641

642 class DhcpEnumSubnetClientsV5Response(NDRCALL):

643 structure = (

644 ('ResumeHandle', DWORD),

645 ('ClientsInfo', LPDHCP_CLIENT_INFO_ARRAY_V5),

646 ('ClientsRead', DWORD),

647 ('ClientsTotal', DWORD),

648 )

649

650 class DhcpGetOptionValueV5(NDRCALL):

651 opnum = 21

652 structure = (

653 ('ServerIpAddress', DHCP_SRV_HANDLE),

654 ('Flags', DWORD),

655 ('OptionID', DHCP_OPTION_ID),

656 ('ClassName', LPWSTR),

657 ('VendorName', LPWSTR),

658 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

659 )

660

661 class DhcpGetOptionValueV5Response(NDRCALL):

662 structure = (

663 ('OptionValue', PDHCP_OPTION_VALUE),

664 ('ErrorCode', ULONG),

665 )

666

667 class DhcpEnumOptionValuesV5(NDRCALL):

668 opnum = 22

669 structure = (

670 ('ServerIpAddress', DHCP_SRV_HANDLE),

671 ('Flags', DWORD),

672 ('ClassName', LPWSTR),

673 ('VendorName', LPWSTR),

674 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

675 ('ResumeHandle', LPDWORD),

676 ('PreferredMaximum', DWORD),

677 )

678

679 class DhcpEnumOptionValuesV5Response(NDRCALL):

680 structure = (

681 ('ResumeHandle', DWORD),

682 ('OptionValues', LPDHCP_OPTION_VALUE_ARRAY),

683 ('OptionsRead', DWORD),

684 ('OptionsTotal', DWORD),

685 ('ErrorCode', ULONG),

686 )

687

688 class DhcpGetAllOptionValues(NDRCALL):

689 opnum = 30

690 structure = (

691 ('ServerIpAddress', DHCP_SRV_HANDLE),

692 ('Flags', DWORD),

693 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

694 )

695

696 class DhcpGetAllOptionValuesResponse(NDRCALL):

697 structure = (

698 ('Values', LPDHCP_ALL_OPTION_VALUES),

699 ('ErrorCode', ULONG),

700 )

701

702 class DhcpEnumSubnetElementsV5(NDRCALL):

703 opnum = 38

704 structure = (

705 ('ServerIpAddress', DHCP_SRV_HANDLE),

706 ('SubnetAddress', DHCP_IP_ADDRESS),

707 ('EnumElementType', DHCP_SUBNET_ELEMENT_TYPE),

708 ('ResumeHandle', LPDWORD),

709 ('PreferredMaximum', DWORD),

710 )

711

712 class DhcpEnumSubnetElementsV5Response(NDRCALL):

713 structure = (

714 ('ResumeHandle', DWORD),

715 ('EnumElementInfo', LPDHCP_SUBNET_ELEMENT_INFO_ARRAY_V5),

716 ('ElementsRead', DWORD),

717 ('ElementsTotal', DWORD),

718 ('ErrorCode', ULONG),

719 )

720

721 class DhcpEnumSubnetClientsVQ(NDRCALL):

722 opnum = 47

723 structure = (

724 ('ServerIpAddress', DHCP_SRV_HANDLE),

725 ('SubnetAddress', DHCP_IP_ADDRESS),

726 ('ResumeHandle', LPDWORD),

727 ('PreferredMaximum', DWORD),

728 )

729

730 class DhcpEnumSubnetClientsVQResponse(NDRCALL):

731 structure = (

732 ('ResumeHandle', LPDWORD),

733 ('ClientInfo', LPDHCP_CLIENT_INFO_ARRAY_VQ),

734 ('ClientsRead', DWORD),

735 ('ClientsTotal', DWORD),

736 ('ErrorCode', ULONG),

737 )

738

739 class DhcpV4GetClientInfo(NDRCALL):

740 opnum = 123

741 structure = (

742 ('ServerIpAddress', DHCP_SRV_HANDLE),

743 ('SearchInfo', DHCP_SEARCH_INFO),

744 )

745

746 class DhcpV4GetClientInfoResponse(NDRCALL):

747 structure = (

748 ('ClientInfo', LPDHCP_CLIENT_INFO_PB),

749 ('ErrorCode', ULONG),

750 )

751

752################################################################################

753# OPNUMs and their corresponding structures

754################################################################################

755 OPNUMS = {

756 0: (DhcpEnumSubnetClientsV5, DhcpEnumSubnetClientsV5Response),

757 2: (DhcpGetSubnetInfo, DhcpGetSubnetInfoResponse),

758 3: (DhcpEnumSubnets, DhcpEnumSubnetsResponse),

759 13: (DhcpGetOptionValue, DhcpGetOptionValueResponse),

760 14: (DhcpEnumOptionValues, DhcpEnumOptionValuesResponse),

761 21: (DhcpGetOptionValueV5, DhcpGetOptionValueV5Response),

762 22: (DhcpEnumOptionValuesV5, DhcpEnumOptionValuesV5Response),

763 30: (DhcpGetAllOptionValues, DhcpGetAllOptionValuesResponse),

764 34: (DhcpGetClientInfoV4, DhcpGetClientInfoV4Response),

765 35: (DhcpEnumSubnetClientsV4, DhcpEnumSubnetClientsV4Response),

766 38: (DhcpEnumSubnetElementsV5, DhcpEnumSubnetElementsV5Response),

767 47: (DhcpEnumSubnetClientsVQ, DhcpEnumSubnetClientsVQResponse),

768 123: (DhcpV4GetClientInfo, DhcpV4GetClientInfoResponse),

769}

770

771

772################################################################################

773# HELPER FUNCTIONS

774################################################################################

775 def hDhcpGetClientInfoV4(dce, searchType, searchValue):

776 request = DhcpGetClientInfoV4()

777

778 request['ServerIpAddress'] = NULL

779 request['SearchInfo']['SearchType'] = searchType

780 request['SearchInfo']['SearchInfo']['tag'] = searchType

781 if searchType == DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress:

782 request['SearchInfo']['SearchInfo']['ClientIpAddress'] = searchValue

783 elif searchType == DHCP_SEARCH_INFO_TYPE.DhcpClientHardwareAddress: 783 ↛ 785line 783 didn't jump to line 785, because the condition on line 783 was never true

784 # This should be a DHCP_BINARY_DATA

785 request['SearchInfo']['SearchInfo']['ClientHardwareAddress'] = searchValue

786 else:

787 request['SearchInfo']['SearchInfo']['ClientName'] = searchValue

788

789 return dce.request(request)

790

791 def hDhcpGetSubnetInfo(dce, subnetaddress):

792 request = DhcpGetSubnetInfo()

793

794 request['ServerIpAddress'] = NULL

795 request['SubnetAddress'] = subnetaddress

796 resp = dce.request(request)

797

798 return resp

799

800 def hDhcpGetOptionValue(dce, optionID, scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL):

801 request = DhcpGetOptionValue()

802

803 request['ServerIpAddress'] = NULL

804 request['OptionID'] = optionID

805 request['ScopeInfo']['ScopeType'] = scopetype

806 if scopetype != DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions and scopetype != DHCP_OPTION_SCOPE_TYPE.DhcpGlobalOptions:

807 request['ScopeInfo']['ScopeInfo']['tag'] = scopetype

808 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

809 request['ScopeInfo']['ScopeInfo']['SubnetScopeInfo'] = options

810 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

811 request['ScopeInfo']['ScopeInfo']['ReservedScopeInfo'] = options

812 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

813 request['ScopeInfo']['ScopeInfo']['MScopeInfo'] = options

814

815 status = system_errors.ERROR_MORE_DATA

816 while status == system_errors.ERROR_MORE_DATA:

817 try:

818 resp = dce.request(request)

819 except DCERPCException as e:

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

821 raise

822 resp = e.get_packet()

823 return resp

824

825 def hDhcpEnumOptionValues(dce, scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL,

826 preferredMaximum=0xffffffff):

827 request = DhcpEnumOptionValues()

828

829 request['ServerIpAddress'] = NULL

830 request['ScopeInfo']['ScopeType'] = scopetype

831 if scopetype != DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions and scopetype != DHCP_OPTION_SCOPE_TYPE.DhcpGlobalOptions:

832 request['ScopeInfo']['ScopeInfo']['tag'] = scopetype

833 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

834 request['ScopeInfo']['ScopeInfo']['SubnetScopeInfo'] = options

835 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

836 request['ScopeInfo']['ScopeInfo']['ReservedScopeInfo'] = options

837 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

838 request['ScopeInfo']['ScopeInfo']['MScopeInfo'] = options

839 request['ResumeHandle'] = NULL

840 request['PreferredMaximum'] = preferredMaximum

841

842 status = system_errors.ERROR_MORE_DATA

843 while status == system_errors.ERROR_MORE_DATA:

844 try:

845 resp = dce.request(request)

846 except DCERPCException as e:

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

848 raise

849 resp = e.get_packet()

850 return resp

851

852 def hDhcpEnumOptionValuesV5(dce, flags=DHCP_FLAGS_OPTION_DEFAULT, classname=NULL, vendorname=NULL,

853 scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL,

854 preferredMaximum=0xffffffff):

855 request = DhcpEnumOptionValuesV5()

856

857 request['ServerIpAddress'] = NULL

858 request['Flags'] = flags

859 request['ClassName'] = classname

860 request['VendorName'] = vendorname

861 request['ScopeInfo']['ScopeType'] = scopetype

862 request['ScopeInfo']['ScopeInfo']['tag'] = scopetype

863 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

864 request['ScopeInfo']['ScopeInfo']['SubnetScopeInfo'] = options

865 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

866 request['ScopeInfo']['ScopeInfo']['ReservedScopeInfo'] = options

867 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

868 request['ScopeInfo']['ScopeInfo']['MScopeInfo'] = options

869 request['ResumeHandle'] = NULL

870 request['PreferredMaximum'] = preferredMaximum

871

872 status = system_errors.ERROR_MORE_DATA

873 while status == system_errors.ERROR_MORE_DATA:

874 try:

875 resp = dce.request(request)

876 except DCERPCException as e:

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

878 raise

879 resp = e.get_packet()

880 return resp

881

882 def hDhcpGetOptionValueV5(dce, option_id, flags=DHCP_FLAGS_OPTION_DEFAULT, classname=NULL, vendorname=NULL,

883 scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL):

884 request = DhcpGetOptionValueV5()

885

886 request['ServerIpAddress'] = NULL

887 request['Flags'] = flags

888 request['OptionID'] = option_id

889 request['ClassName'] = classname

890 request['VendorName'] = vendorname

891 request['ScopeInfo']['ScopeType'] = scopetype

892 request['ScopeInfo']['ScopeInfo']['tag'] = scopetype

893 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions: 893 ↛ 895line 893 didn't jump to line 895, because the condition on line 893 was never false

894 request['ScopeInfo']['ScopeInfo']['SubnetScopeInfo'] = options

895 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

896 request['ScopeInfo']['ScopeInfo']['ReservedScopeInfo'] = options

897 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

898 request['ScopeInfo']['ScopeInfo']['MScopeInfo'] = options

899

900 status = system_errors.ERROR_MORE_DATA

901 while status == system_errors.ERROR_MORE_DATA: 901 ↛ exitline 901 didn't return from function 'hDhcpGetOptionValueV5', because the condition on line 901 was never false

902 try:

903 resp = dce.request(request)

904 except DCERPCException as e:

905 if str(e).find('ERROR_NO_MORE_ITEMS') < 0: 905 ↛ 907line 905 didn't jump to line 907, because the condition on line 905 was never false

906 raise

907 resp = e.get_packet()

908 return resp

909

910 def hDhcpGetAllOptionValues(dce, scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL):

911 request = DhcpGetAllOptionValues()

912

913 request['ServerIpAddress'] = NULL

914 request['Flags'] = NULL

915 request['ScopeInfo']['ScopeType'] = scopetype

916 request['ScopeInfo']['ScopeInfo']['tag'] = scopetype

917 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

918 request['ScopeInfo']['ScopeInfo']['SubnetScopeInfo'] = options

919 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

920 request['ScopeInfo']['ScopeInfo']['ReservedScopeInfo'] = options

921 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

922 request['ScopeInfo']['ScopeInfo']['MScopeInfo'] = options

923

924 status = system_errors.ERROR_MORE_DATA

925 while status == system_errors.ERROR_MORE_DATA:

926 try:

927 resp = dce.request(request)

928 except DCERPCException as e:

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

930 raise

931 resp = e.get_packet()

932 return resp

933

934 def hDhcpEnumSubnets(dce, preferredMaximum=0xffffffff):

935 request = DhcpEnumSubnets()

936

937 request['ServerIpAddress'] = NULL

938 request['ResumeHandle'] = NULL

939 request['PreferredMaximum'] = preferredMaximum

940 status = system_errors.ERROR_MORE_DATA

941 while status == system_errors.ERROR_MORE_DATA:

942 try:

943 resp = dce.request(request)

944 except DCERPCException as e:

945 if str(e).find('STATUS_MORE_ENTRIES') < 0:

946 raise

947 resp = e.get_packet()

948 return resp

949

950 def hDhcpEnumSubnetClientsVQ(dce, preferredMaximum=0xffffffff):

951 request = DhcpEnumSubnetClientsVQ()

952

953 request['ServerIpAddress'] = NULL

954 request['SubnetAddress'] = NULL

955 request['ResumeHandle'] = NULL

956 request['PreferredMaximum'] = preferredMaximum

957 status = system_errors.ERROR_MORE_DATA

958 while status == system_errors.ERROR_MORE_DATA:

959 try:

960 resp = dce.request(request)

961 except DCERPCException as e:

962 if str(e).find('STATUS_MORE_ENTRIES') < 0:

963 raise

964 resp = e.get_packet()

965 return resp

966

967 def hDhcpEnumSubnetClientsV4(dce, preferredMaximum=0xffffffff):

968 request = DhcpEnumSubnetClientsV4()

969

970 request['ServerIpAddress'] = NULL

971 request['SubnetAddress'] = NULL

972 request['ResumeHandle'] = NULL

973 request['PreferredMaximum'] = preferredMaximum

974 status = system_errors.ERROR_MORE_DATA

975 while status == system_errors.ERROR_MORE_DATA:

976 try:

977 resp = dce.request(request)

978 except DCERPCException as e:

979 if str(e).find('STATUS_MORE_ENTRIES') < 0:

980 raise

981 resp = e.get_packet()

982 return resp

983

984 def hDhcpEnumSubnetClientsV5(dce, subnetAddress=0, preferredMaximum=0xffffffff):

985 request = DhcpEnumSubnetClientsV5()

986

987 request['ServerIpAddress'] = NULL

988 request['SubnetAddress'] = subnetAddress

989 request['ResumeHandle'] = NULL

990 request['PreferredMaximum'] = preferredMaximum

991 status = system_errors.ERROR_MORE_DATA

992 while status == system_errors.ERROR_MORE_DATA: 992 ↛ exitline 992 didn't return from function 'hDhcpEnumSubnetClientsV5', because the condition on line 992 was never false

993 try:

994 resp = dce.request(request)

995 except DCERPCSessionError as e:

996 if str(e).find('STATUS_MORE_ENTRIES') < 0: 996 ↛ 998line 996 didn't jump to line 998, because the condition on line 996 was never false

997 raise

998 resp = e.get_packet()

999 return resp

1000

1001 def hDhcpEnumSubnetElementsV5(dce, subnet_address, element_type=DHCP_SUBNET_ELEMENT_TYPE.DhcpIpRanges, preferredMaximum=0xffffffff):

1002 request = DhcpEnumSubnetElementsV5()

1003

1004 request['ServerIpAddress'] = NULL

1005 request['SubnetAddress'] = subnet_address

1006 request['EnumElementType'] = element_type

1007 request['ResumeHandle'] = NULL

1008 request['PreferredMaximum'] = preferredMaximum

1009

1010 status = system_errors.ERROR_MORE_DATA

1011 while status == system_errors.ERROR_MORE_DATA:

1012 try:

1013 resp = dce.request(request)

1014 except DCERPCException as e:

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

1016 raise

1017 resp = e.get_packet()

1018 return resp