บทนำ (Overview)
การ bypass certificate pining บนเครื่อง non root ด้วย objection โดยการติดตั้ง frida gadget ลงใน application แบบ manual จากบทความก่อนหน้านี้ How to bypass certificate pining on Non-root Physical Devices using objection ซึ่งในความเป็นจริงนั้นบาง application ไม่สามารถใช้ objection ในการทำ auto patchapk เช่นรูปด้านล่าง
จาก error ที่แสดงนั้นพบว่าปัญหามาจากการที่ apktool (ซึ่งถูกเรียกด้วย objection) ไม่สามารถทำงานได้อย่างปกติ ซึ่งวิธีการแก้ไขมีดังนี้
- Upgrade apktool ไปยัง version ล่าสุด
- ติดตั้ง framework ลงบน apktool https://ibotpeaches.github.io/Apktool/documentation/#framework-files
ซึ่งวิธีการข้างต้นอาจจะไม่สามารถใช้งานได้เสมอไปหรือใช้เวลาในการแก้ไขได้ในระยะเวลาอันสั้น ดังนั้นถ้า apktool ไม่สามารถใช้งานได้ปกติ (ทำให้ objection ทำงานไม่ปกติ) ในบทความนี้เราจะทำการ patch apk ด้วยตัวเองโดยไม่พึ่ง objection ในการ patch apk
บทความโดย
Hades
Cyber Security Researcher
ขั้นตอน (Steps)
ติดตั้ง Tools
** แนะนำให้ทำการติดตั้ง Tools ทั้งหมดลงบนเครื่อง kali-linux โดย tools ที่ต้องการมีดังนี้ adb, virtualenv, objection, frida, zipalign ซึ่งสามารถดูวิธีการติดตั้งได้จากบทความก่อนหน้านี้ http://blog.itselectlab.com/?p=10943
หลังจากติดตั้ง tools แล้วเราจะเริ่มวิธีการ patch application ด้วยตัวเองดังนี้
- แยกส่วนของ application โดยใช้ apktool
*** เราจะใช้ option –r เพื่อให้ apktool ไม่ decode resource file จะทำให้ error หายไปapktool -r d SSLPinningExample.apk
- เพิ่ม frida gadget ลงใน library ตามชนิดของ android ที่ใช้
- ก่อนเราจะ download Frida gadget เราต้องรู้ architecture ของเครื่องที่เราใช้ก่อน
adb shell cat /proc/cpuinfo
- ทำให้ทราบว่า cpu เป็น arm architecture หลังจากนั้นเข้าไปที่ https://github.com/frida/frida/releases
เนื่องจากจาก armv7 เป็น 32bit version เราจึง download version 32 bit มาก่อน
wget https://github.com/frida/frida/releases/download/10.6.21/frida-gadget-10.6.21-android-arm.so.xz
- หลังจากนั้น run command เพื่อจะหาชื่อของ cpu ที่จะเอาไว้ใช้ในการสร้าง folder ของ library
adb shell getprop ro.product.cpu.abi
- จาก command ข้างต้นพบว่าชื่อที่เราจะใช้ในการสร้าง folder ของ library คือ armeabi-v7a
- สร้าง folder lib/armeabi-v7a ลงใน folder ที่ได้จากการแตก apk
mkdir SSLPinningExample/lib (ถ้ายังไม่มีมาก่อน) mkdir SSLPinningExample/lib/armeabi-v7a
- ทำการ uncompressed และ copy file frida gadget ลงใน folder ที่เราเพิ่งสร้าง
#ต้อง copy ไปในชื่อ libfrida-gadget.so เท่านั้น unxz frida-gadget-10.6.21-android-arm.so.xz # cp frida-gadget-10.6.21-android-arm.so SSLPinningExample/lib/armeabi-v7a/libfrida-gadget.so
- ก่อนเราจะ download Frida gadget เราต้องรู้ architecture ของเครื่องที่เราใช้ก่อน
- ให้ Application ทำการเรียก frida gadget โดยใช้ function load library
- เนื่องจากเราจะต้องให้ frida gadget ถูกเรียกขึ้นมา โดยการแทรก code เพื่อ load firda gadget ซึ่งโดยปกติเราจะแทรกลง Mainactivity ของ application ซึ่งเราจะสามารถหา Mainactiviry ของ application ได้โดย
aapt dump badging SSLPinningExample.apk | grep launchable-activity
- ทำการแก้ไข Mainactivity file โดยการแทรก
.method static constructor <clinit>()V .locals 1 .prologue const-string v0, "frida-gadget" invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V return-void .end method
- *** ถ้ามี method static contractor อยู่แล้วให้แทรก แค่
const-string v0, "frida-gadget" invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V
- เนื่องจากเราจะต้องให้ frida gadget ถูกเรียกขึ้นมา โดยการแทรก code เพื่อ load firda gadget ซึ่งโดยปกติเราจะแทรกลง Mainactivity ของ application ซึ่งเราจะสามารถหา Mainactiviry ของ application ได้โดย
- Build application
apktool b SSLPinningExample/ -o SSLPinning.patched.apk
- Sign application
- ก่อนการ sign application เราต้องสร้าง keystore เพื่อใช้ในการ sign application (ทำแค่ครั้งเดียว)
keytool -genkey -v -keystore hades.keystore -alias hadesKey -keyalg RSA -keysize 2048 -validity 10000
- ทำการ sign application
jarsigner -sigalg SHA1withRSA -digestalg SHA1 -keystore hades.keystore -storepass <password ที่สร้าง> SSLPinning.patched.apk hadesKey
- ใช้ command zipalign หลังจากที่ทำ jarsigner เรียบร้อยแล้ว
zipalign 4 SSLPinning.patched.apk final.SSLpinning.apk
- ก่อนการ sign application เราต้องสร้าง keystore เพื่อใช้ในการ sign application (ทำแค่ครั้งเดียว)
- จากนั้นติดตั้งและ run application
adb install final.SSLpinning.apk
- ถ้า frida gadget ถูกโหลดขึ้นมาจะพบว่า เราสามารถใช้งาน objection ได้ ทำการ bypass certificate pinning โดยใช้ command บน objection
#objection explore
- *** ถ้า application crash ให้ไปตรวจสอบ code ส่วนที่เราแก้ใน Mainactivity หรือลองเปลี่ยน gadget สำหรับ os architecture อื่น
อ้างอิง
- Prathan Phongthiproek, https://www.slideshare.net/pprathan/the-hookshot-runtime-exploitation?qid=45b07a61-03ad-46d9-a2d1-222eb819f0df&v=&b=&from_search=3
- บุญเพิ่ม มาร์ค อัครเดช, http://blog.itselectlab.com/?p=10776, http://blog.itselectlab.com/?page_id=7703
- Hades, http://blog.itselectlab.com/?p=10943
- http://sh3llc0d3r.com/certificate-pinning/
- https://ibotpeaches.github.io/Apktool/documentation/
- https://koz.io/using-frida-on-android-without-root/
- https://techblog.mediaservice.net/2017/07/brida-advanced-mobile-application-penetration-testing-with-frida/
- https://developer.android.com/studio/command-line/zipalign.html
- https://github.com/frida/frida/releases