DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • An Introduction to Object Mutation in JavaScript
  • Metaprogramming With Proxies and Reflect in JavaScript
  • A Comprehensive Guide To Working With JSON in JavaScript
  • JavaScript's Secret Weapon: Unraveling the Mysteries of Proxies

Trending

  • Agentic AI for Automated Application Security and Vulnerability Management
  • Unlocking Data with Language: Real-World Applications of Text-to-SQL Interfaces
  • DGS GraphQL and Spring Boot
  • Infrastructure as Code (IaC) Beyond the Basics
  1. DZone
  2. Coding
  3. Languages
  4. Hacking JavaScript Games: Accessing Private JS Variables at Runtime via Debugging

Hacking JavaScript Games: Accessing Private JS Variables at Runtime via Debugging

When variables and objects I need are created from within anonymous functions I can't get access. In this post, I will explain how to access them.

By 
Alan Richardson user avatar
Alan Richardson
·
Mar. 06, 19 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
18.7K Views

Join the DZone community and get the full member experience.

Join For Free

I like to write little bots from the console to help with application and game automation. But when the variables and objects I need are created from within anonymous functions I can't get access. In this post, I will explain how to access them.

I've tried writing helper code to access the methods within objects and trawl through the DOM to find specifically named objects, and that can help with a large application where the objects are all public.

But when the application is essentially one anonymous function kept alive by event handlers and timers, how do we access the objects?

The short answer is:

  • set a breakpoint on the line of code that uses the object
  • in the console when the breakpoint activates, create a new reference to the object from the window

For example, if there is a pacman object I want to access, then find a line that uses it, breakpoint the line, then from the console:

window.pacman = pacman 

And I now have access to the private pacman object from the console when not in debug mode because the window now has a reference to it.

Manual Step

This requires a manual step before any of my automated bots can be used but if I write down what to 'search for' to find the line in the code then it is pretty easy to repeat.

Security by Private Objects

If we are relying on the objects being private and inaccessible due to the anonymous function then we really shouldn't because if it is in our browser, the user can gain access to it.

Always have protection on your server-side to handle anything thrown at it.

Opens Up New Options

This opens up a bunch of new options for testing and automating modern JavaScript approaches for me.

This can't really be used for continuous integration automating or fully autonomous code injection since there is a manual step involved. But for automating tactically where the user is present, this opens up more possibilities.

Want to See it in Action?

Using the simple open source Pacman clone from platzh1rsch.ch where all the code is wrapped and called from an anonymous function. I show the steps and thought processes for gaining console access to the main game and pacman objects to allow me to write infinite life cheats.

  • http://pacman.platzh1rsch.ch/
  • https://github.com/platzhersh/pacman-canvas
  • http://platzh1rsch.ch/

Object (computer science) JavaScript

Published at DZone with permission of Alan Richardson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • An Introduction to Object Mutation in JavaScript
  • Metaprogramming With Proxies and Reflect in JavaScript
  • A Comprehensive Guide To Working With JSON in JavaScript
  • JavaScript's Secret Weapon: Unraveling the Mysteries of Proxies

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!