-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64-decode
executable file
·68 lines (53 loc) · 2.4 KB
/
base64-decode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env perl
use Numbski;
use MIME::Base64;
use strict;
my $path = shift or die("Please provide a file to decode!");
my $file = new Numbski(
{
'path' => $path,
}
);
$file->read_file;
$file->dos_to_unix;
# How many output files do we have?
foreach my $output_file($file->contents =~ /Content-Disposition: .*?filename="(\S+?)"/g || $file->contents =~ /Content-Type: .*?name="(\S+?)"/g){
print "My output file is: $output_file\n";
# Unset the Input Record Separator
local $/ = undef;
# Pull out our match data. For whatever reason, it insists on coming out as a list rather than a scalar. :(
# my @base64_chunk =
# ($file->contents =~ /Content-Type:.*?\nContent-Transfer-Encoding: base64\nContent-ID:.*?\nContent-Disposition: .*?filename="$output_file"(((.|\n)*?\n+)+(.|\n)*?)\n\n/i)||
# ($file->contents =~ /Content-Type:.*?name="$output_file"\nContent-Transfer-Encoding: base64\nContent-ID:\s+\S+\n+((\S+?\n)+)/i);
my @base64_chunk =~ /((\S+:.*?\n)|(\S+:.*?(file)?name="$output_file"\s*\n))+\n+((\S+?\n)+)/ig;
#($file->contents =~ /Content-Type:.*?\nContent-Transfer-Encoding: base64\nContent-ID:.*?\nContent-Disposition: .*?filename="$output_file"(((.|\n)*?\n+)+(.|\n)*?)\n\n/) ||
#($file->contents =~ /Content-Type:.*?name="$output_file"\nContent-Transfer-Encoding: base64\nContent-ID:\s+\S+\n+(((.|\n)*?\n+)+\S*?)\n\n/i);
#my @base64_chunk = ($file->contents =~ /(Content-Type: .*?name="$output_file"\nContent-Transfer-Encoding: base64\n)/g);
# print "My output file is now: $output_file\n";
print @base64_chunk;
die;
# Since it comes out as a list, we have to test for the data like a list.
my $encoded_data = $1;
# if(scalar(@base64_chunk)){
if($1){
print "$output_file will be processed.\n";
}
else{
print "$output_file will NOT be processed.\n";
next;
}
# Convert that array to a scalar.
# Use while/shift instead of foreach to save memory use.
# This way the array gets smaller as the scalar gets larger.
# while(@base64_chunk){
# $encoded_data .= shift(@base64_chunk);
# }
# Remove any whitespaces or newlines from the encoded data.
$encoded_data =~ s/(\s|\n)//g;
# Open the file in PWD and spit it out in binmode.
open (FILE, ">".$output_file) or die("Cannot open file ".$output_file." for write: $!");
binmode FILE;
print FILE decode_base64($encoded_data);
close FILE;
}
$/ = "\n";