Skip to content

Commit

Permalink
[mssql] less code
Browse files Browse the repository at this point in the history
Signed-off-by: XiaoliChan <30458572+XiaoliChan@users.noreply.github.com>
  • Loading branch information
XiaoliChan committed Dec 12, 2023
1 parent 93f3bcc commit 2c17436
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions nxc/protocols/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,6 @@ def enum_host_info(self):
if self.domain is None:
self.domain = ""

with contextlib.suppress(Exception):
self.conn.disconnect()

def print_host_info(self):
self.logger.display(f"{self.server_os} (name:{self.hostname}) (domain:{self.domain})")
return True
Expand All @@ -116,6 +113,17 @@ def create_conn_obj(self):
self.conn.socket = sock
return True

def reconnect_mssql(func):
def wrapper(self, *args, **kwargs):
with contextlib.suppress(Exception):
self.conn.disconnect()
# When using ccache file, we must need to set target host to hostname when creating connection object.
if self.kerberos:
self.host = self.hostname
self.create_conn_obj()
return func(self, *args, **kwargs)
return wrapper

def check_if_admin(self):
self.admin_privs = False
try:
Expand All @@ -127,6 +135,7 @@ def check_if_admin(self):
if is_admin:
self.admin_privs = True

@reconnect_mssql
def kerberos_login(
self,
domain,
Expand All @@ -137,12 +146,6 @@ def kerberos_login(
kdcHost="",
useCache=False,
):
with contextlib.suppress(Exception):
self.conn.disconnect()
# When using ccache file, we must need to set target host to hostname when creating connection object.
self.host = self.hostname
self.create_conn_obj()

kerb_pass = next(s for s in [self.nthash, password, aesKey] if s) if not all(s == "" for s in [self.nthash, password, aesKey]) else ""

if useCache and kerb_pass == "":
Expand Down Expand Up @@ -197,11 +200,8 @@ def kerberos_login(
add_user_bh(f"{self.hostname}$", self.domain, self.logger, self.config)
return True

@reconnect_mssql
def plaintext_login(self, domain, username, password):
with contextlib.suppress(Exception):
self.conn.disconnect()
self.create_conn_obj()

self.password = password
self.username = username
self.domain = domain
Expand Down Expand Up @@ -235,11 +235,8 @@ def plaintext_login(self, domain, username, password):
add_user_bh(f"{self.hostname}$", self.domain, self.logger, self.config)
return True

@reconnect_mssql
def hash_login(self, domain, username, ntlm_hash):
with contextlib.suppress(Exception):
self.conn.disconnect()
self.create_conn_obj()

self.username = username
self.domain = domain
self.nthash = f':{ntlm_hash.split(":")[1]}' if ntlm_hash.find(":") != -1 else f":{ntlm_hash}"
Expand Down

0 comments on commit 2c17436

Please sign in to comment.