| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package com.example.modifier
- import android.util.Log
- import androidx.core.content.ContextCompat
- import kotlinx.coroutines.Dispatchers
- import kotlinx.coroutines.coroutineScope
- import kotlinx.coroutines.launch
- import kotlinx.coroutines.withContext
- import org.apache.commons.io.FileUtils
- import org.apache.commons.io.IOUtils
- import java.io.File
- import java.io.InterruptedIOException
- class Frida {
- companion object {
- var p: Process? = null
- var script: File? = null
- private val TAG = "FridaScript"
- suspend fun start() {
- if (p != null) {
- p!!.destroy()
- p = null
- }
- if (script != null) {
- script!!.delete()
- script = null
- }
- val context = Utils.getContext()
- val dataDir = ContextCompat.getDataDir(context)
- Utils.copyAssetFolder(context.assets, "bin", File(dataDir, "bin").path)
- val binPath = File(dataDir, "bin/frida-inject-16.3.3-android-arm64").path
- val pid = Utils.runAsRoot("pidof com.android.phone").trim()
- if (!Regex("[0-9]+").matches(pid)) {
- return
- }
- val scriptContent = IOUtils.toString(context.assets.open("scripts/phone.js"), "UTF-8")
- withContext(Dispatchers.IO) {
- script = File.createTempFile("script", ".js")
- FileUtils.writeStringToFile(script, scriptContent, "UTF-8")
- Log.i(TAG, "start: $binPath -p $pid -s $script")
- p = Runtime.getRuntime().exec("su -M")
- p!!.outputStream.bufferedWriter().use {
- it.write("chmod +x $binPath")
- it.newLine()
- it.flush()
- it.write("$binPath -p $pid -s $script")
- it.newLine()
- it.flush()
- }
- coroutineScope {
- launch {
- try {
- p!!.inputStream
- .bufferedReader()
- .useLines { lines ->
- lines.forEach {
- Log.i(TAG, it)
- }
- }
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
- launch {
- try {
- p!!.errorStream
- .bufferedReader()
- .useLines { lines ->
- lines.forEach {
- Log.e(TAG, it)
- }
- }
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
- }
- }
- }
- fun stop() {
- if (p != null) {
- p!!.inputStream.close()
- p!!.errorStream.close()
- p!!.destroy()
- p = null
- }
- if (script != null) {
- script!!.delete()
- script = null
- }
- }
- }
- }
|