Python on windows subprocess don´t work

423 views Asked by At

I want test to open a winrar password protected file, testing with dictionay of words. This is my code, but it don't work can any help me ? thanks

import subprocess
def extractFile(rFile, password):
try:
    subprocess.call(['c:\\mio\\unrar\\unrar.exe -p'+password+'x C:\\mio\\unrar\\'+rFile,'shell=True'])
    return password
except:
    return
def main():    
rFile = "c:\\mio\\unrar\msploit.rar"
passFile = open("C:\\mio\\unrar\\dic.txt")
for line in passFile.readlines():
    password = line.strip('\n')

    guess = extractFile(rFile, password)
    print(password)
    if guess:
       print '[+] Password = ' + password + '\n'
       break
if __name__ == '__main__':
main()
1

There are 1 answers

1
Michał Niklas On

First argument to call() is an array but you use full command. Try something like:

subprocess.call(['unrar.exe', 'x', '-p'+password, rarfn], shell=True)

EDIT:

I think you will also have to check if output file is created. Maybe unrar with incorrect password will not create output file. Check if it is created (for example use os.path.isfile()). You can also examine unrar output to see where is the problem.

EDIT2:

It seems that there was no x rar command to Extract files with full path.

This is working example where I rar-ed with password file Order.htm into Order.rar and then I deleted Order.htm:

rarfn = 'Order.rar'
outfn = 'Order.htm'
if os.path.isfile(outfn):
    print('%s already exists!!!' % (outfn))
else:
    for password in ('ala', 'ma', 'kota', 'zorro', 'rudy'):
        print('testing %s...' % (password))
        subprocess.call(['unrar.exe', 'x', '-p'+password, rarfn], shell=True)
        if os.path.isfile(outfn):
            print('guessed: [%s]' % (password))
            break