From 94ed738515aa6e1a1d346b85b54805e68e36f94c Mon Sep 17 00:00:00 2001 From: Aunali321 <48486084+Aunali321@users.noreply.github.com> Date: Fri, 11 Aug 2023 00:11:31 +0530 Subject: [PATCH] feat: Add `Override certificate pinning` patch (#2781) Co-authored-by: oSumAtrIX --- .../patch/OverrideCertificatePinningPatch.kt | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/main/kotlin/app/revanced/patches/all/misc/network/patch/OverrideCertificatePinningPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/all/misc/network/patch/OverrideCertificatePinningPatch.kt b/src/main/kotlin/app/revanced/patches/all/misc/network/patch/OverrideCertificatePinningPatch.kt new file mode 100644 index 000000000..0a3da723f --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/all/misc/network/patch/OverrideCertificatePinningPatch.kt @@ -0,0 +1,79 @@ +package app.revanced.patches.all.misc.network.patch + +import app.revanced.patcher.annotation.Description +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.data.ResourceContext +import app.revanced.patcher.patch.* +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patches.all.misc.debugging.patch.EnableAndroidDebuggingPatch +import org.w3c.dom.Element +import java.io.File + +@Patch(false) +@Name("Override certificate pinning") +@Description("Overrides certificate pinning, allowing to inspect traffic via a proxy.") +@DependsOn([EnableAndroidDebuggingPatch::class]) +class OverrideCertificatePinningPatch : ResourcePatch { + + override fun execute(context: ResourceContext): PatchResult { + val resXmlDirectory = context["res/xml"] + + // Add android:networkSecurityConfig="@xml/network_security_config" and the "networkSecurityConfig" attribute if it does not exist. + context.xmlEditor["AndroidManifest.xml"].use { editor -> + val document = editor.file + val applicationNode = document.getElementsByTagName("application").item(0) as Element + + if (!applicationNode.hasAttribute("networkSecurityConfig")) { + document.createAttribute("android:networkSecurityConfig") + .apply { value = "@xml/network_security_config" }.let(applicationNode.attributes::setNamedItem) + } + } + + // In case the file does not exist create the "network_security_config.xml" file. + File(resXmlDirectory, "network_security_config.xml").apply { + if (!exists()) { + createNewFile() + writeText( + """ + + + + + + + + + + + + + + + + """ + ) + } else { + // If the file already exists. + readText().let { text -> + if (!text.contains("")) { + writeText( + text.replace( + "", + "\n\n" + ) + ) + } + } + + } + } + + return PatchResultSuccess() + } +}