Computer >> Hướng Dẫn Máy Tính >  >> Kết Nối Mạng >> An Ninh Mạng

Hash (Hàm băm) trong An ninh mạng: Định nghĩa, Ứng dụng & Cơ chế

Hash (Hàm băm) là gì?

Hash (Hàm băm / Băm) là thuật toán toán học **một chiều (one-way function)** nhận đầu vào (input) là dữ liệu tùy ý độ dài (file, message, password, certificate...) và tạo ra đầu ra (output) là chuỗi ký tự **có độ dài CỐ ĐỊNH (fixed-length)** – gọi là **Hash Value / Hash Code / Digest / Checksum / Fingerprint**.

Đặc tính cốt lõi của Cryptographic Hash Function (An ninh mật mã)

  1. Deterministic (Xác định): Cùng input → Luôn ra cùng output.
  2. Quick Computation: Tính toán nhanh cho mọi input size.
  3. Pre-image Resistance (Kháng nguyên ảnh): Khó tìm input `x` từ hash `h(x)` (One-way).
  4. Second Pre-image Resistance: Khó tìm `y ≠ x` sao cho `h(y) = h(x)`.
  5. Collision Resistance (Kháng va chạm): Khó tìm **bất kỳ** `x ≠ y` sao cho `h(x) = h(y)`.
  6. Avalanche Effect (Hiệu ứng tuyết lở): Thay đổi 1 bit input → thay đổi ~50% bit output.

Các thuật toán Hash phổ biến & Trạng thái bảo mật

Thuật toánĐộ dài OutputTrạng tháiỨng dụng điển hình
MD5 (Message Digest 5)128-bit (32 hex)💀 **BROKEN** (Collision attack 2004, Chosen-prefix 2009). KHÔNG DÙNG CHO SECURITY.Checksum file non-security (legacy), ETag HTTP (deprecated).
SHA-1 (Secure Hash Algorithm 1)160-bit (40 hex)💀 **DEPRECATED / BROKEN** (SHAttered 2017 - chosen-prefix collision). KHÔNG DÙNG CHO SECURITY.Git commit ID (legacy), TLS cert (deprecated 2017), legacy code signing.
SHA-2 Family (SHA-224, SHA-256, SHA-384, SHA-512, SHA-512/224, SHA-512/256)224/256/384/512-bit✅ **AN TOÀN (Hiện tại)**. Chuẩn NIST FIPS 180-4. SHA-256 phổ biến nhất.TLS 1.2/1.3, Bitcoin, Code signing, Certificates (RSA/ECDSA), JWT, Password hashing (với salt, iterations - PBKDF2), Blockchain, MACsec, IPsec.
SHA-3 Family (SHA3-224/256/384/512, SHAKE128/256)224/256/384/512-bit✅ **AN TOÀN (Mới nhất - NIST FIPS 202)**. Kiến trúc Keccak (Sponge function), khác SHA-2.Diversification, Future-proofing, SHAKE cho XOF (Extendable Output Function).
BLAKE2 / BLAKE3256/512-bit (tuỳ chỉnh)✅ **RẤT NHANH, AN TOÀN**. BLAKE3 song song hóa cực nhanh (SIMD, multi-core).File integrity (BLAKE3), Password hashing (Argon2 dùng BLAKE2b), Crypto protocols.
RIPEMD-160160-bit⚠️ **Legacy** (Bitcoin Address legacy). Không khuyến khích mới.Bitcoin P2PKH address (với SHA-256).

Ứng dụng Hash trong An ninh mạng

1. Kiểm tra Toàn vẹn Dữ liệu (Integrity Verification)

  • **File Download:** So sánh SHA-256 checksum do vendor cung cấp (`sha256sum file.iso`).
  • **Software Distribution:** Code signing (Authenticode, GPG) – hash file + ký bằng private key.
  • **Blockchain / Git / Merkle Tree:** Hash liên kết block/commit → bất biến (immutable).
  • **FIM (File Integrity Monitoring):** Tripwire, AIDE, OSSEC/Wazuh, Windows Defender ATP – giám sát thay đổi file hệ thống quan trọng.

2. Lưu trữ Mật khẩu (Password Hashing) – **KHÔNG DÙNG HASH THUẦN TUYẾT (MD5/SHA-256) TRỰC TIẾP**

  • Phải dùng **Password Hashing Function (PHF) / Key Derivation Function (KDF)** được thiết kế **chậm (slow), tốn memory (memory-hard), có Salt**:
  • Argon2id (Chiến thắng PHC 2015) – Tiêu chuẩn vàng hiện nay.
  • scrypt – Memory-hard (Litecoin, Tarsnap).
  • bcrypt – Blowfish-based, work factor (cost), giới hạn 72 bytes.
  • PBKDF2 (HMAC-SHA-256/512) – NIST SP 800-132, iterations cao (≥ 600k). Dùng trong TLS, 1Password, LastPass (cũ).
  • **Salt:** Random unique per password (16-32 bytes) → ngăn Rainbow Table, giống pass → hash khác nhau.

3. Chữ ký số (Digital Signature) & Certificate

  • Hash message (SHA-256) → Ký bằng Private Key (RSA-PSS, ECDSA, EdDSA/Ed25519) → Signature.
  • X.509 Certificate: Hash TBSCertificate → CA ký → Verifier hash lại → Verify signature.

4. Message Authentication Code (MAC / HMAC)

  • HMAC (Hash-based MAC): `HMAC(K, m) = H((K ⊕ opad) || H((K ⊕ ipad) || m))`. Dùng SHA-256/512.
  • Ứng dụng: API Authentication (AWS SigV4, JWT HS256), TLS Record Protocol (HMAC-SHA256), IPsec, SSH, Cookie signing.

5. Proof of Work / Consensus (Blockchain)

  • Bitcoin: `SHA-256(SHA-256(block_header)) < target` (Double SHA-256).
  • Ethereum (PoW cũ): Keccak-256 (SHA-3 variant).

6. Key Derivation & Key Agreement

  • HKDF (HMAC-based KDF) – RFC 5869: Extract-Expand từ IKM (Input Key Material).
  • TLS 1.3 Key Schedule: HKDF-Extract/Expand + Derive-Secret.

7. Định danh & Tra cứu (Identifiers & Lookups)

  • Git Commit Hash (SHA-1 → chuyển sang SHA-256).
  • Docker Image Digest (SHA-256).
  • Certificate Fingerprint (SHA-256).
  • Malware Sample ID (SHA-256 – "hash" trên VirusTotal, Hybrid Analysis).

Tấn công vào Hash & Phòng thủ

Tấn côngMô tảPhòng thủ
Collision AttackTìm x≠y: H(x)=H(y)Dùng SHA-256/384/512, SHA-3, BLAKE2/3. Không dùng MD5/SHA-1.
Length Extension AttackH(secret||msg) → H(secret||msg||padding||ext) (MD5, SHA-1, SHA-2 raw)Dùng HMAC, SHA-3, BLAKE2/3, hoặc SHA-2 với construction an toàn (HMAC, HKDF).
Rainbow Table / Precomputed HashTra bảng hash phổ biến → reverse password**Salt duy nhất cho mỗi pass** + **Slow Hash (Argon2id, scrypt, bcrypt, PBKDF2 high iter)**.
Quantum (Grover's Algorithm)Giảm độ an toàn từ 2^n → 2^(n/2)Dùng SHA-384/512 (192/256-bit post-quantum), SHA-3-384/512. NIST PQC standardize KEM/SIG, không hash.

Hash KHÔNG phải là Mã hóa (Encryption)

Hash: One-way (Một chiều). Không thể giải mã (decrypt) ngược về input. Dùng cho Integrity, Authentication, Identification.

Encryption: Two-way (Hai chiều). Encrypt với Key → Ciphertext. Decrypt với Key → Plaintext. Dùng cho Confidentiality.

Best Practice triển khai Hash trong Code

  • Dùng thư viện chuẩn, đã audit: `hashlib` (Python), `crypto/subtle` (Node.js), `System.Security.Cryptography` (.NET), `java.security.MessageDigest` (Java), `openssl` (C/C++), `ring`/`rust-crypto` (Rust), `golang.org/x/crypto` (Go).
  • **Password:** `argon2` (Python: `argon2-cffi`, Go: `golang.org/x/crypto/argon2`, Node: `@node-rs/argon2`).
  • **File Integrity:** `sha256` / `blake3` (streaming cho file lớn).
  • **HMAC:** `hmac.new(key, msg, hashlib.sha256)` – **Luôn dùng constant-time compare** (`hmac.compare_digest`, `crypto.timingSafeEqual`).
  • **Salt/Nonce/IV:** Sinh bằng CSPRNG (`os.urandom`, `secrets.token_bytes`, `Random.getRandomValues`, `/dev/urandom`, `BCryptGenRandom`, `RtlGenRandom`).