Skip to content

Instantly share code, notes, and snippets.

@nylen
Last active March 16, 2016 06:46
Show Gist options
  • Save nylen/38995549e83d78f806a7 to your computer and use it in GitHub Desktop.
Save nylen/38995549e83d78f806a7 to your computer and use it in GitHub Desktop.
recover my accidentally overwritten ssl certificate :(
#!/usr/bin/env perl
# Run something like this first:
# sudo grep -i -a -B100 -A100 -- '-----BEGIN CERTIFICATE-----' /dev/disk/by-uuid/YOUR_UUID_HERE > whoops.log
# h/t http://unix.stackexchange.com/a/150423/26139
use strict;
use warnings;
open my $log, '<', 'whoops.log';
my $cert = undef;
my $cert_filename = '';
my $cert_num = 0;
my $in_cert = 0;
while ( <$log> ) {
s/\x00+//g;
s/^\s+//;
s/\s+$//;
if ( /^-+BEGIN CERTIFICATE-+$/ ) {
$in_cert = 1;
$cert_num++;
$cert_filename = "recov-$cert_num.crt";
open $cert, '>', $cert_filename;
}
if ( $in_cert ) {
print $cert "$_\n";
if ( /^-+END CERTIFICATE-+$/ ) {
print "wrote $cert_filename\n";
$in_cert = 0;
close $cert;
}
}
}
# then
# for i in recov-*.crt; do echo "$i"; openssl x509 -in "$i" -text -noout > "${i%.crt}.txt"; done
# grep example.com recov-*.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment