Skip to content

Instantly share code, notes, and snippets.

@wodim wodim/proxy.py
Last active Oct 26, 2015

Embed
What would you like to do?
MITM proxy with Twisted. Can be used to tamper with traffic by modifying the rules.json file while it is running.
LISTEN_PORT = 8085
SERVER_PORT = 8085
SERVER_ADDR = "37.187.171.215"
BIND_ADDR = ("10.14.88.15", 0)
from twisted.internet import protocol, reactor
import json
class RulesLoader():
def reread(self, mode, data):
with open("rules.json") as fp:
rules = json.load(fp)
for a, b in rules[mode].items():
data = data.replace(a.decode("hex"), b.decode("hex"))
return data
def incoming(self, data):
return self.reread("incoming", data)
def outgoing(self, data):
return self.reread("outgoing", data)
class ServerProtocol(protocol.Protocol):
def __init__(self):
self.buffer = None
self.client = None
self.rules = RulesLoader()
def connectionMade(self):
factory = protocol.ClientFactory()
factory.protocol = ClientProtocol
factory.server = self
reactor.connectTCP(SERVER_ADDR, SERVER_PORT,
factory=factory, bindAddress=BIND_ADDR)
# Client => Proxy
def dataReceived(self, data):
if self.client:
self.client.write(data)
else:
self.buffer = data
# Proxy => Client
def write(self, data):
print "IN ", repr(data)
data = self.rules.incoming(data)
self.transport.write(data)
class ClientProtocol(protocol.Protocol):
def connectionMade(self):
self.factory.server.client = self
self.write(self.factory.server.buffer)
self.factory.server.buffer = ''
self.rules = RulesLoader()
# Server => Proxy
def dataReceived(self, data):
self.factory.server.write(data)
# Proxy => Server
def write(self, data):
if not data:
return
print "OUT", repr(data)
data = self.rules.outgoing(data)
self.transport.write(data)
def main():
factory = protocol.ServerFactory()
factory.protocol = ServerProtocol
reactor.listenTCP(LISTEN_PORT, factory)
reactor.run()
if __name__ == '__main__':
main()
{
"incoming": {
"526174": "436174"
},
"outgoing": {
"686579": "627965"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.