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# Impacket - Collection of Python classes for working with network protocols.

2#

3# SECUREAUTH LABS. Copyright (C) 2018 SecureAuth Corporation. All rights reserved.

4#

5# This software is provided under a slightly modified version

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

7# for more information.

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# Author:

22# Alberto Solino (@agsolino)

23#

24

25 from __future__ import division

26 from __future__ import print_function

27 from impacket import system_errors

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

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

30 from impacket.dcerpc.v5.rpcrt import DCERPCException

31 from impacket.dcerpc.v5.enum import Enum

32 from impacket.uuid import uuidtup_to_bin

33

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

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

36

37

38 class DCERPCSessionError(DCERPCException):

39 ERROR_MESSAGES = {

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

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

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

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

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

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

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

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

48

49 }

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

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

52

53 def __str__(self):

54 key = self.error_code

55 if key in system_errors.ERROR_MESSAGES:

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

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

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

59 elif key in self.ERROR_MESSAGES:

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

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

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

63 else:

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

65

66################################################################################

67# CONSTANTS

68################################################################################

69 DHCP_SRV_HANDLE = LPWSTR

70 DHCP_IP_ADDRESS = DWORD

71 DHCP_IP_MASK = DWORD

72 DHCP_OPTION_ID = DWORD

73

74# DHCP enumeratiom flags

75 DHCP_FLAGS_OPTION_DEFAULT = 0x00000000

76 DHCP_FLAGS_OPTION_IS_VENDOR = 0x00000003

77

78# Errors

79 ERROR_DHCP_JET_ERROR = 0x00004E2D

80 ERROR_DHCP_SUBNET_NOT_PRESENT = 0x00004E25

81 ERROR_DHCP_SUBNET_EXISTS = 0x00004E54

82################################################################################

83# STRUCTURES

84################################################################################

85# 2.2.1.1.3 DHCP_SEARCH_INFO_TYPE

86 class DHCP_SEARCH_INFO_TYPE(NDRENUM):

87 class enumItems(Enum):

88 DhcpClientIpAddress = 0

89 DhcpClientHardwareAddress = 1

90 DhcpClientName = 2

91

92# 2.2.1.1.11 QuarantineStatus

93 class QuarantineStatus(NDRENUM):

94 class enumItems(Enum):

95 NOQUARANTINE = 0

96 RESTRICTEDACCESS = 1

97 DROPPACKET = 2

98 PROBATION = 3

99 EXEMPT = 4

100 DEFAULTQUARSETTING = 5

101 NOQUARINFO = 6

102

103# 2.2.1.2.7 DHCP_HOST_INFO

104 class DHCP_HOST_INFO(NDRSTRUCT):

105 structure = (

106 ('IpAddress', DHCP_IP_ADDRESS),

107 ('NetBiosName', LPWSTR),

108 ('HostName', LPWSTR),

109 )

110

111# 2.2.1.2.9 DHCP_BINARY_DATA

112 class BYTE_ARRAY(NDRUniConformantArray):

113 item = 'c'

114

115 class PBYTE_ARRAY(NDRPOINTER):

116 referent = (

117 ('Data', BYTE_ARRAY),

118 )

119

120 class DHCP_BINARY_DATA(NDRSTRUCT):

121 structure = (

122 ('DataLength', DWORD),

123 ('Data_', PBYTE_ARRAY),

124 )

125

126 DHCP_CLIENT_UID = DHCP_BINARY_DATA

127

128# 2.2.1.2.11 DATE_TIME

129 class DATE_TIME(NDRSTRUCT):

130 structure = (

131 ('dwLowDateTime', DWORD),

132 ('dwHighDateTime', DWORD),

133 )

134

135# 2.2.1.2.19 DHCP_CLIENT_INFO_VQ

136 class DHCP_CLIENT_INFO_VQ(NDRSTRUCT):

137 structure = (

138 ('ClientIpAddress', DHCP_IP_ADDRESS),

139 ('SubnetMask', DHCP_IP_MASK),

140 ('ClientHardwareAddress', DHCP_CLIENT_UID),

141 ('ClientName', LPWSTR),

142 ('ClientComment', LPWSTR),

143 ('ClientLeaseExpires', DATE_TIME),

144 ('OwnerHost', DHCP_HOST_INFO),

145 ('bClientType', BYTE),

146 ('AddressState', BYTE),

147 ('Status', QuarantineStatus),

148 ('ProbationEnds', DATE_TIME),

149 ('QuarantineCapable', BOOL),

150 )

151

152 class DHCP_CLIENT_SEARCH_UNION(NDRUNION):

153 union = {

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

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

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

157 }

158

159 class DHCP_SEARCH_INFO(NDRSTRUCT):

160 structure = (

161 ('SearchType', DHCP_SEARCH_INFO_TYPE),

162 ('SearchInfo', DHCP_CLIENT_SEARCH_UNION),

163 )

164

165# 2.2.1.2.14 DHCP_CLIENT_INFO_V4

166 class DHCP_CLIENT_INFO_V4(NDRSTRUCT):

167 structure = (

168 ('ClientIpAddress', DHCP_IP_ADDRESS),

169 ('SubnetMask', DHCP_IP_MASK),

170 ('ClientHardwareAddress', DHCP_CLIENT_UID),

171 ('ClientName', LPWSTR),

172 ('ClientComment', LPWSTR),

173 ('ClientLeaseExpires', DATE_TIME),

174 ('OwnerHost', DHCP_HOST_INFO),

175 ('bClientType', BYTE),

176 )

177

178 class DHCP_CLIENT_INFO_V5(NDRSTRUCT):

179 structure = (

180 ('ClientIpAddress', DHCP_IP_ADDRESS),

181 ('SubnetMask', DHCP_IP_MASK),

182 ('ClientHardwareAddress', DHCP_CLIENT_UID),

183 ('ClientName', LPWSTR),

184 ('ClientComment', LPWSTR),

185 ('ClientLeaseExpires', DATE_TIME),

186 ('OwnerHost', DHCP_HOST_INFO),

187 ('bClientType', BYTE),

188 ('AddressState', BYTE),

189 )

190

191 class LPDHCP_CLIENT_INFO_V4(NDRPOINTER):

192 referent = (

193 ('Data', DHCP_CLIENT_INFO_V4),

194 )

195

196 class LPDHCP_CLIENT_INFO_V5(NDRPOINTER):

197 referent = (

198 ('Data', DHCP_CLIENT_INFO_V5),

199 )

200

201# 2.2.1.2.115 DHCP_CLIENT_INFO_PB

202 class DHCP_CLIENT_INFO_PB(NDRSTRUCT):

203 structure = (

204 ('ClientIpAddress', DHCP_IP_ADDRESS),

205 ('SubnetMask', DHCP_IP_MASK),

206 ('ClientHardwareAddress', DHCP_CLIENT_UID),

207 ('ClientName', LPWSTR),

208 ('ClientComment', LPWSTR),

209 ('ClientLeaseExpires', DATE_TIME),

210 ('OwnerHost', DHCP_HOST_INFO),

211 ('bClientType', BYTE),

212 ('AddressState', BYTE),

213 ('Status', QuarantineStatus),

214 ('ProbationEnds', DATE_TIME),

215 ('QuarantineCapable', BOOL),

216 ('FilterStatus', DWORD),

217 ('PolicyName', LPWSTR),

218 )

219

220 class LPDHCP_CLIENT_INFO_PB(NDRPOINTER):

221 referent = (

222 ('Data', DHCP_CLIENT_INFO_PB),

223 )

224

225 class LPDHCP_CLIENT_INFO_VQ(NDRPOINTER):

226 referent = (

227 ('Data', DHCP_CLIENT_INFO_VQ),

228 )

229

230 class DHCP_CLIENT_INFO_VQ_ARRAY(NDRUniConformantArray):

231 item = LPDHCP_CLIENT_INFO_VQ

232

233 class LPDHCP_CLIENT_INFO_VQ_ARRAY(NDRPOINTER):

234 referent = (

235 ('Data', DHCP_CLIENT_INFO_VQ_ARRAY),

236 )

237

238 class DHCP_CLIENT_INFO_ARRAY_VQ(NDRSTRUCT):

239 structure = (

240 ('NumElements', DWORD),

241 ('Clients', LPDHCP_CLIENT_INFO_VQ_ARRAY),

242 )

243

244 class LPDHCP_CLIENT_INFO_ARRAY_VQ(NDRPOINTER):

245 referent = (

246 ('Data', DHCP_CLIENT_INFO_ARRAY_VQ),

247 )

248

249 class DHCP_CLIENT_INFO_V4_ARRAY(NDRUniConformantArray):

250 item = LPDHCP_CLIENT_INFO_V4

251

252 class DHCP_CLIENT_INFO_V5_ARRAY(NDRUniConformantArray):

253 item = LPDHCP_CLIENT_INFO_V5

254

255 class LPDHCP_CLIENT_INFO_V4_ARRAY(NDRPOINTER):

256 referent = (

257 ('Data', DHCP_CLIENT_INFO_V4_ARRAY),

258 )

259

260 class LPDHCP_CLIENT_INFO_V5_ARRAY(NDRPOINTER):

261 referent = (

262 ('Data', DHCP_CLIENT_INFO_V5_ARRAY),

263 )

264

265 class DHCP_CLIENT_INFO_ARRAY_V4(NDRSTRUCT):

266 structure = (

267 ('NumElements', DWORD),

268 ('Clients', LPDHCP_CLIENT_INFO_V4_ARRAY),

269 )

270

271 class DHCP_CLIENT_INFO_ARRAY_V5(NDRSTRUCT):

272 structure = (

273 ('NumElements', DWORD),

274 ('Clients', LPDHCP_CLIENT_INFO_V4_ARRAY),

275 )

276

277 class LPDHCP_CLIENT_INFO_ARRAY_V5(NDRPOINTER):

278 referent = (

279 ('Data', DHCP_CLIENT_INFO_ARRAY_V5),

280 )

281

282 class LPDHCP_CLIENT_INFO_ARRAY_V4(NDRPOINTER):

283 referent = (

284 ('Data', DHCP_CLIENT_INFO_ARRAY_V4),

285 )

286

287 class DHCP_IP_ADDRESS_ARRAY(NDRUniConformantArray):

288 item = DHCP_IP_ADDRESS

289

290 class LPDHCP_IP_ADDRESS_ARRAY(NDRPOINTER):

291 referent = (

292 ('Data', DHCP_IP_ADDRESS_ARRAY),

293 )

294

295 class DHCP_IP_ARRAY(NDRSTRUCT):

296 structure = (

297 ('NumElements', DWORD),

298 ('Elements', LPDHCP_IP_ADDRESS_ARRAY),

299 )

300

301 class DHCP_SUBNET_STATE(NDRENUM):

302 class enumItems(Enum):

303 DhcpSubnetEnabled = 0

304 DhcpSubnetDisabled = 1

305 DhcpSubnetEnabledSwitched = 2

306 DhcpSubnetDisabledSwitched = 3

307 DhcpSubnetInvalidState = 4

308

309 class DHCP_SUBNET_INFO(NDRSTRUCT):

310 structure = (

311 ('SubnetAddress', DHCP_IP_ADDRESS),

312 ('SubnetMask', DHCP_IP_MASK),

313 ('SubnetName', LPWSTR),

314 ('SubnetComment', LPWSTR),

315 ('PrimaryHost', DHCP_HOST_INFO),

316 ('SubnetState', DHCP_SUBNET_STATE),

317 )

318

319 class LPDHCP_SUBNET_INFO(NDRPOINTER):

320 referent = (

321 ('Data', DHCP_SUBNET_INFO),

322 )

323

324 class DHCP_OPTION_SCOPE_TYPE(NDRENUM):

325 class enumItems(Enum):

326 DhcpDefaultOptions = 0

327 DhcpGlobalOptions = 1

328 DhcpSubnetOptions = 2

329 DhcpReservedOptions = 3

330 DhcpMScopeOptions = 4

331

332 class DHCP_RESERVED_SCOPE(NDRSTRUCT):

333 structure = (

334 ('ReservedIpAddress', DHCP_IP_ADDRESS),

335 ('ReservedIpSubnetAddress', DHCP_IP_ADDRESS),

336 )

337

338 class DHCP_OPTION_SCOPE_UNION(NDRUNION):

339 union = {

340 DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions : (),

341 DHCP_OPTION_SCOPE_TYPE.DhcpGlobalOptions : (),

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

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

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

345 }

346

347 class DHCP_OPTION_SCOPE_INFO(NDRSTRUCT):

348 structure = (

349 ('ScopeType', DHCP_OPTION_SCOPE_TYPE),

350 ('ScopeInfo', DHCP_OPTION_SCOPE_UNION),

351 )

352

353 class LPDHCP_OPTION_SCOPE_INFO(NDRPOINTER):

354 referent = (

355 ('Data', DHCP_OPTION_SCOPE_INFO)

356 )

357

358 class DWORD_DWORD(NDRSTRUCT):

359 structure = (

360 ('DWord1', DWORD),

361 ('DWord2', DWORD),

362 )

363

364 class DHCP_BOOTP_IP_RANGE(NDRSTRUCT):

