SlideShare a Scribd company logo
Hooking on Android
2014.07.05
정광운
exsociety@gmail.com
www.CodeEngn.com
2014 CodeEngn Conference 10
Who am I
• 정광운 EXSO (Not EXO)
• 27 years old (Single)
• CNU & Hackershool & Secu87
• Contact Me
– http://facebook.com/exsociety
– exsociety@gmail.com
– http://bananapayload.org
2
What is Hooking?
3
Android System Overview
Applications
Application Framework
Libraries
Linux Kernel
Home ....... Music Browser Office
Viewer
Keypad Driver
Display Driver
WiFi Driver
Camera Driver Flash Memory
Driver
Audio Driver Power Driver
Binder Driver
Notification
Manager
Package
Manager
Telephony
Manager
Resource
Manager
Location
Manager
Window
Manager
View
System
Activity
Manager
Content
Providers
OpenGL|ES
Surface
Manager
Free Type
SQLite
SSL
webkit_libmedia_lib
libc
viewer_lib
SGL
JNI
NDK
SDK
앱 레벨
(JAVA)
시스템 레벨
(C/C++)
Hooking on ARM
Hooking on Android
4
G al
• ARM 기반의 안드로이드 환경
• 시스템의 수정 X (단, 루팅 필요)
• 애플리케이션의 수정 X
• 애플리케이션의 라이브러리 내 함
수에 대한 후킹 수행
5
Design of Hooker
Shared
Library
(.so)
FunctionA
FunctionB
Android Application
Constructor
Target Library
Injected Library
branch
Function A
Ins 2
Ins 3
Ins 4
Ins 1
Ins 2
Ins 3
Ins 1
Branch
Hook_Function A
Orig_Function A
6
Shared Library Injection
• Call dlopen() using ptrace() on
application
7
Shared Library Injection
1) Find dlopen() address
Can not found libdl.so on maps
/system/bin/linker 소스코드 中
dlopen()
/system/bin/linker
dlsysm()
libdl.so
offset
dlopen() Address
= base address of linker +
offset
8
2) write library path
- use stack
- PTRACE_POKEDATA
ptrace(PTRACE_POKEDATA, pid, dst address, 4byte_data)
9
ARM Instruction mode
32-bit
32-bit
32-bit
32-bit
32-bit
16-bit
16-bit
16-bit
16-bit
16-bit
ARM
Instruction
Instruction
Instruction
Instruction
Instruction
Thumb
31 0
Function Address
CPSR Register
T = 0 : ARM Mode
T = 1 Thumb Mode
10
3) Call dlopen() ß Thumb
Debugger Application
Backup Register
Values
Set Breakpoint at Next Instruction
Change Register
Values• pc = dlopen() addr
• r0 = stack addr
• r1 = 0
• lr = next instruction’s
addr (pc)
Restore Registers Values,
Remove Breakpoint
Debugger Applicatio
n
Backup Register
Values
Overwrite Code at Next Instruction
Change Register
Values• pc = pc+4
• r0 = stack addr
• r1 = 0
• lr = next instruction’s
addr (pc)
Restore Registers Values,
Restore Code ,
Remove Breakpoint
• break
• ldr pc, [pc, #0]
• 0x0
• dlopen() address
Thumb 모드 ARM 모드
11
4) result
Useage : injector [pid] [Library Full Path]
12
끝
Function Hooking
1) Find function information
- Reference Header file
- Use Hex-ray
13
https://github.com/EiNSTeiN-/hexrays-python
2) Install Hooker
14
Target Library
Injected Library
LDR PC, [PC]
Function A
NOP
Hook_FunctionA Addr
Ins 4
Ins 1
Ins 2
Ins 1
Ins 2
Ins 3
Branch
Hook_Function A
Orig_Function A
Target Library
Injected Library
Function A
Push {r5}
add r5, pc, #4
ldr r5, {r5}
bx r5
Hook_FunctionA Addr
Ins 7
pop {r5}
Ins 1
Ins 2
Ins 1
Ins 6
Branch
Hook_Function A
Orig_Function A
ARM->ARM Thumb->ARM
Why…?
• Internal memory reference
15
Function A Orig_Function A
Original Code
JMP
Target Function+N
JMP
Target Function+N
Injected LibraryTarget Library
Access Violation
Reference Reference
Data Offset
Code Offet
• External memory reference
16
Function A
Target Function Original Function
Original Code
JMP
Target Function+N
JMP
Target Function+N
Injected LibraryTarget Library
Branch
Branch
Access Violation
Solution
• 귀찮으니 그냥 복사하자…
17
Target Library
Injected Library
LDR PC, [PC]
Function A
NOP
Hook_FunctionA Addr
Ins 4
Ins 1
Ins 2
Hook_Function A
Copied Target Library
Function A
Ins 1
Ins 2
Ins 3
Ins 4
Function Pointer
Issue 1. Global Hook
18
• 애플리케이션 생성 과정
System Server zygote zygote’ Application
① 앱 실행 요청 ③ 앱 적재② fork() 호출
Activity
Manager
Activity
Manager
Package
Manager
Package
Manager
Window
Manager
Window
Manager
…
Dalvik VM
libc
Dalvik VM
libc
Preloaded
class
Preloaded
class
Preloaded
resource
Preloaded
resource
Dalvik VM
libc
Preloaded
class
Preloaded
class
Preloaded
resource
Preloaded
resource
Dalvik VM
libc
Preloaded
class
Preloaded
class
Preloaded
resource
Preloaded
resource
Android
Application
Issue 2. 대상 라이브러리가
로드되기 전..
• 라이브러리 로드 함수를 후킹
– dlopen() = 10byte // 최소 12바이트 필요
– dvmLoadNativeCode(char const*, Object*, char**)
• dvmLoadNativeCode 종료 시점에 추가적인 Hooker 설치
19
How to use
• download : http://bananapayload.org
20
[library path] [Name / Offset] [Function Type]
/system/lib/libc.so malloc void *malloc(size_t size)
/system/lib/test.so 0x400 void sub400(int, int)
./ genLibrarySource [define File] [output path]
Define Format
Source Code
Edit Source & Edit makefile & make library
Useage : injector [pid] [Library Full Path] Hook Success
21
22
www.CodeEngn.com
2014 CodeEngn Conference 10

More Related Content

PDF
[2014 CodeEngn Conference 11] 김호빈 - Android Bootkit Analysis EN
PDF
Native hook mechanism in Android Bionic linker
PPTX
4055-841_Project_ShailendraSadh
PDF
Introduction to ida python
PPTX
Introduction to Python for Security Professionals
PDF
Python build your security tools.pdf
PDF
Introduction to Python for Bioinformatics
PDF
Python for Penetration testers
[2014 CodeEngn Conference 11] 김호빈 - Android Bootkit Analysis EN
Native hook mechanism in Android Bionic linker
4055-841_Project_ShailendraSadh
Introduction to ida python
Introduction to Python for Security Professionals
Python build your security tools.pdf
Introduction to Python for Bioinformatics
Python for Penetration testers

What's hot (20)

PPTX
Dock ir incident response in a containerized, immutable, continually deploy...
PPT
嵌入式Linux課程-GNU Toolchain
PDF
XFLTReat: a new dimension in tunnelling
PDF
Java Logging discussion Log4j,Slf4j
ODP
Pycon Sec
PDF
ROS Hands-On Intro/Tutorial (Robotic Vision Summer School 2015) #RVSS #ACRV
PDF
Toward dynamic analysis of obfuscated android malware
PDF
Playing games-in-the-sandbox-dynamic-analysis-and-modern-evasion-tactics copy1
PDF
Fluentd Hacking Guide at RubyKaigi 2014
PDF
Is ruby logger thread(process)-safe? at RubyConf 2013
PDF
PyCon Taiwan 2013 Tutorial
PPTX
Penetration testing using python
PDF
Debugging concurrency programs in go
PDF
Advanced debugging  techniques in different environments
PDF
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
PDF
Fluentd - Set Up Once, Collect More
PPTX
VB2013 - Security Research and Development Framework
PDF
Open source projects with python
PDF
Building robust and friendly command line applications in go
Dock ir incident response in a containerized, immutable, continually deploy...
嵌入式Linux課程-GNU Toolchain
XFLTReat: a new dimension in tunnelling
Java Logging discussion Log4j,Slf4j
Pycon Sec
ROS Hands-On Intro/Tutorial (Robotic Vision Summer School 2015) #RVSS #ACRV
Toward dynamic analysis of obfuscated android malware
Playing games-in-the-sandbox-dynamic-analysis-and-modern-evasion-tactics copy1
Fluentd Hacking Guide at RubyKaigi 2014
Is ruby logger thread(process)-safe? at RubyConf 2013
PyCon Taiwan 2013 Tutorial
Penetration testing using python
Debugging concurrency programs in go
Advanced debugging  techniques in different environments
AVTOKYO2013.5 Detail of CVE-2013-4787 (Master Key Vulnerability)
Fluentd - Set Up Once, Collect More
VB2013 - Security Research and Development Framework
Open source projects with python
Building robust and friendly command line applications in go
Ad

Viewers also liked (9)

PDF
[2014 CodeEngn Conference 11] 남대현 - iOS MobileSafari Fuzzer 제작 및 Fuzzing
PDF
[2014 CodeEngn Conference 11] 박한범 - 가상화 기술과 보안
PDF
[2014 CodeEngn Conference 11] 이경식 - 동적 추적 프레임워크를 이용한 OS X 바이너리 분석
PDF
[2014 CodeEngn Conference 11] 박세한 - IE 1DAY Case Study EN
PDF
[2014 CodeEngn Conference 11] 김호빈 - Android Bootkit Analysis KO
PDF
[2014 CodeEngn Conference 10] 노용환 - 디버거 개발, 삽질기
PDF
[2014 CodeEngn Conference 11] 김기홍 - 빅데이터 기반 악성코드 자동 분석 플랫폼
PDF
[2013 CodeEngn Conference 09] 김홍진 - 보안컨설팅 이해 및 BoB 보안컨설팅 인턴쉽
PDF
기업 IT 인프라 환경 최적화를 위한 하이브리드 클라우드 적용 방안 - AWS Summit Seoul 2017
[2014 CodeEngn Conference 11] 남대현 - iOS MobileSafari Fuzzer 제작 및 Fuzzing
[2014 CodeEngn Conference 11] 박한범 - 가상화 기술과 보안
[2014 CodeEngn Conference 11] 이경식 - 동적 추적 프레임워크를 이용한 OS X 바이너리 분석
[2014 CodeEngn Conference 11] 박세한 - IE 1DAY Case Study EN
[2014 CodeEngn Conference 11] 김호빈 - Android Bootkit Analysis KO
[2014 CodeEngn Conference 10] 노용환 - 디버거 개발, 삽질기
[2014 CodeEngn Conference 11] 김기홍 - 빅데이터 기반 악성코드 자동 분석 플랫폼
[2013 CodeEngn Conference 09] 김홍진 - 보안컨설팅 이해 및 BoB 보안컨설팅 인턴쉽
기업 IT 인프라 환경 최적화를 위한 하이브리드 클라우드 적용 방안 - AWS Summit Seoul 2017
Ad

More from Code Engn (20)

PDF
[2014 CodeEngn Conference 11] 최우석 - 자바스크립트 난독화 너네 뭐니?
PDF
[2014 CodeEngn Conference 11] 박세한 - IE 1DAY Case Study KO
PDF
[2014 CodeEngn Conference 11] 정든품바 - 웹성코드
PDF
[2014 CodeEngn Conference 10] 심준보 - 급전이 필요합니다
PDF
[2013 CodeEngn Conference 09] x15kangx - MS Office 2010 문서 암호화 방식 분석 결과
PDF
[2013 CodeEngn Conference 09] proneer - Malware Tracker
PDF
[2013 CodeEngn Conference 09] BlueH4G - hooking and visualization
PDF
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
PDF
[2013 CodeEngn Conference 09] 제갈공맹 - MS 원데이 취약점 분석 방법론
PDF
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
PDF
[2010 CodeEngn Conference 04] Max - Fighting against Botnet
PDF
[2010 CodeEngn Conference 04] window31 - Art of Keylogging 키보드보안과 관계없는 키로거들
PDF
[2010 CodeEngn Conference 04] hahah - Defcon 18 CTF 문제풀이
PDF
[2009 CodeEngn Conference 03] externalist - Reversing Undocumented File Forma...
PDF
[2009 CodeEngn Conference 03] hkpco - DEFCON CTF 2009 Binary Leetness 100-500...
PDF
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
PDF
[2009 CodeEngn Conference 03] sionics, kaientt - (파일바이러스 치료로직 개발자 입장에서 본) 파일 ...
PDF
[2013 CodeEngn Conference 08] pwn3r - Pwning multiplayer game - case Starcraf...
PDF
[2013 CodeEngn Conference 08] CherishCat - 각종 취약점과 대응방안 & 해킹, 보안 문제풀이
PDF
[2013 CodeEngn Conference 08] Homeless - Android 악성앱 필터링 시스템
[2014 CodeEngn Conference 11] 최우석 - 자바스크립트 난독화 너네 뭐니?
[2014 CodeEngn Conference 11] 박세한 - IE 1DAY Case Study KO
[2014 CodeEngn Conference 11] 정든품바 - 웹성코드
[2014 CodeEngn Conference 10] 심준보 - 급전이 필요합니다
[2013 CodeEngn Conference 09] x15kangx - MS Office 2010 문서 암호화 방식 분석 결과
[2013 CodeEngn Conference 09] proneer - Malware Tracker
[2013 CodeEngn Conference 09] BlueH4G - hooking and visualization
[2013 CodeEngn Conference 09] wh1ant - various tricks for linux remote exploits
[2013 CodeEngn Conference 09] 제갈공맹 - MS 원데이 취약점 분석 방법론
[2013 CodeEngn Conference 09] Park.Sam - 게임 해킹툴의 변칙적 공격 기법 분석
[2010 CodeEngn Conference 04] Max - Fighting against Botnet
[2010 CodeEngn Conference 04] window31 - Art of Keylogging 키보드보안과 관계없는 키로거들
[2010 CodeEngn Conference 04] hahah - Defcon 18 CTF 문제풀이
[2009 CodeEngn Conference 03] externalist - Reversing Undocumented File Forma...
[2009 CodeEngn Conference 03] hkpco - DEFCON CTF 2009 Binary Leetness 100-500...
[2009 CodeEngn Conference 03] koheung - 윈도우 커널 악성코드에 대한 분석 및 방법
[2009 CodeEngn Conference 03] sionics, kaientt - (파일바이러스 치료로직 개발자 입장에서 본) 파일 ...
[2013 CodeEngn Conference 08] pwn3r - Pwning multiplayer game - case Starcraf...
[2013 CodeEngn Conference 08] CherishCat - 각종 취약점과 대응방안 & 해킹, 보안 문제풀이
[2013 CodeEngn Conference 08] Homeless - Android 악성앱 필터링 시스템

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Business Ethics Teaching Materials for college
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Institutional Correction lecture only . . .
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Cell Structure & Organelles in detailed.
PDF
Complications of Minimal Access Surgery at WLH
PDF
Pre independence Education in Inndia.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
Business Ethics Teaching Materials for college
O5-L3 Freight Transport Ops (International) V1.pdf
Cell Types and Its function , kingdom of life
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Institutional Correction lecture only . . .
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
STATICS OF THE RIGID BODIES Hibbelers.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Microbial diseases, their pathogenesis and prophylaxis
Week 4 Term 3 Study Techniques revisited.pptx
Pharma ospi slides which help in ospi learning
Basic Mud Logging Guide for educational purpose
Cell Structure & Organelles in detailed.
Complications of Minimal Access Surgery at WLH
Pre independence Education in Inndia.pdf

[2014 CodeEngn Conference 10] 정광운 - 안드로이드에서도 한번 후킹을 해볼까 (Hooking on Android)

  • 2. Who am I • 정광운 EXSO (Not EXO) • 27 years old (Single) • CNU & Hackershool & Secu87 • Contact Me – http://facebook.com/exsociety – exsociety@gmail.com – http://bananapayload.org 2
  • 4. Android System Overview Applications Application Framework Libraries Linux Kernel Home ....... Music Browser Office Viewer Keypad Driver Display Driver WiFi Driver Camera Driver Flash Memory Driver Audio Driver Power Driver Binder Driver Notification Manager Package Manager Telephony Manager Resource Manager Location Manager Window Manager View System Activity Manager Content Providers OpenGL|ES Surface Manager Free Type SQLite SSL webkit_libmedia_lib libc viewer_lib SGL JNI NDK SDK 앱 레벨 (JAVA) 시스템 레벨 (C/C++) Hooking on ARM Hooking on Android 4
  • 5. G al • ARM 기반의 안드로이드 환경 • 시스템의 수정 X (단, 루팅 필요) • 애플리케이션의 수정 X • 애플리케이션의 라이브러리 내 함 수에 대한 후킹 수행 5
  • 6. Design of Hooker Shared Library (.so) FunctionA FunctionB Android Application Constructor Target Library Injected Library branch Function A Ins 2 Ins 3 Ins 4 Ins 1 Ins 2 Ins 3 Ins 1 Branch Hook_Function A Orig_Function A 6
  • 7. Shared Library Injection • Call dlopen() using ptrace() on application 7
  • 8. Shared Library Injection 1) Find dlopen() address Can not found libdl.so on maps /system/bin/linker 소스코드 中 dlopen() /system/bin/linker dlsysm() libdl.so offset dlopen() Address = base address of linker + offset 8
  • 9. 2) write library path - use stack - PTRACE_POKEDATA ptrace(PTRACE_POKEDATA, pid, dst address, 4byte_data) 9
  • 11. 3) Call dlopen() ß Thumb Debugger Application Backup Register Values Set Breakpoint at Next Instruction Change Register Values• pc = dlopen() addr • r0 = stack addr • r1 = 0 • lr = next instruction’s addr (pc) Restore Registers Values, Remove Breakpoint Debugger Applicatio n Backup Register Values Overwrite Code at Next Instruction Change Register Values• pc = pc+4 • r0 = stack addr • r1 = 0 • lr = next instruction’s addr (pc) Restore Registers Values, Restore Code , Remove Breakpoint • break • ldr pc, [pc, #0] • 0x0 • dlopen() address Thumb 모드 ARM 모드 11
  • 12. 4) result Useage : injector [pid] [Library Full Path] 12 끝
  • 13. Function Hooking 1) Find function information - Reference Header file - Use Hex-ray 13 https://github.com/EiNSTeiN-/hexrays-python
  • 14. 2) Install Hooker 14 Target Library Injected Library LDR PC, [PC] Function A NOP Hook_FunctionA Addr Ins 4 Ins 1 Ins 2 Ins 1 Ins 2 Ins 3 Branch Hook_Function A Orig_Function A Target Library Injected Library Function A Push {r5} add r5, pc, #4 ldr r5, {r5} bx r5 Hook_FunctionA Addr Ins 7 pop {r5} Ins 1 Ins 2 Ins 1 Ins 6 Branch Hook_Function A Orig_Function A ARM->ARM Thumb->ARM
  • 15. Why…? • Internal memory reference 15 Function A Orig_Function A Original Code JMP Target Function+N JMP Target Function+N Injected LibraryTarget Library Access Violation Reference Reference Data Offset Code Offet
  • 16. • External memory reference 16 Function A Target Function Original Function Original Code JMP Target Function+N JMP Target Function+N Injected LibraryTarget Library Branch Branch Access Violation
  • 17. Solution • 귀찮으니 그냥 복사하자… 17 Target Library Injected Library LDR PC, [PC] Function A NOP Hook_FunctionA Addr Ins 4 Ins 1 Ins 2 Hook_Function A Copied Target Library Function A Ins 1 Ins 2 Ins 3 Ins 4 Function Pointer
  • 18. Issue 1. Global Hook 18 • 애플리케이션 생성 과정 System Server zygote zygote’ Application ① 앱 실행 요청 ③ 앱 적재② fork() 호출 Activity Manager Activity Manager Package Manager Package Manager Window Manager Window Manager … Dalvik VM libc Dalvik VM libc Preloaded class Preloaded class Preloaded resource Preloaded resource Dalvik VM libc Preloaded class Preloaded class Preloaded resource Preloaded resource Dalvik VM libc Preloaded class Preloaded class Preloaded resource Preloaded resource Android Application
  • 19. Issue 2. 대상 라이브러리가 로드되기 전.. • 라이브러리 로드 함수를 후킹 – dlopen() = 10byte // 최소 12바이트 필요 – dvmLoadNativeCode(char const*, Object*, char**) • dvmLoadNativeCode 종료 시점에 추가적인 Hooker 설치 19
  • 20. How to use • download : http://bananapayload.org 20 [library path] [Name / Offset] [Function Type] /system/lib/libc.so malloc void *malloc(size_t size) /system/lib/test.so 0x400 void sub400(int, int) ./ genLibrarySource [define File] [output path] Define Format Source Code Edit Source & Edit makefile & make library Useage : injector [pid] [Library Full Path] Hook Success
  • 21. 21