How to Use GDB to find the encryption key in the android so file

Source: Internet
Author: User

In an article from xda developer, the author describes in detail how to use GDB + IDA to find the encryption key of Lua files in Angry Birds Rio Android. Although there is no reverse engineering, but maybe it will be used one day, first record it.

 

Well... I have attached a debugger to native code, set breakpoints, analyzed registers, memory, etc. it wasn' t that easy though. it took me several days to start debugging and get first key, but I got second one in about 1 hour.

Actually I don't really need that key, I can't even play Angry Birds Rio on my old G1, but it was challenging and I love challenges plus I have learned a lot about GDB, javaser, ARM architecture, etc.

So I want to thank you, goddchen, for giving me an opportunity to learn & play

OK, let's move on...

First, I have disassembled libangrybirds. so using IDA Pro 5.5. I was able to examine code and attach Ida To gdbserver on a device, but unfortunately it wasn' t working properly. ida was thinking that libangrybirds. so is a main binary of a process it attached to, But it shoshould look into loaded shared libs instead. weird, but I didn't find a way to attach it properly. and this is pity, because IDA is a great tool and it wocould make debugging a pleasure, but I had to use GDB instead.

Second, Android has problems with debugging multi-threaded native code. mt support was added in ndk R5 and because of some bugs it's not possible on a system older than gingerbread.

Third, you cocould attach GDB manually, but ndk-GDB script does great work for you. You will have to do some tricks to use it with 3rd party app though.

Fourth, it seems libangrybirds. so is a Java code compiled to native or something like that. there are objects like fileinputstream, byteoutputstream, Etc ., but there are also some API differencies. we'll see string and array <uchar> objects, but it's usually easy to find a pointer to simple uchar [].

Steps to start native code debugging:

  1. Upgrade to gingerbread (yeah, I had to do that. Hacking requires you to sacrifle ice yourself a bit). Or you can use an emulator.
  2. Install ndk> = R5.
  3. Decode Angry Birds Rio using apktool. you coshould just unzip it, But decoded app is much more similiar to original sources, so it's more compatible with ndk. for example ndk-GDB reads androidmanifest. XML to get package name. of course you coshould fake simple androidmanifest. XML and other files if you want.
  4. Rename lib dir to libs.
  5. Fake JNI/Android. mk file. I have copied one from Hello-JNI sample and didn't even bother to modify Module name: http://pastebin.com/hmbxt 5cm.
  6. Copy libs/armeabi */libangrybirds. So to OBJ/local/armeabi */. Normally this is done by ndk-build command.
  7. Fake libs/armeabi */GDB. setup file. It shoshould be something like: http://pastebin.com/BYm13RKz, but second line isn't that important.
  8. Angry Birds Rio APK contains old gdbserver and you need one from ndk R5. grab $ {ndk_root}/toolchains/arm-linux-androideabi-4.4.3/prebuilt/gdbserver and push it to/data/COM. rovio. angrybirdsrio/lib.
  9. Ufff... You coshould now try to run: ndk-GDB-verbose-launch = com. Rovio. ka3d. App.
  10. After few seconds you shocould See "(GDB)" Prompt and game shocould be paused on the device.
  11. Run 'info shared 'and check if libangrybirds. So is loaded. If not then something is wrong.

OK, let's find a key for levels Lua files:

  1. Set a breakpoint for gamelua: loadlevel ()-find this method in IDA pro and copy its export name:

    Code:
    (gdb) br _ZN7GameLua9loadLevelEN4lang6StringEBreakpoint 1 at 0x80468e4c

     

  2. Resume game and open some level. You shoshould hit a breakpoint: code:
    (gdb) cContinuing.[New Thread 5857][Switching to Thread 5857]Breakpoint 1, 0x80468e4c in GameLua::loadLevel () from /home/brutall/t-angrybirds/com.rovio.angrybirdsrio-1/obj/local/armeabi/libangrybirds.so

     

  3. Look into Ida and note there are 2 LANG: string objects passed as first arguments to method, so pointers are in R1 and R2 registers. we need to examine these objects and find pointers to raw char []. fortunately LANG: string is very simple wrapper around char [], so pointer is first (and only one, I think) Member of string: code:
    (gdb) x/4x $r10x4395e66c:0x00a405f00x00153b280x804ec7780x00000000(gdb) x/s 0x00a405f00xa405f0: "levels/warehouse/Level190"

    Yey, finally we see something

  4. Let's move to Lang: aesutil: decrypt () method. It's named _ zn4lang7aesutil7decrypterkns_5arrayihees4_rs2 _, so: code:
    (gdb) advance _ZN4lang7AESUtil7decryptERKNS_5ArrayIhEES4_RS2_0x80539894 in lang::AESUtil::decrypt () from /home/brutall/t-angrybirds/com.rovio.angrybirdsrio-1/obj/local/armeabi/libangrybirds.so

     

  5. As you can see decrypt () gets 3 array <uchar> objects and 2 of them are const. it's quite easy to guess they re: Key, encrypted data and container for decrypted data. let's check this: code:
    (gdb) x/4x $r10x1592b0:0x001595280x000000200x000000200x7b206e65

    0 × 00000020 = 32-Yes, length of AES key first 4 bytes of an array object is a pointer to raw char [] and second 4 bytes contain length of an array. now we cocould read contents of an array:

    Code:
    (gdb) x/s 0x001595280x159528: "USCaPQpA4TSNVxMI1v9SK9UC0yZuAnb2a"

    As you can see there are 33 chars instead of 32. This is because array stores its length, so char [] isn' t null-terminated. Ignore last "A" char.

  6. We cocould also look into second const array to be sure that encoded string is exactly the same as contents of Lua file: code:
    (gdb) x/4x $r20x4395d6f4:0x009ca2480x000004a00x000004a00x00000378(gdb) x/4x 0x009ca2480x9ca248:0x3347b5dc0x260484460x1a0c12310x35d3f99c

    First 16 bytes are the same, length of data is also OK.

