A certain Crackme came out with a string encoded with something called custom_base64 ... To get the Flag, you have to decode it to find the original string. What is custom_base64 ... To find the answer, we went to the Amazon hinterland ...
There is a dictionary in which 000000 to 111111 are replaced with characters in the Base64 mechanism. In normal Base64, a dictionary to which ʻABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + /` is applied is used in order. The contents of the custom_base64 function were replaced with the specified dictionary. [Article implementing Base64] In this article, the decoder is implemented in Python with reference to 1.
Roughly check the Base64 encoding process. See other sites for details.
Decoding is easy if you understand the encoding mechanism! Basically, just follow the reverse procedure!
SG9nZUhvZ2U=
→ SG9nZUhvZ2U
SG9nZUhvZ2U
→ 010010 000110 111101 100111 011001 010100 100001 101111 011001 110110 010100
01001000 01101111 01100111 01100101 01001000 01101111 01100111 01100101 00
01001000 → H
01101111 → o
01100111 → g
01100101 → e
01001000 → H
01101111 → o
01100111 → g
01100101 → e
custom_base64_decoder.py
import sys
import argparse
BYTE_SIZE = 8
# 000000 ->A function that creates a dictionary-type list character by character up to 111111
def makeDict(base64Dict_seed):
dictionary = {}
for i in range(0, 64):
dictionary[format(i, '06b')] = base64Dict_seed[i]
return dictionary
#A function that lists the string s separated by n characters
def split(string, n):
split_list = []
for i in range(0, len(string), n):
split_list.append(string[i:i+n])
return split_list
#If the string does not have n characters, it will be n characters`c`Add
def fillBlank(s, n, c):
mod = len(s) % n
if mod == 0:
return s
else:
margin = n - mod
return s + c * margin
#Passing a dictionary value returns the dictionary key
def getValue(key, items):
for v in items.items():
# print(v[1])
if v[1] == key:
# print(v)
return v[0]
return ''
def main():
# -You can enter a custom dictionary by adding k
parser = argparse.ArgumentParser(
description='custom Base64 Decoder')
parser.add_argument('-k', '--key', help="Use custom Seed to encrypt in base64 ", \
default="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/")
parser.add_argument('text', help='base text')
args = parser.parse_args()
# 0.Make a dictionary
base64Dict = makeDict(args.key)
# 1. '='Remove
text = args.text.replace("=", '')
binStr = ""
# 2.Use the conversion table to convert characters to binary and connect them.
for i in text:
binStr += getValue(i, base64Dict)
# 3.Divide the binary into 8 bits, encode 3.Since the 0s added by are left over, delete them.
splitCount = 8
s = split(binStr, splitCount)
if (len(s[-1]) != 8):
s.pop(-1)
# 4.Convert binary bits to ASCII
result =""
for c in s:
print(c + " → " + chr(int(c, 2)))
result += chr(int(c, 2))
print(result)
if __name__ == "__main__":
main()
$ python3 customBase64Decoder.py <Base64 text>
$ python3 customBase64Decoder.py -k <Custom dictionary> <Base64 text>
$ python3 customBase64Decoder.py SG9nZUhvZ2U=
$ python3 customBase64Decoder.py -k xEPOKnvADqeG0m1VkZ47CM653jrtbzLsTc2ypoYUSWJ9ludQig+awf8XF/RNHBhI 4vBUjCcQj8C=
HogeHoge
Base64 I fully understood. With this, you can make an original Base64 and make secret communication, you did it
Sample code can be found on GitHub
--Encoder - https://github.com/itiB/sandpit/blob/master/tools/customBase64Encoder.py --Decoder - https://github.com/itiB/sandpit/blob/master/tools/customBase64Decoder.py
What is base64? ?? I implemented it for understanding --qiita https://qiita.com/PlanetMeron/items/2905e2d0aa7fe46a36d4
Recommended Posts