X509TrustManager ที่ไม่ปลอดภัย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมวดหมู่ OWASP: MASVS-CODE: คุณภาพของโค้ด
ภาพรวม
คลาส X509TrustManager
มีหน้าที่รับผิดชอบในการยืนยันความถูกต้องของเซิร์ฟเวอร์ระยะไกล
โดยจะตรวจสอบใบรับรองของเซิร์ฟเวอร์
การติดตั้งใช้งาน X509TrustManager
ที่ไม่ปลอดภัยในแอปพลิเคชัน Android คือการติดตั้งใช้งานที่ไม่ได้ยืนยันความถูกต้องของเซิร์ฟเวอร์ที่แอปพลิเคชันกำลังสื่อสารด้วยอย่างถูกต้อง ซึ่งอาจทำให้ผู้โจมตี
แอบอ้างเป็นเซิร์ฟเวอร์ที่ถูกต้องและหลอกให้แอปพลิเคชันส่งข้อมูลที่ละเอียดอ่อน
ไปยังผู้โจมตีได้
ช่องโหว่นี้มีอยู่เนื่องจากเมื่อใช้คลาส X509TrustManager
Java/Android จะอนุญาตให้มีการลบล้างการยืนยันเซิร์ฟเวอร์ทั้งหมด คลาส
X509TrustManager
มีฟังก์ชันที่น่าสนใจ 2 ฟังก์ชัน ได้แก่
checkServerTrusted()
และ getAcceptedIssuers()
คุณกำหนดค่าฟังก์ชัน
การเรียกเหล่านี้ให้เชื่อถือใบรับรอง X.509 ทั้งหมดได้ สุดท้ายนี้ ตรรกะการตรวจสอบที่กำหนดเองอาจมีข้อบกพร่องหรือไม่สมบูรณ์ และอนุญาตการเชื่อมต่อที่ไม่คาดคิด
ในทุกกรณีเหล่านี้ วัตถุประสงค์ของคลาสจะถูกปฏิเสธและเครือข่าย
การเชื่อมต่อที่สร้างขึ้นตามเอาต์พุต X509TrustManager
จะไม่ปลอดภัย
ผลกระทบ
การใช้งาน X509TrustManager ที่ไม่ปลอดภัยอาจทำให้เกิดช่องโหว่ที่ใช้ในการโจมตีแบบแทรกกลางการสื่อสาร (Man-in-the-Middle หรือ MitM) ในการจราจรของข้อมูลในเครือข่ายจากแอปพลิเคชันของเหยื่อได้
ผลกระทบจากการใช้ประโยชน์จากโค้ดที่ไม่ปลอดภัยนี้คือผู้โจมตีในเครือข่าย (จากระยะไกลหรือในเครื่อง) สามารถบุกรุกข้อมูลเครือข่ายของแอปพลิเคชันของผู้ใช้ได้หากโค้ดนี้เริ่มทำงาน ผลกระทบขึ้นอยู่กับเนื้อหาของ
การรับส่งข้อมูลเครือข่ายที่เปิดเผยโดยไม่ตั้งใจ (PII, ข้อมูลส่วนตัว,
ค่าเซสชันที่ละเอียดอ่อน ข้อมูลเข้าสู่ระบบของบริการ ฯลฯ)
การลดปัญหา
ใช้ฟังก์ชัน NetworkSecurityConfig.xml เพื่อให้มั่นใจว่าการเชื่อมต่อทั้งหมดใน
การผลิต การทดสอบ การแก้ไขข้อบกพร่อง และขั้นตอนการพัฒนาได้รับการจัดการอย่างเหมาะสม
แทนที่จะใช้หรือติดตั้งใช้งานโค้ดการตรวจสอบใบรับรอง TLS/SSL ที่กำหนดเอง หากจำเป็นต้องใช้ใบรับรองที่ลงนามด้วยตนเองสำหรับการทดสอบและบิลด์การแก้ไขข้อบกพร่อง ให้พิจารณาใช้ NetworkSecurityConfig แทนการใช้ X509TrustManager
ที่กำหนดเอง
แหล่งข้อมูล
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 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"]]