HostnameVerifier không an toàn
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Danh mục OWASP: MASVS-CODE: Chất lượng mã
Tổng quan
Quá trình triển khai HostnameVerifier
chịu trách nhiệm xác minh rằng tên máy chủ trong chứng chỉ của máy chủ khớp với tên của máy chủ mà ứng dụng đang cố kết nối.
Quá trình triển khai HostnameVerifier
không an toàn trong ứng dụng Android là quá trình không xác minh đúng tên máy chủ đang giao tiếp với ứng dụng. Điều này có thể cho phép kẻ tấn công mạo danh máy chủ hợp pháp và lừa ứng dụng gửi dữ liệu nhạy cảm cho kẻ tấn công.
Lỗ hổng bảo mật này tồn tại vì lớp HostnameVerifier
có các lệnh gọi hàm có thể bỏ qua quá trình xác thực tên máy chủ của chứng chỉ X.509 và thay vào đó chỉ xác minh hàm băm của chứng chỉ này. Có một sự hiểu nhầm phổ biến là hàm SSLSession#isValid
thực hiện một thao tác liên quan đến bảo mật, trong khi thực tế mục đích của hàm này chỉ là kiểm tra xem một phiên hoạt động có hợp lệ và có sẵn không để tiếp tục hoặc tham gia; cả hai đều không xác thực được tính bảo mật của một phiên hoạt động. Lớp HostnameVerifier
đã được thay thế bằng NetworkSecurityConfig.
Mức độ tác động
Quá trình triển khai HostnameVerifier
không an toàn có thể dẫn đến các lỗ hổng bảo mật. Các lỗ hổng này có thể dùng để thực hiện những cuộc tấn công MiTM (xen giữa) đối với lưu lượng truy cập mạng từ ứng dụng bị hại. Tác động của việc khai thác mã không an toàn này là dữ liệu mạng ứng dụng của người dùng có thể bị kẻ tấn công mạng xâm phạm (từ xa hoặc cục bộ) nếu mã này được kích hoạt. Tác động này phụ thuộc vào nội dung của lưu lượng truy cập mạng vô tình bị lộ (thông tin nhận dạng cá nhân, thông tin cá nhân, giá trị phiên hoạt động nhạy cảm, thông tin đăng nhập dịch vụ, v.v.).
Giải pháp giảm thiểu
Hãy sử dụng chức năng NetworkSecurityConfig.xml để đảm bảo rằng tất cả các kết nối ở giai đoạn chính thức, kiểm thử, gỡ lỗi và phát triển đều được xử lý đúng cách thay vì sử dụng hoặc triển khai mã xác thực chứng chỉ TLS/SSL tuỳ chỉnh.
Tài nguyên
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2023-12-14 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2023-12-14 UTC."],[],[],null,["# Unsafe HostnameVerifier\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\nThe [`HostnameVerifier`](/reference/javax/net/ssl/HostnameVerifier#verify(java.lang.String,%20javax.net.ssl.SSLSession)) implementation is responsible for verifying that the\nhostname in the server's certificate matches the hostname of the server that the\nclient is trying to connect to.\n\nAn unsafe HostnameVerifier implementation in an Android application is an\nimplementation that does not properly verify the hostname of the server with\nwhich the application is communicating. This can allow an attacker to\nimpersonate a legitimate server and trick the application into sending sensitive\ndata to the attacker.\n\nThis vulnerability exists because the `HostnameVerifier` class has function\ncalls that can skip X.509 certificate hostname validation and, instead, only\nverify the hash of the certificate. A common misconception is that the\n[`SSLSession#isValid`](/reference/javax/net/ssl/SSLSession#isValid()) function performs a security-related operation, when\nin reality its purpose is only to check if a session is valid and available for\nresuming or joining; neither of which validate the *security* of a session. The\nHostnameVerifier class has been superseded by [NetworkSecurityConfig](/training/articles/security-config).\n\nImpact\n------\n\nUnsafe HostnameVerifier implementations can lead to vulnerabilities which can be\nused to perform MiTM (Man-in-The-Middle) attacks on network traffic from the\nvictim application. The impact of exploiting this insecure code is that a user's\napplication network data can be compromised by network attackers (remotely or\nlocally) if this code is triggered. The impact is dependent on the content of\nthe network traffic being inadvertently exposed (PII, private information,\nsensitive session values, service credentials, etc).\n\nMitigations\n-----------\n\nUse the [NetworkSecurityConfig.xml](/training/articles/security-config) to ensure that all\nproduction, testing, debugging, and dev stage connections are properly handled\nrather than using or implementing custom TLS/SSL certificate validation code.\n\nResources\n---------\n\n- [Network Security Configuration Documentation](/training/articles/security-config)\n- [This check looks for implementations of HostnameVerifier whose verify method always returns true (thus trusting any hostname)](https://googlesamples.github.io/android-custom-lint-rules/checks/BadHostnameVerifier.md.html)\n- [Developer documentation for the HostnameVerifier class](/reference/javax/net/ssl/HostnameVerifier#verify(java.lang.String,%20javax.net.ssl.SSLSession))\n- [AllowAllHostnameVerifierDetector class in Android](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/AllowAllHostnameVerifierDetector.java)"]]