Downloading All Images From Mobile Requests

Published: Sunday, September 08, 2024

Wouldn’t it be cool to automatically have every image that appears on your phone, as a result of an HTTP request, get saved to your computer? Well, here you go.

  • Download mitmproxy linux binaries from https://www.mitmproxy.org.
  • Start mitmproxy on your desktop with ./mitmweb -s /path/to/mitmproxy_image_dl.py. The server will run on port 8080 by default. The addon, mitmproxy_image_dl.py, is included below.
  • Open up the firewall for 8080 on your desktop with something like:
    • sudo ufw allow from <IP address of your phone e.g. 192.168.1.46> to any port 8080
    • sudo ufw allow from 192.168.1.0/24 to any port 8080
  • On your Android phone, change your Wi-Fi settings for the current access point:
    • Proxy: Manual
    • Proxy hostname: <IP address of your desktop e.g. 192.168.1.45>
    • Proxy port: 8080
  • Turn off any VPN.
  • Toggle your WiFi off and on.
  • Open http://mitm.it in a browser to download a CA file.
  • Install the cert by either:
    • Opening the CA file in Files
    • Going to somewhere similar to Settings -> Security and privacy -> More security and privacy -> Encryption & credentials -> Install a certificate
  • If applicable, tap Install anyway to dismiss the warning and select the CA file.

Here is the content for mitmproxy_image_dl.py which makes the image downloading all possible.

import os
from mitmproxy import http # you might need to install mitmproxy with `python3.12 -m pip install mitmproxy`

class ImageSaver:
    def __init__(self, save_dir):
        self.save_dir = save_dir
        os.makedirs(self.save_dir, exist_ok=True)

    def response(self, flow: http.HTTPFlow):
        content_type = flow.response.headers.get("Content-Type", "")
        if "image" in content_type:
            image_ext = content_type.split("/")[1]
            if image_ext in ["jpeg", "png", "gif", "webp"]:
                filename = flow.request.url.split("/")[-1].split("?")[0]
                if not filename:
                    filename = f"image_{flow.id}.{image_ext}"
                elif not filename.endswith(f".{image_ext}"):
                    filename += f".{image_ext}"

                filepath = os.path.join(self.save_dir, filename)
                with open(filepath, "wb") as f:
                    f.write(flow.response.content)
                print(f"Saved: {filepath}")

addons = [
    ImageSaver(save_dir="/path/to/image_saving_directory")
]

Enjoy!

Comment
Optional
No comments yet...