365 structure = (

366 ('StartAddress', DHCP_IP_ADDRESS),

367 ('EndAddress', DHCP_IP_ADDRESS),

368 ('BootpAllocated', ULONG),

369 ('MaxBootpAllowed', DHCP_IP_ADDRESS),

370 ('MaxBootpAllowed', ULONG ),

371 )

372

373 class DHCP_IP_RESERVATION_V4(NDRSTRUCT):

374 structure = (

375 ('ReservedIpAddress', DHCP_IP_ADDRESS),

376 ('ReservedForClient', DHCP_CLIENT_UID),

377 ('bAllowedClientTypes', BYTE),

378 )

379

380 class DHCP_IP_RANGE(NDRSTRUCT):

381 structure = (

382 ('StartAddress', DHCP_IP_ADDRESS),

383 ('EndAddress', DHCP_IP_ADDRESS),

384 )

385

386 class DHCP_IP_CLUSTER(NDRSTRUCT):

387 structure = (

388 ('ClusterAddress', DHCP_IP_ADDRESS),

389 ('ClusterMask', DWORD),

390 )

391

392 class DHCP_SUBNET_ELEMENT_TYPE(NDRENUM):

393 class enumItems(Enum):

394 DhcpIpRanges = 0

395 DhcpSecondaryHosts = 1

396 DhcpReservedIps = 2

397 DhcpExcludedIpRanges = 3

398 DhcpIpUsedClusters = 4

399 DhcpIpRangesDhcpOnly = 5

400 DhcpIpRangesDhcpBootp = 6

401 DhcpIpRangesBootpOnly = 7

402

403 class DHCP_SUBNET_ELEMENT_UNION_V5(NDRUNION):

404 union = {

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

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

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

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

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

410 }

411

412 class DHCP_SUBNET_ELEMENT_DATA_V5(NDRSTRUCT):

413 structure = (

414 ('ElementType', DHCP_SUBNET_ELEMENT_TYPE),

415 ('Element', DHCP_SUBNET_ELEMENT_UNION_V5),

416 )

417

418 class LPDHCP_SUBNET_ELEMENT_DATA_V5(NDRUniConformantArray):

419 item = DHCP_SUBNET_ELEMENT_DATA_V5

420

421 class DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5(NDRSTRUCT):

422 structure = (

423 ('NumElements', DWORD),

424 ('Elements', LPDHCP_SUBNET_ELEMENT_DATA_V5),

425 )

426

427 class LPDHCP_SUBNET_ELEMENT_INFO_ARRAY_V5(NDRPOINTER):

428 referent = (

429 ('Data', DHCP_SUBNET_ELEMENT_INFO_ARRAY_V5)

430 )

431

432 class DHCP_OPTION_DATA_TYPE(NDRENUM):

433 class enumItems(Enum):

434 DhcpByteOption = 0

435 DhcpWordOption = 1

436 DhcpDWordOption = 2

437 DhcpDWordDWordOption = 3

438 DhcpIpAddressOption = 4

439 DhcpStringDataOption = 5

440 DhcpBinaryDataOption = 6

441 DhcpEncapsulatedDataOption = 7

442 DhcpIpv6AddressOption = 8

443

444 class DHCP_OPTION_ELEMENT_UNION(NDRUNION):

445 commonHdr = (

446 ('tag', DHCP_OPTION_DATA_TYPE),

447 )

448 union = {

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

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

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

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

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

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

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

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

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

458 }

459

460 class DHCP_OPTION_DATA_ELEMENT(NDRSTRUCT):

461 structure = (

462 ('OptionType', DHCP_OPTION_DATA_TYPE),

463 ('Element', DHCP_OPTION_ELEMENT_UNION),

464 )

465

466 class DHCP_OPTION_DATA_ELEMENT_ARRAY2(NDRUniConformantArray):

467 item = DHCP_OPTION_DATA_ELEMENT

468

469 class LPDHCP_OPTION_DATA_ELEMENT(NDRPOINTER):

470 referent = (

471 ('Data', DHCP_OPTION_DATA_ELEMENT_ARRAY2),

472 )

473

474 class DHCP_OPTION_DATA(NDRSTRUCT):

475 structure = (

476 ('NumElements', DWORD),

477 ('Elements', LPDHCP_OPTION_DATA_ELEMENT),

478 )

479

480 class DHCP_OPTION_VALUE(NDRSTRUCT):

481 structure = (

482 ('OptionID', DHCP_OPTION_ID),

483 ('Value', DHCP_OPTION_DATA),

484 )

485

486 class PDHCP_OPTION_VALUE(NDRPOINTER):

487 referent = (

488 ('Data', DHCP_OPTION_VALUE),

489 )

490

491 class DHCP_OPTION_VALUE_ARRAY2(NDRUniConformantArray):

492 item = DHCP_OPTION_VALUE

493

494 class LPDHCP_OPTION_VALUE(NDRPOINTER):

495 referent = (

496 ('Data', DHCP_OPTION_VALUE_ARRAY2),

497 )

498

499 class DHCP_OPTION_VALUE_ARRAY(NDRSTRUCT):

500 structure = (

501 ('NumElements', DWORD),

502 ('Values', LPDHCP_OPTION_VALUE),

503 )

504

505 class LPDHCP_OPTION_VALUE_ARRAY(NDRPOINTER):

506 referent = (

507 ('Data', DHCP_OPTION_VALUE_ARRAY),

508 )

509

510 class DHCP_ALL_OPTION_VALUES(NDRSTRUCT):

511 structure = (

512 ('ClassName', LPWSTR),

513 ('VendorName', LPWSTR),

514 ('IsVendor', BOOL),

515 ('OptionsArray', LPDHCP_OPTION_VALUE_ARRAY),

516 )

517

518 class OPTION_VALUES_ARRAY(NDRUniConformantArray):

519 item = DHCP_ALL_OPTION_VALUES

520

521 class LPOPTION_VALUES_ARRAY(NDRPOINTER):

522 referent = (

523 ('Data', OPTION_VALUES_ARRAY),

524 )

525

526 class DHCP_ALL_OPTIONS_VALUES(NDRSTRUCT):

527 structure = (

528 ('Flags', DWORD),

529 ('NumElements', DWORD),

530 ('Options', LPOPTION_VALUES_ARRAY),

531 )

532

533 class LPDHCP_ALL_OPTION_VALUES(NDRPOINTER):

534 referent = (

535 ('Data', DHCP_ALL_OPTIONS_VALUES),

536 )

537

538################################################################################

539# RPC CALLS

540################################################################################

541# Interface dhcpsrv

542 class DhcpGetSubnetInfo(NDRCALL):

543 opnum = 2

544 structure = (

545 ('ServerIpAddress', DHCP_SRV_HANDLE),

546 ('SubnetAddress', DHCP_IP_ADDRESS),

547 )

548

549 class DhcpGetSubnetInfoResponse(NDRCALL):

550 structure = (

551 ('SubnetInfo', LPDHCP_SUBNET_INFO),

552 ('ErrorCode', ULONG),

553 )

554

555 class DhcpEnumSubnets(NDRCALL):

556 opnum = 3

557 structure = (

558 ('ServerIpAddress', DHCP_SRV_HANDLE),

559 ('ResumeHandle', LPDWORD),

560 ('PreferredMaximum', DWORD),

561 )

562

563 class DhcpEnumSubnetsResponse(NDRCALL):

564 structure = (

565 ('ResumeHandle', LPDWORD),

566 ('EnumInfo', DHCP_IP_ARRAY),

567 ('EnumRead', DWORD),

568 ('EnumTotal', DWORD),

569 ('ErrorCode', ULONG),

570 )

571

572 class DhcpGetOptionValue(NDRCALL):

573 opnum = 13

574 structure = (

575 ('ServerIpAddress', DHCP_SRV_HANDLE),

576 ('OptionID', DHCP_OPTION_ID),

577 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

578 )

579

580 class DhcpGetOptionValueResponse(NDRCALL):

581 structure = (

582 ('OptionValue', PDHCP_OPTION_VALUE),

583 ('ErrorCode', ULONG),

584 )

585

586 class DhcpEnumOptionValues(NDRCALL):

587 opnum = 14

588 structure = (

589 ('ServerIpAddress', DHCP_SRV_HANDLE),

590 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

591 ('ResumeHandle', LPDWORD),

592 ('PreferredMaximum', DWORD),

593 )

594

595 class DhcpEnumOptionValuesResponse(NDRCALL):

596 structure = (

597 ('ResumeHandle', DWORD),

598 ('OptionValues', LPDHCP_OPTION_VALUE_ARRAY),

599 ('OptionsRead', DWORD),

600 ('OptionsTotal', DWORD),

601 ('ErrorCode', ULONG),

602 )

603

604 class DhcpGetClientInfoV4(NDRCALL):

605 opnum = 34

606 structure = (

607 ('ServerIpAddress', DHCP_SRV_HANDLE),

608 ('SearchInfo', DHCP_SEARCH_INFO),

609 )

