DEV Community

Cover image for Modifying a site's JavaScript with mitmproxy
fx2301
fx2301

Posted on

4 3

Modifying a site's JavaScript with mitmproxy

Why?

You want to bypass client-side constraints in a JavaScript app you're hacking.

When?

Easiest to do when <script integrity> (Subresource Integrity) is not in use.

How?

mitmdump -s myscript.py
Enter fullscreen mode Exit fullscreen mode

myscript.py:

from mitmproxy import http

def response(flow: http.HTTPFlow) -> None:
  if flow.response and flow.response.content:
    flow.response.content = flow.response.content.replace(
      UNHACKED_FRAGMENT,
      HACKED_FRAGMENT
    )
Enter fullscreen mode Exit fullscreen mode

Gotchas

One mistake you can make it trying to replace the fragments of code you see in your browser debugger. That won't necessarily correspond 1-1 (e.g. in the case of unobfuscated code). That's why the examples here don't match against variable names or internal function names.

Examples

Disabling logic guards

unhacked.js:

if (!email.endswith('@trusted.com')) {
  return;
}
Enter fullscreen mode Exit fullscreen mode

hacked.js:

if (!email.includes('@')) {
  return;
}
Enter fullscreen mode Exit fullscreen mode

script.py:

flow.response.content = flow.response.content.replace(
  b'endsWith("@trusted.com")',
  b'includes("@")'
)
Enter fullscreen mode Exit fullscreen mode

Adding allowed file extensions for upload

unhacked.js:

const allowed = ['png', 'jpg'];
Enter fullscreen mode Exit fullscreen mode

hacked.js:

const allowed = ['png', 'exe', 'jpg'];
Enter fullscreen mode Exit fullscreen mode

script.py:

flow.response.content = flow.response.content.replace(
  b"'png',",
  b"'png','exe',"
)
Enter fullscreen mode Exit fullscreen mode

Art licensed under Creative Commons by OpenClipart-Vectors

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up