SlideShare una empresa de Scribd logo
1 de 56
Headshot
Game Hacking on macOS
Jai Verma
About Me
• Engineer at Qualcomm
• CTF Player
• Pwn and RE
What?
• Make a basic trainer for an open-source FPS
game (Assault Cube)
• https://assault.cubers.net
• Aimbot, ESP, Unlimited Ammo…
• How to approach this problem?
Why?
• Little guided documentation online about
game hacking on macOS
• Lots of tutorials for Windows
• To win at PUBG :P
Things we need
• We need to read and modify game process memory
• We possibly need to run our code in context of the game
process
• Tools:
• Disassembler: IDA Pro, radare2, …
• Debugger: lldb, gdb
• DBI: Frida
• OS API: Mach API (Mach is part of the XNU kernel)
Things we need
•First of all we need to find our health and
ammo in memory so that we can change it
•How do we do this?
•Debugger? - too tedious, have to stop process
execution
•Cheat Engine? - very powerful and easy to use
•Frida! - fast and easy to use, lower level of
abstraction
Needle in the Haystack
• Memory.scanSync(address, size, pattern)
• Memory.readByteArray(address, length)
• Memory.writeByteArray(address, bytes)
What just happened!?
• That was Frida’s API being used for
modifying process memory
• This was highly abstracted and works on
multiple platforms - Windows, Linux, macOS,
iOS, Android
• Now you might be wondering? So how does
this actually work internally? SHOW ME MAC
SPECIFIC CODE! ANYONE CAN DO THIS WITH
FRIDA!
Under the microscope
• kern_return_t mach_vm_read(vm_map_t
target_task, mach_vm_address_t address,
mach_vm_size_t size, vm_offset_t *data,
mach_msg_type_number_t *dataCnt);
• kern_return_t mach_vm_write(vm_map_t
target_task, mach_vm_address_t address,
vm_offset_t data, mach_msg_type_number_t
dataCnt);
Now what?
•We’ve just found the address of our ammo for a
particular instance of the game
•This address might change from match to match
and will definitely change when we restart the
game since it is a heap address and we have ASLR
•We need to find our player object address on the
heap and then a pointer to our player object
which might be stored somewhere in a ‘rw-‘
segment of our game binary like the ‘.data’
segment
• Frida has a MemoryAccessMonitor API as well
which we could leverage if we were on
Windows (doesn’t support macOS yet)
• We’ll just take help from lldb instead
• We can use a watchpoint to monitor
instructions which write to our ammo
address which might be calculated by adding
an offset to our player object address
What was all that?
• Our health was at 100 to begin with
• Then we set a watchpoint which would be triggered
whenever any instruction writes to our health
address and the new value isn’t 100
• When the watchpoint is hit, we see that our health
has reduced to 84 (eax) which happens when you get
shot
• So clearly our health is at [esi+0xf8]
• So our player object should be at [esi]. Bingo!
• We can scan the ‘rw-‘ segments of the
address space our binary is mapped into for
our player object pointer
Making an aimbot
• What’s that?
• It automatically locks your aim on to your opponents
head so that you can easily kill them and show off your
mad skills
• Need to calculate yaw and pitch angles
• All this info is stored in our player object. Find the
offset just like we found health and ammo
• Similar to our player, all the enemy player object
pointers are stored in memory adjacent to our player
pointer
X
Z
α
Pitch (Side View)
(x1,
y1,
z1)
(x2,
y2,
z2)
X
Y
Yaw (Top View)
β
(x1,
y1,
z1)
(x2,
y2,
z2)
Pitch Demo
•Pitch = tan-1((z2-z1) / dist)
•Yaw = tan-1((y2-y1) / (x2-x1))
•Dist = Euclidean distance = 

√((x2-x1)2 + (y2-y1)2)
•This is a good start, but we also need to check
whether the enemy we’re locking on to is visible or
not
•Games define a function generally called TraceLine
which gives us coordinates and a boolean which
signifies whether the a line drawn from A to B
collides with anything
• So A here is us and B is the bad guy we want to
kill
• Since this function is defined in the game
binary and is present in the game process memory
while running, we need to find a way to call
this with our parameters
• Mach API to the rescue again
• kern_return_t thread_create_running(task_t
parent_task, thread_state_flavor_t flavor,
thread_state_t new_state, mach_msg_type_number_t
new_stateCnt, thread_act_t *child_act);
•thread_create_running creates and starts a
new thread with a state that we specify
•This state includes the processor registers
so we can execute our own code in the context
of the remote process by setting eip state
•For this we need to allocate a region of
memory to hold our code (r-x) and a region
for the function stack (rw-)
•This game is a 32-bit process so function
arguments are passed on the stack (x86)
• kern_return_t mach_vm_allocate(vm_map_t
target, mach_vm_address_t *address,
mach_vm_size_t size, int flags);
• kern_return_t mach_vm_protect(vm_map_t
target_task, mach_vm_address_t address,
mach_vm_size_t size, boolean_t set_maximum,
vm_prot_t new_protection);
Traceline Demo
• We can also use Frida’s NativeFunction API
to call process functions if they follow a
standard calling convention
• Or you can use x86Writer for more fine
tuned use cases
Aimbot Demo
What else?
• Alright so now we have a functional aimbot
which doesn’t blindly aim at walls
• ESP! - Extra Sensory Perception
• Draw bounding boxes on all enemies so that
we can easily find them, even through
walls!
Like Superman...
• Assault Cube uses OpenGL for rendering
• We can therefore call OpenGL functions for
our own use
• OpenGL rendering has to be done in the main
thread though!
• Or we could use Apple’s Cocoa API too
OpenGL Rendering Pipeline
• I won’t be going into the details of the
various transforms that one has to go through
to display an object on the screen
• You can read about them on this very helpful
website: http://www.songho.ca/opengl/
gl_transform.html
• All I’ll say is that we need to find a model-
view-projection matrix in process memory and
multiply enemy position coordinates with it to
get on-screen pixel coordinates
Local Space
• A generic rendering pipeline looks like
this:
View Space
Clip Space
Model Matrix World Space View Matrix
Projection
Matrix
Perspective Division
& Viewport Transform
Screen
Coordinates
• The only hard part is locating the mvp
matrix in memory
• After that it’s just some matrix
multiplication and calling OpenGL API
• But how do we actually call these functions
• We can use Frida’s Interceptor API to
attach to a function that is executed on
the main thread or completely replace a
function’s implementation with our own!
• This can easily be done using Mach API as
well. All we need are calls to vm_allocate,
vm_protect, vm_write to make a ‘code cave’
for our code
Before Interceptor.attach
After Interceptor.attach
ESP Demo
• The place where I’ve attached and inserted
my code is not ideal as it causes the
bounding boxes to flicker
• This is probably due to double buffering
used by OpenGL and I’m drawing my stuff on
the wrong buffer
A little bit of Cocoa
•Use Cocoa API?
•Create NSWindow as an overlay
•Create a transparent NSView and set that as
contentView of overlay NSWindow
•Draw bounding boxes in NSView by overriding
NSView’s [- drawRect:] function
•Set needsDisplay to 1 to tell NSView to redraw
bounding boxes
• Remember to call drawing functions for
Cocoa in main thread!
Cocoa Demo
• Flickering is gone :)
• But it’s too slow :(
• But all that’s my problem
• Both these issues can be fixed by proper
usage of the APIs
What else?
• Other possible methods for doing this are
dylib injection and method swizzling
• These techniques also work well for iOS
apps (both jailbroken and non-jailbroken)
• I wrote about hacking a minesweeper game
for iOS using these techniques and all the
details are present at https://
jaiverma.github.io/blog/ios-game-hacking if
you want to read about it
Resources and Thanks!
• Frida (https://frida.re/)
• https://github.com/rentzsch/mach_inject/ - big
help in understanding thread_create_running
• Rake from https://guidedhacking.com/ on
helping me understand OpenGL rendering
pipeline
• Apple Docs (https://developer.apple.com/
documentation/kernel/)
Conclusion
• All I did in this presentation was describe
how we can hack a game using Mach API, but
a lot of malicious things are possible when
you have control of a process
• This is usually the case when malware has
infected a system
• Malware can easily siphon off sensitive
information from applications to a remote
server
• I will post all code to GitHub soon
• https://github.com/jaiverma/
• Twitter: _jaiverma
Thank you!

Más contenido relacionado

La actualidad más candente

MMO Design Architecture by Andrew
MMO Design Architecture by AndrewMMO Design Architecture by Andrew
MMO Design Architecture by Andrew
Agate Studio
 
作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)
Ying wei (Joe) Chou
 
1 회사및게임소개자료
1 회사및게임소개자료1 회사및게임소개자료
1 회사및게임소개자료
정의 윤
 
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
devCAT Studio, NEXON
 
Beyond Gamification: 7 Core Concepts for Creating Compelling Products
Beyond Gamification: 7 Core Concepts for Creating Compelling Products Beyond Gamification: 7 Core Concepts for Creating Compelling Products
Beyond Gamification: 7 Core Concepts for Creating Compelling Products
Amy Jo Kim
 
Fosdem 18: Securing embedded Systems using Virtualization
Fosdem 18: Securing embedded Systems using VirtualizationFosdem 18: Securing embedded Systems using Virtualization
Fosdem 18: Securing embedded Systems using Virtualization
The Linux Foundation
 

La actualidad más candente (20)

Building And Releasing A Massively Multiplayer Online Game
Building And Releasing A Massively Multiplayer Online GameBuilding And Releasing A Massively Multiplayer Online Game
Building And Releasing A Massively Multiplayer Online Game
 
MMO Design Architecture by Andrew
MMO Design Architecture by AndrewMMO Design Architecture by Andrew
MMO Design Architecture by Andrew
 
Pf: the OpenBSD packet filter
Pf: the OpenBSD packet filterPf: the OpenBSD packet filter
Pf: the OpenBSD packet filter
 
GDC Europe 2014: Unreal Engine 4 for Programmers - Lessons Learned & Things t...
GDC Europe 2014: Unreal Engine 4 for Programmers - Lessons Learned & Things t...GDC Europe 2014: Unreal Engine 4 for Programmers - Lessons Learned & Things t...
GDC Europe 2014: Unreal Engine 4 for Programmers - Lessons Learned & Things t...
 
게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개게임제작개론 : #0 과목소개
게임제작개론 : #0 과목소개
 
게임서버프로그래밍 #4 - 멀티스레드 프로그래밍
게임서버프로그래밍 #4 - 멀티스레드 프로그래밍게임서버프로그래밍 #4 - 멀티스레드 프로그래밍
게임서버프로그래밍 #4 - 멀티스레드 프로그래밍
 
作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)作業系統數位教材(劉政雄)(1 9)
作業系統數位教材(劉政雄)(1 9)
 
