Skip to content

Instantly share code, notes, and snippets.

@TheManchineel
Last active June 30, 2023 18:26
Show Gist options
  • Save TheManchineel/386717249f4fa0cc8a69d8122bbff245 to your computer and use it in GitHub Desktop.
Save TheManchineel/386717249f4fa0cc8a69d8122bbff245 to your computer and use it in GitHub Desktop.
import mitmproxy
from mitmproxy import ctx
from base64 import b64encode
origin_client_id = "5JHxEu-4wnFfBA"
custom_client_id = "YOUR_CLIENT_ID_HERE"
redirect_uri= "apollo://reddit-oauth"
authorize_url = f"client_id={origin_client_id}"
wanted_url = f"https://www.reddit.com/api/v1/authorize?client_id={custom_client_id}&response_type=code&state=RedditKit&redirect_uri={redirect_uri}&duration=permanent&scope=account,creddits,edit,flair,history,identity,livemanage,modconfig,modflair,modlog,modothers,modposts,modself,modwiki,mysubreddits,privatemessages,read,report,save,submit,subscribe,vote,wikiedit,wikiread,modcontributors,modtraffic,modmail,structuredstyles"
access_token_url = "https://www.reddit.com/api/v1/access_token"
fake_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15"
class FixApolloToken:
def response(self, flow: mitmproxy.http.HTTPFlow):
# check for the URL we want to intercept
if authorize_url in flow.request.pretty_url:
ctx.log.info("Intercepted log-in!")
# replace Apollo's client ID with custom client ID by redirecting
flow.response = mitmproxy.http.Response.make(
302, "", {"Location": wanted_url}
)
class RewriteUserAgent:
def request(self, flow: mitmproxy.http.HTTPFlow):
# at some point, Reddit stopped allowing mobile browser log-ins to third-party apps altogether, this makes it look like we're on Mac
flow.request.headers["User-Agent"] = fake_user_agent
class RewriteBasicAuthUsername:
def request(self, flow: mitmproxy.http.HTTPFlow):
# check for the URL we want to intercept
if flow.request.pretty_url == access_token_url:
ctx.log.info("Intercepted token request!")
# replace Apollo's client ID with custom client ID in the username field of the HTTP Basic auth header
flow.request.headers["Authorization"] = f"Basic {b64encode(f'{custom_client_id}:'.encode()).decode()}"
addons = [FixApolloToken(), RewriteBasicAuthUsername(), RewriteUserAgent()]
# mitmweb -s mitmproxy_script.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment