question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Seeking help on how to dump flows and start mitmproxy all from a script

See original GitHub issue
Steps to reproduce the problem:

Not an issue. I am failing to understand how i can start the proxy from a script, and then write any flows on a file. Here is my script so far. Script is a put together form various script ideas by googling around. didnt want to create an issue, but couldnt really get in help on twitter/slack/irc etc. I understand this is simper to do using subprocess, but i am not trying to use subprocess

import os
import sys

from mitmproxy import http
# from mitmproxy.master import Master
import mitmproxy.master as master
from mitmproxy.addons import core
from mitmproxy.proxy import ProxyConfig, ProxyServer
from mitmproxy.options import Options
from mitmproxy import io, http


class Writer(master.Master, http.HTTPFlow):
    def __init__(self, options, server, file_path):
        master.Master.__init__(self, options)
        self.server = server
        self.file_path = file_path
        self.f = open(self.file_path, 'wb+')
        self.w = io.FlowWriter(self.f)

    def run(self):
        try:
            master.Master.run(self)
        except KeyboardInterrupt:
            self.shutdown()
            sys.exit(0)
    
    def writestuff(self):
        flo = http.HTTPFlow
        print(flo)
        self.w.add(flo)


def start_proxy(port, file_path):
    options = Options(
        listen_port=port,
    )
    config = ProxyConfig(
        options=options,
    )
    server = ProxyServer(config)
    # print(dir(http.HTTPFlow))
    m = Writer(options, server, file_path)
    m.addons.add(core.Core())
    m.run()
    m.writestuff()

start_proxy(8080, '/tmp/dumptest')
System information

Mitmproxy: 4.0.4 Python: 3.7.0 OpenSSL: OpenSSL 1.1.0h 27 Mar 2018 Platform: Darwin-16.7.0-x86_64-i386-64bit

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
0xEmanuelcommented, Nov 6, 2018

This worked for me: (remove mode='transparent', if you dont want to run mitmproxy in transparent mode)

from mitmproxy import proxy, options
from mitmproxy.tools.dump import DumpMaster

class AddHeader:
    def request(self, flow):
        if flow.request.pretty_host == "example.org":
            flow.request.host = "mitmproxy.org"

def start():
    myaddon = AddHeader()
    opts = options.Options(listen_host='0.0.0.0', listen_port=8080, mode='transparent', confdir='/home/user/.mitmproxy')
    pconf = proxy.config.ProxyConfig(opts)
    m = DumpMaster(opts)
    m.server = proxy.server.ProxyServer(pconf)
    m.addons.add(myaddon)

    try:
        m.run()
    except KeyboardInterrupt:
        m.shutdown()

start()

Another alternative without subprocess, but probably not what you want:

from mitmproxy.tools import _main
_main.mitmdump(["--mode", "transparent", "--set", "confdir=/home/user/.mitmproxy", "-s", "/home/user/myscript_containing_addons.py" ])
1reaction
xuelliucommented, Jan 10, 2019

options.Options(block_global=False, listen_host=‘0.0.0.0’, listen_port=listen_port, mode = mode, output_path = output_path, scripts = scripts)

using this way, error raised: ‘Unknown options: block_global, output_path, scripts’

how to add those option supports which are not in Options?

Using this way, it works. Cool~~~

    from mitmproxy.addons import block

    pconf = proxy.config.ProxyConfig(opts)
    m = DumpMaster(opts)
    m.addons.add(block)
    m.options.set('block_global=false')
    m.server = proxy.server.ProxyServer(pconf)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Examples - mitmproxy docs
Enable response streaming for all HTTP flows. ... This script demonstrates how to generate a mitmproxy dump file, as it would also be...
Read more >
Mitmproxy how to launch from script, and save dumps to file
Issue is, this creates an error AttributeError: No such option: body_size_limit which seems to be mitigated with master.addons.add(core.Core) ...
Read more >
Mitmproxy Cheat Sheet & Quick Reference - QuickRef.ME
mitmproxy is a free and open source interactive HTTPS proxy. This is a quick reference cheat sheet to the mitmproxy.
Read more >
Inline Scripts - Introduction — mitmproxy 0.18 documentation
mitmproxy has a powerful scripting API that allows you to modify flows on-the-fly or rewrite previously saved flows locally. All events that deal...
Read more >
Mitmproxy Cheat Sheet - stut-it Martin Stut
When starting to use Mitmproxy (see also my blog post about setting it ... d, delete flow ... w, save all flows matching...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found