/

Erase Action

Action Type: ERASE

Securely deletes sensitive data from your application before terminating the process.

Available for: All platforms (Mobile and Desktop)


How It Works

When the Erase action is triggered, the Monitor framework:

  1. Clears SharedPreferences containing sensitive data
  2. Deletes private database files
  3. Overwrites sensitive files with random data
  4. Terminates the application process
  5. Ensures no trace of sensitive information remains

When to Use

Recommended for:

  • Financial applications handling payment data
  • Applications with personal identifiable information (PII)
  • Apps managing health and medical records
  • Systems storing encryption keys and tokens
  • Any app with proprietary or classified data

Not recommended for:

  • Applications without sensitive data
  • Scenarios where data preservation is required

Configuration

JSON Configuration

JSON
{
  "protections": [
    {
      "type": "DebuggerDetection",
      "action": "erase",
      "interval": 3000
    },
    {
      "type": "MemoryDumpDetection",
      "action": "erase",
      "interval": 3000
    }
  ]
}

Code-Based Configuration (Kotlin)

Kotlin
import com.bytehide.monitor.Monitor
import com.bytehide.monitor.core.action.ActionType
import com.bytehide.monitor.core.protection.ProtectionModuleType

Monitor.configure { config ->
    config.addProtection(
        ProtectionModuleType.DEBUGGER_DETECTION,
        ActionType.ERASE,
        3000
    )

    config.addProtection(
        ProtectionModuleType.MEMORY_DUMP_DETECTION,
        ActionType.ERASE,
        3000
    )
}

Code-Based Configuration (Java)

Java
import com.bytehide.monitor.Monitor;
import com.bytehide.monitor.core.action.ActionType;
import com.bytehide.monitor.core.protection.ProtectionModuleType;

Monitor.configure(config -> {
    config.addProtection(
        ProtectionModuleType.DEBUGGER_DETECTION,
        ActionType.ERASE,
        3000
    );

    config.addProtection(
        ProtectionModuleType.MEMORY_DUMP_DETECTION,
        ActionType.ERASE,
        3000
    );
});

Code Examples

Clearing SharedPreferences (Kotlin)

Before the Erase action terminates your app, implement cleanup for SharedPreferences:

Kotlin
import android.content.Context
import android.content.SharedPreferences

// Clear all SharedPreferences
fun clearAllSharedPreferences(context: Context) {
    val sharedPreferences = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE)
    sharedPreferences.edit().clear().commit()
}

// Clear sensitive data only
fun clearSensitiveData(context: Context) {
    val sharedPreferences = context.getSharedPreferences("secure_data", Context.MODE_PRIVATE)
    val editor = sharedPreferences.edit()
    editor.remove("api_token")
    editor.remove("user_password")
    editor.remove("encryption_key")
    editor.commit()
}

Clearing SharedPreferences (Java)

Java
import android.content.Context;
import android.content.SharedPreferences;

// Clear all SharedPreferences
public static void clearAllSharedPreferences(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
    sharedPreferences.edit().clear().commit();
}

// Clear sensitive data only
public static void clearSensitiveData(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences("secure_data", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.remove("api_token");
    editor.remove("user_password");
    editor.remove("encryption_key");
    editor.commit();
}

Clearing Databases (Kotlin)

Kotlin
import android.content.Context
import android.database.sqlite.SQLiteDatabase

// Clear all database contents
fun clearDatabase(context: Context) {
    val dbPath = context.getDatabasePath("app_database.db").absolutePath
    val database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE)

    database.execSQL("DELETE FROM users")
    database.execSQL("DELETE FROM sessions")
    database.execSQL("DELETE FROM credentials")
    database.execSQL("VACUUM")

    database.close()
}

Clearing Databases (Java)

Java
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

// Clear all database contents
public static void clearDatabase(Context context) {
    String dbPath = context.getDatabasePath("app_database.db").getAbsolutePath();
    SQLiteDatabase database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);

    database.execSQL("DELETE FROM users");
    database.execSQL("DELETE FROM sessions");
    database.execSQL("DELETE FROM credentials");
    database.execSQL("VACUUM");

    database.close();
}

Securely Deleting Files (Kotlin)

Kotlin
import android.content.Context
import java.io.File

// Securely delete a file with data overwriting
fun secureShredFile(file: File) {
    if (file.exists()) {
        val size = file.length()
        // Overwrite with random data
        file.outputStream().use { out ->
            out.write(ByteArray(size.toInt()) { kotlin.random.Random.nextByte() })
        }
        // Delete the file
        file.delete()
    }
}

// Clear cache directory
fun clearCacheDirectory(context: Context) {
    val cacheDir = context.cacheDir
    cacheDir.listFiles()?.forEach { file ->
        secureShredFile(file)
    }
}

// Clear app-specific sensitive files
fun clearAppSensitiveFiles(context: Context) {
    val filesDir = context.filesDir
    filesDir.listFiles()?.forEach { file ->
        if (file.name.contains("sensitive") || file.name.contains("secret")) {
            secureShredFile(file)
        }
    }
}

Securely Deleting Files (Java)

Java
import android.content.Context;
import java.io.File;
import java.util.Random;

// Securely delete a file with data overwriting
public static void secureShredFile(File file) {
    if (file.exists()) {
        long size = file.length();
        try (java.io.FileOutputStream out = new java.io.FileOutputStream(file)) {
            byte[] random = new byte[(int) size];
            new Random().nextBytes(random);
            out.write(random);
        } catch (Exception e) {
            e.printStackTrace();
        }
        file.delete();
    }
}

// Clear cache directory
public static void clearCacheDirectory(Context context) {
    File cacheDir = context.getCacheDir();
    File[] files = cacheDir.listFiles();
    if (files != null) {
        for (File file : files) {
            secureShredFile(file);
        }
    }
}

// Clear app-specific sensitive files
public static void clearAppSensitiveFiles(Context context) {
    File filesDir = context.getFilesDir();
    File[] files = filesDir.listFiles();
    if (files != null) {
        for (File file : files) {
            if (file.getName().contains("sensitive") || file.getName().contains("secret")) {
                secureShredFile(file);
            }
        }
    }
}

Erase with Custom Cleanup (Kotlin)

For maximum control, use the Custom action to implement comprehensive cleanup before termination:

Kotlin
Monitor.configure { config ->
    config.registerCustomAction("erase_with_cleanup") { threat ->
        val context = getApplicationContext() // Your Context instance

        // Clear SharedPreferences
        clearAllSharedPreferences(context)

        // Clear database
        clearDatabase(context)

        // Clear cache and files
        clearCacheDirectory(context)
        clearAppSensitiveFiles(context)

        // Log the security incident
        println("Critical threat detected: ${threat.description}")

        // Terminate the process
        System.exit(1)
    }
}

Best Practices

  • Combine with Logging: Log the threat before erasing to preserve incident details
  • Test Data Wipe: Regularly test your data cleanup implementation
  • Use Secure Methods: Overwrite data multiple times for critical information
  • Document Sensitive Data: Know where all sensitive information is stored
  • Verify Deletion: Confirm that sensitive data is actually deleted, not just marked for deletion
  • Back Up Critical Data: Consider secure backup systems for critical non-sensitive data
  • Multiple Overwrites: For extremely sensitive data, overwrite multiple times with random bytes

Close Action

Terminate the application without data cleanup

Custom Action

Execute comprehensive custom cleanup logic

Log Action

Record incidents before taking action

Previous
Block