{"id":9660,"date":"2022-11-21T20:58:42","date_gmt":"2022-11-21T20:58:42","guid":{"rendered":"https:\/\/stevepedwards.today\/DebianAdmin\/?p=9542"},"modified":"2025-06-01T07:59:27","modified_gmt":"2025-06-01T06:59:27","slug":"ssl-on-ubuntu-apache2-creating-self-signed-certificates-checking-ssl-traffic-with-tcpdump-and-wireshark","status":"publish","type":"post","link":"https:\/\/stevepedwards.today\/DebianAdmin\/ssl-on-ubuntu-apache2-creating-self-signed-certificates-checking-ssl-traffic-with-tcpdump-and-wireshark\/","title":{"rendered":"SSL on Ubuntu Apache2 &#8211; Creating Self Signed Certificates, Checking SSL Traffic with TCPDUMP and Wireshark"},"content":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9660\" class=\"pvc_stats all  \" data-element-id=\"9660\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Programmers need to write their web apps for secure server hosting practically universally now, so you should also be writing them in VS Code\/other IDE with a Live Server set up for SSL as in the last Post, but if you also write at home and host on a local apache2 server or similar, it should also be using SSL too so you know your creation works on SSL(TLS)\/HTTPS\/port 443 platforms, rather than be surprised when first loaded to a secure web host...<\/p>\n<p>Is my SSL connection encrypted if the locally created certificate isn't trusted?<br \/>\nYes - you can see the pink packet in Wireshark on port 443 - so how do you install a certificate on linux Apache2? Read on..:<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/Wireshark443.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-9546\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/Wireshark443.png\" alt=\"\" width=\"1440\" height=\"860\" \/><\/a><\/p>\n<p>SSL consists of two major parts:<\/p>\n<p>the encryption of the data<br \/>\nthe validation that you are actually talking to the expected server<br \/>\nIf you get the warning about an untrusted certificate than the encryption will still work, but you cannot be sure that you are talking to the expected server. This means a man in the middle attack might be possible where an active attacker will decrypt, sniff, and re-encrypt the traffic. That is instead of this:<\/p>\n<p>Browser &lt;----------- encrypted -----------------------&gt; Bank<br \/>\nyou get this:<\/p>\n<p>Browser &lt;-- encrypted --&gt; Attacker &lt;--- encrypted ----&gt; Bank<br \/>\nIn this case the attacker can sniff all data (passwords etc) and even modify the data and the client will not notice it. The connections are still encrypted, but not end-to-end (browser-to-server) but browser-to-attacker and again attacker-to-server.<\/p>\n<p>Usually you should not override the warning by the browser because chances are high that there is a man in the middle attack going on. Only in the case where you know that the certificate is the expected one (verify fingerprint, not just the subject of the certificate) you can override the warning.<\/p>\n<p>Note that there are cases of legal man in the middle attacks, i.e. SSL interception done by antivirus proxies or by middleboxes (firewalls) so that these can analyse the encrypted traffic. In this case your computer is either automatically configured to trust these certificates or you need to explicitly import the proxy-CA which signed the new certificates. If you are having such kind of problem while using your own computer inside the company please ask the network administrator how you should proceed and don't simply accept the certificates.<\/p>\n<p>Step 1: This step before amending \/etc\/apache2\/sites-available\/default-ssl.conf:<\/p>\n<p><span style=\"color: #0000ff;\">sudo a2enmod ssl<\/span><\/p>\n<p>Step 2 \u2013 Creating the SSL Certificate<\/p>\n<p><span style=\"color: #0000ff;\">sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout \/etc\/ssl\/private\/apache-selfsigned.key -out \/etc\/ssl\/certs\/apache-selfsigned.crt<\/span><\/p>\n<p>Common Name (eg, your name or your server's hostname) []:localhost<\/p>\n<p><span style=\"color: #0000ff;\">ls \/etc\/ssl\/certs\/apache-selfsigned.crt<\/span><br \/>\n<span style=\"color: #ff0000;\">\/etc\/ssl\/certs\/apache-selfsigned.crt<\/span><\/p>\n<p>Step 3 \u2013 Configuring Apache to Use SSL - create a local server named conf file:<\/p>\n<p><span style=\"color: #0000ff;\">cd \/etc\/apache2\/sites-available<\/span><\/p>\n<p>stevee@localhost:\/etc\/apache2\/sites-available$<span style=\"color: #0000ff;\"> ls<\/span><br \/>\n<span style=\"color: #ff0000;\">000-default.conf default-ssl<\/span><\/p>\n<p>make a conf file for your new SSL site for your server name:<\/p>\n<p>stevee@localhost:\/etc\/apache2\/sites-available$ <span style=\"color: #0000ff;\">sudo touch localhost.conf<\/span><\/p>\n<p>stevee@localhost:\/etc\/apache2\/sites-available$<span style=\"color: #0000ff;\"> ls<\/span><br \/>\n<span style=\"color: #ff0000;\">000-default.conf localhost.conf default-ssl<\/span><\/p>\n<p>stevee@localhost:\/var\/www$ <span style=\"color: #0000ff;\">sudo a2ensite<\/span><\/p>\n<p><span style=\"color: #ff0000;\">Your choices are: 000-default localhost default-ssl<\/span><br \/>\n<span style=\"color: #ff0000;\">Which site(s) do you want to enable (wildcards ok)?<\/span><\/p>\n<p><span style=\"color: #ff0000;\">localhost<\/span><br \/>\n<span style=\"color: #ff0000;\">Enabling site localhost.<\/span><br \/>\n<span style=\"color: #ff0000;\">To activate the new configuration, you need to run:<\/span><br \/>\n<span style=\"color: #0000ff;\">systemctl reload apache2<\/span><\/p>\n<p>Add the following red settings to your empty site file to suit your site and folders where you created the certificates :<\/p>\n<p><span style=\"color: #0000ff;\">sudo vi \/etc\/apache2\/sites-available\/localhost.conf<\/span><\/p>\n<p><span style=\"color: #ff0000;\">&lt;IfModule mod_ssl.c&gt;<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">&lt;VirtualHost *:443&gt;<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">ServerAdmin webmaster@localhost<\/span><br \/>\n<span style=\"color: #ff0000;\">ServerName localhost<\/span><br \/>\n<span style=\"color: #ff0000;\">DocumentRoot \/var\/www<\/span><\/p>\n<p><strong><span style=\"color: #ff0000;\">SSLEngine on<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">SSLCertificateFile \/etc\/ssl\/certs\/apache-selfsigned.crt<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">SSLCertificateKeyFile \/etc\/ssl\/private\/apache-selfsigned.key<\/span><\/strong><br \/>\n<span style=\"color: #ff0000;\">&lt;\/VirtualHost&gt;<\/span><\/p>\n<p><span style=\"color: #0000ff;\">sudo apache2ctl configtest<\/span><br \/>\nSyntax OK<\/p>\n<p><span style=\"color: #0000ff;\">sudo systemctl reload apache2<\/span><\/p>\n<p>Step 4 \u2014 Redirecting HTTP to HTTPS<\/p>\n<p><span style=\"color: #0000ff;\">sudo vi \/etc\/apache2\/sites-enabled\/000-default.conf<\/span><\/p>\n<p><span style=\"color: #ff0000;\">&lt;VirtualHost *:80&gt;<\/span><br \/>\n<span style=\"color: #ff0000;\">#ServerName www.example.com<\/span><br \/>\n<strong><span style=\"color: #ff0000;\">ServerName localhost<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #ff0000;\">DocumentRoot \/var\/www<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">Redirect \/ https:\/\/stevepedwards.today\/<\/span><\/strong><\/p>\n<p>Save the file in vim with<\/p>\n<p><span style=\"color: #ff0000;\">:wq<\/span><\/p>\n<p><span style=\"color: #0000ff;\">sudo apachectl configtest<\/span><br \/>\nSyntax OK<\/p>\n<p><span style=\"color: #0000ff;\">sudo systemctl reload apache2<\/span><\/p>\n<p>Now you can browse to your home site and change the address bar prefix to https:\/\/ OR hit Shift-F5 to make your page cache refresh to get the new SSL site.<\/p>\n<p>As the certificate is self-signed so unverified, the browser will complain:<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/CertFake.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-9549\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/CertFake.png\" alt=\"\" width=\"1921\" height=\"1033\" \/><\/a><\/p>\n<p>Continue on and you will get to your site but with the HTTPS struck through - but as seen in wireshark - the traffic IS still encrypted on port 443 using TLS:<\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/HTTPSLinedOut.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-9550 size-medium\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/HTTPSLinedOut.png\" alt=\"\" width=\"1921\" height=\"1033\" \/><\/a><\/p>\n<p>To capture and read the packets using tcpdump and wireshark:<\/p>\n<p><span style=\"color: #0000ff;\"> sudo apt install wireshark tcpdump<\/span><\/p>\n<p>tcpdump can only write to files of particular suffix - a dump.txt file for example will give a Permission Denied.<\/p>\n<p>dump.pcap works fine.<\/p>\n<p>Capture a small file of SSL traffic by being ready to click to your non HTTPS site once you start tcpdump running - stop the capture with Ctrl-C - as this checks that the re-direct from http port 80 to SSL port 443 works AND the site traffic captured is encrypted:<\/p>\n<p><span style=\"color: #0000ff;\"> sudo tcpdump -i ens5 -w dump.pcap<\/span><\/p>\n<p>Now you can read it back on the local server where Wireshark is installed to give the screen view at the start of the Post - it needs an xserver, so you cannot see output over remote SSH without further tech wizardry to run a GUI app over SSH:<\/p>\n<p><span style=\"color: #0000ff;\"> ssh -X stevee@192.168.1.11<\/span><\/p>\n<p>once logged in, cd to the dumpfile folder on the Apache server, and in it's full 27 inch, Win11, remote monitor glory you see Transport Layer Security version 1.3 used for the encryption:<\/p>\n<p><span style=\"color: #0000ff;\">wireshark -r dump.pcap<\/span><\/p>\n<p><a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/SSH-X.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-9552 size-medium\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/uploads\/2022\/11\/SSH-X.png\" alt=\"\" width=\"1920\" height=\"1032\" \/><\/a><\/p>\n<p data-sourcepos=\"3:1-3:188\">The \"HTTPS struck through\" and \"Not Secure\" message in the browser bar (or similar warnings like \"Your connection is not private\") <strong>is entirely due to the certificate being self-signed.<\/strong><\/p>\n<p data-sourcepos=\"5:1-5:24\">Here's why this happens:<\/p>\n<ol data-sourcepos=\"7:1-9:0\">\n<li data-sourcepos=\"7:1-7:403\"><strong>Trust Chain:<\/strong> When your browser connects to an HTTPS website, it receives a certificate from the server. The browser then tries to verify this certificate by checking if it's been signed by a trusted Certificate Authority (CA). All major browsers (Chrome, Firefox, Edge, Safari) come with a pre-installed list of widely recognized and trusted CAs (like DigiCert, Let's Encrypt, GlobalSign, etc.).<\/li>\n<li data-sourcepos=\"8:1-9:0\"><strong>Self-Signed Means Untrusted:<\/strong> A self-signed certificate is one that <em>you<\/em> (or your server, in this case, Apache) created and signed yourself, rather than getting it from a recognized CA. Since your browser doesn't recognize your \"signing authority\" as a trusted CA, it flags the connection as \"Not Secure\" because it cannot verify the identity of the server. It doesn't mean the encryption isn't working; it just means the browser can't confirm who you are.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_9660\" class=\"pvc_stats all  \" data-element-id=\"9660\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/stevepedwards.today\/DebianAdmin\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p>Programmers need to write their web apps for secure server hosting practically universally now, so you should also be writing them in VS Code\/other IDE with a Live Server set up for SSL as in the last Post, but if you also write at home and host on a local apache2 server or similar, it <a href=\"https:\/\/stevepedwards.today\/DebianAdmin\/ssl-on-ubuntu-apache2-creating-self-signed-certificates-checking-ssl-traffic-with-tcpdump-and-wireshark\/\" class=\"more-link\">...<span class=\"screen-reader-text\">\u00a0 SSL on Ubuntu Apache2 &#8211; Creating Self Signed Certificates, Checking SSL Traffic with TCPDUMP and Wireshark<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-9660","post","type-post","status-publish","format-standard","hentry","category-post"],"a3_pvc":{"activated":true,"total_views":29,"today_views":0},"_links":{"self":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/comments?post=9660"}],"version-history":[{"count":14,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9660\/revisions"}],"predecessor-version":[{"id":10278,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/posts\/9660\/revisions\/10278"}],"wp:attachment":[{"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/media?parent=9660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/categories?post=9660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevepedwards.today\/DebianAdmin\/wp-json\/wp\/v2\/tags?post=9660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}