1 회사및게임소개자료
1 회사및게임소개자료1 회사및게임소개자료
1 회사및게임소개자료
 
게임제작개론: #1 게임 구성 요소의 이해
게임제작개론: #1 게임 구성 요소의 이해게임제작개론: #1 게임 구성 요소의 이해
게임제작개론: #1 게임 구성 요소의 이해
 
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
이승재, 실버바인 서버엔진 2 설계 리뷰, NDC2018
 
Beyond Gamification: 7 Core Concepts for Creating Compelling Products
Beyond Gamification: 7 Core Concepts for Creating Compelling Products Beyond Gamification: 7 Core Concepts for Creating Compelling Products
Beyond Gamification: 7 Core Concepts for Creating Compelling Products
 
Fosdem 18: Securing embedded Systems using Virtualization
Fosdem 18: Securing embedded Systems using VirtualizationFosdem 18: Securing embedded Systems using Virtualization
Fosdem 18: Securing embedded Systems using Virtualization
 
게임제작개론 : #6 게임 시스템 구조에 대한 이해
게임제작개론 : #6 게임 시스템 구조에 대한 이해게임제작개론 : #6 게임 시스템 구조에 대한 이해
게임제작개론 : #6 게임 시스템 구조에 대한 이해
 
Acquisition - Retention - Monetization : Feeding the Funnel
Acquisition - Retention - Monetization : Feeding the FunnelAcquisition - Retention - Monetization : Feeding the Funnel
Acquisition - Retention - Monetization : Feeding the Funnel
 
[IGC 2017] 아마존 구승모 - 게임 엔진으로 서버 제작 및 운영까지
[IGC 2017] 아마존 구승모 - 게임 엔진으로 서버 제작 및 운영까지[IGC 2017] 아마존 구승모 - 게임 엔진으로 서버 제작 및 운영까지
[IGC 2017] 아마존 구승모 - 게임 엔진으로 서버 제작 및 운영까지
 
트레블헌터 개발기획서
트레블헌터 개발기획서트레블헌터 개발기획서
트레블헌터 개발기획서
 
게임제작개론 : #9 라이브 서비스
게임제작개론 : #9 라이브 서비스게임제작개론 : #9 라이브 서비스
게임제작개론 : #9 라이브 서비스
 
게임회사 실무용어 완전정복! 쿡앱스 용어정리집
게임회사 실무용어 완전정복! 쿡앱스 용어정리집 게임회사 실무용어 완전정복! 쿡앱스 용어정리집
게임회사 실무용어 완전정복! 쿡앱스 용어정리집
 
Curso introdução ao Software Livre
Curso introdução ao Software LivreCurso introdução ao Software Livre
Curso introdução ao Software Livre
 
Garena Online
Garena OnlineGarena Online
Garena Online
 

Similar a BSidesDelhi 2018: Headshot - Game Hacking on macOS

The Next Mainstream Programming Language: A Game Developer's Perspective
The Next Mainstream Programming Language: A Game Developer's PerspectiveThe Next Mainstream Programming Language: A Game Developer's Perspective
The Next Mainstream Programming Language: A Game Developer's Perspective
kfrdbs
 

Similar a BSidesDelhi 2018: Headshot - Game Hacking on macOS (20)

From Web to Mobile with Stage 3D
From Web to Mobile with Stage 3DFrom Web to Mobile with Stage 3D
From Web to Mobile with Stage 3D
 
Cocos2d programming
Cocos2d programmingCocos2d programming
Cocos2d programming
 
West Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
West Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...West Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
West Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
 
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
East Coast DevCon 2014: Game Programming in UE4 - Game Framework & Sample Pro...
 
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo SwitchBringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
Bringing Supernatural Thriller, "Oxenfree" to Nintendo Switch
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDL
 
4Developers 2015: Gamedev-grade debugging - Leszek Godlewski
4Developers 2015: Gamedev-grade debugging - Leszek Godlewski4Developers 2015: Gamedev-grade debugging - Leszek Godlewski
4Developers 2015: Gamedev-grade debugging - Leszek Godlewski
 
Build a serverless distributed Pong game with Azure
Build a serverless distributed Pong game with AzureBuild a serverless distributed Pong game with Azure
Build a serverless distributed Pong game with Azure
 
Java on the GPU: Where are we now?
Java on the GPU: Where are we now?Java on the GPU: Where are we now?
Java on the GPU: Where are we now?
 
Gdc gameplay replication in acu with videos
Gdc   gameplay replication in acu with videosGdc   gameplay replication in acu with videos
Gdc gameplay replication in acu with videos
 
Cocos2d game programming 2
Cocos2d game programming 2Cocos2d game programming 2
Cocos2d game programming 2
 
iOS Game Development With UIKit
iOS Game Development With UIKitiOS Game Development With UIKit
iOS Game Development With UIKit
 
Soc research
Soc researchSoc research
Soc research
 
Pong
PongPong
Pong
 
The Next Mainstream Programming Language: A Game Developer's Perspective
The Next Mainstream Programming Language: A Game Developer's PerspectiveThe Next Mainstream Programming Language: A Game Developer's Perspective
The Next Mainstream Programming Language: A Game Developer's Perspective
 
Overview of graphics systems
Overview of  graphics systemsOverview of  graphics systems
Overview of graphics systems
 
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
Developing and optimizing a procedural game: The Elder Scrolls Blades- Unite ...
 
2004: Söldner - a Post Mortem
2004: Söldner - a Post Mortem2004: Söldner - a Post Mortem
2004: Söldner - a Post Mortem
 
Building a World in the Clouds: MMO Architecture on AWS (MBL304) | AWS re:Inv...
Building a World in the Clouds: MMO Architecture on AWS (MBL304) | AWS re:Inv...Building a World in the Clouds: MMO Architecture on AWS (MBL304) | AWS re:Inv...
Building a World in the Clouds: MMO Architecture on AWS (MBL304) | AWS re:Inv...
 
Developing Next-Generation Games with Stage3D (Molehill)
Developing Next-Generation Games with Stage3D (Molehill) Developing Next-Generation Games with Stage3D (Molehill)
Developing Next-Generation Games with Stage3D (Molehill)
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 

BSidesDelhi 2018: Headshot - Game Hacking on macOS

  • 1. Headshot Game Hacking on macOS Jai Verma
  • 2. About Me • Engineer at Qualcomm • CTF Player • Pwn and RE
  • 3. What? • Make a basic trainer for an open-source FPS game (Assault Cube) • https://assault.cubers.net • Aimbot, ESP, Unlimited Ammo… • How to approach this problem?
  • 4. Why? • Little guided documentation online about game hacking on macOS • Lots of tutorials for Windows • To win at PUBG :P
  • 5. Things we need • We need to read and modify game process memory • We possibly need to run our code in context of the game process • Tools: • Disassembler: IDA Pro, radare2, … • Debugger: lldb, gdb • DBI: Frida • OS API: Mach API (Mach is part of the XNU kernel)
  • 6. Things we need •First of all we need to find our health and ammo in memory so that we can change it •How do we do this? •Debugger? - too tedious, have to stop process execution •Cheat Engine? - very powerful and easy to use •Frida! - fast and easy to use, lower level of abstraction
  • 7. Needle in the Haystack • Memory.scanSync(address, size, pattern) • Memory.readByteArray(address, length) • Memory.writeByteArray(address, bytes)
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. What just happened!? • That was Frida’s API being used for modifying process memory • This was highly abstracted and works on multiple platforms - Windows, Linux, macOS, iOS, Android • Now you might be wondering? So how does this actually work internally? SHOW ME MAC SPECIFIC CODE! ANYONE CAN DO THIS WITH FRIDA!
  • 13. Under the microscope • kern_return_t mach_vm_read(vm_map_t target_task, mach_vm_address_t address, mach_vm_size_t size, vm_offset_t *data, mach_msg_type_number_t *dataCnt); • kern_return_t mach_vm_write(vm_map_t target_task, mach_vm_address_t address, vm_offset_t data, mach_msg_type_number_t dataCnt);
  • 14. Now what? •We’ve just found the address of our ammo for a particular instance of the game •This address might change from match to match and will definitely change when we restart the game since it is a heap address and we have ASLR •We need to find our player object address on the heap and then a pointer to our player object which might be stored somewhere in a ‘rw-‘ segment of our game binary like the ‘.data’ segment
  • 15. • Frida has a MemoryAccessMonitor API as well which we could leverage if we were on Windows (doesn’t support macOS yet) • We’ll just take help from lldb instead • We can use a watchpoint to monitor instructions which write to our ammo address which might be calculated by adding an offset to our player object address
  • 16.
  • 17. What was all that? • Our health was at 100 to begin with • Then we set a watchpoint which would be triggered whenever any instruction writes to our health address and the new value isn’t 100 • When the watchpoint is hit, we see that our health has reduced to 84 (eax) which happens when you get shot • So clearly our health is at [esi+0xf8] • So our player object should be at [esi]. Bingo!
  • 18. • We can scan the ‘rw-‘ segments of the address space our binary is mapped into for our player object pointer
  • 19. Making an aimbot • What’s that? • It automatically locks your aim on to your opponents head so that you can easily kill them and show off your mad skills • Need to calculate yaw and pitch angles • All this info is stored in our player object. Find the offset just like we found health and ammo • Similar to our player, all the enemy player object pointers are stored in memory adjacent to our player pointer
  • 20.
  • 24. •Pitch = tan-1((z2-z1) / dist) •Yaw = tan-1((y2-y1) / (x2-x1)) •Dist = Euclidean distance = 
 √((x2-x1)2 + (y2-y1)2) •This is a good start, but we also need to check whether the enemy we’re locking on to is visible or not •Games define a function generally called TraceLine which gives us coordinates and a boolean which signifies whether the a line drawn from A to B collides with anything
  • 25. • So A here is us and B is the bad guy we want to kill • Since this function is defined in the game binary and is present in the game process memory while running, we need to find a way to call this with our parameters • Mach API to the rescue again • kern_return_t thread_create_running(task_t parent_task, thread_state_flavor_t flavor, thread_state_t new_state, mach_msg_type_number_t new_stateCnt, thread_act_t *child_act);
  • 26. •thread_create_running creates and starts a new thread with a state that we specify •This state includes the processor registers so we can execute our own code in the context of the remote process by setting eip state •For this we need to allocate a region of memory to hold our code (r-x) and a region for the function stack (rw-) •This game is a 32-bit process so function arguments are passed on the stack (x86)
  • 27. • kern_return_t mach_vm_allocate(vm_map_t target, mach_vm_address_t *address, mach_vm_size_t size, int flags); • kern_return_t mach_vm_protect(vm_map_t target_task, mach_vm_address_t address, mach_vm_size_t size, boolean_t set_maximum, vm_prot_t new_protection);
  • 29. • We can also use Frida’s NativeFunction API to call process functions if they follow a standard calling convention • Or you can use x86Writer for more fine tuned use cases
  • 30.
  • 32. What else? • Alright so now we have a functional aimbot which doesn’t blindly aim at walls • ESP! - Extra Sensory Perception • Draw bounding boxes on all enemies so that we can easily find them, even through walls!
  • 34. • Assault Cube uses OpenGL for rendering • We can therefore call OpenGL functions for our own use • OpenGL rendering has to be done in the main thread though! • Or we could use Apple’s Cocoa API too
  • 35. OpenGL Rendering Pipeline • I won’t be going into the details of the various transforms that one has to go through to display an object on the screen • You can read about them on this very helpful website: http://www.songho.ca/opengl/ gl_transform.html • All I’ll say is that we need to find a model- view-projection matrix in process memory and multiply enemy position coordinates with it to get on-screen pixel coordinates
  • 36. Local Space • A generic rendering pipeline looks like this: View Space Clip Space Model Matrix World Space View Matrix Projection Matrix Perspective Division & Viewport Transform Screen Coordinates
  • 37. • The only hard part is locating the mvp matrix in memory • After that it’s just some matrix multiplication and calling OpenGL API
  • 38.
  • 39.
  • 40. • But how do we actually call these functions • We can use Frida’s Interceptor API to attach to a function that is executed on the main thread or completely replace a function’s implementation with our own! • This can easily be done using Mach API as well. All we need are calls to vm_allocate, vm_protect, vm_write to make a ‘code cave’ for our code
  • 41.
  • 45. • The place where I’ve attached and inserted my code is not ideal as it causes the bounding boxes to flicker • This is probably due to double buffering used by OpenGL and I’m drawing my stuff on the wrong buffer
  • 46. A little bit of Cocoa •Use Cocoa API? •Create NSWindow as an overlay •Create a transparent NSView and set that as contentView of overlay NSWindow •Draw bounding boxes in NSView by overriding NSView’s [- drawRect:] function •Set needsDisplay to 1 to tell NSView to redraw bounding boxes
  • 47.
  • 48.
  • 49. • Remember to call drawing functions for Cocoa in main thread!
  • 51. • Flickering is gone :) • But it’s too slow :( • But all that’s my problem • Both these issues can be fixed by proper usage of the APIs
  • 52. What else? • Other possible methods for doing this are dylib injection and method swizzling • These techniques also work well for iOS apps (both jailbroken and non-jailbroken) • I wrote about hacking a minesweeper game for iOS using these techniques and all the details are present at https:// jaiverma.github.io/blog/ios-game-hacking if you want to read about it
  • 53. Resources and Thanks! • Frida (https://frida.re/) • https://github.com/rentzsch/mach_inject/ - big help in understanding thread_create_running • Rake from https://guidedhacking.com/ on helping me understand OpenGL rendering pipeline • Apple Docs (https://developer.apple.com/ documentation/kernel/)
  • 54. Conclusion • All I did in this presentation was describe how we can hack a game using Mach API, but a lot of malicious things are possible when you have control of a process • This is usually the case when malware has infected a system • Malware can easily siphon off sensitive information from applications to a remote server
  • 55. • I will post all code to GitHub soon • https://github.com/jaiverma/ • Twitter: _jaiverma