Niebezpieczny X509TrustManager
Zadbaj o dobrą organizację dzięki kolekcji
Zapisuj i kategoryzuj treści zgodnie ze swoimi preferencjami.
Kategoria OWASP: MASVS-CODE: jakość kodu
Omówienie
Klasa X509TrustManager
odpowiada za weryfikację autentyczności serwera zdalnego. Odbywa się to przez sprawdzenie certyfikatu serwera.
Niezabezpieczona implementacja X509TrustManager
w aplikacji na Androida to implementacja, która nie weryfikuje prawidłowo autentyczności serwera, z którym komunikuje się aplikacja. Może to umożliwić osobie przeprowadzającej atak podszywanie się pod legalny serwer i nakłonienie aplikacji do wysłania do niej danych wrażliwych.
Luka w zabezpieczeniach występuje, ponieważ użycie klasy X509TrustManager
w Java/Android umożliwia całkowite zastąpienie weryfikacji serwera. Klasa X509TrustManager
ma 2 interesujące funkcje: checkServerTrusted()
i getAcceptedIssuers()
. Te wywołania funkcji można skonfigurować tak, aby ufały wszystkim certyfikatom X.509. Na koniec trzeba dodać, że niestandardowa logika weryfikacji może zawierać błędy lub być niekompletna i umożliwiać nieoczekiwane połączenia.
We wszystkich tych przypadkach cel klasy został zanegowany, a połączenie sieciowe nawiązane na podstawie danych wyjściowych X509TrustManager
nie jest bezpieczne.
Wpływ
Potencjalnie niebezpieczne implementacje interfejsu X509TrustManager mogą prowadzić do luk w zabezpieczeniach, które można wykorzystać do przeprowadzania ataków typu „man-in-the-middle” (MITM) na ruch sieciowy z aplikacji ofiary. Jeśli ten niebezpieczny kod zostanie uruchomiony, dane sieciowe aplikacji użytkownika mogą zostać naruszone przez atakujących w sieci (zdalnie lub lokalnie). Wpływ zależy od treści
nieumyślnie ujawnionego ruchu sieciowego (informacje umożliwiające identyfikację osoby, informacje prywatne,
wartości sesji o charakterze kontrowersyjnym, dane logowania do usług itp.).
Środki ograniczające ryzyko
Używaj funkcji NetworkSecurityConfig.xml, aby mieć pewność, że wszystkie połączenia na etapach produkcji, testowania, debugowania i programowania są prawidłowo obsługiwane, zamiast używać lub wdrażać niestandardowy kod weryfikacji certyfikatów TLS/SSL. Jeśli w przypadku kompilacji testowych i debugowania konieczne jest użycie certyfikatu podpisanego samodzielnie, rozważ użycie NetworkSecurityConfig zamiast implementowania niestandardowego X509TrustManager
.
Materiały
Treść strony i umieszczone na niej fragmenty kodu podlegają licencjom opisanym w Licencji na treści. Java i OpenJDK są znakami towarowymi lub zastrzeżonymi znakami towarowymi należącymi do firmy Oracle lub jej podmiotów stowarzyszonych.
Ostatnia aktualizacja: 2025-07-27 UTC.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-07-27 UTC."],[],[],null,["# Unsafe X509TrustManager\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 `X509TrustManager` class is responsible for verifying the authenticity of a\nremote server. It does this by validating the server's certificate.\n\nAn insecure `X509TrustManager` implementation in an Android application is an\nimplementation that does not properly verify the authenticity 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\nThe vulnerability exists because using the [`X509TrustManager`](/reference/javax/net/ssl/X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate%5B%5D,%20java.lang.String)) class,\nJava/Android allows the complete overriding of server verification. The\n`X509TrustManager` class has two functions of interest:\n[`checkServerTrusted()`](/reference/javax/net/ssl/X509TrustManager#checkServerTrusted(java.security.cert.X509Certificate%5B%5D,%20java.lang.String)) and [`getAcceptedIssuers()`](/reference/javax/net/ssl/X509TrustManager#getAcceptedIssuers()). These function\ncalls can be configured to trust all X.509 certificates. Finally, custom\nvalidation logic may be buggy or incomplete and permit unexpected connections.\nIn all these cases, the purpose of the class has been negated and the network\nconnection established based on the `X509TrustManager` output is not secure.\n\nImpact\n------\n\nUnsafe X509TrustManager 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) functionality 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. If\nusing a self-signed certificate is needed for test and debug builds, consider\nusing NetworkSecurityConfig instead of implementing a custom `X509TrustManager`.\n\nResources\n---------\n\n- [Play Warning docs](https://support.google.com/faqs/answer/6346016)\n- [Documentation to assist configuring the Network security configuration xml file.](/training/articles/security-config)\n- [Developer documentation for the TrustManager class.](/reference/javax/net/ssl/TrustManager)\n- [This check looks for X.509TrustManager implementations whose checkServerTrusted or checkClientTrusted methods do nothing (thus trusting any certificate chain).](https://googlesamples.github.io/android-custom-lint-rules/checks/TrustAllX509TrustManager.md.html)\n- [This check looks for custom X.509TrustManager implementations.](https://googlesamples.github.io/android-custom-lint-rules/checks/CustomX509TrustManager.md.html)\n- \u003chttps://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/X509TrustManagerDetector.java\u003e"]]