Smali - Decompiling/[Modifying]/Compiling

Reading time: 6 minutes

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Wakati mwingine ni ya kuvutia kubadilisha msimbo wa programu ili kupata habari zilizofichwa kwako (labda nywila au bendera zilizofichwa vizuri). Kisha, inaweza kuwa ya kuvutia decompile apk, badilisha msimbo na ucompile tena.

Marejeleo ya Opcodes: http://pallergabor.uw.hu/androidblog/dalvik_opcodes.html

Njia ya Haraka

Kwa kutumia Visual Studio Code na nyongeza ya APKLab, unaweza decompile kiotomatiki, badilisha, compile tena, saini na kusakinisha programu bila kutekeleza amri yoyote.

Script nyingine inayorahisisha kazi hii sana ni https://github.com/ax/apk.sh

Decompile APK

Kwa kutumia APKTool unaweza kufikia msimbo wa smali na rasilimali:

bash
apktool d APP.apk

Ikiwa apktool inakupa makosa yoyote, jaribu kusanidi toleo jipya zaidi

Baadhi ya faili za kuvutia unapaswa kuangalia ni:

  • res/values/strings.xml (na xml zote ndani ya res/values/*)
  • AndroidManifest.xml
  • Faili yoyote yenye kiendelezi .sqlite au .db

Ikiwa apktool ina shida katika kufafanua programu, angalia https://ibotpeaches.github.io/Apktool/documentation/#framework-files au jaribu kutumia hoja -r (Usifafanue rasilimali). Kisha, ikiwa shida ilikuwa katika rasilimali na si katika msimbo wa chanzo, hutakuwa na shida hiyo (hutaweza pia kufafanua rasilimali).

Badilisha msimbo wa smali

Unaweza kubadilisha maagizo, kubadilisha thamani ya baadhi ya mabadiliko au kuongeza maagizo mapya. Ninabadilisha msimbo wa Smali kwa kutumia VS Code, kisha unasanidi smalise extension na mhariri atakuambia ikiwa kuna agizo lolote lililo sahihi.
Baadhi ya esemples zinaweza kupatikana hapa:

Au unaweza kuangalia hapa chini baadhi ya mabadiliko ya Smali yaliyoelezewa.

Recompile APK

Baada ya kubadilisha msimbo unaweza kurekebisha msimbo kwa kutumia:

bash
apktool b . #In the folder generated when you decompiled the application

Itakuwa nafasi ya kuunda APK mpya ndani ya folda dist.

Ikiwa apktool itatoa makosa, jaribu kufunga toleo jipya

Saini APK mpya

Kisha, unahitaji kuunda funguo (utahitaji kupewa nenosiri na taarifa zingine ambazo unaweza kujaza kwa bahati):

bash
keytool -genkey -v -keystore key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias <your-alias>

Hatimaye, saini APK mpya:

bash
jarsigner -keystore key.jks path/to/dist/* <your-alias>

Optimize new application

zipalign ni chombo cha kuoanisha archive ambacho kinatoa uboreshaji muhimu kwa faili za programu za Android (APK). More information here.

bash
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
zipalign -v 4 infile.apk

Saini APK mpya (tena?)

Ikiwa unataka kutumia apksigner badala ya jarsigner, unapaswa kusaini apk baada ya kutumia ukandamizaji na zipaling. LAKINI KUMBUKA KWAMBA UNAPASWA KUSAINI PROGRAMU MARA MOJA TU KWA jarsigner (kabla ya zipalign) AU KWA aspsigner (baada ya zipaling).

bash
apksigner sign --ks key.jks ./dist/mycompiled.apk

Kubadilisha Smali

Kwa msimbo wa Hello World Java ufuatao:

java
public static void printHelloWorld() {
System.out.println("Hello World")
}

Kod ya Smali itakuwa:

java
.method public static printHelloWorld()V
.registers 2
sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
const-string v1, "Hello World"
invoke-virtual {v0,v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
return-void
.end method

The Smali instruction set is available here.

Mabadiliko ya Mwanga

Badilisha thamani za awali za kigezo ndani ya kazi

Baadhi ya vigezo vinafafanuliwa mwanzoni mwa kazi kwa kutumia opcode const, unaweza kubadilisha thamani zake, au unaweza kufafanua mpya:

bash
#Number
const v9, 0xf4240
const/4 v8, 0x1
#Strings
const-string v5, "wins"

Operesheni za Msingi

bash
#Math
add-int/lit8 v0, v2, 0x1 #v2 + 0x1 and save it in v0
mul-int v0,v2,0x2 #v2*0x2 and save in v0

#Move the value of one object into another
move v1,v2

#Condtions
if-ge #Greater or equals
if-le #Less or equals
if-eq #Equals

#Get/Save attributes of an object
iget v0, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Save this.o inside v0
iput v0, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Save v0 inside this.o

#goto
:goto_6 #Declare this where you want to start a loop
if-ne v0, v9, :goto_6 #If not equals, go to: :goto_6
goto :goto_6 #Always go to: :goto_6

Mabadiliko Makubwa

Kurekodi

bash
#Log win: <number>
iget v5, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I #Get this.o inside v5
invoke-static {v5}, Ljava/lang/String;->valueOf(I)Ljava/lang/String; #Transform number to String
move-result-object v1 #Move to v1
const-string v5, "wins" #Save "win" inside v5
invoke-static {v5, v1}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I #Logging "Wins: <num>"

Mapendekezo:

  • Ikiwa unataka kutumia mabadiliko yaliyotangazwa ndani ya kazi (yaliyotangazwa v0,v1,v2...) weka mistari hii kati ya .local <nambari> na matangazo ya mabadiliko (const v0, 0x1)
  • Ikiwa unataka kuweka msimbo wa kuandika katikati ya msimbo wa kazi:
  • Ongeza 2 kwa idadi ya mabadiliko yaliyotangazwa: Mfano: kutoka .locals 10 hadi .locals 12
  • Mabadiliko mapya yanapaswa kuwa nambari zinazofuata za mabadiliko yaliyotangazwa tayari (katika mfano huu yanapaswa kuwa v10 na v11, kumbuka kwamba inaanza na v0).
  • Badilisha msimbo wa kazi ya kuandika na tumia v10 na v11 badala ya v5 na v1.

Toasting

Kumbuka kuongeza 3 kwa idadi ya .locals mwanzoni mwa kazi.

Msimbo huu umeandaliwa kuingizwa katika katikati ya kazi (badilisha nambari ya mabadiliko kama inavyohitajika). Itachukua thamani ya this.o, kubadilisha kuwa String na kisha kufanya toast na thamani yake.

bash
const/4 v10, 0x1
const/4 v11, 0x1
const/4 v12, 0x1
iget v10, p0, Lcom/google/ctf/shallweplayagame/GameActivity;->o:I
invoke-static {v10}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;
move-result-object v11
invoke-static {p0, v11, v12}, Landroid/widget/Toast;->makeText(Landroid/content/Context;Ljava/lang/CharSequence;I)Landroid/widget/Toast;
move-result-object v12
invoke-virtual {v12}, Landroid/widget/Toast;->show()V

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks