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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  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.

Alan Richardson user avatar by
Alan Richardson
·
Mar. 06, 19 · Tutorial
Like (1)
Save
Tweet
Share
16.63K 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.

Popular on DZone

  • Journey to Event Driven, Part 1: Why Event-First Programming Changes Everything
  • Container Security: Don't Let Your Guard Down
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • How To Build an Effective CI/CD Pipeline

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: