Skip to content

Commit f227432

Browse files
authored
Merge pull request Pennyw0rth#739 from Pennyw0rth/fix_is_dc
Fix function to check if hosts is a dc or not
2 parents 0630e22 + ca932eb commit f227432

1 file changed

Lines changed: 55 additions & 7 deletions

File tree

nxc/protocols/smb.py

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -663,17 +663,65 @@ 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=1):
667+
"""Check multiple DC-specific ports in case first check fails"""
668+
import socket
669+
dc_ports = [88, 389, 636, 3268, 9389] # Kerberos, LDAP, LDAPS, Global Catalog, ADWS
670+
open_ports = 0
671+
672+
for port in dc_ports:
673+
try:
674+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
675+
sock.settimeout(timeout)
676+
result = sock.connect_ex((self.host, port))
677+
if result == 0:
678+
self.logger.debug(f"Port {port} is open on {self.host}")
679+
open_ports += 1
680+
sock.close()
681+
except Exception:
682+
pass
683+
# If 3 or more DC ports are open, likely a DC
684+
return open_ports >= 3
685+
666686
def is_host_dc(self):
667687
from impacket.dcerpc.v5 import nrpc, epm
688+
668689
self.logger.debug("Performing authentication attempts...")
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+
epm.hept_map(self.host, nrpc.MSRPC_UUID_NRPC, protocol="ncacn_ip_tcp")
696+
self.isdc = True
697+
return True
698+
except DCERPCException:
699+
self.logger.debug("Error while connecting to host: DCERPCException, which means this is probably not a DC!")
700+
except TimeoutError:
701+
self.logger.debug("Timeout while connecting to host: likely not a DC or host is unreachable.")
702+
except Exception as e:
703+
self.logger.debug(f"Error while connecting to host: {e}")
704+
self.isdc = False
705+
return False
706+
else:
707+
self.logger.debug("Port 135 is closed, skipping MSRPC check...")
708+
# Fallback to checking DC ports
709+
if self.check_dc_ports():
710+
self.logger.debug("Host appears to be a DC (multiple DC ports open)")
711+
self.isdc = True
712+
return True
713+
714+
def _is_port_open(self, port, timeout=1):
715+
"""Check if a specific port is open on the target host."""
716+
import socket
669717
try:
670-
epm.hept_map(self.host, nrpc.MSRPC_UUID_NRPC, protocol="ncacn_ip_tcp")
671-
self.isdc = True
672-
return True
673-
except DCERPCException:
674-
self.logger.debug("Error while connecting to host: DCERPCException, which means this is probably not a DC!")
675-
self.isdc = False
676-
return False
718+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
719+
sock.settimeout(timeout)
720+
result = sock.connect_ex((self.host, port))
721+
return result == 0
722+
except Exception as e:
723+
self.logger.debug(f"Error checking port {port} on {self.host}: {e}")
724+
return False
677725

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

0 commit comments

Comments
 (0)