Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Steroids for your App Security Assessment

Marco Grassi
November 13, 2014

Steroids for your App Security Assessment

In this talk we will put our skills in apps security assessment on Steroids. We will develop and deploy custom runtime modifications for Android and iOS, both at application level and OS level, in order to perform protections bypass, advanced analysis and bending the application's behaviour to our analysis purposes, with real world examples from our experience.

Marco Grassi

November 13, 2014
Tweet

More Decks by Marco Grassi

Other Decks in Research

Transcript

  1. The  Problem • We want to trace the code in

    mobile applications that we are analyzing • We want to modify some behaviours and inputs that are out of our control • We want to do it in the less time consuming way and as stealthy as possible (from the apps prospective).
  2. Today we will focus on Android and iOS. Windows Phone

    is gaining some traction. The concepts potentially apply to this platform as well, but we are very distant from the iOS/Android support and tools
  3. Our  Guide  Principles  to  solve  those  Problems •Simplicity:  We  want

     to  keep  our  design  as  simple  as   possible  and  as  open  as  possible  to  extensions  in  the   future.   •Reuse:  We  want  to  use  as  much  code  as  possible  between   different  assessments  and  different  platforms  if  possible.   •Stealthiness:  We  don’t  want  to  be  easily  detected  by  the   application  under  analysis.
  4. How  to  solve  our  problems? Why  solving  our  problems  statically

     will  break  our  guide  principles  
 (Simplicity,  Reuse,  Stealthiness)? We  can  potentially  instrument  applications  directly  by  patching   their  code  (or  OS  or  kernel).  (APIMonitor  and  others)   •This  can  be  difficult  in  iOS  applications  or  native  code  on   Android  (Simplicity)   •Where  it’s  feasible,  like  on  Android  Dalvik  code  for  example,  it   will  require  instrumenting  every  single  app.  (Reuse)   •It  will  trigger  even  simple  tampering  detection.  (Stealthiness)
  5. For  the  reasons  that  we  mentioned  in  the  previous  slides,

     we  will  experiment  and   try  to  solve  those  problems  dynamically,  by  injecting  at  runtime  our  code,  with   various  solutions  and  leveraging  different  frameworks  and  methodologies. •Simplicity:  We  will  write  our  injected  code  in  high  level  languages   •Reuse:  We  can  reuse  both  the  tracing  infrastructure  that  we  make,  that  will  be   app  independent,  and  also  lot  of  the  tracing  code,  for  example  if  you  hook  into   the  “open”  function  into  libc,  you  can  reuse  it  to  track  file  opening  in  all  the   applications   •Stealthiness:  We  don’t  alter  the  application,  and  we  have  a  more  privileged  access   to  the  OS,  so  we  can  hide  more  easily.
  6. Why  the  presentation  is  called  Steroids  for  apps? A  “steroid”

     is  just  some  custom  code/ patch  that  we  will  “inject”  in  the   system  or  process  under  analysis  at   runtime,  leveraging  various   frameworks.   This  additional  code  will  be  used  to   enhance  our  visibility,  or  to  change  the   behaviours  and  inputs  of  the   applications/system.
  7. Malware  Bazaar In the talk we will make very small

    digressions to see how the malware is adopting those or similar techniques. The slides related to malware will be marked with the bazaar symbol on the left.
  8. 1Password  (Android):  Our  Easy  Goals • Hide root on Android

    • Hook into their cryptography code to improve our understanding.
  9. What  we  will  hook Trivial,  we  will  patch  to  return

     always  false We  will  insert  a  hook  here  to  print  the  logMsg
  10. What  is  already  available?  What  can  we  leverage? Xposed Framework

    http://repo.xposed.info/ Cydia Substrate http://www.cydiasubstrate.com/ Cycript http://www.cycript.org/ ddi / adbi https://github.com/crmulliner/ Frida http://www.frida.re/
  11. Xposed  Framework  and  Cydia  Substrate “Modify behavior without patches or

    ROMs. Substrate makes it easy to modify software, even without the source code, and in a way that allows users to easily choose which changes they want. - saurik “Xposed is a framework for modules that can change the behavior of the system and apps without touching any APKs.” - rovo89
  12. Xposed  Framework  and  Cydia  Substrate •They  work  similarly   •Essentially

     you  hook  a   method,  and  you  write   some  additional  code   to  execute  when  that   method  is  called WorkFlow 1.Write  your  code  and  filtering   metadata  (for  what  processes   to  hook)   2.Deploy  your  code  on  the   device   3.Reboot  or  Respring/restart   zygote
  13. Android  Malware  leveraging  Xposed •Hiding  features,  steal  passwords  from  EditText

     by  hooking  methods   •http://blog.avlyun.com/1361.html  (in  Chinese)
  14. iOS  Spyware  leveraging  Substrate Commercial  Spyware  (iOS/Android  and    

    other  platforms) • It  uses  this  framework  to  implement  trivial  “rootkit”  functionalities  on  iOS,  to  hide   the  Cydia.app  from  a  Jailbroken  device  to  appear  unjailbroken  with  a  quick  look.   • You  can  type  “4433*29342”  in  the  SpringBoard  search  bar,  to  hide  or  display  the   Cydia  icon.   • The  code  in  SpringBoard  is  hooked.  When  that  magic  code  is  detected  Cydia  is   hidden  or  displayed.   • This  hooking  mechanism  is  deployed  as  a  MobileSubstrate  extension
  15. Summary Xposed  is  open  source  and  actively   maintained.  It’s

     Android  only  and   for  now  it  supports  only  hooking   Dalvik  methods.  There  are  a  lot  of   modules  developed  on  it  to   checkout.   It’s  probably  the  best  framework   for  Android  to  invest  in  today,  for   it’s  active  support  and  community. Cydia  Substrate  supports  iOS/ Android  and  it  can  hook  pretty   much  everything,  ObjC,  Dalvik  and   native  methods.  On  iOS  the  tweak   community  is  very  active.   Unfortunately  it’s  closed  source   and  so  it  can  be  risky  to  invest  time   developing  tools  on  top  of  it,   especially  on  Android.
  16. Stuff  to  checkout • https://github.com/Fuzion24/ JustTrustMe  (SSL  Pinning  kill  

    switch)   • https://github.com/M66B/ XPrivacy  (manage  sensitive  data   returned  to  the  applications  via   Android  APIs) • iSECPartners  Introspy  Projects:  -­‐   http://isecpartners.github.io/ Introspy-­‐Android/  -­‐  http:// isecpartners.github.io/Introspy-­‐ iOS/   • SSL  kill  switch  for  iOS  https:// github.com/iSECPartners/ios-­‐ssl-­‐ kill-­‐switch
  17. These first 2 frameworks are very good, but they are

    oriented to make more persistent changes that the ones we need for code tracing or small patches to a single application and the development cycle is quite slow, with development, deployment and reboot. Can we do a little bit better, still using those frameworks?
  18. MY SUGGESTION IS CREATE YOUR OWN ABSTRACTION ON TOP OF

    THOSE FRAMEWORKS • Define targets, hooks and actions generically • Place them at runtime using reflection with your own module • In this way you can define them in a structured file and restart zygote, and you will reuse the code!
  19. My  (first  try)  abstraction  layer  as  example Kankuro - Puppet

    Master from the Sand Village (Naruto) He fights instrumenting bodies and puppets with chakra strings. Very simple, Android only, can leverage Xposed or Cydia Substrate. It will read the hook to deploy from a JSON file that we will push on the device.
  20. How  to  Use  and  Example  Hooks {      

       "com.spotify.music":                    [                          {                                  "classCanonicalName":  "android.app.Activity",                                  "methodName":  "onCreate",                                  "hookType":  "method",                                  "hookAction":  "logcat"                          }                  ],          "com.agilebits.onepassword":                  [                          {                                  "classCanonicalName":  "com.agilebits.onepassword.mgr.EncryptionMgr",                                  "methodName":  "showLogMsg",                                  "hookType":  "method",                                  "hookAction":  "logcat"                          }                  ]   } ##  define  hooks  and  action  in  a  
 ##  tracking_hooks.json   $  adb  push  tracking_hooks.json  /data/ local/tmp/   ##  Let  the  hooking  framework  read  the
 ##  hooks   ##  from  other  processes   $  adb  shell  chmod  644  /data/local/tmp/ tracking_hooks.json   ##  Enable  your  module/restart  zygote The  last  one  is  a  PITA!
  21. Hooks  Output We  inserted  our  “probes”  in   the  app,

     without  having  to   write  additional  Java  code,   saving  time.  Now  we  can   hook  and  logs  intelligently   the  methods  that  we  are   interested  into,  tracking  the   flow  of  execution  and   printing  parameters,  or  we   can  patch  methods  to   return  different  values.
  22. Limitations  and  problems •Already  pointed  out:  we  must  reboot/restart  zygote/springboard

     if  we   modify  our  code   •Our  injected  code  is  running  in  the  process  context,  so  it  inherits  also  its   capabilities.  Often  those  capabilities  are  not  enough  for  us.  Think  of   Android  as  an  example:  if  our  target  process  does  not  have  Internet   permission,  we  are  limited  in  how  we  can  exfiltrate  our  traces  and   informations   •We  often  have  to  leverage/hack  logcat  and  world  read/writable  locations,   we  would  like  a  more  robust  communication  channel,  platform   independent
  23. “Your own scripts get injected into black box processes to

    execute custom debugging logic. Hook any function, spy on crypto APIs or trace private application code.” - frida.re
  24. Seems exactly what we need :) It’s the one I

    want to push in this talk, because it should receive more attention!
  25. How  Does  It  Work? • Injects Google V8 Javascript engine,

    on top of which the Javascript code that we will write run. • Being inside the same process, our javascript code has full memory access and we can read or modify what we need. • Frida offers javascript apis for us for hooking, communicating with the PC side of Frida software, and many other things.
  26. My  other  (ongoing)  Experiment… Sasori  -­‐  Sasori  of  the  Red

     Sand  (from  Naruto)   He  built  Kankuro's  fighting  puppets  because  he   also  fight  with  puppets  and  chakra  strings.   He  transformed  himself  into  a  puppet.   He  can  transmigrate  his  heart  into  other  puppets.   Based  on  Frida,  support  Android  and  iOS  for  now.
  27. High  Level  Structure Frida  (Device   Side) Frida  APIs Process

     To  Analyze   (for  example   1Password)
 pid:  18729 Sasori  pc  side
  28. Asking  for  pid  list Frida  (Device   Side) Frida  APIs

    Process  To  Analyze   (for  example   1Password)
 pid:  18729 Sasori  pc  side ./sasori.py --android -l Can  I  haz  PIDs? Sure,  …     com.agilebits.onepassword     has  18729  …  
  29. Injecting  the  Agent  into  a  PID Frida  (Device   Side)

    Frida  APIs Process  To  Analyze   (for  example   1Password)
 pid:  18729 Sasori  pc  side ./sasori.py --android 18729 Inject into  pid  18729
  30. The  agent  is  loaded Frida  (Device   Side) Frida  APIs

    Process  To  Analyze   (for  example   1Password)
 pid:  18729 Sasori  pc  side Loaded the agent press ENTER to stop [*] platform: linux [*] arch: arm [*] pointer size: 4 [*] Found Dalvik Runtime! Now  we  can  communicate   with  the  agent  and  we  can   modify  and  inspect  the   application!
  31. Now  we  can  hook  and  reimplement  any  method  we  want

     thanks  to  Frida  APIs.   For  example,  we  will  hook  the  showLogMsg  method  that  we  already  shown  before: <-Trace-> method: (com.agilebits.onepassword.mgr.EncryptionMgr.showLogMsg) args: [Validating password:sasoritest] <-Trace-> method: (com.agilebits.onepassword.mgr.EncryptionMgr.showLogMsg) args: [Failed pwd validation: pad block corrupted] <-Trace-> method: (com.agilebits.onepassword.mgr.EncryptionMgr.showLogMsg) args: [Validating password:methodtrack] <-Trace-> method: (com.agilebits.onepassword.mgr.EncryptionMgr.showLogMsg) args: [Done decrypt] <-Trace-> method: (com.agilebits.onepassword.mgr.EncryptionMgr.showLogMsg) args: [validatePassword OK for:533 ms]
  32. How  Does  It  works? [Debug] base: 0x67b02000 path: /system/lib/libwebviewchromium.so [Debug]

    base: 0x6aa2a000 path: /system/lib/libcgdrv.so [Debug] base: 0x6b035000 path: /data/local/tmp/.frida-49[..]/libfrida-agent.so Android A  shared  library  is  injected  into  the  target  process: [Debug] base: 0x191048000 path: /System/Library/PrivateFrameworks/ IncomingCallFilter.framework/IncomingCallFilter [Debug] base: 0x18f4dc000 path: /System/Library/PrivateFrameworks/FTAWD.framework/FTAWD [Debug] base: 0x1053dc000 path: /private/var/root/.frida-a2[..]/libfrida-agent.dylib iOS  8.1  64bit
  33. Current  structure Command  line  interface   written  in  Python  that

      leverages  Frida  APIs.   Print  output  and   communicate  with  the   agent  running  inside  of   the  process. Agent  written  in  Javascript,   running  on  top  of  Google   V8  JS  engine  inside  our   target  process.   It’s  completely   independent  from  the   client  side  and  can  be   cross  platform. sasori.py sasori-agent.js
  34. What  improvements  we  had  here? A  LOT! •We  don’t  have

     to  reboot  or  restart  zygote  or  respring  or   even  restart  the  app  anymore!  Our  development  time  is   dramatically  reduced!   •We  have  the  code  running  on  the  device,  written  in   javascript,  completely  separated  from  the  “server”  logic   and  testable!  (yes  if  the  instrumentation  code  size  become   important,  we  would  love  to  be  able  to  write  software   tests)   •The  agent  can  be  crossplatform,  it  can  detect  by  itself  what   platform  it’s  running,  and  code  reuse  it  very  good.   •We  can  hook  and  modify  pretty  much  everything  in   userspace,  Dalvik,  ObjC,  native  code.
  35. What  capabilities  we  have? A  very  good  bidirectional,  platform  independent

       communication  channel  with  our  agent. send({'type': 'debug', 'message': 'base: ' + module.base + ' path: ' + module.path}); def _process_message(self, message, data, process): … print ‘[Debug] ‘ + message
  36. recv([type, ]callback) … Dalvik.perform(function () { var EncryptionMgr = Dalvik.use("com.agilebits.one

    password.mgr.EncryptionMgr"); EncryptionMgr.showLogMsg.impl ementation = function (param) { ... }; }); script.post_message({"type": "hook", [...]}) What  capabilities  we  have?
  37. What  capabilities  we  have? •We  can  hook  Dalvik,  ObjC  and

     native  methods,  pretty  much   everything.   •We  have  full  memory  access,  read  memory,  write  memory,  scan   memory  (very  interesting  for  game  cheats  :)  )   •We  can  know  at  runtime  on  what  platform  we  are  running,  from  our   agent,  for  example  with  Dalvik.available, and  know  on  what   kind  of  architecture  we  are  running.   •See  http://www.frida.re/docs/javascript-­‐api/  for  a  better   understanding  of  what  you  can  do.
  38. What  can  be  improved? In  Web  Development,  we  can  go

     with  full-­‐stack  javascript.   Would  it  be  cool  to  do  it  also  in  our  dynamic  analysis   system,  with  Frida  Node.js  APIs  ?   The  idea  already  surfaced  in  Frida’s  website.   In  this  way  we  can  potentially  build  a  full-­‐stack  javascript   dynamic  analysis  platform.
  39. What  can  be  improved? ART  runtime  is  not  yet  supported

     on  Android  (but  it’s  not  even  the  other   frameworks  currently)   It  needs  some  additional  research  on  how  to  hook  into  the  application   before  it  starts,  to  be  able  to  place  the  hooks  before  the  app  get  a  chance   to  execute.   Some  bugfixes  that  will  come  naturally  with  more  maturity  of  the  Frida   Android/iOS  side  code  and  contributions/testing.
  40. Android  Malware  leveraging  ptrace On  Linux/Android,  Frida  leverages  ptrace  to

     inject  the   shared  library  into  the  selected  process.   Recently  an  Android  Malware  was  found  using  ptrace  to   dynamically  inject  code  in  third  party  processes.   Samples:  http://contagiominidump.blogspot.it/2014/10/ android-­‐chathook-­‐ptrace.html   Writeup  (Chinese)  :  http://blog.csdn.net/androidsecurity/ article/details/27504615