Skip to content

Commit 9a75539

Browse files
committed
check only if port 135 is open, better faster
1 parent 14b1bc6 commit 9a75539

1 file changed

Lines changed: 40 additions & 22 deletions

File tree

nxc/protocols/smb.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def generate_tgt(self):
663663
except Exception as e:
664664
self.logger.fail(f"Failed to get TGT: {e}")
665665

666-
def check_dc_ports(self, timeout=2):
666+
def check_dc_ports(self, timeout=1):
667667
"""Check multiple DC-specific ports in case first check fails"""
668668
import socket
669669
dc_ports = [88, 389, 636, 3268] # Kerberos, LDAP, LDAPS, Global Catalog
@@ -687,30 +687,48 @@ def is_host_dc(self):
687687
from impacket.dcerpc.v5 import transport, nrpc
688688

689689
self.logger.debug("Performing authentication attempts...")
690-
try:
691-
rpctransport = transport.DCERPCTransportFactory(f"ncacn_ip_tcp:{self.host}[135]")
692-
rpctransport.set_connect_timeout(5)
690+
691+
# First check if port 135 is open
692+
if self._is_port_open(135):
693+
self.logger.debug("Port 135 is open, attempting MSRPC connection...")
694+
try:
695+
rpctransport = transport.DCERPCTransportFactory(f"ncacn_ip_tcp:{self.host}[135]")
696+
rpctransport.set_connect_timeout(5)
693697

694-
dce = rpctransport.get_dce_rpc()
695-
dce.connect()
696-
dce.bind(nrpc.MSRPC_UUID_NRPC)
698+
dce = rpctransport.get_dce_rpc()
699+
dce.connect()
700+
dce.bind(nrpc.MSRPC_UUID_NRPC)
697701

698-
self.isdc = True
699-
dce.disconnect()
700-
return True
701-
except DCERPCException:
702-
self.logger.debug("Error while connecting to host: DCERPCException, which means this is probably not a DC!")
703-
except TimeoutError:
704-
self.logger.debug("Timeout while connecting to host: likely not a DC or host is unreachable.")
705-
except Exception as e:
706-
self.logger.debug(f"Error while connecting to host: {e}")
702+
self.isdc = True
703+
dce.disconnect()
704+
return True
705+
except DCERPCException:
706+
self.logger.debug("Error while connecting to host: DCERPCException, which means this is probably not a DC!")
707+
except TimeoutError:
708+
self.logger.debug("Timeout while connecting to host: likely not a DC or host is unreachable.")
709+
except Exception as e:
710+
self.logger.debug(f"Error while connecting to host: {e}")
711+
self.isdc = False
712+
return False
713+
else:
714+
self.logger.debug("Port 135 is closed, skipping MSRPC check...")
715+
# Fallback to checking DC ports
716+
if self.check_dc_ports():
717+
self.logger.debug("Host appears to be a DC (multiple DC ports open)")
718+
self.isdc = True
719+
return True
707720

708-
if self.check_dc_ports():
709-
self.logger.debug("Host appears to be a DC (multiple DC ports open)")
710-
self.isdc = True
711-
return True
712-
self.isdc = False
713-
return False
721+
def _is_port_open(self, port, timeout=1):
722+
"""Check if a specific port is open on the target host."""
723+
import socket
724+
try:
725+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
726+
sock.settimeout(timeout)
727+
result = sock.connect_ex((self.host, port))
728+
return result == 0
729+
except Exception as e:
730+
self.logger.debug(f"Error checking port {port} on {self.host}: {e}")
731+
return False
714732

715733
@requires_admin
716734
def execute(self, payload=None, get_output=False, methods=None) -> str:

0 commit comments

Comments
 (0)