Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import csv
  2. import requests
  3. from bs4 import BeautifulSoup
  4. import argparse
  5. import sys
  6. import re
  7. from time import sleep
  8.  
  9.  
  10. # define arguments
  11. parser = argparse.ArgumentParser(description='Scrape haveibeenpwned for email breach tools')
  12. parser.add_argument('-e', "--email", help='Email to search')
  13. parser.add_argument('-s', "--search", help='Search databases ')
  14. parser.add_argument("-l", "--list")
  15. parser.add_argument('-o', '--output', help='Output results to csv')
  16. args = parser.parse_args()
  17. # check if any arge are present befor starting the prog
  18. if len(sys.argv) < 2:
  19. parser.print_usage()
  20. sys.exit(1)
  21. # set up our vars
  22. emails_to_check = []
  23. email_not_pwned = []
  24. email_pwned = []
  25. pwned_email_info = {}
  26. #set up vars from argpharse
  27. email_input_list = args.list
  28. search_bol = args.search
  29. output_csv_file = args.output
  30. target_email = args.email
  31.  
  32. #email regex for seing if a email matches the regex
  33. def read_from_file(input_list_file):
  34. with open(input_list_file, "r") as input_file:
  35. print("starting search with these emails!")
  36. for current_email_line in input_file:
  37. print(current_email_line)
  38. regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
  39. match = re.match(regex, current_email_line)
  40. if match == None:
  41. print(f"{current_email_line} dosnt match email syntax")
  42. else:
  43. print("Email matched regex: {}".format(email))
  44. emails_to_check.append(current_email_line)
  45.  
  46. return(emails_to_check)
  47.  
  48. def get_source(account):
  49. print(f'starting search for: {account}')
  50. url = f"https://haveibeenpwned.com/account/{account}"
  51. base_request = requests.get(f"https://haveibeenpwned.com/account/{account}").text
  52. soup = BeautifulSoup(base_request, "lxml")
  53. database_list = []
  54. pwn_count = 0
  55. for match in soup.find_all("span", class_="pwnedCompanyTitle"):
  56. #pwn_co print(pwned_email_info)unt += 1
  57. #print(match.text.replace(":", ''))
  58. database_list.append(match.text.replace(":", ""))
  59. pwned_email_info[account] = database_list
  60. return(pwned_email_info)
  61. #print(pwn_count)
  62.  
  63. def main():
  64. if target_email != None:
  65. get_source(target_email)
  66. elif email_input_list != None:
  67. read_from_file(email_input_list)
  68. for test in emails_to_check:
  69. get_source(test)
  70. sleep(3)
  71. print(pwned_email_info)
  72.  
  73. if __name__ == "__main__":
  74. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement