packageorg.owasp.mastestappimportandroid.content.Contextimportandroid.util.Logimportjava.io.Fileimportjava.io.FileOutputStreamimportjava.io.IOExceptionclassMastgTest(privatevalcontext:Context){funmastgTest():String{valinternalStorageDir=context.filesDirvalfileName=File(internalStorageDir,"secret.txt")valfileNameOfBackupExcludedFile=File(internalStorageDir,"backup_excluded_secret.txt")valfileContent="secr3tPa\$\$W0rd\n"try{FileOutputStream(fileName).use{output->output.write(fileContent.toByteArray())Log.d("WriteInternalStorage","File written to internal storage successfully.")}FileOutputStream(fileNameOfBackupExcludedFile).use{output->output.write(fileContent.toByteArray())Log.d("WriteInternalStorage","File written to internal storage successfully.")}}catch(e:IOException){Log.e("WriteInternalStorage","Error writing file to internal storage",e)return"ERROR!!\n\nError writing file to internal storage"}return"SUCCESS!!\n\nFiles saved to $internalStorageDir"}}
#!/bin/bash# Default package nameif[-z"$1"];thenecho"No package name provided. Usage: $0 <package_name>"exit1elsepackage_name="$1"fi# Script from https://developer.android.com/identity/data/testingbackup# Initialize and create a backup
adbshellbmgrenabletrue
adbshellbmgrtransportcom.android.localtransport/.LocalTransport|grep-q"Selected transport"||(echo"Error: error selecting local transport";exit1)
adbshellsettingsputsecurebackup_local_transport_parameters'is_encrypted=true'
adbshellbmgrbackupnow"$package_name"|grep-F"Package $package_name with result: Success"||(echo"Backup failed";exit1)# Uninstall and reinstall the app to clear the data and trigger a restoreapk_path_list=$(adbshellpmpath"$package_name")OIFS=$IFSIFS=$'\n'apk_number=0forapk_linein$apk_path_listdo((++apk_number))apk_path=${apk_line:8:1000}adbpull"$apk_path""myapk${apk_number}.apk"doneIFS=$OIFS
adbshellpmuninstall--user0"$package_name"apks=$(seq-f'myapk%.f.apk'1$apk_number)
adbinstall-multiple-t--user0$apks# Clean up
adbshellbmgrtransportcom.google.android.gms/.backup.BackupTransportService
rm$apksecho"Done"
For simplicity, in run.sh we restrict the files to the filesDir directory (/data/user/0/org.owasp.mastestapp/files/ which is equivalent to /data/data/org.owasp.mastestapp/files/).
The run.sh script does the following:
Takes a snapshot of the app data before the backup.
Runs the backup script, which:
backs up the app data.
uninstalls the app.
restores the app data.
Takes a snapshot of the app data after the restore.
Retrieves the list of restored files from the device.
The test fails because secret.txt is restored from the backup and it contains sensitive data.
restored_files/secret.txt
1
secr3tPa$$W0rd
Note that output_after.txt does not contain the backup_excluded_secret.txt file, which is expected as it was marked as exclude in the backup_rules.xml file.