phone.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. function trace(tag) {
  2. Log.e((tag || '') + Java.use('android.util.Log').getStackTraceString(Java.use('java.lang.Throwable').$new()))
  3. }
  4. function buff2json(buf) {
  5. console.log(`buffer length: ${buf.byteLength}`)
  6. try {
  7. var decoded = String.fromCharCode(...new Uint8Array(buf))
  8. console.log(`decoded: ${decoded}`)
  9. return JSON.parse(decoded.trim())
  10. } catch (e) {
  11. console.error(e)
  12. return null
  13. }
  14. }
  15. class Interaction {
  16. failure(err) {
  17. console.error(err.message)
  18. Java.use('android.util.Log').d('frida-phone', err.message)
  19. }
  20. accepted(connection) {
  21. console.warn('accepted')
  22. connection.input.read(2000).then((data) => {
  23. try {
  24. const json = buff2json(data)
  25. console.log('received', json)
  26. this.messageFn && this.messageFn(json)
  27. } catch (e) {}
  28. connection.close()
  29. })
  30. }
  31. accept_loop(listener) {
  32. var next_iter = this.accept_loop.bind(this, listener)
  33. listener
  34. .accept()
  35. .then(this.accepted.bind(this))
  36. .catch(this.failure.bind(this))
  37. .finally(function () {
  38. setImmediate(next_iter)
  39. })
  40. }
  41. listened(listener) {
  42. console.warn('listened')
  43. this.accept_loop(listener)
  44. }
  45. start(port, messageFn) {
  46. this.messageFn = messageFn
  47. console.warn('starting on port', port)
  48. Socket.listen({ family: 'ipv4', host: '0.0.0.0', port: port })
  49. .then(this.listened.bind(this))
  50. .catch(this.failure.bind(this))
  51. }
  52. }
  53. setImmediate(() => {
  54. Java.perform(function () {
  55. const Log = Java.use('android.util.Log')
  56. const Uri = Java.use('android.net.Uri')
  57. const File = Java.use('java.io.File')
  58. const BufferedReader = Java.use('java.io.BufferedReader')
  59. const FileInputStream = Java.use('java.io.FileInputStream')
  60. const FileOutputStream = Java.use('java.io.FileOutputStream')
  61. const InputStreamReader = Java.use('java.io.InputStreamReader')
  62. const OutputStreamWriter = Java.use('java.io.OutputStreamWriter')
  63. function log(msg) {
  64. console.log(`\x1b[32m[system_server] ${msg}\x1b[0m`)
  65. Log.d('frida-phone', msg + '')
  66. }
  67. function getContext() {
  68. try {
  69. var ActivityThread = Java.use('android.app.ActivityThread')
  70. var application = ActivityThread.currentApplication()
  71. return application.getApplicationContext()
  72. } catch (e) {
  73. console.log(e)
  74. return null
  75. }
  76. }
  77. function readFile(file) {
  78. if (!file.exists()) {
  79. return null
  80. }
  81. var fileInputStream = FileInputStream.$new(file)
  82. var inputStreamReader = InputStreamReader.$new(Java.cast(fileInputStream, Java.use('java.io.InputStream')))
  83. var bufferedReader = BufferedReader.$new(inputStreamReader)
  84. var line
  85. var content = ''
  86. while ((line = bufferedReader.readLine()) !== null) {
  87. content += line + '\n'
  88. }
  89. bufferedReader.close()
  90. inputStreamReader.close()
  91. fileInputStream.close()
  92. return content
  93. }
  94. function writeFile(file, content) {
  95. if (!file.exists()) {
  96. file.createNewFile()
  97. }
  98. var fileOutputStream = FileOutputStream.$new(file)
  99. var outputStreamWriter = OutputStreamWriter.$new(
  100. Java.cast(fileOutputStream, Java.use('java.io.OutputStream'))
  101. )
  102. outputStreamWriter.write(content, 0, content.length)
  103. outputStreamWriter.flush()
  104. outputStreamWriter.close()
  105. fileOutputStream.close()
  106. }
  107. function readConfig() {
  108. const context = getContext()
  109. const configFile = File.$new(context.getExternalFilesDir('config'), 'config.json')
  110. log(`read config from ${configFile.getAbsolutePath()}`)
  111. const json = readFile(configFile)
  112. if (!json) {
  113. return {}
  114. } else {
  115. log(`config: ${json}`)
  116. return JSON.parse(json)
  117. }
  118. }
  119. function saveConfig(config) {
  120. const context = getContext()
  121. const configFile = File.$new(context.getExternalFilesDir('config'), 'config.json')
  122. log(`save config to ${configFile.getAbsolutePath()}`)
  123. const json = JSON.stringify(config)
  124. log(`config: ${json}`)
  125. writeFile(configFile, json)
  126. }
  127. function queryConfig(key) {
  128. const context = getContext()
  129. if (!context) {
  130. return null
  131. }
  132. const cr = context.getContentResolver()
  133. const uri = Uri.parse('content://SimInfo')
  134. const cursor = cr.query(uri, null, null, null, null)
  135. if (!cursor) {
  136. return null
  137. }
  138. if (!cursor.moveToFirst()) {
  139. cursor.close()
  140. return null
  141. }
  142. const idx = cursor.getColumnIndex(key)
  143. if (idx < 0) {
  144. cursor.close()
  145. return null
  146. }
  147. const value = cursor.getString(idx)
  148. cursor.close()
  149. return value
  150. }
  151. // log(readConfig('serial_no'))
  152. // setTimeout(() => {
  153. // new Interaction().start(23946, (msg) => {
  154. // log(`received message: ${JSON.stringify(msg)}`)
  155. // if (msg.action === 'saveConfig') {
  156. // config = msg.data
  157. // saveConfig(config)
  158. // }
  159. // })
  160. // }, 30000)
  161. const PhoneInterfaceManager = Java.use('com.android.phone.PhoneInterfaceManager')
  162. PhoneInterfaceManager.getImeiForSlot.overload('int', 'java.lang.String', 'java.lang.String').implementation =
  163. function (slotId, callingPackage, callingFeatureId) {
  164. const original = this.getImeiForSlot(slotId, callingPackage, callingFeatureId)
  165. const spoofed = readConfig().imei || original
  166. log(`PhoneInterfaceManager.getImeiForSlot(${slotId}, ${callingPackage}, ${callingFeatureId}) called`)
  167. log(` ${original} -> ${spoofed}`)
  168. return spoofed
  169. }
  170. })
  171. })