As you can see there is AES: blockmode passed to AES: ecrypt (). it wocould be quite hard to interpret it without headers, so I was trying various block modes and I found that CBC with empty initial vector decodes to string starting with '7z '. for me that meant: Mission successfull

OK, highscores. Lua and settings. Lua files now. technique is very similar, but there are some differences:

  • Different keys.
  • They aren't loaded using gamelua: loadlevel (), but gamelua: loadpersistentfile (). You cocould find this very easily, searching for "highscores. Lua" in Ida.
  • If you examine gamelua: loadpersistentfile () method you will see it doesn't load files using fileinputstream, but IO: appdatainputstream, so we have to be sure, what exactly is being decrypted.
  • Annoying thing is that GDB can't catch highscores/settings loading, because they're loaded too soon-before GDB attach itself.

Maybe there is a better solution to last problem, but I 've got decided to add some thread. sleep () call just after system. loadlibrary (), so GDB will attach before highscores. lua loading.

  1. Open smali/COM/Rovio/ka3d/APP. smali, and add 2 lines of code just after loadlibrary () call in oncreate () method:

    Code:
        invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V    const-wide/16 v0, 5000    invoke-static {v0, v1}, Ljava/lang/Thread;->sleep(J)V

     

  2. Run ndk-GDB-verbose-launch = com. Rovio. ka3d. App.
  3. Set a breakpoint for gamelua: loadpersistentfile () method and check which file is being loaded: code:
    (gdb) br _ZN7GameLua18loadPersistentFileERKN4lang6StringEBreakpoint 1 at 0x80457030(gdb) cContinuing.[New Thread 6735][Switching to Thread 6735]Breakpoint 1, 0x80457030 in GameLua::loadPersistentFile () from /home/brutall/t-angrybirds/com.rovio.angrybirdsrio-1/obj/local/armeabi/libangrybirds.so(gdb) x/s $r20x4395e3b8: "highscores.lua"

    I'm not sure why it's R2, not R1 and why there is no LANG: string, but Char [] directly. I think this isn't a pointer to string, but string itself, passed to method in registers, so its char [] is in R2.

  4. Now advance to Lang: aesutil: decrypt () method and read key as usual: code:
    (gdb) advance _ZN4lang7AESUtil7decryptERKNS_5ArrayIhEES4_RS2_0x80539894 in lang::AESUtil::decrypt () from /home/brutall/t-angrybirds/com.rovio.angrybirdsrio-1/obj/local/armeabi/libangrybirds.so(gdb) x/4x $r10x159294:0x001596200x000000200x000000200x00159518(gdb) x/s 0x001596200x159620: "44iUY5aTrlaYoet9lapRlaK1Ehlec5i0"

     

  5. Because of that appdatainputstream object, we need to check if encrypted data is the same as file contents. Pull highscores. Lua file from a device and run: code:
    (gdb) x/4x $r20x4395ddc4:0x0015bc000x000000400x000000400x00000001(gdb) x/16x 0x0015bc000x15bc00:0x2271b7770xe6f19f4c0x2489a3160xfae1aee20x15bc10:0x82e0ef380xe84fc25d0xb196adac0xbf0304390x15bc20:0xb6b9bade0x3046af120xe8eeeb0d0x20e8037c0x15bc30:0x1a405edf0xc218f7f60xc29209e20x9ad03e8c

    Yeah, this is my highscores. Lua file.

  6. Same for settings. Lua file to check if it's encrypted with the same key. It is.
  7. After decrypting these files we'll see some weird chars at the end of decoded data. Few seconds on the Wikipedia and we'll know this is just pkcs7 padding scheme.

Now we have got everything we want

Ahh, not exactly everything... I wocould be really happy to know, how to properly attach IDA for debugging-It wocould be much easier, even if GDB interface is also very good.

 

Oh, forget to mention the author of this article, brut. all. You should be able to think of this name if you have an Android smartphone. That's right. This is the author of Brut Google Maps. I have his app on my mobile phone. In addition, he also released apktool, an open-source tool on Google Code, dedicated for reverse engineering of APK, of course it is not needed now.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.