Skip to content

Add dtls support - #5397

Merged
mhils merged 29 commits into
mitmproxy:mainfrom
kckeiks:add-dtls-support
Jul 29, 2022
Merged

mhils merged 29 commits into
mitmproxy:mainfrom
kckeiks:add-dtls-support

Conversation

@kckeiks

@kckeiks kckeiks commented Jun 4, 2022 •

Copy link
Copy Markdown
Contributor

Description

Issue #5359

Checklist

  • parse client hello
  • add raw UDP layer
  • remove test logging
  • I have updated tests where applicable.
  • I have added an entry to the CHANGELOG.
  • Integrate with multi-server mode.

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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! 😃

Comment thread mitmproxy/addons/proxyserver.py Outdated
"127.0.0.1",
"8084",
transparent=False,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that but I need to look at it closer. I will circle back about this.

@kckeiks

kckeiks commented Jun 5, 2022 •

Copy link
Copy Markdown
Contributor Author

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?

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.

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 was on the fence about this so I'll change it as you're suggesting.

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?

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.

@kckeiks
kckeiks force-pushed the add-dtls-support branch from 7b05d79 to 530bb44 Compare June 9, 2022 02:09
@meitinger meitinger mentioned this pull request Jun 14, 2022
2 tasks done
@kckeiks

kckeiks commented Jun 14, 2022

Copy link
Copy Markdown
Contributor Author

Per the discussion in #5414, commits 9d0ae05 and ebd2fe0 contain a rough wip idea for the RelayLayer, TCPLayer and UDPLayer. I'll have some bandwidth on Thursday and the weekend so I'll address feedback then and improve the code.

@kckeiks

kckeiks commented Jun 14, 2022

Copy link
Copy Markdown
Contributor Author

Do we want to add support for this feature for reverse proxies at the moment?

@mhils

mhils commented Jun 15, 2022

Copy link
Copy Markdown
Member

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.

@kckeiks

kckeiks commented Jun 15, 2022

Copy link
Copy Markdown
Contributor Author

Thanks, I'll ignore that feature. Yeah, AFAIK it only concerns DoS by clients.

@kckeiks
kckeiks force-pushed the add-dtls-support branch 4 times, most recently from e97844b to 55fe7cd Compare July 1, 2022 22:30
@kckeiks
kckeiks force-pushed the add-dtls-support branch from d40a9f7 to 3057234 Compare July 2, 2022 00:11
Comment thread setup.cfg Outdated
mitmproxy/proxy/server.py
mitmproxy/proxy/layers/tls.py
mitmproxy/proxy/layers/dtls.py
mitmproxy/tls.py

@kckeiks kckeiks Jul 2, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread setup.cfg Outdated
mitmproxy/proxy/config.py
mitmproxy/proxy/server.py
mitmproxy/proxy/layers/tls.py
mitmproxy/proxy/layers/dtls.py

@kckeiks kckeiks Jul 2, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. :)

@kckeiks

kckeiks commented Jul 2, 2022 •

Copy link
Copy Markdown
Contributor Author

This is ready for another round of review. I couldn't fix the Codecov failures. Virtually all of the complaints come from layers/dtls.py which is being ignored as explained above.

@kckeiks
kckeiks force-pushed the add-dtls-support branch 3 times, most recently from 195b632 to 1ca1c9c Compare July 2, 2022 20:48
@kckeiks
kckeiks requested a review from mhils July 18, 2022 16:46
@kckeiks
kckeiks force-pushed the add-dtls-support branch 4 times, most recently from d62b740 to a749692 Compare July 28, 2022 16:10
@kckeiks

kckeiks commented Jul 28, 2022 •

Copy link
Copy Markdown
Contributor Author

@mhils I fixed the conflicts. The windows build is failing on the command mitmweb --version with ValueError: Fernet key must be 32 url-safe base64-encoded bytes. I'm not sure why that is or if it's a known issue?

@mhils

mhils commented Jul 29, 2022

Copy link
Copy Markdown
Member

@kckeiks: Thanks! The Windows build error is on me, I'll handle it. :)

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread test/mitmproxy/proxy/test_mode_specs.py Outdated
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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a valid destination port here in case we ever get to tightening the check.

Comment thread setup.cfg Outdated
mitmproxy/proxy/config.py
mitmproxy/proxy/server.py
mitmproxy/proxy/layers/tls.py
mitmproxy/proxy/layers/dtls.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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. :)

kckeiks and others added 25 commits July 29, 2022 19:02
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
mhils force-pushed the add-dtls-support branch from 2734bb3 to c3f238f Compare July 29, 2022 17:03

@mhils mhils left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 🍰 Thank you very much for contribution this feature, it's a fantastic addition to mitmproxy. 😃

@mhils
mhils enabled auto-merge July 29, 2022 17:04
@mhils
mhils merged commit 6cfde13 into mitmproxy:main Jul 29, 2022
@kckeiks

kckeiks commented Jul 29, 2022 •

Copy link
Copy Markdown
Contributor Author

Awesome! cake Thank you very much for contribution this feature, it's a fantastic addition to mitmproxy. smiley

My pleasure!

I was just in the process of pushing the recompiled (with 0.10 compiler) ksy file to this branch but I was too slow 😆. Would you like a separate PR for it? nvm I just noticed you beat me to it. Thanks. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants