NekoX/TMessagesProj/src/main/java/tw/nekomimi/nekogram/parts/Signtures.kt

90 lines
2.5 KiB
Kotlin
Raw Normal View History

2021-05-06 14:51:10 +02:00
/***
* If you modify and release but do not release the source code, you violate the GPL, so this is made.
*
* @author nekohasekai
*/
2021-04-05 21:14:02 +02:00
package tw.nekomimi.nekogram.parts
import android.content.Context
import android.content.pm.PackageManager.GET_SIGNATURES
import android.content.pm.PackageManager.GET_SIGNING_CERTIFICATES
2021-05-06 14:51:10 +02:00
import android.content.pm.Signature
2021-04-05 21:14:02 +02:00
import android.os.Build
2021-05-06 14:51:10 +02:00
import android.os.Process
2021-04-05 21:14:02 +02:00
import cn.hutool.crypto.digest.DigestUtil
2021-05-06 14:51:10 +02:00
import org.telegram.messenger.AndroidUtilities
2021-04-05 21:14:02 +02:00
import org.telegram.messenger.FileLog
2021-05-06 14:51:10 +02:00
val devKeys = arrayOf(
"32250A4B5F3A6733DF57A3B9EC16C38D2C7FC5F2F693A9636F8F7B3BE3549641"
2021-04-05 21:14:02 +02:00
)
2021-05-06 14:51:10 +02:00
fun Context.getSignature(): Signature {
2021-04-05 21:14:02 +02:00
val appInfo = packageManager.getPackageInfo(
2021-05-06 09:09:55 +02:00
packageName,
if (Build.VERSION.SDK_INT >= 28) GET_SIGNING_CERTIFICATES else GET_SIGNATURES
2021-04-05 21:14:02 +02:00
)
2021-05-06 14:51:10 +02:00
return if (Build.VERSION.SDK_INT >= 28) {
appInfo.signingInfo.apkContentsSigners[0]
} else {
appInfo.signatures[0]
}
}
2021-04-05 21:14:02 +02:00
2021-05-06 14:51:10 +02:00
fun Context.getSha256Signature(): String {
return DigestUtil.sha256Hex(getSignature().toByteArray()).uppercase()
2021-04-05 21:14:02 +02:00
}
fun Context.isVerified(): Boolean {
val packageName = packageName
if (!packageName.contains("nekox")) {
FileLog.w("packageName changed, don't check signature")
return true
}
2021-05-06 14:51:10 +02:00
when (val s = getSha256Signature()) {
in devKeys,
-> return true
2021-04-05 21:14:02 +02:00
else -> {
FileLog.w("Unknown signature: $s")
}
}
return false
2021-05-06 14:51:10 +02:00
}
fun Context.checkMT() {
val fuckMT = Runnable {
Thread.setDefaultUncaughtExceptionHandler(null)
Thread.currentThread().uncaughtExceptionHandler = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
Process.killProcess(Process.myPid())
} catch (e: Exception) {
}
}
Runtime.getRuntime().exit(0)
}
try {
Class.forName("bin.mt.apksignaturekillerplus.HookApplication")
AndroidUtilities.runOnUIThread(fuckMT)
return
} catch (ignored: ClassNotFoundException) {
}
if (isVerified()) return
val manifestMF = javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
if (manifestMF == null) {
FileLog.w("/META-INF/MANIFEST.MF not found")
return
}
val input = manifestMF.bufferedReader()
val headers = input.use { (0 until 5).map { readLine() } }.joinToString("\n")
// WTF version?
if (headers.contains("Android Gradle 3.5.0")) {
AndroidUtilities.runOnUIThread(fuckMT)
}
2021-04-05 21:14:02 +02:00
}