Fix SSH login challenge verification
This commit is contained in:
1 parent
e96356a422
commit
92a387e4e4
1 file changed
+41
-4
+41
-4
@@ -600,12 +600,36 @@ def build_ssh_auth_message(ticket, username="", host="", issued_at=0):
|
|||||||
return "\n".join(lines) + "\n"
|
return "\n".join(lines) + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
def ssh_auth_message_variants(message):
|
||||||
|
text = str(message or "")
|
||||||
|
variants = []
|
||||||
|
seen = set()
|
||||||
|
|
||||||
|
def add(value):
|
||||||
|
if value not in seen:
|
||||||
|
seen.add(value)
|
||||||
|
variants.append(value)
|
||||||
|
|
||||||
|
add(text)
|
||||||
|
normalized = text.replace("\r\n", "\n").replace("\r", "\n")
|
||||||
|
add(normalized)
|
||||||
|
add(normalized.replace("\n", "\r\n"))
|
||||||
|
trimmed = normalized.rstrip("\r\n")
|
||||||
|
if trimmed:
|
||||||
|
add(trimmed)
|
||||||
|
add(trimmed + "\n")
|
||||||
|
add(trimmed.replace("\n", "\r\n"))
|
||||||
|
add(trimmed.replace("\n", "\r\n") + "\r\n")
|
||||||
|
return variants
|
||||||
|
|
||||||
|
|
||||||
def verify_ssh_auth_signature(ssh_keys, username, message, signature_text):
|
def verify_ssh_auth_signature(ssh_keys, username, message, signature_text):
|
||||||
signature_text = str(signature_text or "").strip()
|
signature_text = str(signature_text or "").strip()
|
||||||
if "BEGIN SSH SIGNATURE" not in signature_text:
|
if "BEGIN SSH SIGNATURE" not in signature_text:
|
||||||
raise ValueError("Вставь ASCII SSH signature целиком")
|
raise ValueError("Вставь ASCII SSH signature целиком")
|
||||||
namespace = ssh_auth_namespace()
|
namespace = ssh_auth_namespace()
|
||||||
principal = ssh_auth_principal(username)
|
principal = ssh_auth_principal(username)
|
||||||
|
message_variants = ssh_auth_message_variants(message)
|
||||||
for item in [sanitize_ssh_key_record(row) for row in (ssh_keys or [])]:
|
for item in [sanitize_ssh_key_record(row) for row in (ssh_keys or [])]:
|
||||||
if not item:
|
if not item:
|
||||||
continue
|
continue
|
||||||
@@ -615,6 +639,7 @@ def verify_ssh_auth_signature(ssh_keys, username, message, signature_text):
|
|||||||
sig_file = tmp_dir / "challenge.sig"
|
sig_file = tmp_dir / "challenge.sig"
|
||||||
allowed.write_text(f"{principal} {item['public_key']}\n", encoding="utf-8")
|
allowed.write_text(f"{principal} {item['public_key']}\n", encoding="utf-8")
|
||||||
sig_file.write_text(signature_text + "\n", encoding="utf-8")
|
sig_file.write_text(signature_text + "\n", encoding="utf-8")
|
||||||
|
for candidate in message_variants:
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
[
|
[
|
||||||
@@ -630,7 +655,7 @@ def verify_ssh_auth_signature(ssh_keys, username, message, signature_text):
|
|||||||
"-s",
|
"-s",
|
||||||
str(sig_file),
|
str(sig_file),
|
||||||
],
|
],
|
||||||
input=str(message or "").encode("utf-8"),
|
input=candidate.encode("utf-8"),
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
timeout=10,
|
timeout=10,
|
||||||
@@ -9266,8 +9291,13 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
if not ssh_keys:
|
if not ssh_keys:
|
||||||
raise ValueError("Для входа ED25519 пока нет зарегистрированных ключей")
|
raise ValueError("Для входа ED25519 пока нет зарегистрированных ключей")
|
||||||
issued_at = now_ts()
|
issued_at = now_ts()
|
||||||
ticket = put_auth_flow("ssh-login", {"username": auth.get("username", "admin"), "issued_at": issued_at})
|
host = self.request_host()
|
||||||
challenge = build_ssh_auth_message(ticket, auth.get("username", "admin"), self.request_host(), issued_at)
|
ticket = put_auth_flow("ssh-login", {"username": auth.get("username", "admin"), "issued_at": issued_at, "host": host})
|
||||||
|
challenge = build_ssh_auth_message(ticket, auth.get("username", "admin"), host, issued_at)
|
||||||
|
with AUTH_FLOW_LOCK:
|
||||||
|
state = AUTH_FLOW_STATE.get(ticket)
|
||||||
|
if state:
|
||||||
|
state["challenge"] = challenge
|
||||||
self.send_json(
|
self.send_json(
|
||||||
200,
|
200,
|
||||||
{
|
{
|
||||||
@@ -9290,7 +9320,14 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
if not flow:
|
if not flow:
|
||||||
raise ValueError("ED25519-челлендж истек, начни вход заново")
|
raise ValueError("ED25519-челлендж истек, начни вход заново")
|
||||||
auth = load_auth()
|
auth = load_auth()
|
||||||
challenge = build_ssh_auth_message(ticket, auth.get("username", "admin"), self.request_host(), flow.get("issued_at", 0))
|
challenge = str(flow.get("challenge") or "")
|
||||||
|
if not challenge:
|
||||||
|
challenge = build_ssh_auth_message(
|
||||||
|
ticket,
|
||||||
|
auth.get("username", "admin"),
|
||||||
|
flow.get("host", "") or self.request_host(),
|
||||||
|
flow.get("issued_at", 0),
|
||||||
|
)
|
||||||
matched = verify_ssh_auth_signature(auth.get("ssh_keys", []), auth.get("username", "admin"), challenge, payload.get("signature", ""))
|
matched = verify_ssh_auth_signature(auth.get("ssh_keys", []), auth.get("username", "admin"), challenge, payload.get("signature", ""))
|
||||||
|
|
||||||
def mutator(current):
|
def mutator(current):
|
||||||
|
|||||||
Reference in new issue
Block a user