Skip to content

Commit 47b1c1f

Browse files
fix(wmi): add auth_level to kerberos auth, revert my previous changes, move Impacket silencing, add debugging
1 parent 65a41b8 commit 47b1c1f

2 files changed

Lines changed: 21 additions & 20 deletions

File tree

nxc/logger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def __init__(self, extra=None):
9393
self.logger = logging.getLogger("nxc")
9494
self.extra = extra
9595
self.output_file = None
96-
96+
97+
logging.getLogger("impacket").disabled = True
9798
logging.getLogger("pypykatz").disabled = True
9899
logging.getLogger("minidump").disabled = True
99100
logging.getLogger("lsassy").disabled = True

nxc/protocols/wmi.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import logging
32

43
from io import StringIO
54
from datetime import datetime
@@ -62,8 +61,10 @@ def proto_logger(self):
6261
)
6362

6463
def create_conn_obj(self):
64+
connection_target = fr"ncacn_ip_tcp:{self.remoteName}[{self.port!s}]"
65+
self.logger.debug(f"Creating WMI connection object to {connection_target}")
6566
try:
66-
rpctansport = transport.DCERPCTransportFactory(fr"ncacn_ip_tcp:{self.remoteName}[{self.port!s}]")
67+
rpctansport = transport.DCERPCTransportFactory(connection_target)
6768
rpctansport.set_credentials(username="", password="", domain="", lmhash="", nthash="", aesKey="")
6869
rpctansport.setRemoteHost(self.host)
6970
rpctansport.set_connect_timeout(self.args.rpc_timeout)
@@ -73,9 +74,10 @@ def create_conn_obj(self):
7374
dce.bind(MSRPC_UUID_PORTMAP)
7475
dce.disconnect()
7576
except Exception as e:
76-
self.logger.debug(str(e))
77+
self.logger.debug(f"Received error creating WMI connection object: {e}")
7778
return False
7879
else:
80+
self.logger.debug(f"Successfully created WMI connection object to {connection_target}")
7981
self.conn = rpctansport
8082
return True
8183

@@ -152,14 +154,11 @@ def check_if_admin(self):
152154
iInterface = dcom.CoCreateInstanceEx(CLSID_WbemLevel1Login, IID_IWbemLevel1Login)
153155
flag, self.stringBinding = dcom_FirewallChecker(iInterface, self.host, self.args.rpc_timeout)
154156
except Exception as e:
157+
self.logger.debug(f"Received error while checking admin: {e}")
155158
if "dcom" in locals():
156159
dcom.disconnect()
157-
158-
if "KDC_ERR_PREAUTH_FAILED" in str(e):
159-
self.logger.fail("KDC_ERR_PREAUTH_FAILED returned - check if Kerberos and DNS are working!")
160-
elif "access_denied" not in str(e).lower():
160+
if "access_denied" not in str(e).lower():
161161
self.logger.fail(str(e))
162-
return False
163162
else:
164163
if not flag or not self.stringBinding:
165164
dcom.disconnect()
@@ -185,7 +184,7 @@ def check_if_admin(self):
185184
self.admin_privs = True
186185

187186
def kerberos_login(self, domain, username, password="", ntlm_hash="", aesKey="", kdcHost="", useCache=False):
188-
logging.getLogger("impacket").disabled = True
187+
self.logger.debug("Starting WMI login with Kerberos")
189188
lmhash = ""
190189
nthash = ""
191190
self.password = password
@@ -205,37 +204,39 @@ def kerberos_login(self, domain, username, password="", ntlm_hash="", aesKey="",
205204

206205
if useCache and kerb_pass == "":
207206
ccache = CCache.loadFile(os.getenv("KRB5CCNAME"))
207+
self.logger.debug(f"Using ccache from {ccache}")
208208
username = ccache.credentials[0].header["client"].prettyPrint().decode().split("@")[0]
209209
self.username = username
210-
211210
used_ccache = " from ccache" if useCache else f":{process_secret(kerb_pass)}"
211+
212212
try:
213+
self.logger.debug(f"Attempting to connect via WMI to {self.host}")
213214
self.conn.set_credentials(username=username, password=password, domain=domain, lmhash=lmhash, nthash=nthash, aesKey=self.aesKey)
214215
self.conn.setRemoteHost(self.host)
215216
self.conn.set_kerberos(True, kdcHost)
216217
dce = self.conn.get_dce_rpc()
217218
dce.set_auth_type(RPC_C_AUTHN_GSS_NEGOTIATE)
219+
dce.set_auth_level(RPC_C_AUTHN_LEVEL_PKT_PRIVACY)
218220
dce.connect()
219221
dce.bind(MSRPC_UUID_PORTMAP)
220222
except Exception as e:
221223
dce.disconnect()
222224
error_msg = str(e).lower()
223-
self.logger.debug(error_msg)
225+
self.logger.debug(f"WMI errored while connecting: {error_msg}")
224226
if "unpack requires a buffer of 4 bytes" in error_msg:
225227
error_msg = "Kerberos authentication failure"
226228
out = f"{self.domain}\\{self.username}{used_ccache} {error_msg}"
227229
self.logger.fail(out)
228-
elif "kerberos sessionerror" in str(e).lower():
230+
elif "kerberos sessionerror" in error_msg:
229231
out = f"{self.domain}\\{self.username}{used_ccache} {next(iter(e.getErrorString()))}"
230232
self.logger.fail(out, color="magenta")
231-
return False
232233
else:
233234
out = f"{self.domain}\\{self.username}{used_ccache} {e!s}"
234235
self.logger.fail(out, color="red")
235236
return False
236237
else:
237238
try:
238-
# Get data from rpc connection if got vaild creds
239+
self.logger.debug("Got valid creds, trying to get data from RPC connection")
239240
entry_handle = epm.ept_lookup_handle_t()
240241
request = epm.ept_lookup()
241242
request["inquiry_type"] = 0x0
@@ -257,12 +258,11 @@ def kerberos_login(self, domain, username, password="", ntlm_hash="", aesKey="",
257258
return False
258259
else:
259260
self.doKerberos = True
260-
if self.check_if_admin():
261-
out = f"{self.domain}\\{self.username}{used_ccache} {self.mark_pwned()}"
262-
self.logger.success(out)
263-
return True
261+
self.check_if_admin()
262+
out = f"{self.domain}\\{self.username}{used_ccache} {self.mark_pwned()}"
263+
self.logger.success(out)
264264
dce.disconnect()
265-
return False
265+
return True
266266

267267
def plaintext_login(self, domain, username, password):
268268
self.password = password

0 commit comments

Comments
 (0)