Skip to content

Commit

Permalink
cleanup(ruff): finish cleaning up E722 (do not euse bare except)
Browse files Browse the repository at this point in the history
  • Loading branch information
Marshall-Hallenbeck committed Oct 6, 2023
1 parent b38ad26 commit 389b381
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion nxc/protocols/rdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def create_conn_obj(self):
if "Reason:" not in str(e):
try:
info_domain = self.conn.get_extra_info()
except:
except Exception:
pass
else:
self.domain = info_domain["dnsdomainname"]
Expand Down
24 changes: 12 additions & 12 deletions nxc/protocols/smb.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def kerberos_login(self, domain, username, password="", ntlm_hash="", aesKey="",
if self.args.continue_on_success and self.signing:
try:
self.conn.logoff()
except:
except Exception:
pass
self.create_conn_obj()

Expand Down Expand Up @@ -496,7 +496,7 @@ def plaintext_login(self, domain, username, password):
if self.args.continue_on_success and self.signing:
try:
self.conn.logoff()
except:
except Exception:
pass
self.create_conn_obj()
return True
Expand Down Expand Up @@ -561,7 +561,7 @@ def hash_login(self, domain, username, ntlm_hash):
if self.args.continue_on_success and self.signing:
try:
self.conn.logoff()
except:
except Exception:
pass
self.create_conn_obj()
return True
Expand Down Expand Up @@ -638,12 +638,12 @@ def check_if_admin(self):
dce = rpctransport.get_dce_rpc()
try:
dce.connect()
except:
except Exception:
pass
else:
try:
dce.bind(scmr.MSRPC_UUID_SCMR)
except:
except Exception:
pass
try:
# 0xF003F - SC_MANAGER_ALL_ACCESS
Expand Down Expand Up @@ -682,7 +682,7 @@ def execute(self, payload=None, get_output=False, methods=None):
exec_method = WMIEXEC(self.host if not self.kerberos else self.hostname + "." + self.domain, self.smb_share_name, self.username, self.password, self.domain, self.conn, self.kerberos, self.aesKey, self.kdcHost, self.hash, self.args.share, logger=self.logger, timeout=self.args.dcom_timeout, tries=self.args.get_output_tries)
self.logger.info("Executed command via wmiexec")
break
except:
except Exception:
self.logger.debug("Error executing command via wmiexec, traceback:")
self.logger.debug(format_exc())
continue
Expand All @@ -691,7 +691,7 @@ def execute(self, payload=None, get_output=False, methods=None):
exec_method = MMCEXEC(self.host if not self.kerberos else self.hostname + "." + self.domain, self.smb_share_name, self.username, self.password, self.domain, self.conn, self.args.share, self.hash, self.logger, self.args.get_output_tries, self.args.dcom_timeout)
self.logger.info("Executed command via mmcexec")
break
except:
except Exception:
self.logger.debug("Error executing command via mmcexec, traceback:")
self.logger.debug(format_exc())
continue
Expand All @@ -700,7 +700,7 @@ def execute(self, payload=None, get_output=False, methods=None):
exec_method = TSCH_EXEC(self.host if not self.kerberos else self.hostname + "." + self.domain, self.smb_share_name, self.username, self.password, self.domain, self.kerberos, self.aesKey, self.kdcHost, self.hash, self.logger, self.args.get_output_tries, self.args.share)
self.logger.info("Executed command via atexec")
break
except:
except Exception:
self.logger.debug("Error executing command via atexec, traceback:")
self.logger.debug(format_exc())
continue
Expand All @@ -709,7 +709,7 @@ def execute(self, payload=None, get_output=False, methods=None):
exec_method = SMBEXEC(self.host if not self.kerberos else self.hostname + "." + self.domain, self.smb_share_name, self.conn, self.args.port, self.username, self.password, self.domain, self.kerberos, self.aesKey, self.kdcHost, self.hash, self.args.share, self.args.port, self.logger, self.args.get_output_tries)
self.logger.info("Executed command via smbexec")
break
except:
except Exception:
self.logger.debug("Error executing command via smbexec, traceback:")
self.logger.debug(format_exc())
continue
Expand Down Expand Up @@ -887,7 +887,7 @@ def sessions(self):
if session.sesi10_cname.find(self.local_ip) == -1:
self.logger.highlight(f"{session.sesi10_cname:<25} User:{session.sesi10_username}")
return sessions
except:
except Exception:
pass

def disks(self):
Expand Down Expand Up @@ -1435,7 +1435,7 @@ def dpapi(self):
if self.pvkbytes is None and self.no_da is None and self.args.local_auth is False:
try:
results = self.db.get_domain_backupkey(self.domain)
except:
except Exception:
self.logger.fail(
"Your version of nxcdb is not up to date, run nxcdb and create a new workspace: \
'workspace create dpapi' then re-run the dpapi option"
Expand Down Expand Up @@ -1701,7 +1701,7 @@ def add_ntds_hash(ntds_hash, host_id):
add_ntds_hash.added_to_db += 1
return
raise
except:
except Exception:
self.logger.debug("Dumped hash is not NTLM, not adding to db for now ;)")
else:
self.logger.debug("Dumped hash is a computer account, not adding to db")
Expand Down
4 changes: 2 additions & 2 deletions nxc/protocols/smb/db_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def display_hosts(self, hosts):

try:
os = host[4].decode()
except:
except Exception:
os = host[4]
try:
smbv1 = host[6]
Expand Down Expand Up @@ -310,7 +310,7 @@ def do_hosts(self, line):

try:
os = host[4].decode()
except:
except Exception:
os = host[4]
try:
dc = host[5]
Expand Down
2 changes: 1 addition & 1 deletion nxc/protocols/smb/mmcexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def __init__(self, host, share_name, username, password, domain, smbconnection,
)
try:
iInterface = self.__dcom.CoCreateInstanceEx(string_to_bin("49B2791A-B1AE-4C90-9B8E-E860BA07F889"), IID_IDispatch)
except:
except Exception:
# Make it force break function
self.__dcom.disconnect()
flag, self.__stringBinding = dcom_FirewallChecker(iInterface, self.__timeout)
Expand Down
4 changes: 2 additions & 2 deletions nxc/protocols/smb/smbexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def execute_fileless(self, data):
try:
self.logger.debug(f"Remote service {self.__serviceName} started.")
scmr.hRStartServiceW(self.__scmr, service)
except:
except Exception:
pass
self.logger.debug(f"Remote service {self.__serviceName} deleted.")
scmr.hRDeleteService(self.__scmr, service)
Expand Down Expand Up @@ -233,5 +233,5 @@ def finish(self):
scmr.hRDeleteService(self.__scmr, service)
scmr.hRControlService(self.__scmr, service, scmr.SERVICE_CONTROL_STOP)
scmr.hRCloseServiceHandle(self.__scmr, service)
except:
except Exception:
pass
2 changes: 1 addition & 1 deletion nxc/protocols/smb/wmiexec.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def execute_handler(self, data):
try:
self.logger.debug("Executing remote")
self.execute_remote(data)
except:
except Exception:
self.cd("\\")
self.execute_remote(data)

Expand Down
8 changes: 2 additions & 6 deletions nxc/protocols/winrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,8 @@ def enum_host_info(self):

try:
smb_conn.logoff()
except:
except Exception:
pass
# except Exception as e:
# self.logger.fail(
# f"Error retrieving host domain: {e} specify one manually with the '-d' flag"
# )

if self.args.domain:
self.domain = self.args.domain
Expand Down Expand Up @@ -317,7 +313,7 @@ def hash_login(self, domain, username, ntlm_hash):
def execute(self, payload=None, get_output=False):
try:
r = self.conn.execute_cmd(self.args.execute, encoding=self.args.codec)
except:
except Exception:
self.logger.info("Cannot execute command, probably because user is not local admin, but" " powershell command should be ok!")
r = self.conn.execute_ps(self.args.execute)
self.logger.success("Executed command")
Expand Down
4 changes: 2 additions & 2 deletions nxc/protocols/winrm/db_navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def display_hosts(self, hosts):

try:
os = host[5].decode()
except:
except Exception:
os = host[5]

links = self.db.get_admin_relations(host_id=host_id)
Expand Down Expand Up @@ -84,7 +84,7 @@ def do_hosts(self, line):

try:
os = host[5].decode()
except:
except Exception:
os = host[5]

data.append([host_id, ip, port, hostname, domain, os])
Expand Down
8 changes: 4 additions & 4 deletions nxc/protocols/wmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def enum_host_info(self):
self.conn.connect()
self.conn.send(packet.get_packet())
buffer = self.conn.recv()
except:
except Exception:
buffer = 0

if buffer != 0:
Expand All @@ -130,17 +130,17 @@ def enum_host_info(self):
if av_pairs[ntlm.NTLMSSP_AV_HOSTNAME][1] is not None:
try:
self.hostname = av_pairs[ntlm.NTLMSSP_AV_HOSTNAME][1].decode("utf-16le")
except:
except Exception:
self.hostname = self.host
if av_pairs[ntlm.NTLMSSP_AV_DNS_DOMAINNAME][1] is not None:
try:
self.domain = av_pairs[ntlm.NTLMSSP_AV_DNS_DOMAINNAME][1].decode("utf-16le")
except:
except Exception:
self.domain = self.args.domain
if av_pairs[ntlm.NTLMSSP_AV_DNS_HOSTNAME][1] is not None:
try:
self.fqdn = av_pairs[ntlm.NTLMSSP_AV_DNS_HOSTNAME][1].decode("utf-16le")
except:
except Exception:
pass
if "Version" in ntlmChallenge.fields:
version = ntlmChallenge["Version"]
Expand Down

0 comments on commit 389b381

Please sign in to comment.