Guest User

Untitled

a guest
Jan 26th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. import os
  2. import re
  3. import mysql.connector
  4. from mysql.connector import Error
  5.  
  6. def getFiles(currentWD):
  7. filesToRead = []
  8. for dirname, dirnames, filenames in os.walk(currentWD):
  9. for filename in filenames:
  10. # ignore some files we don't care about
  11. excludeList = ['errors', '.DS_Store', '._']
  12. if any(item in filename for item in excludeList):
  13. pass
  14. else:
  15. filePath = os.path.join(dirname, filename)
  16. filesToRead.append(filePath)
  17. return filesToRead
  18.  
  19. def dbConnection():
  20. # Connect to a MySQL instance
  21. try:
  22. print "[*] Establishing Connection to your MySQL Server...\n"
  23.  
  24. connection = mysql.connector.connect(host='localhost',
  25. database='CollectionOne',
  26. user='root',
  27. password='4ZaCtr1wfY0M4zo')
  28.  
  29. if connection.is_connected():
  30. dbInfo = connection.get_server_info()
  31. print "[*] You are connected to your MySQL Server. This server is running MySQL version: ", dbInfo
  32. cursor = connection.cursor()
  33. cursor.execute("select database();")
  34. record = cursor.fetchone()
  35. print "[*] You are connected to database: ", record
  36.  
  37. except Error as e:
  38. print "[!] Error while connecting to MySQL - ", e
  39.  
  40. return connection
  41.  
  42. def processFile(workingFile, file_folder, file_name, sqlConnection):
  43. # Process each file in the working directory
  44.  
  45. # Assign some needed variables to capture the data being passed from other functions (probably not needed)
  46. connection = sqlConnection
  47.  
  48. fileFolder = file_folder
  49. fileName = file_name
  50.  
  51. # Open each file
  52. with open(workingFile) as workingFile:
  53. workingFile = workingFile.readlines()
  54.  
  55. for line in workingFile:
  56. # Run some checks to see if the record being read is an email/password combo or a username/password combo
  57.  
  58. # A simple check to see if the line may contain an email address (probably a better way to do this)
  59. if '@' in line:
  60. try:
  61. # strip out any whitespace and newline characters
  62. line.strip()
  63.  
  64. # split the email and passwords by known delimiters
  65. splitLine = re.split(":|;|\|", line)
  66.  
  67. # assigning each `part` to a variable and stripping again (had an issue where passwords were including
  68. # return characters (probably a better way to do this)
  69. email = splitLine[0].strip()
  70. password = splitLine[1].strip()
  71.  
  72. # stripping the email username and domain from the email address and assigning each to a variable
  73. emailANDdomain = email.split('@')
  74. userName = emailANDdomain[0].strip()
  75. domain = emailANDdomain[1].strip()
  76.  
  77.  
  78. cursor = connection.cursor()
  79.  
  80. # build your query
  81. insertQuery = """ INSERT INTO `emails_and_passwords`
  82. (`email`, `password`, `username`, `domain`, `folderName`, `fileName`)
  83. VALUES (%s,%s,%s,%s,%s,%s)"""
  84.  
  85. # assign appropriate variables to your query
  86. insertTuple = email, password, userName, domain, fileFolder, fileName
  87.  
  88. # push and commit changes to the MySQL database
  89. result = cursor.execute(insertQuery, insertTuple)
  90. connection.commit()
  91.  
  92. except Exception as error:
  93. # Handle any errors and roll back changes to the database to prevent corruption
  94. connection.rollback()
  95. print "[!] (PROCESSED AS EMAIL) Failed to insert record into MySQL database {}".format(error)
  96.  
  97. # save any errors caught to a text file for each folder processed - This will be used to improve the
  98. # script at a later date
  99. textFileName = fileFolder + '_errors.txt'
  100. with open(textFileName, 'a') as errors:
  101. # indicate the record was processed in the email statement
  102. errors.write("[PROCESSED AS EMAIL] - "+line)
  103. errors.close()
  104. # Keep it moving
  105. pass
  106.  
  107. # if the above statement fails, process as a username/password combo
  108. else:
  109. try:
  110. # strip out any whitespace and newline characters
  111. line.strip()
  112.  
  113. # split the email and passwords by known delimiters
  114. splitLine = re.split(":|;|\|", line)
  115.  
  116. # assigning each `part` to a variable and stripping again (had an issue where passwords were including
  117. # return characters (probably a better way to do this)
  118. username = splitLine[0].strip()
  119. password = splitLine[1].strip()
  120.  
  121. cursor = connection.cursor()
  122.  
  123. # build your query
  124. insertQuery = """ INSERT INTO `usernames_and_passwords`
  125. (`username`, `password`, `folderName`, `fileName`)
  126. VALUES (%s,%s,%s,%s)"""
  127.  
  128. # assign appropriate variables to your query
  129. insertTuple = username, password, fileFolder, fileName
  130.  
  131. # push and commit changes to the MySQL database
  132. result = cursor.execute(insertQuery, insertTuple)
  133. connection.commit()
  134.  
  135. except Exception as error:
  136. # Handle any errors and roll back changes to the database to prevent corruption
  137. connection.rollback()
  138. print "[!] (PROCESSED AS U/N) Failed to insert record into MySQL database {}".format(error)
  139.  
  140. # save any errors caught to a text file for each folder processed - This will be used to improve the
  141. # script at a later date
  142. textFileName = fileFolder + '_errors.txt'
  143. with open(textFileName, 'a') as errors:
  144. # indicate the record was processed in the username/password statement
  145. errors.write("[PROCESSED AS U/N] - "+line)
  146. errors.close()
  147. # Keep it moving
  148. pass
  149.  
  150.  
  151. def main():
  152. # Throw this script in the directory you want or use the below os.chdir to specify the directory you want to look
  153. # in (just make sure to comment line 148 out if you move the script to your working directory
  154. os.chdir('/Volumes/StorageDriv/CollectionOne')
  155. currentWD = os.getcwd()
  156.  
  157. # Pass the current working directory to function to get a list of files to process and assign it to a variable
  158. filesToProcess = getFiles(currentWD)
  159.  
  160. # Useful to see if you are looking at the right directory
  161. print "[****] Here are the files found for processing: [****]\n", filesToProcess
  162.  
  163. # Establish a variable to pass later on for the database connection
  164. sqlConnection = dbConnection()
  165.  
  166. for file in filesToProcess:
  167. # For each file in the working directory - process each file
  168. print "[****] Processing file: ", file
  169.  
  170. # Get the file folder and file name for each file processed - assign to a variable to pass to on to the
  171. # processing function
  172. str_split = file.split('/')
  173. file_folder = str_split[len(str_split)-2]
  174. file_name =str_split[len(str_split)-1]
  175.  
  176. # Invoke the processing function - Pass all needed data
  177. processFile(file, file_folder, file_name, sqlConnection)
  178. main()
Add Comment
Please, Sign In to add comment