Recognize the original EzDent application login

This commit is contained in:
SantaSpeen committed 2026-11-22 21:06:33 +03:00
1 parent 81e074b7c3
commit a46a111d50
1 file changed
+53 -14
+53 -14
View File
@@ -20,8 +20,15 @@ final class WebConsoleAPI {
private let passwordDigest = SHA256.hash(data: Data(
(ProcessInfo.processInfo.environment["EZSERVER_CONSOLE_PASSWORD"] ?? "0000").utf8
))
private static let applicationFingerprint = Data(base64Encoded: "7dH5qqbP4LiANpOsC28VFALLLHuZyhWZjON7NKoO7T8=")!
private struct Session {
let expiry: Date
let administrator: Bool
}
private let signingKey = SymmetricKey(size: .bits256)
private var sessions: [String: Date] = [:]
private var sessions: [String: Session] = [:]
private var events: [[String: Any]] = []
private var failures = 0
private var lockedUntil = Date.distantPast
@@ -71,9 +78,13 @@ final class WebConsoleAPI {
}
let now = Date()
guard let authorization, authorization.hasPrefix("Bearer "),
let expires = sessions[String(authorization.dropFirst(7))], expires > now else {
let session = sessions[String(authorization.dropFirst(7))], session.expiry > now else {
return Reply(["errorMessage": "Auth token has expired"], status: "401 Unauthorized", errorCode: -8001511)
}
guard session.administrator else {
return Reply(["errorMessage": "Administrator login required"],
status: "403 Forbidden", errorCode: -1013)
}
let input = (try? JSONSerialization.jsonObject(with: body)) as? [String: Any] ?? [:]
switch (path[0], path[1], method) {
case ("e2ds", "useraccountsbynamer2", "GET"):
@@ -188,12 +199,9 @@ final class WebConsoleAPI {
}
guard let request = try? JSONDecoder().decode(LoginRequest.self, from: body),
let login = Self.decrypt(request.data),
login.auth.decoded(login.auth.usr) == Self.admin,
login.auth.exp >= now.timeIntervalSince1970,
login.auth.exp < Double(Int32.max),
let password = login.auth.decoded(login.auth.sec),
zip(SHA256.hash(data: Data(password.utf8)), passwordDigest)
.reduce(UInt8(0), { $0 | ($1.0 ^ $1.1) }) == 0 else {
let administrator = role(for: login.auth) else {
failures += 1
if failures >= 8 {
lockedUntil = now.addingTimeInterval(60)
@@ -204,19 +212,36 @@ final class WebConsoleAPI {
status: "401 Unauthorized", errorCode: -8001001)
}
failures = 0
sessions = sessions.filter { $0.value > now }
if sessions.count >= 32, let oldest = sessions.min(by: { $0.value < $1.value }) {
sessions = sessions.filter { $0.value.expiry > now }
if sessions.count >= 32, let oldest = sessions.min(by: { $0.value.expiry < $1.value.expiry }) {
sessions.removeValue(forKey: oldest.key)
}
let expiry = Int(login.auth.exp)
guard let token = try? makeToken(login, issuedAt: Int(now.timeIntervalSince1970), expiry: expiry) else {
return Reply(status: "503 Service Unavailable", errorCode: -1005)
}
sessions[token] = Date(timeIntervalSince1970: Double(expiry))
event(type: "LOGIN", message: "Logged in to EzServer Web Console.")
sessions[token] = Session(expiry: Date(timeIntervalSince1970: Double(expiry)), administrator: administrator)
if administrator {
event(type: "LOGIN", message: "Logged in to EzServer Web Console.")
}
return Reply(["token": token])
}
private func role(for auth: Login.Auth) -> Bool? {
guard let username = auth.decoded(auth.usr), let password = auth.decoded(auth.sec) else { return nil }
if username == Data(Self.admin.utf8) {
return Self.matches(SHA256.hash(data: password), passwordDigest) ? true : nil
}
// The bundled client uses the original temporary application account before user login.
let fingerprint = SHA256.hash(data: username + Data([0]) + password)
return Self.matches(fingerprint, Self.applicationFingerprint) ? false : nil
}
private static func matches<A: Sequence, B: Sequence>(_ lhs: A, _ rhs: B) -> Bool
where A.Element == UInt8, B.Element == UInt8 {
zip(lhs, rhs).reduce(UInt8(0)) { $0 | ($1.0 ^ $1.1) } == 0
}
private func makeToken(_ login: Login, issuedAt: Int, expiry: Int) throws -> String {
func base64URL(_ bytes: Data) -> String {
bytes.base64EncodedString().replacingOccurrences(of: "+", with: "-")
@@ -270,12 +295,26 @@ final class WebConsoleAPI {
let sec: String
let exp: Double
func decoded(_ value: String) -> String? {
private enum CodingKeys: String, CodingKey { case enc, usr, sec, exp }
init(from decoder: Decoder) throws {
let fields = try decoder.container(keyedBy: CodingKeys.self)
enc = try fields.decode(String.self, forKey: .enc)
usr = try fields.decode(String.self, forKey: .usr)
sec = try fields.decode(String.self, forKey: .sec)
if let numeric = try? fields.decode(Double.self, forKey: .exp) {
exp = numeric
} else {
exp = Double(try fields.decode(String.self, forKey: .exp)) ?? 0
}
}
func decoded(_ value: String) -> Data? {
switch enc {
case "raw":
return value
return Data(value.utf8)
case "b64":
return Data(base64Encoded: value).flatMap { String(data: $0, encoding: .utf8) }
return Data(base64Encoded: value)
case "hex":
let digits = Array(value.utf8)
guard digits.count.isMultiple(of: 2) else { return nil }
@@ -285,7 +324,7 @@ final class WebConsoleAPI {
else { return nil }
bytes.append(byte)
}
return String(bytes: bytes, encoding: .utf8)
return Data(bytes)
default:
return nil
}