onCreate(...)
method in the activity with which the application starts to run.apktool.jar
extracting the apktool.jar
file from the archive into any folder.onCreate(...)
method of this activity. The application starts and immediately after calling the constructor of this activity, the onCreate(...)
method will be called. As a result, management will end up in our endless loop. While the cycle is spinning there, we will leisurely attach the debugger to the running application, set a breakpoint immediately after our infinite loop, and then take advantage of the debugger’s capabilities and make control of this loop out. And immediately got on our breakpoint. As you can see, everything is elementary.temp
directory using the Apk-tool. Do not use the -d
option: java -jar apktool.jar d my.app.apk temp
temp/smali
directory you will have a bunch of .smali files.temp/AndroidManifest.xml
file, find the activity from <intent -filter="-filter"> <action android:name="android.intent.action.MAIN"> <category android:name="android.intent.category.LAUNCHER"> </intent>
android.app.Activity
class). Search in the depth directory temp/smali
.onCreate(...)
method in this class and immediately after the call (usually this call goes right at the beginning) invoke-super {p0, p1}, Landroid/app/Activity;->onCreate(Landroid/os/Bundle;)V
onCreate(...)
enter the following code: :debug sget v0, Lmy/activity/class/MyActivity;->debugFlag:I if-nez v0, :debug
MyActivity
, the real name activity should be used, and instead of v0
in this code you can use any suitable local register. If there is no suitable register, add it by editing the directives .locals
and / or .registers
accordingly. .field static debugFlag:I = 0x01
temp
directory back to your .apk file, again without the -d
option: java -jar apktool.jar b temp my.app.apk
my.app.apk
should be my.app.apk
somewhere before.my.app.apk
. In the beginning of the onCreate(...)
method in the class of the activity with which the application starts, we entered an infinite loop. Well, take this patched my.app.apk
and follow the step-by-step instructions from my yesterday’s article (see the Debug section). Note that at the ninth step of this instruction, after you start the application, you will see a black screen. This is normal, it should be so! This simply means that immediately after launching the application, our patched onCreate(...)
method was called and the control fell into our endless loop. If after some time, Android prompts you to close the application because it does not respond - refuse and go on strictly according to the instructions!onCreate(...)
method you patched. Use the pause button on the NetBeans debug panel. Then in this open .java file, put the breakpoint on the first instruction after the code of the infinite loop that you entered in onCreate(...)
. Then, using the function of viewing and editing variables in the NetBeans debugger, change the value of the debugFlag
field to 0
and click on the “continue debugging” button on the debugging panel in NetBeans. The control will exit from an infinite loop and will immediately reach your breakpoint.onCreate(...)
method!android.os.Debug.waitForDebugger()
method for the same purposes for which we use an infinite loop in this article. And this same reader is probably surprised that we have built up a garden with a cycle here, although it would have been easy to write the call of just one static method to the beginning of our onCreate(...)
: invoke-static {}, Landroid/os/Debug;->waitForDebugger()V
android.os.Debug.waitForDebugger()
does not always work and not for everyone. For many (including me) right after the call to android.os.Debug.waitForDebugger()
application really “freezes” and waits for the debugger to join it. This can be seen even in DDMS - a small red beetle icon appears opposite the application. But as soon as we attach the debugger to the application, control immediately passes to the next instruction after android.os.Debug.waitForDebugger()
and the application starts running further without stopping. We just do not have time to set the breakpoint after android.os.Debug.waitForDebugger()
. For a discussion of this, see, for example, here .android.os.Debug.waitForDebugger()
for someone, and for someone, I don't know yet. Maybe in the comments someone will give an explanation about this. Also in the comments you can and should ask questions about the article. I will try to respond as quickly as possible, but if I’m stupid, please be patient. I will try to answer all.Source: https://habr.com/ru/post/150862/
All Articles