610

611 class DhcpGetClientInfoV4Response(NDRCALL):

612 structure = (

613 ('ClientInfo', LPDHCP_CLIENT_INFO_V4),

614 ('ErrorCode', ULONG),

615 )

616

617 class DhcpEnumSubnetClientsV4(NDRCALL):

618 opnum = 35

619 structure = (

620 ('ServerIpAddress', DHCP_SRV_HANDLE),

621 ('SubnetAddress', DHCP_IP_ADDRESS),

622 ('ResumeHandle', DWORD),

623 ('PreferredMaximum', DWORD),

624 )

625

626 class DhcpEnumSubnetClientsV4Response(NDRCALL):

627 structure = (

628 ('ResumeHandle', LPDWORD),

629 ('ClientInfo', LPDHCP_CLIENT_INFO_ARRAY_V4),

630 ('ClientsRead', DWORD),

631 ('ClientsTotal', DWORD),

632 ('ErrorCode', ULONG),

633 )

634

635# Interface dhcpsrv2

636

637 class DhcpEnumSubnetClientsV5(NDRCALL):

638 opnum = 0

639 structure = (

640 ('ServerIpAddress', DHCP_SRV_HANDLE),

641 ('SubnetAddress', DHCP_IP_ADDRESS),

642 ('ResumeHandle', LPDWORD),

643 ('PreferredMaximum', DWORD),

644 )

645

646 class DhcpEnumSubnetClientsV5Response(NDRCALL):

647 structure = (

648 ('ResumeHandle', DWORD),

649 ('ClientsInfo', LPDHCP_CLIENT_INFO_ARRAY_V5),

650 ('ClientsRead', DWORD),

651 ('ClientsTotal', DWORD),

652 )

653

654 class DhcpGetOptionValueV5(NDRCALL):

655 opnum = 21

656 structure = (

657 ('ServerIpAddress', DHCP_SRV_HANDLE),

658 ('Flags', DWORD),

659 ('OptionID', DHCP_OPTION_ID),

660 ('ClassName', LPWSTR),

661 ('VendorName', LPWSTR),

662 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

663 )

664

665 class DhcpGetOptionValueV5Response(NDRCALL):

666 structure = (

667 ('OptionValue', PDHCP_OPTION_VALUE),

668 ('ErrorCode', ULONG),

669 )

670

671 class DhcpEnumOptionValuesV5(NDRCALL):

672 opnum = 22

673 structure = (

674 ('ServerIpAddress', DHCP_SRV_HANDLE),

675 ('Flags', DWORD),

676 ('ClassName', LPWSTR),

677 ('VendorName', LPWSTR),

678 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

679 ('ResumeHandle', LPDWORD),

680 ('PreferredMaximum', DWORD),

681 )

682

683 class DhcpEnumOptionValuesV5Response(NDRCALL):

684 structure = (

685 ('ResumeHandle', DWORD),

686 ('OptionValues', LPDHCP_OPTION_VALUE_ARRAY),

687 ('OptionsRead', DWORD),

688 ('OptionsTotal', DWORD),

689 ('ErrorCode', ULONG),

690 )

691

692 class DhcpGetAllOptionValues(NDRCALL):

693 opnum = 30

694 structure = (

695 ('ServerIpAddress', DHCP_SRV_HANDLE),

696 ('Flags', DWORD),

697 ('ScopeInfo', DHCP_OPTION_SCOPE_INFO),

698 )

699

700 class DhcpGetAllOptionValuesResponse(NDRCALL):

701 structure = (

702 ('Values', LPDHCP_ALL_OPTION_VALUES),

703 ('ErrorCode', ULONG),

704 )

705

706 class DhcpEnumSubnetElementsV5(NDRCALL):

707 opnum = 38

708 structure = (

709 ('ServerIpAddress', DHCP_SRV_HANDLE),

710 ('SubnetAddress', DHCP_IP_ADDRESS),

711 ('EnumElementType', DHCP_SUBNET_ELEMENT_TYPE),

712 ('ResumeHandle', LPDWORD),

713 ('PreferredMaximum', DWORD),

714 )

715

716 class DhcpEnumSubnetElementsV5Response(NDRCALL):

717 structure = (

718 ('ResumeHandle', DWORD),

719 ('EnumElementInfo', LPDHCP_SUBNET_ELEMENT_INFO_ARRAY_V5),

720 ('ElementsRead', DWORD),

721 ('ElementsTotal', DWORD),

722 ('ErrorCode', ULONG),

723 )

724

725 class DhcpEnumSubnetClientsVQ(NDRCALL):

726 opnum = 47

727 structure = (

728 ('ServerIpAddress', DHCP_SRV_HANDLE),

729 ('SubnetAddress', DHCP_IP_ADDRESS),

730 ('ResumeHandle', LPDWORD),

731 ('PreferredMaximum', DWORD),

732 )

733

734 class DhcpEnumSubnetClientsVQResponse(NDRCALL):

735 structure = (

736 ('ResumeHandle', LPDWORD),

737 ('ClientInfo', LPDHCP_CLIENT_INFO_ARRAY_VQ),

738 ('ClientsRead', DWORD),

739 ('ClientsTotal', DWORD),

740 ('ErrorCode', ULONG),

741 )

742

743 class DhcpV4GetClientInfo(NDRCALL):

744 opnum = 123

745 structure = (

746 ('ServerIpAddress', DHCP_SRV_HANDLE),

747 ('SearchInfo', DHCP_SEARCH_INFO),

748 )

749

750 class DhcpV4GetClientInfoResponse(NDRCALL):

751 structure = (

752 ('ClientInfo', LPDHCP_CLIENT_INFO_PB),

753 ('ErrorCode', ULONG),

754 )

755

756################################################################################

757# OPNUMs and their corresponding structures

758################################################################################

759 OPNUMS = {

760 0: (DhcpEnumSubnetClientsV5, DhcpEnumSubnetClientsV5Response),

761 2: (DhcpGetSubnetInfo, DhcpGetSubnetInfoResponse),

762 3: (DhcpEnumSubnets, DhcpEnumSubnetsResponse),

763 13: (DhcpGetOptionValue, DhcpGetOptionValueResponse),

764 14: (DhcpEnumOptionValues, DhcpEnumOptionValuesResponse),

765 21: (DhcpGetOptionValueV5, DhcpGetOptionValueV5Response),

766 22: (DhcpEnumOptionValuesV5, DhcpEnumOptionValuesV5Response),

767 30: (DhcpGetAllOptionValues, DhcpGetAllOptionValuesResponse),

768 34: (DhcpGetClientInfoV4, DhcpGetClientInfoV4Response),

769 35: (DhcpEnumSubnetClientsV4, DhcpEnumSubnetClientsV4Response),

770 38: (DhcpEnumSubnetElementsV5, DhcpEnumSubnetElementsV5Response),

771 47: (DhcpEnumSubnetClientsVQ, DhcpEnumSubnetClientsVQResponse),

772 123: (DhcpV4GetClientInfo, DhcpV4GetClientInfoResponse),

773}

774

775

776################################################################################

777# HELPER FUNCTIONS

778################################################################################

779 def hDhcpGetClientInfoV4(dce, searchType, searchValue):

780 request = DhcpGetClientInfoV4()

781

782 request['ServerIpAddress'] = NULL

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

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

785 if searchType == DHCP_SEARCH_INFO_TYPE.DhcpClientIpAddress:

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

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

788 # This should be a DHCP_BINARY_DATA

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

790 else:

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

792

793 return dce.request(request)

794

795 def hDhcpGetSubnetInfo(dce, subnetaddress):

796 request = DhcpGetSubnetInfo()

797

798 request['ServerIpAddress'] = NULL

799 request['SubnetAddress'] = subnetaddress

800 resp = dce.request(request)

801

802 return resp

803

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

805 request = DhcpGetOptionValue()

806

807 request['ServerIpAddress'] = NULL

808 request['OptionID'] = optionID

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

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

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

812 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

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

814 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

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

816 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

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

818

819 status = system_errors.ERROR_MORE_DATA

820 while status == system_errors.ERROR_MORE_DATA:

821 try:

822 resp = dce.request(request)

823 except DCERPCException as e:

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

825 raise

826 resp = e.get_packet()

827 return resp

828

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

830 preferredMaximum=0xffffffff):

831 request = DhcpEnumOptionValues()

832

833 request['ServerIpAddress'] = NULL

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

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

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

837 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

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

839 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

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

841 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

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

843 request['ResumeHandle'] = NULL

844 request['PreferredMaximum'] = preferredMaximum

845

846 status = system_errors.ERROR_MORE_DATA

847 while status == system_errors.ERROR_MORE_DATA:

848 try:

849 resp = dce.request(request)

850 except DCERPCException as e:

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

852 raise

853 resp = e.get_packet()

854 return resp

855

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

857 scopetype=DHCP_OPTION_SCOPE_TYPE.DhcpDefaultOptions, options=NULL,

858 preferredMaximum=0xffffffff):

859 request = DhcpEnumOptionValuesV5()

860

861 request['ServerIpAddress'] = NULL

862 request['Flags'] = flags

863 request['ClassName'] = classname

864 request['VendorName'] = vendorname

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

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

867 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

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

869 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

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

871 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

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

873 request['ResumeHandle'] = NULL

874 request['PreferredMaximum'] = preferredMaximum

875

876 status = system_errors.ERROR_MORE_DATA

877 while status == system_errors.ERROR_MORE_DATA:

878 try:

879 resp = dce.request(request)

880 except DCERPCException as e:

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

882 raise

883 resp = e.get_packet()

884 return resp

885

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

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

888 request = DhcpGetOptionValueV5()

889

890 request['ServerIpAddress'] = NULL

891 request['Flags'] = flags

892 request['OptionID'] = option_id

893 request['ClassName'] = classname

894 request['VendorName'] = vendorname

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

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

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

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

899 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

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

901 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

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

903

904 status = system_errors.ERROR_MORE_DATA

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

906 try:

907 resp = dce.request(request)

908 except DCERPCException as e:

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

910 raise

911 resp = e.get_packet()

912 return resp

913

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

915 request = DhcpGetAllOptionValues()

916

917 request['ServerIpAddress'] = NULL

918 request['Flags'] = NULL

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

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

921 if scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpSubnetOptions:

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

923 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpReservedOptions:

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

925 elif scopetype == DHCP_OPTION_SCOPE_TYPE.DhcpMScopeOptions:

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

927

928 status = system_errors.ERROR_MORE_DATA

929 while status == system_errors.ERROR_MORE_DATA:

930 try:

931 resp = dce.request(request)

932 except DCERPCException as e:

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

934 raise

935 resp = e.get_packet()

936 return resp

937

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

939 request = DhcpEnumSubnets()

940

941 request['ServerIpAddress'] = NULL

942 request['ResumeHandle'] = NULL

943 request['PreferredMaximum'] = preferredMaximum

944 status = system_errors.ERROR_MORE_DATA

945 while status == system_errors.ERROR_MORE_DATA:

946 try:

947 resp = dce.request(request)

948 except DCERPCException as e:

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

950 raise

951 resp = e.get_packet()

952 return resp

953

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

955 request = DhcpEnumSubnetClientsVQ()

956

957 request['ServerIpAddress'] = NULL

958 request['SubnetAddress'] = NULL

959 request['ResumeHandle'] = NULL

960 request['PreferredMaximum'] = preferredMaximum

961 status = system_errors.ERROR_MORE_DATA

962 while status == system_errors.ERROR_MORE_DATA:

963 try:

964 resp = dce.request(request)

965 except DCERPCException as e:

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

967 raise

968 resp = e.get_packet()

969 return resp

970

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

972 request = DhcpEnumSubnetClientsV4()

973

974 request['ServerIpAddress'] = NULL

975 request['SubnetAddress'] = NULL

976 request['ResumeHandle'] = NULL

977 request['PreferredMaximum'] = preferredMaximum

978 status = system_errors.ERROR_MORE_DATA

979 while status == system_errors.ERROR_MORE_DATA:

980 try:

981 resp = dce.request(request)

982 except DCERPCException as e:

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

984 raise

985 resp = e.get_packet()

986 return resp

987

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

989 request = DhcpEnumSubnetClientsV5()

990

991 request['ServerIpAddress'] = NULL

992 request['SubnetAddress'] = subnetAddress

993 request['ResumeHandle'] = NULL

994 request['PreferredMaximum'] = preferredMaximum

995 status = system_errors.ERROR_MORE_DATA

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

997 try:

998 resp = dce.request(request)

999 except DCERPCSessionError as e:

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

1001 raise

1002 resp = e.get_packet()

1003 return resp

1004

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

1006 request = DhcpEnumSubnetElementsV5()

1007

1008 request['ServerIpAddress'] = NULL

1009 request['SubnetAddress'] = subnet_address

1010 request['EnumElementType'] = element_type

1011 request['ResumeHandle'] = NULL

1012 request['PreferredMaximum'] = preferredMaximum

1013

1014 status = system_errors.ERROR_MORE_DATA

1015 while status == system_errors.ERROR_MORE_DATA:

1016 try:

1017 resp = dce.request(request)

1018 except DCERPCException as e:

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

1020 raise

1021 resp = e.get_packet()

1022 return resp