Skip to content

Instantly share code, notes, and snippets.

@5ec1cff
Created August 3, 2023 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 5ec1cff/70065058eedcf97af4340c775fe649e3 to your computer and use it in GitHub Desktop.
Save 5ec1cff/70065058eedcf97af4340c775fe649e3 to your computer and use it in GitHub Desktop.
CorePatch detector
package io.github.a13e300.demo.maho
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageInstaller
import android.content.pm.PackageInstaller.EXTRA_STATUS
import android.content.pm.PackageInstaller.STATUS_PENDING_USER_ACTION
import io.github.a13e300.demo.BuildConfig
import java.util.UUID
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream
// code decompiled from momo-v4.4.1 (class: Bi)
// momo: https://t.me/magiskalpha/529
// https://t.me/real5ec1cff/32
// true meaning abnormal
fun detectCorePatch(context: Context): Boolean {
val session: PackageInstaller.Session
val uuid = UUID.randomUUID().toString()
var result = false
val broadcast = PendingIntent.getBroadcast(
context,
0,
Intent(uuid).setPackage(BuildConfig.APPLICATION_ID),
PendingIntent.FLAG_MUTABLE
)
val countDownLatch = CountDownLatch(1)
val receiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val extras = intent.extras ?: return
result = extras.getInt(EXTRA_STATUS) == STATUS_PENDING_USER_ACTION
countDownLatch.countDown()
}
}
context.registerReceiver(receiver, IntentFilter(uuid))
val packageInstaller = context.packageManager.packageInstaller
try {
session = packageInstaller.openSession(
packageInstaller.createSession(
PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
)
)
} catch (t: SecurityException) {
return true
}
ZipOutputStream(session.openWrite("package", 0L, -1L)).use { zipOutputStream ->
zipOutputStream.putNextEntry(ZipEntry("AndroidManifest.xml"))
ZipFile(context.packageCodePath).use { zipFile ->
zipFile.getInputStream(zipFile.getEntry("AndroidManifest.xml"))
.use { it.copyTo(zipOutputStream) }
}
zipOutputStream.finish()
}
session.commit(broadcast.intentSender)
countDownLatch.await(4L, TimeUnit.SECONDS)
context.unregisterReceiver(receiver)
session.abandon()
session.close()
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment