Skip to content

Commit 6593fa8

Browse files
committed
Extracting privilege checks from plaintext_login
1 parent 07540ee commit 6593fa8

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

nxc/protocols/ssh.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ def plaintext_login(self, username, password, private_key=""):
102102
password if password != "" else "",
103103
key=private_key,
104104
)
105-
106105
else:
107106
self.logger.debug(f"Logging {self.host} with username: {self.username}, password: {self.password}")
108107
self.conn.connect(
@@ -118,8 +117,12 @@ def plaintext_login(self, username, password, private_key=""):
118117
cred_id = self.db.add_credential("plaintext", username, password)
119118

120119
# Some IOT devices will not raise exception in self.conn._transport.auth_password / self.conn._transport.auth_publickey
120+
# Also an early check if we are on Linux or not, as on windows only stderr and not stdout is returned ("id" is not implemented)
121121
_, stdout, _ = self.conn.exec_command("id")
122122
stdout = stdout.read().decode(self.args.codec, errors="ignore")
123+
124+
self.check_privs(cred_id, stdout)
125+
return True
123126
except AuthenticationException as e:
124127
if "Private key file is encrypted" in str(e):
125128
self.logger.fail(f"{username}:{process_secret(password)} Could not load private key, error: {e}")
@@ -136,45 +139,43 @@ def plaintext_login(self, username, password, private_key=""):
136139
self.logger.exception(e)
137140
self.conn.close()
138141
return False
139-
else:
140-
shell_access = False
141-
host_id = self.db.get_hosts(self.host)[0].id
142-
143-
if not stdout:
144-
_, stdout, _ = self.conn.exec_command("whoami /priv")
145-
stdout = stdout.read().decode(self.args.codec, errors="ignore")
146-
self.server_os_platform = "Windows"
147-
if "SeDebugPrivilege" in stdout:
148-
self.admin_privs = True
149-
elif "SeUndockPrivilege" in stdout:
150-
self.admin_privs = True
151-
self.uac = "with UAC - "
152-
153-
if not stdout:
154-
self.logger.debug(f"User: {self.username} can't get a basic shell")
155-
self.server_os_platform = "Network Devices"
156-
shell_access = False
157-
else:
158-
shell_access = True
159142

160-
self.db.add_loggedin_relation(cred_id, host_id, shell=shell_access)
143+
def check_privs(self, cred_id, stdout):
144+
shell_access = False
145+
host_id = self.db.get_hosts(self.host)[0].id
161146

162-
if shell_access and self.server_os_platform == "Linux":
163-
self.check_linux_priv()
164-
if self.admin_privs:
165-
self.logger.debug(f"User {username} logged in successfully and is root!")
166-
if self.args.key_file:
167-
self.db.add_admin_user("key", username, password, host_id=host_id, cred_id=cred_id)
168-
else:
169-
self.db.add_admin_user("plaintext", username, password, host_id=host_id, cred_id=cred_id)
147+
# If we have stdout we know it must be linux, "id" is not implemented on Windows
148+
if not stdout:
149+
self.server_os_platform = "Windows"
150+
_, stdout, _ = self.conn.exec_command("whoami /priv")
151+
stdout = stdout.read().decode(self.args.codec, errors="ignore")
152+
if "SeDebugPrivilege" in stdout:
153+
self.admin_privs = True
154+
elif "SeUndockPrivilege" in stdout:
155+
self.admin_privs = True
156+
self.uac = "with UAC - "
157+
158+
if not stdout:
159+
self.logger.debug(f"User: {self.username} can't get a basic shell")
160+
self.server_os_platform = "Network Devices"
161+
shell_access = False
162+
else:
163+
shell_access = True
170164

171-
if self.args.key_file:
172-
password = f"{process_secret(password)} (keyfile: {self.args.key_file})"
165+
self.db.add_loggedin_relation(cred_id, host_id, shell=shell_access)
173166

174-
display_shell_access = f"{self.uac}{self.server_os_platform}{' - Shell access!' if shell_access else ''}"
175-
self.logger.success(f"{username}:{process_secret(password)} {self.mark_pwned()} {highlight(display_shell_access)}")
167+
if shell_access and self.server_os_platform == "Linux":
168+
self.check_linux_priv()
169+
if self.admin_privs:
170+
self.logger.debug(f"User {self.username} logged in successfully and is root!")
171+
if self.args.key_file:
172+
self.db.add_admin_user("key", self.username, self.password, host_id=host_id, cred_id=cred_id)
173+
else:
174+
self.db.add_admin_user("plaintext", self.username, self.password, host_id=host_id, cred_id=cred_id)
176175

177-
return True
176+
out = process_secret(self.password) if not self.args.key_file else f"{process_secret(self.password)} (keyfile: {self.args.key_file})"
177+
display_shell_access = f"{self.uac}{self.server_os_platform}{' - Shell access!' if shell_access else ''}"
178+
self.logger.success(f"{self.username}:{process_secret(out)} {self.mark_pwned()} {highlight(display_shell_access)}")
178179

179180
def check_linux_priv(self):
180181
self.admin_privs = False

0 commit comments

Comments
 (0)