SBN

Reverse Proxy and TLS Termination

PolarProxy is primarily a TLS forward proxy,
but it can also be used as a TLS termination proxy or reverse TLS proxy to intercept and decrypt incoming TLS traffic,
such as HTTPS or IMAPS, before it is forwarded to a server.
The proxied traffic can be accessed in decrypted form as a PCAP formatted data stream, which allows
real-time analysis of the decrypted traffic by an IDS
as well as post incident forensics with Wireshark.

PolarProxy version 0.8.14 and later can import an existing
X.509 server certificate
(aka leaf certificate or end-entity certificate) in order to perform the TLS decryption
using a valid certificate signed by a trusted certificate authority.
If no server certificate is provided, then PolarProxy falls back to generating server certificates on the fly
and signing them with its own root CA certificate.

There are two principal ways to run PolarProxy as a reverse proxy,
either as a TLS termination proxy or as a reverse proxy that decrypts and re-encrypts the traffic.

PolarProxy as a TLS Termination Proxy

TLS Termination Proxy

The TLS termination proxy mode is useful in order to offload the task of performing TLS encryption
to PolarProxy instead of doing the decryption on the web server.
This mode can also be used when the proxied services don’t support TLS encryption,
such as legacy web servers or servers hosting other unencrypted services that you want to secure with TLS.

The following command sequence shows how to create a Let’s Encrypt SSL certificate, convert it to the PKCS#12 format,
and load the server certificate into PolarProxy to terminate incoming HTTPS connections.
In this setup PolarProxy decrypts the TLS traffic and relays the HTTP traffic to the web server on TCP port 80.

sudo certbot certonly –manual –preferred-challenges dns -d example.com,www.example.com

sudo openssl pkcs12 -export -out /etc/example.p12 -inkey /etc/letsencrypt/live/example.com/privkey.pem -in /etc/letsencrypt/live/example.com/fullchain.pem –passout pass:PASSWORD

sudo mkdir /var/log/TlsTerminationProxy/

sudo ./PolarProxy –terminate –connect 10.1.2.3 –nosni www.example.com –servercert example.com,www.example.com:/etc/example.p12:PASSWORD -p 443,80,80 -o /var/log/TlsTerminationProxy/

Here’s a breakdown of the arguments sent to PolarProxy:

  • –terminate : Terminate incoming TLS sessions and forward proxied traffic in unencrypted form.
  • –connect 10.1.2.3 : Forward all proxied traffic to 10.1.2.3 instead of connecting to the host name provided in the
    SNI extension of the TLS ClientHello message.
  • –nosni www.example.com : Treat incoming TLS sessions that don’t define a host name with the SNI extension as if they wanna to connect to “www.example.com”.
  • –servercert example.com,www.example.com:/etc/example.p12:PASSWORD : Use the server certificate “/etc/example.p12” for incoming connections to “example.com” and “www.example.com”.
  • -p 443,80,80 : Listen on TCP port 443, save decrypted traffic in PCAP file as if it was directed to port 80, forward decrypted traffic to port 80.
  • -o /var/log/TlsTerminationProxy/ : Save decrypted traffic to hourly rotated PCAP files in “/var/log/TlsTerminationProxy/”.

PolarProxy is a generic TLS proxy that doesn’t care what application layer protocol the TLS tunnel carries.
So if you want to terminate the TLS encryption of incoming IMAPS sessions as well,
then simply append an additional argument saying “-p 993,143,143” to also forward decrypted IMAP sessions to 10.1.2.3.
This method can be used in order to wrap almost any TCP based protocol in a TLS tunnel,
which can be useful for privacy reasons as well as to prevent network monitoring tools from detecting
the actual application layer protocol.

PolarProxy as a Reverse TLS Proxy

Reverse TLS Proxy

There are setups for which it is preferable to also encrypt the internal sessions between PolarProxy and the final server.
One such setup is when the server is hosting a web service with support for the HTTP/2 protocol,
which in practice always uses TLS. Luckily PolarProxy is designed to decrypt and re-encrypt proxied traffic
while also forwarding important TLS parameters, such as
ALPN and
SNI, between the internal and external TLS sessions.

To use TLS encryption on the inside as well as outside of PolarProxy,
simply do as explained in the previous TLS termination section,
but remove the “–terminate” argument and change the port argument to “-p 443,80,443” like this:

sudo ./PolarProxy –connect 10.1.2.3 –nosni www.example.com –servercert example.com,www.example.com:/etc/example.p12:PASSWORD -p 443,80,443 -o /var/log/ReverseTlsProxy/

PolarProxy will save the decrypted traffic as cleartext HTTP (or HTTP/2) to PCAP files in the “/var/log/ReverseTlsProxy/” directory.

Real-Time Analysis of Decrypted Traffic

Both the external (client-to-proxy) and internal (proxy-to-server) TCP sessions,
in the reverse TLS proxy example above, are encrypted with TLS.
This prevents passive network security monitoring tools, such as
IDSs,
DPI and
DLP appliances,
from analyzing the application layer data being sent and received.
The PCAP files written to “/var/log/ReverseTlsProxy/”
can be a valuable forensic asset when investigating an incident,
but a real-time stream of the decrypted data is needed in order to
swiftly detect and alert on potential security breaches and other incidents.

PolarProxy’s “–pcapoverip” option can be used to provide such a real-time stream
of the decrypted data passing through the proxy.
This data can easily be sent to a network interface using tcpreplay, as explained in our blog post
Sniffing Decrypted TLS Traffic with Security Onion”.

Security Considerations

The examples shown in this blog post all run PolarProxy with root privileges using sudo,
which can be dangerous from a security perspective.
PolarProxy is actually designed to be run without root privileges,
but doing so prevents it from listening on a port below 1024.
Luckily, this issue can easily be overcome with a simple port forwarding or redirect rule.
The following iptables redirect rule can be used if PolarProxy is listening on TCP port 20443
and incoming HTTPS request are arriving to the eth0 interface of the proxy:

iptables -t nat -A PREROUTING -i eth0 -p tcp –dport 443 -j REDIRECT –to 20443

PolarProxy does not support loading settings from a config file.
The password for the PKCS12 certificate will therefore need to be supplied on the command line,
which can make it visible from a process listing.
If this is a concern for you, then please consider using “hidepid” to hide processes from other users.
You can find instructions on how to use hidepid in hardening guides for
Debian,
Arch,
SUSE
and most other Linux flavors.

Facebook Share on Facebook  Twitter Tweet  Reddit Submit to reddit.com


*** This is a Security Bloggers Network syndicated blog from NETRESEC Network Security Blog authored by Erik Hjelmvik. Read the original post at: http://www.netresec.com/?page=Blog&month=2020-03&post=Reverse-Proxy-and-TLS-Termination