HackTricks
Search…
Pentesting
Powered By GitBook
Smali - Decompiling/[Modifying]/Compiling
Sometimes it is interesting to modify the application code to access hidden information for you (maybe well obfuscated passwords or flags). Then, it could be interesting to decompile the apk, modify the code and recompile it.

Fast Way

Using Visual Studio Code and the APKLab extension, you can automatically decompile, modify, recompile, sign & install the application without executing any command.

Decompile the APK

Using APKTool you can access to the smali code and resources:
1
apktool d APP.apk
Copied!
If apktool gives you any error, try installing the latest version
Some interesting files you should look are:
    res/values/strings.xml (and all xmls inside res/values/*)
    AndroidManifest.xml
    Any file with extension .sqlite or .db
If apktool has problems decoding the application take a look to https://ibotpeaches.github.io/Apktool/documentation/#framework-files or try using the argument -r (Do not decode resources). Then, if the problem was in a resource and not in the source code, you won't have the problem (you won't also decompile the resources).

Change smali code

You can change instructions, change the value of some variables or add new instructions. I change the Smali code using VS Code, you then install the smalise extension and the editor will tell you if any instruction is incorrect. Some examples can be found here:

Recompile the APK

After modifying the code you can recompile the code using:
1
apktool b . #In the folder generated when you decompiled the application
Copied!
It will compile the new APK inside the dist folder.
If apktool throws an error, try installing the latest version****

Sing the new APK

Then, you need to generate a key (you will be asked for a password and for some information that you can fill randomly):
1
keytool -genkey -v -keystore key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias <your-alias>
Copied!
Finally, sign the new APK:
1
jarsigner -keystore key.jks path/to/dist/* <your-alias>
Copied!

Optimize new application

zipalign is an archive alignment tool that provides important optimisation to Android application (APK) files. More information here.
1
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
2
zipalign -v 4 infile.apk
Copied!

Sign the new APK (again?)

If you prefer to use **[apksigner](https://developer.android.com/studio/command-line/apksigner) instead of jarsigner, you should sing the apk after applying the optimization with zipaling. BUT NOTICE THAT YOU ONLY HAVE TO SIGN THE APPLCIATION ONCE** WITH jarsigner (before zipalign) OR WITH aspsigner(after zipaling).
1
apksigner sign --ks key.jks ./dist/mycompiled.apk
Copied!

Modifying Smali

For the following Hello World Java code:
1
public static void printHelloWorld() {
2
System.out.println("Hello World")
3
}
Copied!
The Smali code would be:
1
.method public static printHelloWorld()V
2
.registers 2
3
sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
4
const-string v1, "Hello World"
5
invoke-virtual {v0,v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
6
return-void
7
.end method
Copied!
The Smali instruction set is available here.

Light Changes

Modify initial values of a variable inside a function

Some variables are defined at the beginning of the function using the opcode const, you can modify its values, or you can define new ones:
1
#Number
2
const v9, 0xf4240
3
const/4 v8, 0x1
4
#Strings
5
const-string v5, "wins"
Copied!

Basic Operations

1
#Math
2
add-int/lit8 v0, v2, 0x1 #v2 + 0x1 and save it in v0
3
mul-int v0,v2,0x2 #v2*0x2 and save in v0
4
5
#Move the value of one object into another
6
move v1,v2
7
8
#Condtions
9
if-ge #Greater or equals
10
if-le #Less or equals
11
if-eq #Equals
12
13
#Get/Save attributes of an object
14
iget v0, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Save this.o inside v0
15
iput v0, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Save v0 inside this.o
16
17
#goto
18
:goto_6 #Declare this where you want to start a loop
19
if-ne v0, v9, :goto_6 #If not equals, go to: :goto_6
20
goto :goto_6 #Always go to: :goto_6
Copied!

Bigger Changes

Logging

1
#Log win: <number>
2
iget v5, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Get this.o inside v5
3
invoke-static {v5}, Ljava/lang/String;->valueOf(I)Ljava/lang/String; #Transform number to String
4
move-result-object v1 #Move to v1
5
const-string v5, "wins" #Save "win" inside v5
6
invoke-static {v5, v1}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I #Logging "Wins: <num>"
Copied!
Recommendations:
    If you are going to use declared variables inside the function (declared v0,v1,v2...) put these lines between the .local <number> and the declarations of the variables (const v0, 0x1)
    If you want to put the logging code in the middle of the code of a function:
      Add 2 to the number of declared variables: Ex: from .locals 10 to .locals 12
      The new variables should be the next numbers of the already declared variables (in this example should be v10 and v11, remember that it starts in v0).
      Change the code of the logging function and use v10 and v11 instead of v5 and v1.

Toasting

Remember to add 3 to the number of .locals at the begging of the function.
This code is prepared to be inserted in the middle of a function (change the number of the variables as necessary). It will take the value of this.o, transform it to String and them make a toast with its value.
1
const/4 v10, 0x1
2
const/4 v11, 0x1
3
const/4 v12, 0x1
4
iget v10, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I
5
invoke-static {v10}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;
6
move-result-object v11
7
invoke-static {p0, v11, v12}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
8
move-result-object v12
9
invoke-virtual {v12}, Landroid/widget/Toast;->show()V
Copied!
Last modified 11d ago