User Tools

Site Tools


secres:https-misc

HTTPS et SSL/TLS

Voici quelques notes en vrac sur HTTPS et SSL/TLS.

Les exemples ci-dessous sont écrits en Python : https://docs.python.org/3/library/ssl.html

Client HTTPS

En guise d'échauffement, voici un exemple de client HTTPS : httpsget.py

$ ./httpsget www.python.org 443

Client/Serveur Echo en SSL

Pour aller un peu plus loin, voici un exemple de client/serveur Echo en SSL : sslsocket.zip. Il faut dans cet exemple générer le certificat du CA et du serveur pour localhost (127.0.0.1). Ceux fournis expirent au bout de 255 jours ! Voir README.

$ ./sslserver.py
$ ./sslclient.py
  

Proxy SSL dans un LAN

Voici le code de notre proxy SSL : sslproxy.py. Ce code n'est pas très générique : il est écrit sur mesure pour l'interception du site https://www.python.org en utilisant un faux certifcat fake.crt/fake.key signé par notre propre CA (ca.crt/ca.key). Voir la section suivante pour générer ces certificats.

On considère le LAN suivant (192.168.0.0/24) :

V ---- [LAN] ----- GW -----> Internet
         |
       PROXY

avec :

  • V, la victime (client HTTPS) ;
  • PROXY, la machine du LAN qui héberge notre proxy SSL ;
  • GW, la gateway vers Internet !

Nous allons uniquement nous intéresser à l'interception HTTPS pour un site particulier (https://www.python.org) afin d'illustrer le principe.

PROXY$ sudo iptables -A OUTPUT -p icmp --icmp-type redirect -j DROP
# PROXY$ echo 0 | sudo tee /proc/sys/net/ipv4/conf/*/send_redirects
PROXY$ sudo iptables -t nat -A PREROUTING -i wlp2s0 -d www.python.org -p tcp --dport 443 \
  -j REDIRECT --to-port 4444
PROXY$ sudo bash -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'
PROXY$ ./sslproxy.py 4444

V$ route del default
V$ route add default gw @PROXY
V$ wget -4 --ca-certificate=ca.crt https://www.python.org

Test du Proxy SSL en local

Pour tester en local notre Proxy SSL, le redirect est un peu plus tricky, car il faut distinguer localement le traffic partant du client web de celui partant du proxy, alors qu'ils ont la même destination. Voici l'astuce que j'utilise :

$ sudo groupadd noredirect
$ sudo iptables -t nat -F
$ sudo iptables -t nat -A OUTPUT -m owner ! --gid-owner noredirect  -p tcp -d www.python.org --dport 443 \
  -j REDIRECT --to-port 4444
$ sudo sg noredirect './sslproxy.py 4444'
$ wget -4 --ca-certificate=ca.crt https://www.python.org

Génération d'un fake Certificat

 $ certtool --generate-privkey --outfile ca.key
 $ certtool --generate-self-signed --load-privkey ca.key --outfile ca.crt
 $ certtool --certificate-info --infile ca.crt
  • La plupart des champs peuvent rester vides.
  • Common name: CA
  • The certificate will expire in (days): 255
  • Does the certificate belong to an authority? (y/N): y
  • Will the certificate be used to sign other certificates? (y/N): y
 $ certtool --generate-privkey --outfile fake.key
 $ certtool --generate-certificate --load-privkey fake.key --outfile fake.crt --load-ca-certificate ca.crt \
   --load-ca-privkey ca.key
 $ certtool --certificate-info --infile fake.crt
  • La plupart des champs peuvent rester vides
  • DNSName=www.pyhon.org
  • The certificate will expire in (days): 255
  • Will the certificate be used for signing (required for TLS)? (y/N): y
  • Will the certificate be used for encryption (not required for TLS)? (y/N): y
secres/https-misc.txt · Last modified: 2024/03/18 15:06 by 127.0.0.1