ENع
→Authentication
الدرس 04 · الأسس

Passwords, MFA and passkeys

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة12 خطوة5 أسئلةBeginner
عن هذا الدرس

Authentication is how a site checks that you are really you. Follow one sign-up from start to finish: the site saves a random salt and a slow, one-way hash of the password (eight lines of real Python), never the password itself, and signs you in by comparing hashes. See what a database leak gives away, what NIST's password rules ask for, how an authenticator code adds a second factor and how it can still be phished, and why a passkey, a key pair tied to one site, resists phishing.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 4 · تفاعلي
01 / 12

Proving who you are

Authentication means proving you are who you say you are before a site lets you in. Each proof is a factor. There are three kinds: something you know (a password), something you have (a phone) and something you are (a fingerprint).

authentication   proving who you are
factor           one kind of proof
  know           a password or PIN
  have           a phone, a security key
  are            a fingerprint, a face
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Proving who you are

    Authentication means proving you are who you say you are before a site lets you in. Each proof is a factor. There are three kinds: something you know (a password), something you have (a phone) and something you are (a fingerprint).

    authentication   proving who you are
    factor           one kind of proof
      know           a password or PIN
      have           a phone, a security key
      are            a fingerprint, a face
  2. Signing up with a password

    Ana signs up at shop.example with a long passphrase. It travels inside HTTPS, so nobody on the way can read it. The site saves her username. It must never save the password itself.

    site        shop.example
    user        ana
    passphrase  plum-ferry-1979  (15 characters)
    travels     inside HTTPS (encrypted)
    saved       the username, never the password
  3. A random salt

    First the site makes a salt: 16 random bytes from os.urandom, new for every user. It is saved next to the username. A salt is not secret. It makes two people with the same password get different hashes.

    salt = os.urandom(16)    16 random bytes (128 bits)
    example       1f9a2c7e5b3d8046a1c9e27f0b6d4a35
    one per user  same password, different salt,
                  different hash
    NIST          a salt of at least 32 bits
  4. A slow, one-way hash

    slow() mixes the password and salt through PBKDF2, 600,000 rounds of SHA-256. The result, the hash, is saved. It is one-way: nobody can turn a hash back into the password. Then the password is thrown away.

    hashlib.pbkdf2_hmac("sha256", pw, salt, 600_000)
    rounds   600,000 (OWASP for PBKDF2-HMAC-SHA256)
    hash     7e2781fcb0c52bfa31cc04f6f532be08
             f5e336fd50e114f0d6db9449941a0f83
    saved    salt + hash; the password is forgotten
  5. Signing in: compare hashes

    To sign in, Ana types her passphrase again. The site runs slow() with her saved salt and compares the two hashes with hmac.compare_digest. They match, so it prints True and she is in. A wrong guess gives a different hash.

    typed  = slow(what she typed, saved salt)
    saved  7e2781fcb0c52bfa31cc04f6…
    typed  7e2781fcb0c52bfa31cc04f6…
    print → True       she is in
    compare_digest takes the same time either way
  6. If the database leaks

    Suppose the database is stolen. The attacker gets salts and hashes, not passwords. To find one they must guess, paying 600,000 rounds per guess, separately for each salt. Short or common passwords still fall fast. Long, unusual ones hold.

    stolen       salt + hash for every user
    not stolen   the passwords themselves
    each guess   600,000 rounds of SHA-256
    each salt    guessed separately
    weak spot    short or common passwords
  7. NIST rules for passwords

    NIST says: at least 15 characters if the password is the only factor, allow 64 or more, no forced symbols and no routine changes. Sites must block passwords known from breaches. A password manager makes a different one for every site.

    NIST SP 800-63B-4
      length     15+ alone, 8+ inside MFA
      allow      at least 64 characters
      no         forced symbols, routine changes
      block      known breached or common passwords
    you          a password manager: one per site
  8. MFA: something you have

    Multi-factor authentication adds a second proof. An authenticator app on Ana's phone shows a 6-digit code that changes every 30 seconds, made from a secret shared at setup. She types it in after her password. A stolen password alone is no longer enough.

    MFA     two or more different factors
    code    492 039   (example)
    TOTP    6 digits, a new code every 30 s
            from a secret shared at setup
    she     reads it and types it on the laptop
            (the app itself sends nothing)
    result  password + code: she is in
  9. Codes can still be phished

    A code has one weakness: Ana can be tricked into typing it into a look-alike site, sh0p.example, which passes it on within seconds. CISA rates app codes as vulnerable to phishing. Any MFA is still far better than none.

    look-alike   sh0p.example  (a zero, not an o)
    asks for     password + code
    weakness     a person can type a code anywhere
    CISA         app codes: vulnerable to phishing
                 any MFA is better than no MFA
  10. Passkeys: a key pair

    A passkey replaces the password. Ana's phone makes a key pair just for shop.example. The private key stays on her device. The site saves only the public key, which can check signatures but cannot make them.

    passkey      a key pair for one site
      private    stays on Ana's device
      public     saved by shop.example
    public key   checks signatures, cannot make them
    tied to      shop.example
  11. Signing a challenge

    To sign in, the site sends a fresh random challenge. Ana unlocks her phone with a fingerprint or PIN, and it signs the challenge with the private key. The site checks the signature with the public key. No secret crosses the network.

    site → phone   a fresh random challenge
    Ana            unlocks: fingerprint or PIN
    phone          signs it with the private key
    phone → site   the signature
    site           checks it with the public key
  12. Why passkeys resist phishing

    Ana's browser tells her phone which site is asking. The passkey is tied to shop.example, so sh0p.example gets no signature, however real it looks. A leaked public key signs nobody in. CISA calls this phishing-resistant MFA.

    asking     sh0p.example
    passkey    only for shop.example
    result     no match: nothing is signed
    leaked     a public key is useless to an attacker
    CISA       FIDO / WebAuthn: phishing-resistant

اختبار يتاح بعد الخطوة الأخيرة.