Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active April 5, 2017 13:25
Show Gist options
  • Save gdamjan/6e2103210393d56f6d41 to your computer and use it in GitHub Desktop.
Save gdamjan/6e2103210393d56f6d41 to your computer and use it in GitHub Desktop.
cgi bin script to check ssl domains for expiry - https://damjan.softver.org.mk/cgi-bin/ssl.sh
#!/bin/bash
set -x
exec 2>/dev/null
unset HTTPS_PROXY
unset HTTP_PROXY
unset http_proxy
WARNDAYS=14
PANICDAYS=6
DOMAIN_LIST_URL=https://raw.githubusercontent.com/skopjehacklab/dns-zone-files/master/ssl/list_of_ssl_enabled_domains.txt
WARNSECONDS=$(($WARNDAYS * 24 * 60 * 60))
PANICSECONDS=$(($PANICDAYS * 24 * 60 * 60))
PANIC () {
echo "<tr class=panic data-expires='$2' data-expires-days=$3 data-issuer='$4'>"
echo "<td class=icon>✗</td><td>$1 <small>(expires in $3 days)</small></td>"
echo "</tr>"
}
WARN () {
echo "<tr class=warn data-expires='$2' data-expires-days=$3 data-issuer='$4'>"
echo "<td class=icon>⚠</td><td>$1 <small>(expires in $3 days)</small></td>"
echo "</tr>"
}
INFO () {
echo "<tr class=info data-expires='$2' data-expires-days=$3 data-issuer='$4'>"
echo "<td class=icon>✓</td><td>$1</td>"
echo "</tr>"
}
ERROR () {
echo "<tr class=error data-expires='' data-expires-days='999999' data-issuer=''>"
echo "<td class=icon>✗</td><td>$1</td>"
echo "</tr>"
}
cat <<EOF
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<html lang="mk">
<head>
<style type="text/css">
#content { color: #888; font-family:monospace; font-size:2em; vertical-align:middle; margin: 1em;}
#content small { font-family: sans-serif; font-size:0.5em; }
#content td { padding-right:1em; }
#content td.icon { padding-right:0.15em; }
#content thead tr td { border-bottom: 1px #888 solid; }
tr.warn { color: #F80 }
tr.panic { color: red }
button#verbose { margin: 1em; }
</style>
<script src="//code.jquery.com/jquery.min.js"></script>
<script>
jQuery(function() {
jQuery('#content tbody tr').sort(function (a, b) {
return + a.dataset.expiresDays - + b.dataset.expiresDays
}).appendTo(jQuery('#content tbody'));
jQuery('#verbose').one('click', function() {
jQuery(this).remove();
jQuery('#content thead').show();
jQuery('#content tbody tr').each(function() {
var tr = jQuery(this);
tr.append(jQuery('<td>').text(this.dataset.issuer));
tr.append(jQuery('<td>').text(this.dataset.expires));
tr.append(jQuery('<td>').text(this.dataset.expiresDays));
tr.find('td small').remove();
});
});
});
</script>
<title>check expiration date of ssl certificates</title>
</head>
<body>
<table id=content>
<thead style="display:none">
<tr><td class=icon></td><td>Domainame</td><td>Issuer</td><td>Expires</td><td>Days</td></tr>
</thead>
<tfoot><tr><td colspan=4>
<button id=verbose>Verbose</button>
</td></tr></tfoot>
<tbody>
EOF
now=`date +%s`
timeout 10 curl "$DOMAIN_LIST_URL" |
egrep -v '^#' | egrep -v '^[[:space:]]*$' |
while read line
do
host=${line%:*}
port=${line#*:}
cert=`timeout 10 openssl s_client -servername "$host" -connect "$host:$port" < /dev/null`
if [[ $? -ne 0 ]]; then
ERROR "$host"
continue
fi
issuer=`echo "$cert" | openssl x509 -noout -issuer`
issuer=${issuer#*/O=}
issuer=${issuer%%/*}
issuer=${issuer//\'/&apos;}
issuer=${issuer//\"/&quot;}
enddate=`echo "$cert" | openssl x509 -noout -enddate | cut -f2 -d=`
expires_timestamp=`date --date="$enddate" +%s`
expires_days=$((($expires_timestamp - $now)/60/60/24))
if ! echo "$cert" | openssl x509 -noout -checkend $PANICSECONDS; then
PANIC "$host" "$enddate" "$expires_days" "$issuer"
elif ! echo "$cert" | openssl x509 -noout -checkend $WARNSECONDS; then
WARN "$host" "$enddate" "$expires_days" "$issuer"
else
INFO "$host" "$enddate" "$expires_days" "$issuer"
fi
done
cat <<EOF
</tbody>
</table>
</body>
</html>
EOF
// inject jquery
var s = document.createElement('script')
s.setAttribute('src', '//code.jquery.com/jquery.min.js')
s.addEventListener('load', function () {
sort()
}, false)
document.head.appendChild(s)
function sort() {
$(function () {
$('body>div').sort(function (a, b) {
return + a.dataset.index - + b.dataset.index
}).appendTo($('body'))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment