Reputation: 1395
Im gathering the following data filenames, usernames and passwords. This data is being gathered by traversing each dir looking for documents(scripts mostly) with credentials in clear text. The idea is to gather evidence of bad practices being followed by system admins.
The my script does this well enough, however I am trying to understand the best way to handle the data. Id like to place the filename and credentials found in that particular file into a dictionary. So the key being the filename and the values being the credentials found in that file.
Ive worked out how to add data to dictionaries but im not entirely sure how to and cant find a way to get 2 lists into a dictionary and the dictionary hosting multiple values for 1 key. Any pointers would be appreciated. The line with #if not m: add non matched data to un_matched list
is currently not used, as suggested by the comment. Id like to add non matched data to another list (for debugging)
Code
dirt = "~/Desktop/tmp"
def get_files():
regs = ["(.*)((U|u)ser(.*))(\s=\s\W\w+\W)", "(.*)((U|u)ser(.*))(\s=\s\w+)", "(.*)((P|p)ass(.*))\s=\s(\W(.*)\W)", "(.*)((P|p)ass(.*))(\s=\s\W\w+\W)"]
combined = "(" + ")|(".join(regs) + ")"
cred_results = []
creds = []
un_matched = []
filesfound = []
for root, dirs, files in os.walk(dirt):
for filename in files:
if filename.endswith(('.bat', '.vbs', '.ps', '.txt')):
readfile = open(os.path.join(root, filename), "r")
for line in readfile:
m = re.match(combined, line)
if m:
creds.append(m.group(0))
#if not m: add non matched data to un_matched list
filesfound.append(os.path.join(root, filename))
cred_results = [line.rstrip() for line in creds]
print cred_results
print filesfound
Current Ouput from script
['strUser = "guytom"', 'strPassword = "P@ssw0rd1"', 'strUsername = "guytom2"', 'strPass = "SECRETPASSWORD"']
['~/Desktop/tmp/Domain/Policies/{31B2F340-016D-11D2-945F-00C04FB984F9}/USER/Scripts/Logon/logonscript1.vbs', '~/Desktop/tmp/Domain/Policies/{31B2F340-016D-11D2-945F-00C04FB984F9}/USER/Scripts/Logon/logonscript2.bat']
Upvotes: 1
Views: 178
Reputation: 180461
You can use a dict with dict.setdefault:
d = {} # create dict
for root, dirs, files in os.walk(dirt):
for filename in files:
if filename.endswith(('.bat', '.vbs', '.ps', '.txt')):
readfile = open(os.path.join(root, filename), "r")
d.setdefault(filename,[]) # set default value to a list
for line in readfile:
m = re.match(combined, line)
if m:
creds.append(m.group(0))
d[filename].append(m.group(0).rstrip()) # append data to the key's list stripping newlines etc..
If you want to keep track of the unmatched data just add a second dict and using with
which will close your files automatically:
for root, dirs, files in os.walk(dirt):
for filename in files:
if filename.endswith(('.bat', '.vbs', '.ps', '.txt')):
with open(os.path.join(root, filename), "r") as readfile:
matched_d.setdefault(filename,[])
unmatched_d.setdefault(filename,[])
for line in readfile:
m = re.match(combined, line)
if m:
creds.append(m.group(0))
d[filename].append(m.group(0).rstrip())
else:
unmatched_d[filename].append(add_data_here)
Upvotes: 2