phone.js 6.3 KB

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