python :: 파일을 base64로 인코딩/디코딩
저런 이유로 예전에 심심풀이로 python으로 만든 프로그램입니다. 간혹 포렌식 문제를 풀 때 이 프로그램이 도움이 되곤 했네요.
사용법은 간단합니다. 인코딩할 때는 -e 옵션을 주고, 디코딩할 때는 -d 옵션을 줍니다. 인/디코딩 옵션 뒤에는 원본 파일 혹은 텍스트 파일의 경로를 입력합니다. 그 뒤에 -o 옵션을 입력한 다음에 출력할 파일의 경로를 입력하면 됩니다.
(예제)
$Base64EnDecoder.py -e summary.doc -o summary.txt
$Base64EnDecoder.py -d summary.txt -o summary.doc
Base64EnDecoder.py
#!/usr/bin/python
import sys
import base64
def error_handle():
print '[+] use: $python Base64EnDecoder.py -e [file] -o [file]'
print '[+] use: $python Base64EnDecoder.py -d [file] -o [file]'
sys.exit(1)
if len(sys.argv) != 5:
error_handle()
try:
input = open(sys.argv[2], 'rb').read()
except:
print '[*] error: file does NOT exist.'
sys.exit(1)
try:
if sys.argv[1] == '-e':
data = base64.b64encode(input)
elif sys.argv[1] == '-d':
data = base64.b64decode(input)
else:
print '[*] error: input option is \'-e\' or \'-d\'.'
error_handle()
except:
print '[*] error: incorrect padding'
sys.exit(1)
if sys.argv[3] == '-o':
output = open(sys.argv[4], 'wb')
output.write(data)
else:
print '[*] error: output option is \'-o\'.'
error_handle()
YUhSMGNEb3ZMM2QzZHk1c2RXNWhjM1j2Y25rdWQyOHVkR012
는 디코딩 하면 뭐가나오나?