Skip to content

Commit 2184bf4

Browse files
committed
DCE/RPC client: improve endpoint mapper resilliency
- close properly when it crashes - fix over SMB where the pipe was open too soon
1 parent ea8c334 commit 2184bf4

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

scapy/layers/msrpce/rpcclient.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ def connect(
197197
transport=self.transport,
198198
ndrendian=self.ndrendian,
199199
verb=self.verb,
200+
ssp=self.ssp,
200201
smb_kwargs=smb_kwargs,
201202
)
202203
if endpoints:
@@ -234,14 +235,15 @@ def connect(
234235
)
235236

236237
if self.transport == DCERPC_Transport.NCACN_NP: # SMB
237-
# If the endpoint is provided, connect to it.
238-
if endpoint is not None:
239-
self.open_smbpipe(endpoint)
240-
241238
# We pack the socket into a SMB_RPC_SOCKET
242239
sock = self.smbrpcsock = SMB_RPC_SOCKET.from_tcpsock(
243240
sock, ssp=self.ssp, **smb_kwargs
244241
)
242+
243+
# If the endpoint is provided, connect to it.
244+
if endpoint is not None:
245+
self.open_smbpipe(endpoint)
246+
245247
self.sock = DceRpcSocket(sock, DceRpc5, **self.dcesockargs)
246248
elif self.transport == DCERPC_Transport.NCACN_IP_TCP:
247249
self.sock = DceRpcSocket(
@@ -351,6 +353,9 @@ def sr1_req(self, pkt, **kwargs):
351353
if "opnum" in kwargs:
352354
opnum["opnum"] = kwargs.pop("opnum")
353355

356+
# Set NDR64
357+
pkt.ndr64 = self.ndr64
358+
354359
# Send/receive
355360
resp = self.sr1(
356361
DceRpc5Request(
@@ -486,7 +491,10 @@ def _check_bind_context(self, interface, contexts) -> bool:
486491
return False
487492

488493
def _bind(
489-
self, interface: Union[DceRpcInterface, ComInterface], reqcls, respcls
494+
self,
495+
interface: Union[DceRpcInterface, ComInterface],
496+
reqcls,
497+
respcls,
490498
) -> bool:
491499
"""
492500
Internal: used to send a bind/alter request
@@ -681,11 +689,10 @@ def _bind(
681689
else:
682690
print(conf.color_theme.fail("! Failure"))
683691
resp.show()
684-
if DceRpc5Fault in resp:
685-
if resp[DceRpc5Fault].payload and not isinstance(
686-
resp[DceRpc5Fault].payload, conf.raw_layer
687-
):
688-
resp[DceRpc5Fault].payload.show()
692+
if resp[DceRpc5Fault].payload and not isinstance(
693+
resp[DceRpc5Fault].payload, conf.raw_layer
694+
):
695+
resp[DceRpc5Fault].payload.show()
689696
else:
690697
print(conf.color_theme.fail("! Failure"))
691698
resp.show()
@@ -900,7 +907,6 @@ def epm_map(self, interface):
900907
return endpoints
901908
elif status == 0x16C9A0D6:
902909
if self.verb:
903-
pkt.show()
904910
print(
905911
conf.color_theme.fail(
906912
"! Server errored: 'There are no elements that satisfy"
@@ -953,7 +959,9 @@ def get_endpoint(
953959
client.connect(ip, endpoint=endpoint, smb_kwargs=smb_kwargs)
954960

955961
client.bind(find_dcerpc_interface("ept"))
956-
endpoints = client.epm_map(interface)
962+
try:
963+
endpoints = client.epm_map(interface)
964+
finally:
965+
client.close()
957966

958-
client.close()
959967
return endpoints

0 commit comments

Comments
 (0)