connect.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. for (let i of classLoaders) {
  178. if (i.toString().includes('service-connectivity')) {
  179. log(i)
  180. Java.classFactory.loader = i
  181. }
  182. }
  183. const ConnectivityService = Java.use('com.android.server.ConnectivityService')
  184. log(ConnectivityService)
  185. // ConnectivityService.getActiveNetworkInfo.overload().implementation = function () {
  186. // log('getActiveNetworkInfo')
  187. // const res = this.getActiveNetworkInfo()
  188. // log(res)
  189. // return res
  190. // }
  191. ConnectivityService.getNetworkInfo.overload('int').implementation = function (networkType) {
  192. log('getNetworkInfo')
  193. const res = this.getNetworkInfo(networkType)
  194. log(res)
  195. return res
  196. }
  197. const InterfaceParams = Java.use('android.net.connectivity.com.android.net.module.util.InterfaceParams')
  198. InterfaceParams.getByName.overload('java.lang.String').implementation = function (name) {
  199. log('getByName')
  200. const res = this.getByName(name)
  201. log(res)
  202. return res
  203. }
  204. InterfaceParams.getMacAddress.overload('java.net.NetworkInterface').implementation = function (
  205. networkInterface
  206. ) {
  207. log('getMacAddress')
  208. const res = this.getMacAddress(networkInterface)
  209. log(res)
  210. return res
  211. }
  212. const NetworkInterface = Java.use('java.net.NetworkInterface')
  213. NetworkInterface.$init.overload().implementation = function () {
  214. log('NetworkInterface')
  215. const res = this.$new()
  216. log(res)
  217. return res
  218. }
  219. NetworkInterface.$init.overload('java.lang.String', 'int', '[Ljava.net.InetAddress;').implementation =
  220. function (name, index, addrs) {
  221. log('NetworkInterface')
  222. const res = this.$new(name, index, addrs)
  223. log(res)
  224. return res
  225. }
  226. const wifiClassLoader = classLoaders.find((i) => i.toString().includes('wifi'))
  227. Java.classFactory.loader = wifiClassLoader
  228. const WifiServiceImpl = Java.use('com.android.server.wifi.WifiServiceImpl')
  229. WifiServiceImpl.getFactoryMacAddresses.overload().implementation = function () {
  230. const original = this.getFactoryMacAddresses()
  231. const spoof = [readConfig().mac || randomMac()]
  232. log(`WifiServiceImpl.getFactoryMacAddresses() called`)
  233. log(` ${original} -> ${spoof}`)
  234. return spoof
  235. }
  236. WifiServiceImpl.getConnectionInfo.overload('java.lang.String', 'java.lang.String').implementation = function (
  237. callingPackage,
  238. callingFeatureId
  239. ) {
  240. const original = this.getConnectionInfo(callingPackage, callingFeatureId)
  241. const originalMac = original.getMacAddress()
  242. const originalBSSID = original.getBSSID()
  243. const spoofedMac = readConfig().mac || randomMac()
  244. const spoofedBSSID = readConfig().bssid || randomMac()
  245. original.setMacAddress(spoofedMac)
  246. original.setBSSID(spoofedBSSID)
  247. log(`WifiServiceImpl.getConnectionInfo(${callingPackage}, ${callingFeatureId}) called`)
  248. log(` MAC: ${originalMac} -> ${spoofedMac}`)
  249. log(` BSSID: ${originalBSSID} -> ${spoofedBSSID}`)
  250. return original
  251. }
  252. const btClassLoader = classLoaders.find((i) => i.toString().includes('service-bluetooth'))
  253. Java.classFactory.loader = btClassLoader
  254. const BluetoothManagerService = Java.use('com.android.server.bluetooth.BluetoothManagerService')
  255. BluetoothManagerService.getAddress.overload('android.content.AttributionSource').implementation = function (
  256. source
  257. ) {
  258. const res = this.getAddress(source)
  259. log(`BluetoothManagerService.getAddress() called: packageName:${source.getPackageName()} -> ${res}`)
  260. return res
  261. }
  262. })
  263. })