SlideShare a Scribd company logo
1 of 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!

More Related Content

What's hot

리플렉션과 가비지 컬렉션
리플렉션과 가비지 컬렉션리플렉션과 가비지 컬렉션
리플렉션과 가비지 컬렉션QooJuice
 
혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버iFunFactory Inc.
 
업적,칭호,타이틀 그게 뭐든간에...
업적,칭호,타이틀 그게 뭐든간에...업적,칭호,타이틀 그게 뭐든간에...
업적,칭호,타이틀 그게 뭐든간에...SeungYeon Jeong
 
ブループリント+ビジュアルスクリプトと仲良くやる方法
ブループリント+ビジュアルスクリプトと仲良くやる方法ブループリント+ビジュアルスクリプトと仲良くやる方法
ブループリント+ビジュアルスクリプトと仲良くやる方法Masahiko Nakamura
 
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사 NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사 Imseong Kang
 
행동 기반 게임오브젝트
행동 기반 게임오브젝트행동 기반 게임오브젝트
행동 기반 게임오브젝트kgun86
 
End to end test automation with cypress
End to end test automation with cypressEnd to end test automation with cypress
End to end test automation with cypressPankajSingh184960
 
Practical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on MobilesPractical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on MobilesValentin Simonov
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with JestMichał Pierzchała
 
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010Ryan Park
 
Unity - Internals: memory and performance
Unity - Internals: memory and performanceUnity - Internals: memory and performance
Unity - Internals: memory and performanceCodemotion
 
Cypress report
Cypress reportCypress report
Cypress reportAdarsh
 
빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)YEONG-CHEON YOU
 
Tips and experience of DX12 Engine development .
Tips and experience of DX12 Engine development .Tips and experience of DX12 Engine development .
Tips and experience of DX12 Engine development .YEONG-CHEON YOU
 
NDC 11 자이언트 서버의 비밀
NDC 11 자이언트 서버의 비밀NDC 11 자이언트 서버의 비밀
NDC 11 자이언트 서버의 비밀승명 양
 
독립 개발 4년차 리뷰
독립 개발 4년차 리뷰독립 개발 4년차 리뷰
독립 개발 4년차 리뷰Daehoon Han
 

What's hot (20)

리플렉션과 가비지 컬렉션
리플렉션과 가비지 컬렉션리플렉션과 가비지 컬렉션
리플렉션과 가비지 컬렉션
 
혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버혼자서 만드는 MMO게임 서버
혼자서 만드는 MMO게임 서버
 
업적,칭호,타이틀 그게 뭐든간에...
업적,칭호,타이틀 그게 뭐든간에...업적,칭호,타이틀 그게 뭐든간에...
업적,칭호,타이틀 그게 뭐든간에...
 
Iocp advanced
Iocp advancedIocp advanced
Iocp advanced
 
ブループリント+ビジュアルスクリプトと仲良くやる方法
ブループリント+ビジュアルスクリプトと仲良くやる方法ブループリント+ビジュアルスクリプトと仲良くやる方法
ブループリント+ビジュアルスクリプトと仲良くやる方法
 
Lock free queue
Lock free queueLock free queue
Lock free queue
 
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사 NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사
NDC 2018 '야생의 땅: 듀랑고' 초반 플레이 변천사
 
행동 기반 게임오브젝트
행동 기반 게임오브젝트행동 기반 게임오브젝트
행동 기반 게임오브젝트
 
End to end test automation with cypress
End to end test automation with cypressEnd to end test automation with cypress
End to end test automation with cypress
 
Practical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on MobilesPractical Guide for Optimizing Unity on Mobiles
Practical Guide for Optimizing Unity on Mobiles
 
Testing in go
Testing in goTesting in go
Testing in go
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
온라인 게임에서 사례로 살펴보는 디버깅 in NDC2010
 
Unity - Internals: memory and performance
Unity - Internals: memory and performanceUnity - Internals: memory and performance
Unity - Internals: memory and performance
 
Cypress report
Cypress reportCypress report
Cypress report
 
빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)빌드관리 및 디버깅 (2010년 자료)
빌드관리 및 디버깅 (2010년 자료)
 
Tips and experience of DX12 Engine development .
Tips and experience of DX12 Engine development .Tips and experience of DX12 Engine development .
Tips and experience of DX12 Engine development .
 
NDC 11 자이언트 서버의 비밀
NDC 11 자이언트 서버의 비밀NDC 11 자이언트 서버의 비밀
NDC 11 자이언트 서버의 비밀
 
Press Button, Drink Coffee : An Overview of UE4 build pipeline and maintenance
Press Button, Drink Coffee : An Overview of UE4 build pipeline and maintenancePress Button, Drink Coffee : An Overview of UE4 build pipeline and maintenance
Press Button, Drink Coffee : An Overview of UE4 build pipeline and maintenance
 
독립 개발 4년차 리뷰
독립 개발 4년차 리뷰독립 개발 4년차 리뷰
독립 개발 4년차 리뷰
 

Similar to BSidesDelhi 2018: Headshot - Game Hacking on macOS

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...Gerke Max Preussner
 
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...Gerke Max Preussner
 
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 SwitchUnity Technologies
 
C game programming - SDL
C game programming - SDLC game programming - SDL
C game programming - SDLWingston
 
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 GodlewskiPROIDEA
 
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 AzureMarco Parenzan
 
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?Dmitry Alexandrov
 
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 videosCharles Lefebvre
 
iOS Game Development With UIKit
iOS Game Development With UIKitiOS Game Development With UIKit
iOS Game Development With UIKitMartin Grider
 
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 Perspectivekfrdbs
 
Overview of graphics systems
Overview of  graphics systemsOverview of  graphics systems
Overview of graphics systemsJay Nagar
 
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 ...Unity Technologies
 
2004: Söldner - a Post Mortem
2004: Söldner - a Post Mortem2004: Söldner - a Post Mortem
2004: Söldner - a Post MortemTeut Weidemann
 
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...Amazon Web Services
 
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) Jean-Philippe Doiron
 

Similar to 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)
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

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