Skip to content

Commit 4cabffb

Browse files
committed
Fix ssh auth with all key files and fix password processing, also add better exception handling
1 parent b0ccbe2 commit 4cabffb

1 file changed

Lines changed: 6 additions & 15 deletions

File tree

nxc/protocols/ssh.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import logging
55
import time
66

7-
from io import StringIO
87
from nxc.config import process_secret
98
from nxc.connection import connection, highlight
109
from nxc.logger import NXCAdapter
@@ -188,19 +187,14 @@ def plaintext_login(self, username, password, private_key=""):
188187
stdout = None
189188
try:
190189
if self.args.key_file or private_key:
191-
self.logger.debug("Logging in with key")
190+
self.logger.debug(f"Logging {self.host} with username: {username}, keyfile: {self.args.key_file}")
192191

193-
if self.args.key_file:
194-
with open(self.args.key_file) as f:
195-
private_key = f.read()
196-
197-
pkey = paramiko.RSAKey.from_private_key(StringIO(private_key), password)
198192
self.conn.connect(
199193
self.host,
200194
port=self.port,
201195
username=username,
202196
passphrase=password if password != "" else None,
203-
pkey=pkey,
197+
key_filename=private_key if private_key else self.args.key_file,
204198
look_for_keys=False,
205199
allow_agent=False,
206200
)
@@ -227,13 +221,10 @@ def plaintext_login(self, username, password, private_key=""):
227221
# Some IOT devices will not raise exception in self.conn._transport.auth_password / self.conn._transport.auth_publickey
228222
_, stdout, _ = self.conn.exec_command("id")
229223
stdout = stdout.read().decode(self.args.codec, errors="ignore")
224+
except SSHException as e:
225+
self.logger.fail(f"{username}:{process_secret(password)} Could not decrypt private key, error: {e}")
230226
except Exception as e:
231-
if self.args.key_file:
232-
password = f"{process_secret(password)} (keyfile: {self.args.key_file})"
233-
if "OpenSSH private key file checkints do not match" in str(e) or "password and salt must not be empty" in str(e):
234-
self.logger.fail(f"{username}:{password} - Could not decrypt key file, wrong password")
235-
else:
236-
self.logger.fail(f"{username}:{password} {e}")
227+
self.logger.fail(f"{username}:{process_secret(password)} {e}")
237228
self.conn.close()
238229
return False
239230
else:
@@ -286,7 +277,7 @@ def plaintext_login(self, username, password, private_key=""):
286277
self.server_os_platform,
287278
"- Shell access!" if shell_access else ""
288279
)
289-
self.logger.success(f"{username}:{password} {self.mark_pwned()} {highlight(display_shell_access)}")
280+
self.logger.success(f"{username}:{process_secret(password)} {self.mark_pwned()} {highlight(display_shell_access)}")
290281

291282
return True
292283

0 commit comments

Comments
 (0)