-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressed-base64-decode.py
82 lines (66 loc) · 2.97 KB
/
compressed-base64-decode.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
Script Name: compressed-base64-decode.py
Author: Justin Lund
Last modified: 04/16/24
Date created: 06/06/23
Version: 1.2
Purpose:
This script is designed to decode comrpessed base 64
It will try decompressing with various algorithms
Usage:
python3 compressed-base64-decode.py -i encoded_base64.txt -o decoded_text.txt
python3 compressed-base64-decode.py -i encoded_base64.txt -p
"""
import base64
import argparse
import zlib
import gzip
import lzma
from io import BytesIO
# Function to decode and decompress base64 data
def base64_decode(base64_data):
# Step 1: Decode the base64 string. This converts the base64 string into a bytes object
decoded_data = base64.b64decode(base64_data)
# List of decompression methods to try
decompress_methods = [gzip_decompress, lzma_decompress, zlib_decompress]
for method in decompress_methods:
try:
decompressed_data = method(decoded_data)
return decompressed_data
except Exception:
# If the decompression method failed, print a message and continue with the next method
print(f"Failed to decompress using {method.__name__}, trying next method.")
# If none of the decompression methods worked, return the original decoded data
print("Unable to decompress data, returning decoded data.")
return decoded_data
def gzip_decompress(data):
return gzip.GzipFile(fileobj=BytesIO(data)).read()
def lzma_decompress(data):
return lzma.decompress(data)
def zlib_decompress(data):
# The second parameter, -zlib.MAX_WBITS, is a magic parameter that tells zlib that
# the data was compressed without any zlib headers - this is how DeflateStream works in .NET.
return zlib.decompress(data, zlib.MAX_WBITS)
def main():
parser = argparse.ArgumentParser(description='Decodes a Base64 string from an input file.')
parser.add_argument('-i', '--input', help='The input file containing the Base64 string.')
parser.add_argument('-p', '--print', action='store_true', help='Print the decoded output to the screen instead of writing to a file.')
parser.add_argument('-o', '--output', type=str, help='Output file path. If not specified, "decoded_output.txt" will be used.')
args = parser.parse_args()
if args.input is None:
print("No input file provided. Please provide an input file with the -i option.")
return
with open(args.input, 'r') as f:
base64_string = f.read().strip()
decoded_data = base64_decode(base64_string)
if args.print:
# Decode bytes to string and print, assuming the data is UTF-8 encoded.
# If the encoding could be different, may need to handle this more flexibly.
print("Decoded data:\n" + decoded_data.decode('utf-8'))
else:
output_file = args.output if args.output else 'decoded_output.txt'
with open(output_file, 'wb') as f:
f.write(decoded_data)
print(f"Decoded data has been written to {output_file}")
if __name__ == "__main__":
main()