Bruteforce Apple Disk Image (DMG) from command line (CLI)
This was used because I wrongly typed part of my password for an old dmg image, so it was a success for my needs.
import subprocess
import sys
password = 'PASS'
file = 'docs.dmg'
charset = 'abc'
def gen_pass(line):
base = len(charset)
result = 0
exponent = 0
genstr = ""
while result < line:
exponent += 1
result += base ** exponent
result -= base ** exponent
for e in range(exponent - 1, -1, -1):
pos = -1
while result < line:
result += base ** e
pos += 1
result -= base ** e
genstr += charset[pos]
return genstr
def check_password(file, password, line):
local_password = '{0}{1}'.format(password, gen_pass(line))
print(line, local_password)
proc, err = subprocess.Popen(
['hdiutil', 'verify', '-stdinpass', file],
bufsize=-1,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE
).communicate(input=local_password)
if 'Authentication error' in err:
check_password(file, password, line+1)
else:
print('Password Found: {0}'.format(local_password))
check_password(file, password, 1)