system_server.js 8.4 KB

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