Repository navigation
Add dtls support - #5397
Add dtls support#5397
Conversation
mhils
left a comment
There was a problem hiding this comment.
Oh wow - this is surprisingly little additional complexity, fantastic! 🥳 I didn't expect the TLS parts to be that reusable for DTLS, this is great news.
High-level comment: I think we should follow a similar structure as we have for TCP, i.e. we add a raw UDP layer akin to mitmproxy/proxy/layers/tcp.py. DTLS connections then have a Proxy Server -> Server DTLS -> Client DTLS -> Raw UDP layer stack. Does that make sense?
Second high-level comment: Given the similarity in the code, I wouldn't be opposed to reusing the existing tls_* hooks instead of adding dtls_* versions. We could probably add an is_dtls flag to TlsData. Either way is perfectly fine with me, feel free to make a call here.
I created two separate layers, one for client and one for the server, as we currently do for TLS layers but I'm not sure we need that split at the moment.
I agree that we don't, but it's probably easier to keep doing that if we want to reuse the existing code. Again, happy with either approach. FWIW I had the TLS layers combined initially, but that turned out to be more of a mess than anything else. It might be a different story for DTLS of course.
configuring ssl context for cookie verification
I'm not too familiar with DTLS, but if I can help you with any pyOpenSSL stuff please feel free to ask dumb questions here or on Slack.
For the current DTLS layer implementation, I've seen that you've basically removed the "we need to parse the clienthello first" logic, which I guess makes sense for a first PoC. That part can be quite essential though, in particular we need the ClientHello info to do ALPN with the server. Can we somehow make this ClientHello peeking/reassembly work for UDP?
remove hard coded server address (Could we pass this information as an option?)
See comment below.
log flows in flowlist
See above - I think a raw UDP layer needs to happen for that. I'm happy to take care of that if you want to focus on DTLS. In that case you can probably just use TCPLayer as a child layer in the meantime. You should get away with ignoring that the connection close logic is incorrect, everything else should be pretty much the same for UDP.
Again, thanks for the fantastic PR! 😃
| "127.0.0.1", | ||
| "8084", | ||
| transparent=False, | ||
| ) |
There was a problem hiding this comment.
FYI I'm currently refactoring the bits here, see #5288 and #5393. DTLS fits quite well into this, it would become yet another proxy mode that you specify as dtls:example.com:4433@localhost:4433 to create a reverse proxy from localhost to example.com. Does that make sense?
(I'll try to get the abovementioned PR merged this week)
There was a problem hiding this comment.
Yeah, I saw that but I need to look at it closer. I will circle back about this.
I need to look closer at what that layer does but I think I know what you mean. I'd like to try building it but I will reach out if I need help. Thank you.
I was on the fence about this so I'll change it as you're suggesting.
👍
You're right. I was having some issues getting it to work when parsing the client hello so that is why I put it off for the moment. I'll add this to the checklist so I can address it. |
|
Do we want to add support for this feature for reverse proxies at the moment? |
If I understand things correctly this only concerns DoS by clients? I'd be fine with ignoring that. Realistically speaking, it will always be easy to overwhelm a mitmproxy instance with requests. We don't intend to compete with nginx. |
|
Thanks, I'll ignore that feature. Yeah, AFAIK it only concerns DoS by clients. |
e97844b to
55fe7cd
Compare
| mitmproxy/proxy/server.py | ||
| mitmproxy/proxy/layers/tls.py | ||
| mitmproxy/proxy/layers/dtls.py | ||
| mitmproxy/tls.py |
There was a problem hiding this comment.
Both .coverage.xml and running pytest --cov locally show that this file has 100% coverage but individual_coverage says it's at 98%. I'm not sure what the issue is....
This was resolved.
| mitmproxy/proxy/config.py | ||
| mitmproxy/proxy/server.py | ||
| mitmproxy/proxy/layers/tls.py | ||
| mitmproxy/proxy/layers/dtls.py |
There was a problem hiding this comment.
I am adding this file because the DTLS layers are simply sub-classes of TLS layers with some copy and pasted code. I'm unable to copy the TLS layer tests because they use the ssl module which does not support DTLS. It didn't seem worth the time to write tests (creating another version of SSLTest that works for DTLS) for the new layers since they are practically identical to the TLS layers which we are already testing. Open to suggestions of course.
There was a problem hiding this comment.
Given how (fantastically) minimal the changes in the DTLS layer now are I think it would make most sense to just merge the TLS and DTLS layers? I think you should be able to add something like
@property
def is_dtls(self):
return self.conn.transport_protocol == "udp"and then get by with very few if-else constructs in the layers. This would also get around the test coverage issue. I will leave it up to you if you want to leave the parse_client_hello stuff in layers/dtls.py (we can add a top-level comment there pointing to tls.py) or rename it and integrate it into tls.py as well. Both is fine with me. :)
|
This is ready for another round of review. I couldn't fix the Codecov failures. Virtually all of the complaints come from |
195b632 to
1ca1c9c
Compare
d62b740 to
a749692
Compare
|
@mhils I fixed the conflicts. The windows build is failing on the command |
|
@kckeiks: Thanks! The Windows build error is on me, I'll handle it. :) |
mhils
left a comment
There was a problem hiding this comment.
Sorry again for the long turnaround, but I'm happy to say this looks fantastic now! My only non-trivial suggestion at this point would be to merge the TLS and DTLS layers, I diffed the functions individually and there are pretty much no substantial differences left. Other than that this looks good to merge!
| @@ -0,0 +1,203 @@ | |||
| # This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild | |||
There was a problem hiding this comment.
Can you please add the corresponding .ksy file, or alternatively get this into https://github.com/kaitai-io/kaitai_struct_formats/tree/master/network (I'm sure they woud appreciate it) and https://github.com/mitmproxy/mitmproxy/blob/main/mitmproxy/contrib/kaitaistruct/make.sh?
We also need to compile the ksy with the latest Kaitai compiler as mitmproxy does not always ship with pkg_resources (#5401).
| assert ProxyMode.parse("socks5") | ||
| assert ProxyMode.parse("dns").resolve_local | ||
| assert ProxyMode.parse("dns:reverse:8.8.8.8") | ||
| assert ProxyMode.parse("dtls:reverse:127.0.0.1:0") |
There was a problem hiding this comment.
Let's use a valid destination port here in case we ever get to tightening the check.
| mitmproxy/proxy/config.py | ||
| mitmproxy/proxy/server.py | ||
| mitmproxy/proxy/layers/tls.py | ||
| mitmproxy/proxy/layers/dtls.py |
There was a problem hiding this comment.
Given how (fantastically) minimal the changes in the DTLS layer now are I think it would make most sense to just merge the TLS and DTLS layers? I think you should be able to add something like
@property
def is_dtls(self):
return self.conn.transport_protocol == "udp"and then get by with very few if-else constructs in the layers. This would also get around the test coverage issue. I will leave it up to you if you want to leave the parse_client_hello stuff in layers/dtls.py (we can add a top-level comment there pointing to tls.py) or rename it and integrate it into tls.py as well. Both is fine with me. :)
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
Signed-off-by: Miguel Guarniz <[email protected]>
mhils
left a comment
There was a problem hiding this comment.
Awesome! 🍰 Thank you very much for contribution this feature, it's a fantastic addition to mitmproxy. 😃
My pleasure!
|
Description
Issue #5359
Checklist