Created
October 24, 2019 09:41
-
-
Save mschmitt/b1690b6cb75537a738157f91a99bf2a2 to your computer and use it in GitHub Desktop.
Decoder for a file/log that contains Base64-encoded words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use diagnostics; | |
use MIME::Base64; | |
# Decoder for a file/log that contains Base64-encoded words | |
binmode STDIN, ':utf8'; | |
binmode STDOUT, ':utf8'; | |
while (my $line1 = <STDIN>){ | |
chomp $line1; | |
# Work on a copy of the original line | |
my $line2 = $line1; | |
# split along non-base64 charset characters | |
my @words = split /[^A-Za-z0-9+\/=]/, $line2; | |
foreach my $encoded1 (@words){ | |
# Decode each base64 string | |
if ($encoded1 =~ /^[A-Za-z0-9+\/=]+$/){ | |
my $decoded = decode_base64($encoded1); | |
# Strip all nonprintable characters that were decoded | |
# from invalid Base64 inputs (which usually are many) | |
$decoded =~ s/[^[:print:]]//g; | |
# Encode back to Base64 to check for plausibility | |
my $encoded2 = encode_base64($decoded); | |
chomp $encoded2; | |
# Replace string if reencoded is same as original | |
if ($encoded1 eq $encoded2){ | |
$line2 =~ s/\Q$encoded1\E/"$decoded"/; | |
} | |
} | |
} | |
#print "\n$line1\n"; | |
print "$line2\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment