Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created March 27, 2019 05:21
Show Gist options
  • Select an option

  • Save theoremoon/edb18da90ed1644c866c6731438f1d71 to your computer and use it in GitHub Desktop.

Select an option

Save theoremoon/edb18da90ed1644c866c6731438f1d71 to your computer and use it in GitHub Desktop.
import subprocess
import sys
import os
from itertools import product
OUTPUT_DIR = 'decrypted_files'
# list up all cipher methods and digest algorithms
lines = subprocess.run(['openssl', 'help'], check=True, stderr=subprocess.PIPE).stderr.decode().splitlines()
ciphers = []
digests = ['']
mode = 0
for l in lines:
if l.startswith('Message Digest commands'):
mode = 1
elif l.startswith('Cipher commands'):
mode = 2
elif mode == 1:
digests.extend(l.split())
elif mode == 2:
ciphers.extend(l.split())
# make output directory
try:
os.mkdir(OUTPUT_DIR)
except FileExistsError:
pass
# try all cipher methods and digest algorithms for decrypt
for pattern in product(ciphers, digests):
cipher, digest = pattern
out_name = os.path.join(OUTPUT_DIR, cipher+'_'+digest)
base_cmd = ['openssl', cipher.lstrip('-'), '-d', '-out', out_name]
digest_opt = ['-md', digest] if digest else []
# call openssl
try:
out = subprocess.run(base_cmd + digest_opt + sys.argv[1:], check=True, stderr=subprocess.PIPE).stderr.decode()
except subprocess.CalledProcessError:
try:
os.remove(out_name)
except OSError:
pass
@theoremoon
Copy link
Author

python3 try-all-openssl.py -in flag.html.enc -pass file:./key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment