Python Script: DOCX Password Hunter

by Vince
in Blog
Hits: 1914

I tried writing this with fewer lines of code using a list of passwords and another attempt with IGNORECASE but neither worked or worked with 100% accuracy.  Rather than spin my wheels, I just went this route with elif.  

We're recursively searching inside of Word docx files for either:  password, Password, or PASSWORD

When we get a match, we print the document location and the line containing our string match. 

Storing passwords in a Word document is a bad practice -- this script shows you why it's a bad practice and why you should use a password manager.

#!/usr/bin/python
import os
import re
import docx
document_list = []
for path, subdirs, files in os.walk(r"./"):
    for name in files:
        if os.path.splitext(os.path.join(path, name))[1] == ".docx":
            document_list.append(os.path.join(path, name))
for document_path in document_list:
    document = docx.Document(document_path)
    for paragraph in document.paragraphs:
        if "password" in paragraph.text:
            print(document_path+':'+paragraph.text)
        elif "Password" in paragraph.text:
            print(document_path+':'+paragraph.text)
        elif "PASSWORD" in paragraph.text:
            print(document_path+':'+paragraph.text)