Frida.kt 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.example.modifier
  2. import android.util.Log
  3. import androidx.core.content.ContextCompat
  4. import com.example.modifier.utils.createFakeSms
  5. import com.example.modifier.utils.getContext
  6. import com.example.modifier.utils.shellRun
  7. import kotlinx.coroutines.Dispatchers
  8. import kotlinx.coroutines.coroutineScope
  9. import kotlinx.coroutines.launch
  10. import kotlinx.coroutines.withContext
  11. import org.apache.commons.io.FileUtils
  12. import org.apache.commons.io.IOUtils
  13. import java.io.File
  14. import java.util.Base64
  15. class Frida {
  16. companion object {
  17. var p: Process? = null
  18. var script: File? = null
  19. suspend fun start() {
  20. if (p != null) {
  21. p!!.destroy()
  22. p = null
  23. }
  24. if (script != null) {
  25. script!!.delete()
  26. script = null
  27. }
  28. val context = getContext()
  29. val dataDir = ContextCompat.getDataDir(context)
  30. Utils.copyAssetFolder(context.assets, "bin", File(dataDir, "bin").path)
  31. val binPath = File(dataDir, "bin/frida-inject-16.3.3-android-arm64").path
  32. val (pid, _) = shellRun("pidof com.android.phone")
  33. if (!Regex("[0-9]+").matches(pid)) {
  34. return
  35. }
  36. val scriptContent = IOUtils.toString(context.assets.open("scripts/phone.js"), "UTF-8")
  37. withContext(Dispatchers.IO) {
  38. script = File.createTempFile("script", ".js")
  39. FileUtils.writeStringToFile(script, scriptContent, "UTF-8")
  40. Log.i(com.example.modifier.baseTag, "start: $binPath -p $pid -s $script")
  41. p = Runtime.getRuntime().exec("su -M")
  42. p!!.outputStream.bufferedWriter().use {
  43. it.write("chmod +x $binPath")
  44. it.newLine()
  45. it.flush()
  46. it.write("$binPath -p $pid -s $script")
  47. it.newLine()
  48. it.flush()
  49. }
  50. coroutineScope {
  51. launch {
  52. try {
  53. p!!.inputStream
  54. .bufferedReader()
  55. .useLines { lines ->
  56. lines.forEach {
  57. Log.i(com.example.modifier.baseTag, it)
  58. }
  59. }
  60. } catch (e: Exception) {
  61. e.printStackTrace()
  62. }
  63. }
  64. launch {
  65. try {
  66. p!!.errorStream
  67. .bufferedReader()
  68. .useLines { lines ->
  69. lines.forEach {
  70. Log.e(com.example.modifier.baseTag, it)
  71. }
  72. }
  73. } catch (e: Exception) {
  74. e.printStackTrace()
  75. }
  76. }
  77. }
  78. }
  79. }
  80. fun stop() {
  81. if (p != null) {
  82. p!!.inputStream.close()
  83. p!!.errorStream.close()
  84. p!!.destroy()
  85. p = null
  86. }
  87. if (script != null) {
  88. script!!.delete()
  89. script = null
  90. }
  91. }
  92. }
  93. }
  94. suspend fun sendSmsFrida(sender: String, msg: String) {
  95. val context = getContext()
  96. try {
  97. val dataDir = ContextCompat.getDataDir(context)
  98. Utils.copyAssetFolder(context.assets, "bin", File(dataDir, "bin").path)
  99. val binPath = File(dataDir, "bin/frida-inject-16.3.3-android-arm64").path
  100. val pduBase64 =
  101. String(Base64.getEncoder().encode(createFakeSms(sender, msg)))
  102. Log.i("Modifier", "pduBase64: $pduBase64")
  103. val script = IOUtils.toString(context.assets.open("scripts/sms.js"), "UTF-8")
  104. .replace("{pduBase64}", pduBase64)
  105. val tmpFile: File
  106. withContext(Dispatchers.IO) {
  107. tmpFile = File.createTempFile("script", ".js")
  108. FileUtils.writeStringToFile(tmpFile, script, "UTF-8")
  109. }
  110. val pid = shellRun("pidof com.android.phone").first.trim()
  111. if (!Regex("[0-9]+").matches(pid)) {
  112. return
  113. }
  114. Log.i("Modifier", "sendSms: $binPath -p $pid -s $tmpFile")
  115. val p = withContext(Dispatchers.IO) {
  116. Runtime.getRuntime().exec("su -M")
  117. }
  118. p.outputStream.bufferedWriter().use {
  119. it.write("chmod +x $binPath")
  120. it.newLine()
  121. it.flush()
  122. it.write("$binPath -p $pid -s $tmpFile")
  123. it.newLine()
  124. it.flush()
  125. }
  126. coroutineScope {
  127. launch {
  128. p.errorStream.bufferedReader().useLines { lines ->
  129. lines.forEach {
  130. Log.e("Modifier", it)
  131. }
  132. }
  133. }
  134. launch {
  135. p.inputStream
  136. .bufferedReader()
  137. .useLines { lines ->
  138. lines.forEach {
  139. Log.i("Modifier", it)
  140. if (it == "OK") {
  141. p.inputStream.close()
  142. p.errorStream.close()
  143. p.destroy()
  144. }
  145. }
  146. }
  147. }
  148. }
  149. withContext(Dispatchers.IO) {
  150. p.waitFor()
  151. }
  152. } catch (e: Exception) {
  153. e.printStackTrace()
  154. }
  155. }