Skip to content

fix: fallback use addr hostname as SNI for TLS socks proxied connection#6218

Merged
ehsandeep merged 1 commit intoprojectdiscovery:devfrom
circleous:tls-over-socks-proxy
May 15, 2025
Merged

fix: fallback use addr hostname as SNI for TLS socks proxied connection#6218
ehsandeep merged 1 commit intoprojectdiscovery:devfrom
circleous:tls-over-socks-proxy

Conversation

@circleous
Copy link
Contributor

@circleous circleous commented May 14, 2025

Proposed changes

Should fixes #6211. Nuclei scan with -proxy set to a socks5 proxy server produce tls: unrecognized name / tls: handshake failure. Not all hosts encounter this issue, but I found that most websites behind cloudflare have this tls issue.

The fixes is simply fallback to use hostname from addr string as ServerName (sni) for TLS Config.

Before
$ ./bin/nuclei -p socks5://127.0.0.1:1080 -u https://myipv4.addr.tools -t ~/nuclei-templates/http/technologies/tech-detect.yaml -duc  -debug
                     __     _
   ____  __  _______/ /__  (_)
  / __ \/ / / / ___/ / _ \/ /
 / / / / /_/ / /__/ /  __/ /
/_/ /_/\__,_/\___/_/\___/_/   v3.4.3

                projectdiscovery.io

[INF] Current nuclei version: v3.4.3 (unknown) - remove '-duc' flag to enable update checks
[INF] Current nuclei-templates version: v10.2.1 (unknown) - remove '-duc' flag to enable update checks
[WRN] Scan results upload to cloud is disabled.
[INF] New templates added in latest release: 42
[INF] Templates loaded for current scan: 1
[INF] Executing 1 signed templates from projectdiscovery/nuclei-templates
[INF] Targets loaded for current scan: 1
[INF] [tech-detect] Dumped HTTP request for https://myipv4.addr.tools

GET / HTTP/1.1
Host: myipv4.addr.tools
User-Agent: Mozilla/5.0 (Debian; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Connection: close
Accept: */*
Accept-Language: en
Accept-Encoding: gzip

[WRN] [tech-detect] Could not execute request for https://myipv4.addr.tools: cause="tls: unrecognized name" chain="got err while executing https://myipv4.addr.tools"
[INF] Scan completed in 2.405888417s. No results found.
After
$ ./bin/nuclei -p socks5://127.0.0.1:1080 -u https://myipv4.addr.tools -t ~/nuclei-templates/http/technologies/tech-detect.yaml -duc  -debug

                     __     _
   ____  __  _______/ /__  (_)
  / __ \/ / / / ___/ / _ \/ /
 / / / / /_/ / /__/ /  __/ /
/_/ /_/\__,_/\___/_/\___/_/   v3.4.3

                projectdiscovery.io

[INF] Current nuclei version: v3.4.3 (unknown) - remove '-duc' flag to enable update checks
[INF] Current nuclei-templates version: v10.2.1 (unknown) - remove '-duc' flag to enable update checks
[WRN] Scan results upload to cloud is disabled.
[INF] New templates added in latest release: 42
[INF] Templates loaded for current scan: 1
[INF] Executing 1 signed templates from projectdiscovery/nuclei-templates
[INF] Targets loaded for current scan: 1
[INF] [tech-detect] Dumped HTTP request for https://myipv4.addr.tools

GET / HTTP/1.1
Host: myipv4.addr.tools
User-Agent: Mozilla/5.0 (Mac OS X 13_2) AppleWebKit/537.36 (KHTML, like Gecko) Edge/117.0 Safari/537.36
Connection: close
Accept: */*
Accept-Language: en
Accept-Encoding: gzip

[DBG] [tech-detect] Dumped HTTP response https://myipv4.addr.tools

HTTP/1.1 200 OK
Connection: close
Content-Length: 12
Access-Control-Allow-Origin: *
Alt-Svc: h3=":443"; ma=2592000
Cache-Control: no-store
Content-Type: text/plain
Date: Wed, 14 May 2025 02:17:38 GMT
Server: nginx

XX.XX.XX.XX
[tech-detect:nginx] [http] [info] https://myipv4.addr.tools
[INF] Scan completed in 1.003708s. 1 matches found.

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Bug Fixes
    • Improved TLS connection handling when using a SOCKS proxy by ensuring the server name is correctly set, enhancing connection reliability and security.

@auto-assign auto-assign bot requested a review from dogancanbakir May 14, 2025 02:53
@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 14, 2025

Walkthrough

The change modifies the DialTLSContext function in the HTTP client pool to ensure that when using a SOCKS proxy and the TLS configuration lacks a server name, the server name is extracted from the address and set in the TLS configuration before establishing a connection.

Changes

File(s) Change Summary
pkg/protocols/http/httpclientpool/clientpool.go Added logic in DialTLSContext to set tlsConfig.ServerName from the address if it is empty when using a SOCKS proxy.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant ClientPool
    participant TLSConfig

    Caller->>ClientPool: DialTLSContext(addr, tlsConfig)
    alt Using SOCKS proxy and tlsConfig.ServerName is empty
        ClientPool->>ClientPool: Extract host from addr
        alt Extraction fails
            ClientPool-->>Caller: Return error
        else Extraction succeeds
            ClientPool->>TLSConfig: Set ServerName to host
        end
    end
    ClientPool->>Caller: Establish TLS connection with updated tlsConfig
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix TLS handshake failure when using a SOCKS5 proxy by ensuring ServerName is set (#6211)

Poem

In the warren of code, a bug did hide,
SOCKS5 and TLS, not side by side.
With host now set, the handshake’s complete,
No more errors, just connections neat!
A hop, a fix, a joyous cheer—
The rabbit’s code runs smooth this year! 🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6d25a5c and 7968f4d.

📒 Files selected for processing (1)
  • pkg/protocols/http/httpclientpool/clientpool.go (1 hunks)
🔇 Additional comments (1)
pkg/protocols/http/httpclientpool/clientpool.go (1)

310-317: Excellent fix for TLS handshake issues when using SOCKS proxies

This is a solid fix for the TLS handshake failures occurring with SOCKS proxies. When the server name isn't explicitly set, extracting it from the address ensures proper SNI (Server Name Indication) during the TLS handshake, which is particularly important for sites behind Cloudflare.

The implementation correctly:

  1. Checks if ServerName is empty
  2. Extracts the host from the address
  3. Sets it as the ServerName for the TLS config

The error handling is also appropriate, returning any errors from SplitHostPort.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@circleous circleous changed the title fix: fallback use default go net.DialTLS for socks proxied connection fix: fallback use addr hostname as SNI for TLS socks proxied connection May 14, 2025
@dogancanbakir dogancanbakir requested a review from Mzack9999 May 14, 2025 03:11
@circleous
Copy link
Contributor Author

Hi @dogancanbakir, will it be possible to get this as hotfix or will need to wait for next month release? 🙇

@dogancanbakir
Copy link
Member

Let's wait for review to be done, releasing is the easy part - no worries!

Copy link
Member

@tarunKoyalwar tarunKoyalwar left a comment

Choose a reason for hiding this comment

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

lgtm !

the core issue seems to be originiating from fastdialer which is not using proxyDialer when dialing tls connections projectdiscovery/fastdialer#457

once we resolve that we can remove this DialTLSContext override block completely

@ehsandeep ehsandeep merged commit b03c304 into projectdiscovery:dev May 15, 2025
19 of 20 checks passed
@ehsandeep ehsandeep removed the request for review from Mzack9999 May 15, 2025 11:16
@ehsandeep
Copy link
Member

thank you @circleous for identifying and fixing this issue.

@dogancanbakir
Copy link
Member

@circleous Preparing for the release at #6224 once reviewed I will make the release, fyi.

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.

[BUG] tls: handshake failure when using socks5 proxy

4 participants