From fe77dd35c67a0dfbd3004fefe97c689f8cfd3291 Mon Sep 17 00:00:00 2001 From: Sandor Nemes Date: Fri, 8 Jan 2016 15:46:59 +0100 Subject: Fixed a problem with the bind address not being used as the source address on outgoing TCP packets --- libmproxy/models/connections.py | 6 ++++-- libmproxy/protocol/base.py | 7 ++++--- libmproxy/protocol/http_replay.py | 4 ++-- libmproxy/proxy/modes/http_proxy.py | 1 + libmproxy/proxy/modes/reverse_proxy.py | 1 + libmproxy/proxy/modes/socks_proxy.py | 4 ++++ libmproxy/proxy/modes/transparent_proxy.py | 1 + 7 files changed, 17 insertions(+), 7 deletions(-) (limited to 'libmproxy') diff --git a/libmproxy/models/connections.py b/libmproxy/models/connections.py index 0991955d..0cfc17d4 100644 --- a/libmproxy/models/connections.py +++ b/libmproxy/models/connections.py @@ -88,8 +88,10 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): class ServerConnection(tcp.TCPClient, stateobject.StateObject): - def __init__(self, address): - tcp.TCPClient.__init__(self, address) + def __init__(self, address, source_address=None): + if source_address: + source_address = (source_address, 0) + tcp.TCPClient.__init__(self, address, source_address) self.via = None self.timestamp_start = None diff --git a/libmproxy/protocol/base.py b/libmproxy/protocol/base.py index af6b1c3b..8a1bea91 100644 --- a/libmproxy/protocol/base.py +++ b/libmproxy/protocol/base.py @@ -109,9 +109,9 @@ class ServerConnectionMixin(object): self.disconnect() """ - def __init__(self, server_address=None): + def __init__(self, server_address=None, source_address=None): super(ServerConnectionMixin, self).__init__() - self.server_conn = ServerConnection(server_address) + self.server_conn = ServerConnection(server_address, source_address) self.__check_self_connect() def __check_self_connect(self): @@ -157,10 +157,11 @@ class ServerConnectionMixin(object): """ self.log("serverdisconnect", "debug", [repr(self.server_conn.address)]) address = self.server_conn.address + source_address = self.server_conn.source_address()[0] self.server_conn.finish() self.server_conn.close() self.channel.tell("serverdisconnect", self.server_conn) - self.server_conn = ServerConnection(address) + self.server_conn = ServerConnection(address, source_address) def connect(self): """ diff --git a/libmproxy/protocol/http_replay.py b/libmproxy/protocol/http_replay.py index b7faad07..409810d5 100644 --- a/libmproxy/protocol/http_replay.py +++ b/libmproxy/protocol/http_replay.py @@ -46,7 +46,7 @@ class RequestReplayThread(threading.Thread): # In all modes, we directly connect to the server displayed if self.config.mode == "upstream": server_address = self.config.upstream_server.address - server = ServerConnection(server_address) + server = ServerConnection(server_address, self.config.host) server.connect() if r.scheme == "https": connect_request = make_connect_request((r.host, r.port)) @@ -68,7 +68,7 @@ class RequestReplayThread(threading.Thread): r.form_out = "absolute" else: server_address = (r.host, r.port) - server = ServerConnection(server_address) + server = ServerConnection(server_address, self.config.host) server.connect() if r.scheme == "https": server.establish_ssl( diff --git a/libmproxy/proxy/modes/http_proxy.py b/libmproxy/proxy/modes/http_proxy.py index c7502c24..27da57dd 100644 --- a/libmproxy/proxy/modes/http_proxy.py +++ b/libmproxy/proxy/modes/http_proxy.py @@ -16,6 +16,7 @@ class HttpProxy(Layer, ServerConnectionMixin): class HttpUpstreamProxy(Layer, ServerConnectionMixin): def __init__(self, ctx, server_address): super(HttpUpstreamProxy, self).__init__(ctx, server_address=server_address) + self.server_conn.source_address = (ctx.config.host, 0) def __call__(self): layer = self.ctx.next_layer(self) diff --git a/libmproxy/proxy/modes/reverse_proxy.py b/libmproxy/proxy/modes/reverse_proxy.py index 28f4e6f8..3ff22b6c 100644 --- a/libmproxy/proxy/modes/reverse_proxy.py +++ b/libmproxy/proxy/modes/reverse_proxy.py @@ -6,6 +6,7 @@ from ...protocol import Layer, ServerConnectionMixin class ReverseProxy(Layer, ServerConnectionMixin): def __init__(self, ctx, server_address, server_tls): super(ReverseProxy, self).__init__(ctx, server_address=server_address) + self.server_conn.source_address = (ctx.config.host, 0) self.server_tls = server_tls def __call__(self): diff --git a/libmproxy/proxy/modes/socks_proxy.py b/libmproxy/proxy/modes/socks_proxy.py index 264c734a..6e0b927b 100644 --- a/libmproxy/proxy/modes/socks_proxy.py +++ b/libmproxy/proxy/modes/socks_proxy.py @@ -8,6 +8,10 @@ from ...protocol import Layer, ServerConnectionMixin class Socks5Proxy(Layer, ServerConnectionMixin): + def __init__(self, ctx): + super(Socks5Proxy, self).__init__(ctx) + self.server_conn.source_address = (ctx.config.host, 0) + def __call__(self): try: # Parse Client Greeting diff --git a/libmproxy/proxy/modes/transparent_proxy.py b/libmproxy/proxy/modes/transparent_proxy.py index da1d4632..995e4bd9 100644 --- a/libmproxy/proxy/modes/transparent_proxy.py +++ b/libmproxy/proxy/modes/transparent_proxy.py @@ -8,6 +8,7 @@ from ...protocol import Layer, ServerConnectionMixin class TransparentProxy(Layer, ServerConnectionMixin): def __init__(self, ctx): super(TransparentProxy, self).__init__(ctx) + self.server_conn.source_address = (ctx.config.host, 0) self.resolver = platform.resolver() def __call__(self): -- cgit v1.2.3 From 918a457f5a6e4b3281f86ba31107d0a05082fb5e Mon Sep 17 00:00:00 2001 From: Sandor Nemes Date: Mon, 18 Jan 2016 09:55:46 +0100 Subject: Minor aesthetic fixes. --- libmproxy/models/connections.py | 2 -- libmproxy/protocol/base.py | 6 +++--- libmproxy/protocol/http_replay.py | 4 ++-- 3 files changed, 5 insertions(+), 7 deletions(-) (limited to 'libmproxy') diff --git a/libmproxy/models/connections.py b/libmproxy/models/connections.py index 0cfc17d4..f5dabe4e 100644 --- a/libmproxy/models/connections.py +++ b/libmproxy/models/connections.py @@ -89,8 +89,6 @@ class ClientConnection(tcp.BaseHandler, stateobject.StateObject): class ServerConnection(tcp.TCPClient, stateobject.StateObject): def __init__(self, address, source_address=None): - if source_address: - source_address = (source_address, 0) tcp.TCPClient.__init__(self, address, source_address) self.via = None diff --git a/libmproxy/protocol/base.py b/libmproxy/protocol/base.py index 8a1bea91..cf8771e0 100644 --- a/libmproxy/protocol/base.py +++ b/libmproxy/protocol/base.py @@ -109,9 +109,9 @@ class ServerConnectionMixin(object): self.disconnect() """ - def __init__(self, server_address=None, source_address=None): + def __init__(self, server_address=None): super(ServerConnectionMixin, self).__init__() - self.server_conn = ServerConnection(server_address, source_address) + self.server_conn = ServerConnection(server_address) self.__check_self_connect() def __check_self_connect(self): @@ -157,7 +157,7 @@ class ServerConnectionMixin(object): """ self.log("serverdisconnect", "debug", [repr(self.server_conn.address)]) address = self.server_conn.address - source_address = self.server_conn.source_address()[0] + source_address = self.server_conn.source_address self.server_conn.finish() self.server_conn.close() self.channel.tell("serverdisconnect", self.server_conn) diff --git a/libmproxy/protocol/http_replay.py b/libmproxy/protocol/http_replay.py index 409810d5..63870dfb 100644 --- a/libmproxy/protocol/http_replay.py +++ b/libmproxy/protocol/http_replay.py @@ -46,7 +46,7 @@ class RequestReplayThread(threading.Thread): # In all modes, we directly connect to the server displayed if self.config.mode == "upstream": server_address = self.config.upstream_server.address - server = ServerConnection(server_address, self.config.host) + server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": connect_request = make_connect_request((r.host, r.port)) @@ -68,7 +68,7 @@ class RequestReplayThread(threading.Thread): r.form_out = "absolute" else: server_address = (r.host, r.port) - server = ServerConnection(server_address, self.config.host) + server = ServerConnection(server_address, (self.config.host, 0)) server.connect() if r.scheme == "https": server.establish_ssl( -- cgit v1.2.3 From 61bd318622fbb3035ad2ff07315ef328e4aad503 Mon Sep 17 00:00:00 2001 From: Sandor Nemes Date: Mon, 18 Jan 2016 15:52:03 +0100 Subject: remove unnecessary source_address from proxy modes --- libmproxy/protocol/base.py | 2 +- libmproxy/proxy/modes/http_proxy.py | 1 - libmproxy/proxy/modes/reverse_proxy.py | 1 - libmproxy/proxy/modes/socks_proxy.py | 1 - libmproxy/proxy/modes/transparent_proxy.py | 1 - 5 files changed, 1 insertion(+), 5 deletions(-) (limited to 'libmproxy') diff --git a/libmproxy/protocol/base.py b/libmproxy/protocol/base.py index cf8771e0..d984cadb 100644 --- a/libmproxy/protocol/base.py +++ b/libmproxy/protocol/base.py @@ -111,7 +111,7 @@ class ServerConnectionMixin(object): def __init__(self, server_address=None): super(ServerConnectionMixin, self).__init__() - self.server_conn = ServerConnection(server_address) + self.server_conn = ServerConnection(server_address, (self.config.host, 0)) self.__check_self_connect() def __check_self_connect(self): diff --git a/libmproxy/proxy/modes/http_proxy.py b/libmproxy/proxy/modes/http_proxy.py index 27da57dd..c7502c24 100644 --- a/libmproxy/proxy/modes/http_proxy.py +++ b/libmproxy/proxy/modes/http_proxy.py @@ -16,7 +16,6 @@ class HttpProxy(Layer, ServerConnectionMixin): class HttpUpstreamProxy(Layer, ServerConnectionMixin): def __init__(self, ctx, server_address): super(HttpUpstreamProxy, self).__init__(ctx, server_address=server_address) - self.server_conn.source_address = (ctx.config.host, 0) def __call__(self): layer = self.ctx.next_layer(self) diff --git a/libmproxy/proxy/modes/reverse_proxy.py b/libmproxy/proxy/modes/reverse_proxy.py index 3ff22b6c..28f4e6f8 100644 --- a/libmproxy/proxy/modes/reverse_proxy.py +++ b/libmproxy/proxy/modes/reverse_proxy.py @@ -6,7 +6,6 @@ from ...protocol import Layer, ServerConnectionMixin class ReverseProxy(Layer, ServerConnectionMixin): def __init__(self, ctx, server_address, server_tls): super(ReverseProxy, self).__init__(ctx, server_address=server_address) - self.server_conn.source_address = (ctx.config.host, 0) self.server_tls = server_tls def __call__(self): diff --git a/libmproxy/proxy/modes/socks_proxy.py b/libmproxy/proxy/modes/socks_proxy.py index 6e0b927b..90788e37 100644 --- a/libmproxy/proxy/modes/socks_proxy.py +++ b/libmproxy/proxy/modes/socks_proxy.py @@ -10,7 +10,6 @@ from ...protocol import Layer, ServerConnectionMixin class Socks5Proxy(Layer, ServerConnectionMixin): def __init__(self, ctx): super(Socks5Proxy, self).__init__(ctx) - self.server_conn.source_address = (ctx.config.host, 0) def __call__(self): try: diff --git a/libmproxy/proxy/modes/transparent_proxy.py b/libmproxy/proxy/modes/transparent_proxy.py index 995e4bd9..da1d4632 100644 --- a/libmproxy/proxy/modes/transparent_proxy.py +++ b/libmproxy/proxy/modes/transparent_proxy.py @@ -8,7 +8,6 @@ from ...protocol import Layer, ServerConnectionMixin class TransparentProxy(Layer, ServerConnectionMixin): def __init__(self, ctx): super(TransparentProxy, self).__init__(ctx) - self.server_conn.source_address = (ctx.config.host, 0) self.resolver = platform.resolver() def __call__(self): -- cgit v1.2.3