mitmproxy load script using API (Python) - python

Good day,
I am trying to implement the mitmproxy into a bigger application.
For that, I need to be able to load those so called inline scripts in my code and not via command line. I could not find any helpful information about that in the documentation.
I am using mitmproxy version 0.17 and Python 2.7.
I know there is a newer version available, but that one didnt worked using the code examples.
This is the base code I have:
from mitmproxy import controller, proxy
from mitmproxy.proxy.server import ProxyServer
class ProxyMaster(controller.Master):
def __init__(self, server):
controller.Master.__init__(self, server)
def run(self):
try:
return controller.Master.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, flow):
flow.reply()
def handle_response(self, flow):
flow.reply()
config = proxy.ProxyConfig(port=8080)
server = ProxyServer(config)
m = ProxyMaster(server)
m.run()
How could I run this proxy using inline scripts?
Thanks in advance

I figured myself out a really ugly workaround.
Instead of using controller.Master I had to use flow.FlowMaster as the controller.Master lib does not seem to be able to handle inline scripts.
For some reason just loading the files did not work, they get triggered immediately, but not by running their matching hooks.
Instead of using the hooks which are not working, I am loading the matching functions as you can see in handle_response (try/except is missing and threading could be useful)
from mitmproxy import flow, proxy
from mitmproxy.proxy.server import ProxyServer
import imp
class ProxyMaster(flow.FlowMaster):
def run(self):
try:
return flow.FlowMaster.run(self)
except KeyboardInterrupt:
self.shutdown()
def handle_request(self, flow):
flow.reply()
def handle_response(self, flow):
for inline_script in self.scripts:
script_file = imp.load_source("response", inline_script.filename)
script_file.response(self, flow)
flow.reply()
proxy_config = proxy.ProxyConfig(port=8080)
server = ProxyServer(proxy_config)
state = flow.State()
m = ProxyMaster(server, state)
m.load_script("upsidedowninternet.py")
m.load_script("add_header.py")
m.run()
Any ideas about doing it the right way are appreciated.

Related

Is there any way to use pyswip and Flask?

I have an python application whose interface is implemented in Flask and i have a module in backend that use pyswip library. The module works perfectly when i run it separately from the application. As i searched, it seems that pyswip is not thread safe.
I get this error on consult function:
swipl_fid = PL_open_foreign_frame()
OSError: exception: access violation reading 0x00000028
I could try to use another SWI-Prolog library, but in my application i need to consult and external .pl file.
Is there any way i could make it work?
Here's how i use the pyswip library:
from pyswip_alt import Prolog
class My_Prolog():
def __init__(self, query):
self.query = query.split()
self.query = ', '.join(self.query)
self.query = '['+self.query + ']'
self.documents_path = "my/path"
self.prolog = Prolog()
self.prolog.consult("facts.pl")
self.prolog_results = []
self.final_result = ''
def process(self):
for res in self.prolog.query("complex_phrase("+self.query+", F)."):
result = []
for atom in res['F']:
result.append(atom.value)
self.prolog_results.append(result)
def run(self):
self.process()
self.final_result = ' '.join(self.final_result)
return self.final_result
And that's the way i use the class:
nl = My_Prolog(query)
nl_query = nl.run()
and all of this is in a function that is run by Flask module.
Simply use a lock?
from multiprocessing import Lock
prologlock = Lock()
#app.route(...)
def handle_x():
with prologlock:
return MyProlog.handle_x()

Use Python to manage content in "Enterprise Architect" software from "sparx systems"

Currently i am working with enterprise architect software for creating packages, diagrams.
Is it possible to work in Enterprise architect software using python script ? Some example like Deleting and creating packages and diagrams etc. If so refer example code or link.
Sure, that's no problem.
import win32com.client
from singleton import Singleton
#Singleton
class Repository:
def __init__(self):
try:
self.eaRep = win32com.client.Dispatch("EA.App").Repository
models = self.eaRep.models
done = True
except Exception as e:
print (e)
done = False
(The #Singleton can be found on the net but you can work without it.)
Then in your main program you can access the repository like
rep = repository.Repository.Instance()
print rep.modules.getAt(0).name
etc. Have fun
import win32com.client
def open_repository(path, login, password):
eaApp = win32com.client.Dispatch("EA.App")
eaRep = eaApp.Repository
if login:
eaRep.SuppressSecurityDialog = True
eaRep.OpenFile2(path, login, password)
else:
eaRep.OpenFile(path)
return eaRep
please use OpenFile for open your model. (OpenFile2 if your model has enabled security)

Sublime Text 3: Simple plug-in that changes color theme depending on remote host

Setup: I use Sublime Text 3 (ST), and I often have 2-3 different sessions with Sublime + iTerm2 open in different remote workspaces using RemoteSubl.
Using a simple batch script, I have set my iTerm2 to change colours (by activating a different iTerm user) when I ssh into a different host.
I was wondering if the same could be done for RemoteSubl? Such that when I open something from a specific host/ip/port, then Sublime opens in a different colour scheme, depending on the host/ip/port.
Solution attempt: So far, this is my attempt at building a small plugin that changes colour scheme when host is remote_host.
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'remote_host':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass
Problem: When using using view.settings().get('remote_subl.host') in the console it works fine, and returns remote_host. However, when running the script view.run_command('example') I get the "Not on remote_host" print, indicating that the try loop fails for some reason.
After Keiths suggestions:
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
view = self.view
host = view.settings().get('remote_subl.host', None)
print(host)
if host:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
if host is None:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Monokai.tmTheme')
print(view.settings().get('color_scheme'))
view isn't an argument that is passed to the TextCommand's run method. Instead, it is a property on self. Changing it to the following should work:
import sublime
import sublime_plugin
class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'dsintmain':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass
I would also recommend printing the exception that occurs to help debug things like this in future. Even better, rather than expecting an exception in normal usage, provide a default value to the get method on the settings (i.e. None) and remove the exception handling altogether.
host = view.settings().get('remote_subl.host', None)
That way, if something does really go wrong, you'll see the traceback in the ST console.

How to use correctly importlib in a flask controller?

I am trying to load a module according to some settings. I have found a working solution but I need a confirmation from an advanced python developer that this solution is the best performance wise as the API endpoint which will use it will be under heavy load.
The idea is to change the working of an endpoint based on parameters from the user and other systems configuration. I am loading the correct handler class based on these settings. The goal is to be able to easily create new handlers without having to modify the code calling the handlers.
This is a working example :
./run.py :
from flask import Flask, abort
import importlib
import handlers
app = Flask(__name__)
#app.route('/')
def api_endpoint():
try:
endpoint = "simple" # Custom logic to choose the right handler
handlerClass = getattr(importlib.import_module('.'+str(endpoint), 'handlers'), 'Handler')
handler = handlerClass()
except Exception as e:
print(e)
abort(404)
print(handlerClass, handler, handler.value, handler.name())
# Handler processing. Not yet implemented
return "Hello World"
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080, debug=True)
One "simple" handler example. A handler is a module which needs to define an Handler class :
./handlers/simple.py :
import os
class Handler:
def __init__(self):
self.value = os.urandom(5)
def name(self):
return "simple"
If I understand correctly, the import is done on each query to the endpoint. It means IO in the filesystem with lookup for the modules, ...
Is it the correct/"pythonic" way to implement this strategy ?
Question moved to codereview. Thanks all for your help : https://codereview.stackexchange.com/questions/96533/extension-pattern-in-a-flask-controller-using-importlib
I am closing this thread.

IE8 automation and https

I'm trying to use IE8 through COM to access a secured site (namely, SourceForge), in Python. Here is the script:
from win32com.client import gencache
from win32com.client import Dispatch
import pythoncom
gencache.EnsureModule('{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}', 0, 1, 1)
class SourceForge(object):
def __init__(self, baseURL='https://sourceforget.net/', *args, **kwargs):
super(SourceForge, self).__init__(*args, **kwargs)
self.__browser = Dispatch('InternetExplorer.Application')
self.__browser.Visible = True
self.__browser.Navigate(baseURL)
def run(self):
while True:
pythoncom.PumpMessages()
def main():
sf = SourceForge()
sf.run()
if __name__ == '__main__':
main()
If I launch IE by hand, fine. If I launch the script, I get a generic error page "Internet Explorer cannot display this page". If I change baseURL to use http instead of https, the script works. I guess this is some security "feature". I tried adding the site to the list of trusted sites. I tried to enable IE scripting in the options for the Internet zone. Doesn't work. Google was no help.
So, does anybody know something about this ? Is there a mysterious option to enable or am I doomed ?
I'm on Windows XP SP3 BTW, Python 2.5 and pywin32 build 213.
I can't open https://sourceforget.net/ -- not by hand, not by script.
Are you sure this link is right?

Categories