Guest User

Untitled

a guest
Jul 23rd, 2016
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Configurable options:
  2.  
  3. #Make sure the path exists or you will spam your list every time the script runs:
  4. $path_to_notified_file = ".\db\pwnd_list.csv"
  5.  
  6. #users to check for breach
  7. $user_list = get-aduser -filter { Emailaddress -like "*" -AND Enabled -eq $true} -prop emailaddress | select -expandproperty Emailaddress
  8.  
  9. #SMTP settings:
  10. $email_notify = $true
  11. $subject = "ATTN: Account was included in a data breach"
  12. $body_html = "Hello,<br>It has been noticed by an automated system that your email address was included in the following data breaches:"
  13. $body_signature = "<br>It is recomended you change your passwords on those systems<br><br>Thank you<br>I_script_stuff Notifier Bot<br>"
  14.  
  15. #email credentials enable tested on gmail. If you don't need credentials set $needs_email_creds to false.
  16. $needs_email_creds = $false
  17. #configure credential file for email password if needed:
  18. $creds_path = ".\cred.txt"
  19. #read-host -assecurestring | convertfrom-securestring | out-file $creds_path
  20.  
  21.  
  22. #SMTP server to use
  23. $smtp = "smtp.gmail.com"
  24. $smtp_port = "587"
  25.  
  26. #process smtp credentials
  27. $pass = get-content $creds_path | convertto-securestring
  28. $credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist "$from", $pass
  29. #
  30. # Search and notify
  31. #
  32. if(test-path $path_to_notified_file ) {
  33. $already_found = get-content $path_to_notified_file
  34. } else {
  35. $already_found = ""
  36. echo "Warning: No file loaded for $path_to_notified_file If this is the first time running the script a file will be created."
  37. sleep 1
  38. }
  39.  
  40. #
  41. #Function that power the script
  42. #
  43. function get-breachedstatus() {
  44.     Param(
  45.         [Parameter(Mandatory = $true)][string]$email,
  46.         [AllowEmptyString()]$brief_report="$true"
  47.     )
  48.    
  49.     try{
  50.         if($brief_report) {
  51.         $url = "https://haveibeenpwned.com/api/v2/breachedaccount/" + $email + "?truncateresponse=true"
  52.         } else {
  53.         $url = "https://haveibeenpwned.com/api/v2/breachedaccount/" + $email
  54.         }
  55.     $result = invoke-restmethod "$url" -UserAgent "I_script_stuff checker 0.01"
  56.     return $result
  57.     } catch {
  58.     return $false
  59.     }
  60. }
  61.  
  62. foreach($email in $user_list) {
  63.     if($result = get-breachedstatus $email $false) {
  64.         $working_email_body = $body_html
  65.         $act_on_notify = $false
  66.         foreach($line in $result) {
  67.         $service = $line.Name
  68.         $breachdate = $line.breachdate
  69.         $breach_record = "$email,$service,$breachdate"
  70.             if($already_found -notcontains $breach_record) {
  71.             echo "$breach_record"
  72.             echo "$breach_record" >> $path_to_notified_file
  73.                 if($email_notify) {
  74.                 $act_on_notify = $true
  75.                 $working_email_body += "<br>" + $breach_record
  76.                 }
  77.             }
  78.         }
  79.         $working_email_body += $body_signature
  80.         if(($email_notify) -and ($act_on_notify)) {
  81.             if($needs_email_creds) {
  82.             Send-MailMessage -from $from -To "$email" -Subject $subject -bodyashtml($working_email_body) -smtpServer "$smtp" -port "$smtp_port" -credential $credentials -UseSsl
  83.             } else {
  84.             Send-MailMessage -from $from -To "$email" -Subject $subject -bodyashtml($working_email_body) -smtpServer "$smtp" -port "$smtp_port"
  85.             }
  86.         }
  87.     }
  88. #lets not abuse the api
  89. sleep 5
  90. }
Advertisement
Add Comment
Please, Sign In to add comment