system_server.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. function trace(tag) {
  2. Log.e((tag || '') + Java.use('android.util.Log').getStackTraceString(Java.use('java.lang.Throwable').$new()))
  3. }
  4. function randomMac() {
  5. var mac = '00:16:3e'
  6. for (var i = 0; i < 3; i++) {
  7. mac += ':' + ('00' + Math.floor(Math.random() * 256).toString(16)).slice(-2)
  8. }
  9. return mac
  10. }
  11. function buff2json(buf) {
  12. console.log(`buffer length: ${buf.byteLength}`)
  13. try {
  14. var decoded = String.fromCharCode(...new Uint8Array(buf))
  15. console.log(`decoded: ${decoded}`)
  16. return JSON.parse(decoded.trim())
  17. } catch (e) {
  18. console.error(e)
  19. return null
  20. }
  21. }
  22. class Interaction {
  23. failure(err) {
  24. console.error(err.message)
  25. }
  26. accepted(connection) {
  27. console.warn('accepted')
  28. connection.input.read(2000).then((data) => {
  29. try {
  30. const json = buff2json(data)
  31. console.log('received', json)
  32. this.messageFn && this.messageFn(json)
  33. } catch (e) {}
  34. connection.close()
  35. })
  36. }
  37. accept_loop(listener) {
  38. var next_iter = this.accept_loop.bind(this, listener)
  39. listener
  40. .accept()
  41. .then(this.accepted.bind(this))
  42. .catch(this.failure.bind(this))
  43. .finally(function () {
  44. setImmediate(next_iter)
  45. })
  46. }
  47. listened(listener) {
  48. console.warn('listened')
  49. this.accept_loop(listener)
  50. }
  51. start(port, messageFn) {
  52. this.messageFn = messageFn
  53. console.warn('starting on port', port)
  54. Socket.listen({ family: 'ipv4', host: '0.0.0.0', port: port })
  55. .then(this.listened.bind(this))
  56. .catch(this.failure.bind(this))
  57. }
  58. }
  59. setImmediate(() => {
  60. Java.perform(function () {
  61. const Log = Java.use('android.util.Log')
  62. const Uri = Java.use('android.net.Uri')
  63. const File = Java.use('java.io.File')
  64. const BufferedReader = Java.use('java.io.BufferedReader')
  65. const FileInputStream = Java.use('java.io.FileInputStream')
  66. const FileOutputStream = Java.use('java.io.FileOutputStream')
  67. const InputStreamReader = Java.use('java.io.InputStreamReader')
  68. const OutputStreamWriter = Java.use('java.io.OutputStreamWriter')
  69. function log(msg) {
  70. console.log(`\x1b[32m[system_server] ${msg}\x1b[0m`)
  71. Log.d('frida-system_server', msg + '')
  72. }
  73. function getContext() {
  74. try {
  75. var ActivityThread = Java.use('android.app.ActivityThread')
  76. var application = ActivityThread.currentApplication()
  77. return application.getApplicationContext()
  78. } catch (e) {
  79. console.log(e)
  80. return null
  81. }
  82. }
  83. function readFile(file) {
  84. if (!file.exists()) {
  85. return null
  86. }
  87. var fileInputStream = FileInputStream.$new(file)
  88. var inputStreamReader = InputStreamReader.$new(Java.cast(fileInputStream, Java.use('java.io.InputStream')))
  89. var bufferedReader = BufferedReader.$new(inputStreamReader)
  90. var line
  91. var content = ''
  92. while ((line = bufferedReader.readLine()) !== null) {
  93. content += line + '\n'
  94. }
  95. bufferedReader.close()
  96. inputStreamReader.close()
  97. fileInputStream.close()
  98. return content
  99. }
  100. function writeFile(file, content) {
  101. if (!file.exists()) {
  102. file.createNewFile()
  103. }
  104. var fileOutputStream = FileOutputStream.$new(file)
  105. var outputStreamWriter = OutputStreamWriter.$new(
  106. Java.cast(fileOutputStream, Java.use('java.io.OutputStream'))
  107. )
  108. outputStreamWriter.write(content, 0, content.length)
  109. outputStreamWriter.flush()
  110. outputStreamWriter.close()
  111. fileOutputStream.close()
  112. }
  113. function readConfig() {
  114. const configFile = File.$new('/data/system/config.json')
  115. log(`read config from ${configFile.getAbsolutePath()}`)
  116. const json = readFile(configFile)
  117. if (!json) {
  118. return {}
  119. } else {
  120. log(`config: ${json}`)
  121. return JSON.parse(json)
  122. }
  123. }
  124. function saveConfig(config) {
  125. const configFile = File.$new('/data/system/config.json')
  126. log(`save config to ${configFile.getAbsolutePath()}`)
  127. const json = JSON.stringify(config)
  128. log(`config: ${json}`)
  129. writeFile(configFile, json)
  130. }
  131. function queryConfig(key) {
  132. const context = getContext()
  133. if (!context) {
  134. return null
  135. }
  136. const cr = context.getContentResolver()
  137. const uri = Uri.parse('content://SimInfo')
  138. const cursor = cr.query(uri, null, null, null, null)
  139. if (!cursor) {
  140. return null
  141. }
  142. if (!cursor.moveToFirst()) {
  143. cursor.close()
  144. return null
  145. }
  146. const idx = cursor.getColumnIndex(key)
  147. if (idx < 0) {
  148. cursor.close()
  149. return null
  150. }
  151. const value = cursor.getString(idx)
  152. cursor.close()
  153. return value
  154. }
  155. // saveConfig({ a: 1 })
  156. let config = readConfig()
  157. new Interaction().start(23947, (msg) => {
  158. console.log('on message', msg)
  159. if (msg.action === 'saveConfig') {
  160. config = msg.data
  161. saveConfig(config)
  162. }
  163. })
  164. const DeviceIdentifiersPolicy = Java.use(
  165. 'com.android.server.os.DeviceIdentifiersPolicyService$DeviceIdentifiersPolicy'
  166. )
  167. DeviceIdentifiersPolicy.getSerial.overload().implementation = function () {
  168. const original = this.getSerial()
  169. const spoof = config.serialNo || original
  170. log(`DeviceIdentifiersPolicy.getSerial() called, returning: ${spoof}, original: ${original}`)
  171. return spoof
  172. }
  173. DeviceIdentifiersPolicy.getSerialForPackage.overload('java.lang.String', 'java.lang.String').implementation =
  174. function (callingPackage, callingFeatureId) {
  175. const original = this.getSerialForPackage(callingPackage, callingFeatureId)
  176. const spoof = config.serialNo || original
  177. log(`DeviceIdentifiersPolicy.getSerialForPackage(${callingPackage}, ${callingFeatureId}) called`)
  178. log(` ${original} -> ${spoof}`)
  179. return spoof
  180. }
  181. const classLoaders = Java.enumerateClassLoadersSync()
  182. const wifiClassLoader = classLoaders.find((i) => i.toString().includes('wifi'))
  183. Java.classFactory.loader = wifiClassLoader
  184. const WifiServiceImpl = Java.use('com.android.server.wifi.WifiServiceImpl')
  185. WifiServiceImpl.getFactoryMacAddresses.overload().implementation = function () {
  186. const original = this.getFactoryMacAddresses()
  187. const spoof = [config.mac || randomMac()]
  188. log(`WifiServiceImpl.getFactoryMacAddresses() called`)
  189. log(` ${original} -> ${spoof}`)
  190. return spoof
  191. }
  192. WifiServiceImpl.getConnectionInfo.overload('java.lang.String', 'java.lang.String').implementation = function (
  193. callingPackage,
  194. callingFeatureId
  195. ) {
  196. const original = this.getConnectionInfo(callingPackage, callingFeatureId)
  197. const originalMac = original.getMacAddress()
  198. const originalBSSID = original.getBSSID()
  199. const spoofedMac = config.mac || randomMac()
  200. const spoofedBSSID = config.bssid || randomMac()
  201. original.setMacAddress(spoofedMac)
  202. original.setBSSID(spoofedBSSID)
  203. log(`WifiServiceImpl.getConnectionInfo(${callingPackage}, ${callingFeatureId}) called`)
  204. log(` MAC: ${originalMac} -> ${spoofedMac}`)
  205. log(` BSSID: ${originalBSSID} -> ${spoofedBSSID}`)
  206. return original
  207. }
  208. })
  209. })