Completely faking connection to server in script
See original GitHub issueIs your feature request related to a problem? Please describe. I haven’t found a good way to completely intercept connections (and all requests) to a server from a script.
Describe the solution you’d like
Perhaps there should be a FakeLayer
or some other mechanism which would simulate the procedure of connecting to an upstream server, without actually doing so.
Describe alternatives you’ve considered
Here is the best solution I found so far:
from mitmproxy import http
from mitmproxy.proxy.protocol import TlsLayer
# The server to which we wish to intercept connections.
# When the hostname doesn't exist, this becomes a hard requirement.
_redirect_source = 'nonexisting.example.net'
# The server we will be redirecting the connection to.
# Can be any server which listens on the same ports as _redirect_source.
_redirect_target = 'www.google.com'
# Change the server address at connection time.
def serverconnect(cc):
if cc.address[0] == _redirect_source:
cc._my_real_address = cc.address # Save original address
cc.address = (_redirect_target, cc.address[1])
return True
# For TLS, we must also patch the SNI hostname.
# Otherwise, certificate validation will fail.
def next_layer(next_layer):
if isinstance(next_layer, TlsLayer):
if next_layer.server_conn.address[0] == _redirect_source:
next_layer._custom_server_sni = _redirect_target
def request(flow: http.HTTPFlow) -> None:
# Now, undo the hostname patch in serverconnect,
# so that mitmproxy's UI shows the right URL.
try:
flow.server_conn.address = flow.server_conn._my_real_address
flow.request.host = flow.server_conn.address[0]
flow.request.port = flow.server_conn.address[1]
except AttributeError:
pass
# Now send our fake response
flow.response = ...
Additional context
The above workaround is not perfect as it still does require a target server to redirect the connection to. (Using a local server poses difficulties as it would have to have a valid SSL certificate for the target redirect domain name.)
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Faking a FTP service - nmap - Stack Overflow
1 Answer 1 ... First, you're missing a \r\n at the end of your fake response that the match line needs. The other...
Read more >How to build a TCP proxy #2: Fake DNS Server | Robert Heaton
Next, find your laptop's local IP address by running a terminal command like ifconfig | grep 'en0' -A 2 | grep netmask (try...
Read more >3 Free Tools to Fake DNS Responses for Malware Analysis
If you don't want to configure a full-blown DNS server, you can use specialized tools such as ApateDNS, FakeDNS and fakeDNS.py. ApateDNS in ......
Read more >How to Fake or Mock an API with JSON Server - Atomic Spin
JSON Server is an easy-to-use module for mocking an API for most of your API needs, including custom routes, POST routes, and faking...
Read more >Faking SQL Server in Haskell - FP Complete
In order to implement a fake SQL Server, we need: A server program that accepts connections from clients and talks their language. A...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, setting
connection_strategy
tolazy
is exactly what should be done here. Thanks for the heads-up, @yufengzjj! 🍰@yufengzjj thanks! i will try that!