pknoth

pknoth

Looking for an `:ssl` option building an https proxy

Hi,

I am building an https proxy and need to forward the encrypted incoming payload of the requests, the proxy being a Man in the middle. I ended up building a gen_tcp server that replies to CONNECT queries from the client by connecting to the remote secure host and forwarding the payloads from both sides.

[gen_tcp listen, accepts...]

  def handle_info({:tcp, socket, payload}, %State{socket: socket, client_socket: nil} = state) do

   [set host, port...]

    with {:ok, client_socket} <-
           :ssl.connect(
             String.to_charlist(host),
             port || 443,
             [
               {:log_level, :all},
               {:packet, :raw},
               {:mode, :binary},
               {:verify, :verify_peer},
               {:cacerts, :public_key.cacerts_get()}
             ],
             @connect_timeout
           ) do
      :gen_tcp.send(socket, "HTTP/1.1 200 OK\r\n\r\n")

      :inet.setopts(socket, active: :once)
      :ssl.setopts(client_socket, active: :once)
      {:noreply, %{state | client_socket: client_socket, start: start}}
    end
  end

[...]

  def handle_info({:ssl, socket, payload}, %State{client_socket: socket} = state) do
    :ssl.setopts(socket, active: :once)
    :gen_tcp.send(state.socket, payload)
    {:noreply, state}
  end

  def handle_info({:tcp, socket, payload}, %State{socket: socket} = state) do
    :inet.setopts(socket, active: :once)
    :ssl.send(state.client_socket, payload)
    {:noreply, state}
  end

The problem here is that the incoming payload (from the remote server) is decrypted by the erlang :ssl library which makes the client receive clear messages instead of encrypted ones, raising an SSL error. Note that the forwarding looks fine with the HTTP version of the snippet.

Does anyone know if there is an :ssl.connect option that disables payload decrypt?

Thanks.

Marked As Solved

pknoth

pknoth

Thank you for your reply.

The issue was the proxy was performing the handshake instead of the client which caused the encryption/decrypt problem. The solution was to open a TCP connection toward the target server (not an SSL one) and let the client upgrade to TLS.

Also Liked

D4no0

D4no0

Makes complete sense if you are using :ssl on proxy.

In your case this is not a https proxy, but one at TCP/SSL level, as once the ssl handshake is complete, there is no way you can decrypt and filter the traffic on your proxy.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement