JesusFreke / smali

smali/baksmali
6.29k stars 1.07k forks source link

Understanding Rewriting Certain Instructions in Dex File #790

Open kunal-mehta opened 3 years ago

kunal-mehta commented 3 years ago

Hi,

I am creating an android application where I need to mock the BiometricPrompt's authenticate() method and replace it with a custom method which based on the response triggers the existing defined conditions/callbacks in the AuthenticationCallback() method.

What I have done so far is:

  1. Write a basic app that uses BiometricPrompt from Android's version 29 and BiometricManager from Android's version 28.
  2. Generated an APK.
  3. used dexlib2 to generate dex files.
  4. Identified the instructions that I need to replace. These instructions are an instance of DexBackedInstruction35c with an opcode VIRTUAL_INVOKE.

I have tried using DexBackedMethod to update this but doesnt provide me with the expected result.

Trying to follow, https://gist.github.com/JesusFreke/6945806 example, My methodNeedsModification looks like this:

private static boolean methodNeedsModification(@Nonnull MethodImplementation implementation) {
        for (Instruction instruction: implementation.getInstructions()) {
            if (instruction instanceof DexBackedInstruction35c && instruction.getOpcode() == Opcode.INVOKE_VIRTUAL){
                String biometricPrompt = ((DexBackedInstruction35c)instruction).getReference().toString();
                if(biometricPrompt.contains("BiometricPrompt") && biometricPrompt.contains("authenticate")){
                    System.out.println(biometricPrompt);
                    return true;
                }
            }
        }
        return false;
    }

Sample MethodInstructions to be updated:

Landroid/hardware/biometrics/BiometricPrompt;->authenticate(Landroid/os/CancellationSignal;Ljava/util/concurrent/Executor;Landroid/hardware/biometrics/BiometricPrompt$AuthenticationCallback;)V
here
Lcom/example/biometricswizzlespoc/BiometricApi29;->authenticate(Landroidx/biometric/BiometricPrompt$PromptInfo;)V
here

How do I update/replace this instruction in modifyMethod ?