Mhonn YbañezSRE, DevOps, AI Engineer
 Today I Learned
Read the full page

How does a VPN work?

Posted
Tags
networking, security, fundamentals

HTTPS hides what I send but not who I am talking to. A VPN wraps my whole packet, destination included, inside an encrypted packet addressed to the VPN server, which unwraps it and forwards it. My ISP only sees the VPN server, the website only sees the VPN server, and the VPN provider now sees what my ISP used to.

I got curious about how a VPN actually works, and found a KodeKloud short that explains it really well in under two minutes.

The question I had

How does a VPN work? And why would I need one if my connection is already encrypted with HTTPS?

What I found

What HTTPS hides, and what it doesn't

Say I open google.com and search for something. My browser sends the request over HTTPS, so everything inside it is encrypted. My internet provider (ISP) can't read what I searched.

But HTTPS hides what I send, not who I am talking to. Every packet carries a destination IP address on the outside, so routers know where to deliver it. That address points to Google. So my ISP knows I am talking to Google, and when I open Instagram next, it knows that too. Over a month, it builds a pretty good list of the sites I visit.

The tunnel is an envelope

People say a VPN "tunnels" traffic. The tunnel is really just a packet inside another packet:

  1. My device builds the same packet as before: destination Google, data encrypted with HTTPS.
  2. Before it leaves, the VPN app encrypts that whole packet, Google's address included, and wraps it in an outer packet addressed to the VPN server.
  3. My ISP only sees packets going to the VPN server, whether I open google.com or facebook.com.
  4. The VPN server removes the outer layer, takes out my original packet, and sends it on to Google.
  5. Google replies to the VPN server, and the VPN server wraps the reply and sends it back to me.

Who sees what

Without a VPNWith a VPN
My ISPWhich sites I visit, not the contentOnly that I talk to a VPN server
VPN providerNothingWhich sites I visit, not the content
The websiteMy IP addressThe VPN server's IP address

Moving trust, not removing it

Since the VPN server unwraps the outer layer, my VPN provider can now see which sites I visit. It still can't read inside HTTPS. So a VPN doesn't make the list disappear. It moves it from my ISP to my VPN provider, and I am choosing which one I trust more.

VPNs were built for companies

VPNs were around for companies long before people used them for privacy, and most employees still use one every day. Companies keep things like internal databases in a private network that the internet can't reach. To let someone working from home in:

  • The company puts a VPN server at the edge of the private network. One side faces the internet, the other side can talk to the private network.
  • An employee connects to it first and proves who they are, usually with multi-factor authentication (a password plus a code on their phone).
  • The VPN server checks whether that person is allowed in.
  • After that it is the same envelope: the server unwraps my packet and passes it into the private network.

What the video leaves out

  • My ISP can often see the site name, not just the IP. DNS lookups are often plain text, and the site name is sent in the clear when an HTTPS connection starts (SNI). A VPN hides these only if DNS goes through the tunnel too. If it doesn't, that is called a DNS leak.
  • Not everything has to go through the tunnel. With split tunneling, only some traffic uses the VPN. Company VPNs often send only company traffic through it.
  • A VPN is not anonymity. The site sees the VPN server's IP, but if I am logged in, or cookies and browser fingerprinting identify me, it still knows it is me.

Where my epiphany came in

Watching this, it clicked that this is just the client-server model again. The VPN server is the server, and its job is to hide where my request came from and send it on for me.

One detail I had to correct in my own head: the VPN doesn't make a new request on my behalf. It forwards my original packet and swaps my address for its own on the way out. My HTTPS session still runs from my device all the way to Google, which is why the VPN provider can't read it. Something that does make a fresh request for me is closer to a proxy.

Takeaway

HTTPS protects what I send, and a VPN hides who I am talking to from my network. It does that by wrapping my packet in an envelope addressed to the VPN server, which means the VPN provider now sees what my ISP used to. Using a VPN is really a choice of whom to trust.