26
Android Mobile Pentest 101 © tsug0d, September 2018

Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

AndroidMobilePentest 101©tsug0d,September2018

Page 2: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Bài7– HookingVớiFridaMụctiêu:SửdụngđượcFridađểhook

Page 3: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

- LàmộtDynamicinstrumentation (tracing,profiling, vàdebugging trong lúcappchạy)toolkitchodevelopers, reverse-engineers,vàsecurityresearchers.

- Mộtsốđặctínhnổibậtcủafrida:

Description

Page 4: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

- CàiđặtFridaCLITools:pip3installfrida-tools- CàiđặtFridaPythonbindings (Sửdụngchínhtrongbàiviết)pip3installfrida- Kiểmtralạixemfridađãcàithànhcôngchưa:

Installation->Client(máythật)

Page 5: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Installation->Server(điệnthoạiảo)

- Tải frida-server binary từtrang releasepage,cụthểởbàinàylàfrida-server-12.1.2-android-x86.xz(Nếukhông rõthìthửhếtbinary luônarm/arm64/x86-64/x86)- frida-server versionphảiđúngvới Fridaversion.

Page 6: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Installation- Tổngquanthìsetupnósẽnhưthếnày

Frida-client(useCLIorpythonbinding tointeractwiththeandroid)

Frida-serverListenonport27042bydefaultFrida-server

(mặcđịnhlistentrênport27042)

Page 7: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Installation

- Kiểmtraxemsetupđúngchưa,từmáythật,gõlệnh:frida-ps -U

Thànhcông

Page 8: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Nhưđãnóiởtrên,chúngtasẽtậptrungvào“frida pythonbindings”.- Sửdụng fileapkgốc“InsecureBankv2”đểlàmvídụ (nhớxoácáicũđivàcàilại)

Page 9: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Đểsửdụng fridatrongpython, taimportnó:import frida- Bâygiờsửdụng get_usb_device() function đểlấythông tinvềdevicedevice=frida.get_usb_device()

- Rồispawnapplên:pid=device.spawn(“com.android.insecurebankv2”)device.resume(pid)

- Bâygiờchúng tađãcóđượcpidcủaappvừaspawn,attachnótạosession:session=device.attach(id)

- Giờthìinjectđoạnhook_scriptcủachúng tavàochương trìnhthôi:script=session.create_script(hook_script)script.load()

Page 10: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Đoạnpythoncodediễngiảiởtrên:

Page 11: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Tớilúcnàybạnsẽtựhỏi hook_script làcáigìđúngkhông, nólàcáclệnhchúng tacungcấpchoFridasửdụng Javascript API.Vớinó,chúng tacóthểtương táctrựctiếpvớiJavafunctionsvàobjects.

- Lưuýđoạncodechúngtacungcấpchofridasẽnằmbêntrong Java.perform(function(){ ...}),cáinàylàbắtbuộc,kiểucúphápcủaFridaJavaAPI.hook_script sẽnhưthếnày:

hook_script=“””Java.perform(function (){//dosomething});“””- Bâygiờquyếtđịnhhookcáigìnhé, trởvềcáiappnè,nhớlỗiweakcryptoởbài3không (Phân tíchtĩnh)?- Lúcđóthìcầnphảihiểucrypto,rồimôphỏng lạitrênpython, blah...blah....Lỡlúcđókhông tìmđược

key,hayđoạncryptoquávãihàthìchịuhả?- Rấtmaymắn,Fridakhông xoắnmấycáinàyJ

Page 12: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Chúng tatìmđượcđoạncodedecrypttrongcom/android/insecurebankv2/CryptoClass.class

- Biếtrằngnónhậnvàocrypt-text,decryptvàtrảvềplainText,khôngcầnquantâmnólàmgìbêntrong.Rấtrõràng,chỉcầngọi functionđólênsửdụngcrypt-texttamuốn làokayJ

Page 13: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Vậygọifunction lênbằngFridanhưnào?Chúng tasẽsửdụng Java.choose()- Tríchtừdocument fridagốc:

- Nósẽscantrênheap,tìminstancecủaclassName tamuốn, nếukhớp,onMatch callbacksẽđượcthựcthi,dựavàođótagọihàmcủainstancetìmđược.

Page 14: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Codehiệngiờnósẽnhưthếnày:

className

Instance củaclassName tìm được trên heap

Sử dụng instance để gọifunction

Page 15: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Kếtquả:

- Nhưđãbiết,Dinesh@123$ làpasswordcủauserdinesh J

Page 16: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- ThêmvàivídụnữachovuiJ- Nhớđoạndetectrootcodehông?Chúng tasẽhookvàsửanó,let’sFrida!- Đoạncodeđónằmtrongcom/android/insecurebankv2/PostLogin.class

- Nhưhình,nếudoesSuperuserApkExist(String paramString) trảvềtrue, thìrootdetected.Mặcđịnhthìchúng tađãvượtquađoạnchecknàyrồi,nhưngmàmìnhhemthích,mìnhmuốnsửanó lạiđểnódetectmìnhchovuiJ =>makeittrue!

Page 17: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Hàmnàytrảvềbool val,hijackgiátrịtrảvềlàxong

- Nhưnào?Lầnnàysửdụng Java.use()- Tríchtừdocument fridagốc:

Page 18: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings

- Ngắngọn là Java.use()chophépchúngtaghiđè1method trongclass.- Đầutiên chọnclasscómethodđó:Java.use(‘com.android.insecurebankv2.PostLogin’)- Sauđóthìghiđèsửdụng implementation:class_PostLogin.doesSuperuserApkExist.implementation =function (){

//whatever,inthiscase,returntrue}

- Done

Page 19: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Codenhưsau:

UseimplementationtooverridefunctiondoesSuperuserApkExist content

Page 20: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Vàođiệnthoạiảo,đăngnhập,bịpháthiệnrootroài(>_<!)

Normalstart,bydefault Startandhooked byfridaJ

Page 21: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Mộtvídụkhácnhé,đasốcácappđềucóclient-sidefilter,giờsửdụng fridađểbypassthửxem- Nhìnđoạncodenàytrongcom/android/insecurebankv2/ChangePassword$RequestChangePasswordTask.class:

Page 22: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Đểýđoạnregex:((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})Đoạnnàykiểmtrađộphứctạpcủa password,nghĩalànếuchúng tađổipassworddễquáthìsẽkhông được- Mình làngườiđơngiảnnênmìnhđổithửpasswordthành“1234”:

Fail,vìpwphảitừ6->20kítự“1234”chỉcó4thôi

Page 23: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Chúng tasẽsửdụng fridađểbypass.Lầnnàykhông injectvàoappclassnữa,vìregexmethod làtừjavapackage,

khôngphảitừcodecủadev.

from

- Hook:regex_Pattern_hook =Java.use('java.util.regex.Pattern')- Khácvớilầntrước,takhông thểxàiregex_Pattern_hook.compile.implementation bởivìtrongPatternpackagecó

nhiều loạicompilemethods (compile(String x);compile(String x,int y);etc…),dođófridasẽbịrối!- Chúng taphảinóivới frida chínhxácmethodnàođểhook,ởđâylà:compile(String x)- Sửdụngoverload:regex_Pattern_hook.compile.overload("java.lang.String").implementation

Page 24: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Code:

Sửa regex thành “.*”,nghĩa là cho phép mọi trường hợp

Page 25: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Trong một số trường hợp, sau khi hook app sẽ bị treo (chắc do máy ver cũ), thì reload script 1 lần

nữa là app sẽ chạy bình thường (hook đúng nha, chứ hook sai thì fail chắc rồi)- Thử nào:

Page 26: Android Mobile Pentest 101 · Java.use(‘com.android.insecurebankv2.PostLogin’) - Sau đó thì ghi đè sử dụng implementation: class_PostLogin.doesSuperuserApkExist.implementation=

Pythonbindings- Đăngnhậpvàxemtronglogcatđểconfirm:

- Cònnhiều fridaapihữudụngkháckhixàivớipython: send, recv andrpcFulldocs:https://www.frida.re/docs/javascript-api/