Sanitize personal Android devices

Andrea Dainese
July 09, 2022
Post cover

Following an idea of Micheal Bazzel , I decided to write a short procedure to remove unwanted software from Android smartphones. It’s not only about cleaning the phone, it’s about privacy. My old approach required a rootable phone, this one doesn’t and doesn’t affect the warranty.

Uninstalled packages will be kept in the ROM, but they are removed from the running OS.

Be aware: you should not remove any software you suspect because you are probably making your phone unusable and only a hard reset will fix it.

Connect your phone

Install Android Debug Bridge (ADB) on your platform. The following commands work on Raspberry Pi OS:

sudo apt install android-tools-adb

Now enable USB Debugging mode. Go to Settings > About Phone > Software Information and click 7 times on Build Number, then go to Settings > Developer Options > Enable > USB Debugging and enable it.

Connect the phone via USB cable to your computer and restart ADB:

adb kill-server
adb devices
- daemon not running; starting now at tcp:5037
- daemon started successfully
List of devices attached
520011a1a0123143        device

Let’s get the ID of the current user (usually 0):

adb shell pm list users
Users:
        UserInfo{0:+39 333 000 1234:13} running

Now we can:

  • list installed and uninstalled packages (list packages);
  • uninstall a package (uninstall);
  • disable a package (pm disable-user):
  • reinstall a package (install-existing);
  • enable a package (enable).

Android sanitization

Let’s find some unwanted packages, for example, let’s assume we want to remove some social networks apps:

adb shell pm list packages --user 0 | egrep -i "linkedin|microsoft" | cut -d ":" -f2 | sort
com.facebook.appmanager
com.facebook.katana
com.facebook.services
com.facebook.system
com.linkedin.android

Then uninstall them one by one:

adb shell pm uninstall -k --user 0 com.linkedin.android

Or, maybe, we want to script our task:

PKGS_TO_UNINSTALL=$(adb shell pm list packages --user 0 | egrep -i "linkedin|microsoft" | cut -d ":" -f2)
for P in $PKGS_TO_UNINSTALL; do adb shell pm uninstall -k --user 0 $P; done

Disabling an app marks it unavailable (which can easily be reverted), while uninstalling physically removes the app and all connected data from the device. The flag -k keep the data and cache directories around after package removal.

Restoring a critical app

In my test I removed some critical apps. We can restore them without issues:

adb shell cmd package install-existing com.linkedin.android
adb shell pm enable com.linkedin.android

Finding app info

Besides some famous apps, on most of them, we have to find some additional information. I automated this long task by writing a simple Python scripts:

#!/usr/bin/env python3

import requests
from lxml.html import fromstring

with open('package_list.txt') as f:
    lines = f.readlines()

print("Package,Name,Author,Category")
for line in lines:
    package_name = line.strip()
    package_title = 'N/A'
    package_author = 'N/A'
    package_category = 'N/A'
    url = f'https://apkcombo.com/en/visionprovider/{package_name}/'

    r = requests.get(url)
    if r.status_code == 200:
        tree = fromstring(r.content)
        information_table = tree.xpath("//div[@class='information-table']").pop()
        package_title = tree.findtext('.//title').split(' APK ')[0]
        try:
            package_author = information_table.xpath("//a[contains(@href,'developer')]")[0].text
        except:
            pass
        try:
            package_category = information_table.xpath("//a[contains(@href,'category')]")[3].text
        except:
            pass

    print(f'{package_name},{package_title},{package_author},{package_category}')

We can now lookup app details for each installed app:

sudo apt install python3-pip
pip3 install lxml -U
adb shell pm list packages -u | cut -d":" -f2 > package_list.txt
./app_resolver.py > package_list.csv

Finding the uninstalled app

Finally, we want to save the list of uninstalled apps. If we used the above commands and flags, we can list installed packages, list installed and uninstalled packages, and compare them:

adb shell pm list packages > a
adb shell pm list packages -u > b
diff a b | grep "^>" | cut -d":" -f2 | sort

From my testing devices I removed:

PackageNameAuthorCategory
com.android.emailHUAWEI EmailHUAWEITools
com.android.mediacenterHuawei MusicHuawei Internet ServicesTools
com.example.android.notepadNotePadXiaomi Inc.Tools
com.facebook.appmanagerN/AN/AN/A
com.facebook.katanaFacebookMeta PlatformsInc.
com.facebook.servicesFacebook ServicesSamsung Electronics Co., Ltd.Tools
com.facebook.systemFacebook App InstallerSamsung Electronics Co., Ltd.Tools
com.google.android.apps.docsGoogle DriveGoogle LLCProductivity
com.google.android.apps.photosGoogle PhotosGoogle LLCPhotography
com.google.android.apps.tachyonGoogle DuoGoogle LLCCommunication
com.google.android.musicGoogle Play MusicGoogle LLCMusic & Audio
com.google.android.videosGoogle TVGoogle LLCVideo Players & Editors
com.google.android.youtubeYouTubeGoogle LLCVideo Players & Editors
com.huawei.android.mirrorshareWireless projectionHUAWEITools
com.huawei.android.totemweatherHUAWEI WeatherHuaweiTools
com.huawei.appmarketHuawei AppGalleryHuaweiTools
com.huawei.compassHuawei CompassUploaderTools
com.huawei.hidiskHuawei File ManagerHuawei Internet ServicesTools
com.huawei.hwdetectrepairSmart diagnosisHuawei Internet ServiceTools
com.huawei.hwidHuawei Mobile ServicesHuawei Internet ServicesTools
com.huawei.mirrorN/AN/AN/A
com.huawei.phoneserviceHiCareHuawei Internet ServicesTools
com.huawei.searchHUAWEI HiSearchHUAWEITools
com.huawei.systemmanagerHUAWEI OptimizerHUAWEITools
com.huawei.vassistantHiVoiceHUAWEITools
com.linkedin.androidLinkedInLinkedInBusiness
com.microsoft.office.excelMicrosoft ExcelMicrosoft CorporationProductivity
com.microsoft.office.powerpointMicrosoft PowerPointMicrosoft CorporationProductivity
com.microsoft.office.wordMicrosoft WordMicrosoft CorporationProductivity
com.microsoft.skydriveMicrosoft OneDriveMicrosoft CorporationProductivity
com.microsoft.translatorMicrosoft TranslatorMicrosoft CorporationProductivity
com.nuance.swype.emuiSwype for HuaweiNuance Communications, IncTools
com.samsung.android.app.spageBixby HomeSamsung Electronics Co., Ltd.Tools
com.samsung.android.bbc.fileproviderKnoxBBCProviderSamsung Electronics Co., Ltd.Tools
com.samsung.android.loolSamsung Device CareSamsung Electronics Co., Ltd.Tools
com.samsung.knox.securefolderSecure FolderSamsung Electronics Co., Ltd.Business
com.samsung.android.scloudSamsung CloudSamsung Electronics Co., Ltd.Tools
com.samsung.android.securitylogagentSecurityLogAgentSamsung Electronics Co., Ltd.Tools
com.samsung.android.visionintelligenceBixby VisionSamsung Electronics Co., Ltd.Tools
com.sec.android.app.samsungappsSamsung Galaxy StoreSamsung Electronics Co. LtdEntertainment
com.sec.enterprise.knox.shareddevice.keyguardSharedDeviceKeyguardSamsung Electronics Co., Ltd.Tools
com.sec.spp.pushSamsung Push ServiceSamsung Electronics Co., Ltd.Communication
com.topjohnwu.magiskMagisktopjohnwuTools
de.axelspringer.yana.zeropageupdayupday GmbH & Co. KGNews & Magazines
me.twrp.twrpappOfficial TWRP AppTeam Win LLCTools

Conclusions

I honestly hate bloatware. They impact my privacy by tracking and stealing data. I think that this is a good method to disable unwanted software. It’s not a perfect method because we will never know what exactly is installed in our phones, but it’s an easy and affordable method for many people.

There are other methods, but they are for advanced users.

References