如何使用Frida获取ANDROID_ID

时间:2019-02-21 10:19:18

标签: java android frida

ANDROID_ID在Android中的每个应用程序中都是唯一的。

要在Android内的Java中使用ANDROID_ID,请使用以下代码:

import android.content.Context;
import android.content.ContentResolver;
import android.provider.Settings;
protected void onCreate(...) {
  context = (Context)this;
  String androidId = Settings.Secure.getString((ContentResolver)context.getContentResolver(), (String)"android_id");
}

但是我想在我的手机上的其他应用程序中运行它。
我想为此使用Frida

我正在用Python加载注入的脚本:

import frida
device = frida.get_usb_device()
pid = device.spawn(["com.target.app"])
device.resume(pid)
time.sleep(1) #Without it Java.perform silently fails
session = device.attach(pid)
script = session.create_script(open("jsfrida.js").read())
script.load()

#prevent the python script from terminating
raw_input()

但是在我的脚本中,我不知道如何调用它,这是我尝试过的:

Java.perform(function (){
  console.log("Inside java perform function");
  var ActivityThread = Java.use('android.app.ActivityThread');
  var Context = Java.use('android.content.Context');
  var settings = Java.use('android.provider.Settings.Secure');
  var ctx = Java.cast(ActivityThread.currentApplication().getApplicationContext(), Context);
  //console.log(ctx.getPackageName());
  //console.log(ctx.getContentResolver());
  var androidId = settings.Secure.getString(ctx.getContentResolver(), "android_id");
  console.log(androidId);
  console.log("END");
 });

但是它不会打印androidId,它只会打印:

Script loaded successfully 
Inside java perform function

1 个答案:

答案 0 :(得分:2)

  • Secure是内部类,因此您需要使用$
  • 要获取CotentResolver,您可以调用getContentResolver而无需强制转换。
$ frida -Uf com.app.example --no-pause
     ____
    / _  |   Frida 12.1.2 - A world-class dynamic instrumentation toolkit
   | (_| |
    > _  |   Commands:
   /_/ |_|       help      -> Displays the help system
   . . . .       object?   -> Display information about 'object'
   . . . .       exit/quit -> Exit
   . . . .
   . . . .   More info at http://www.frida.re/docs/home/
Spawned `com.app.example`. Resuming main thread!                    
[Android::com.app.example]-> 
function getContext() {
  return Java.use('android.app.ActivityThread').currentApplication().getApplicationContext().getContentResolver();
}                                         
function logAndroidId() {
  console.log('[?]', Java.use('android.provider.Settings$Secure').getString(getContext(), 'android_id'));
}
undefined
[Android::com.app.example]-> Java.perform(logAndroidId)
[?] 52d1497b52bf8a11
undefined
[Android::com.app.example]->