{"id":384,"date":"2016-02-17T12:09:45","date_gmt":"2016-02-17T11:09:45","guid":{"rendered":"http:\/\/www.michael.tassati.at\/?p=384"},"modified":"2016-02-17T12:16:00","modified_gmt":"2016-02-17T11:16:00","slug":"have-i-been-pwned-test-all-users-of-your-domain","status":"publish","type":"post","link":"https:\/\/www.michael.tassati.at\/?p=384","title":{"rendered":"have i been pwned? Test all users of your domain"},"content":{"rendered":"<p>Aus einem Powershell-Beispiel von &#8222;IT Pro blog&#8220; weiterentwickelt<br \/>\nhttps:\/\/infracloud.wordpress.com\/2015\/10\/29\/have-you-been-pwned-use-powershell-to-find-out\/<br \/>\n<a href=\"http:\/\/www.michael.tassati.at\/wp-content\/uploads\/2016\/02\/Get-Pwned.ps1_.txt\">Get-Pwned.ps1<\/a><\/p>\n<p>Import-Module &#8222;C:\\adm\\custom\\BasicTools.psm1&#8220;<br \/>\n$Global:CheckURI = &#8222;https:\/\/haveibeenpwned.com\/api\/v2\/breachedaccount&#8220;<br \/>\n$global:OutputPath = &#8222;C:\\&#8220;<br \/>\n$global:results = @()<br \/>\nFunction ValidateAddress($Emailaddress){<br \/>\n    try{<br \/>\n        $Request = Invoke-WebRequest -Uri &#8222;$global:CheckURI\/$Emailaddress&#8220;<br \/>\n        $Response = ConvertFrom-Json $Request<br \/>\n        Return $Response<br \/>\n    }<br \/>\n    catch [exception]<br \/>\n    {<br \/>\n        Return $null<br \/>\n    }<br \/>\n}<br \/>\nFunction IsValidEMail($email){<br \/>\n\u00a0 \u00a0 $EmailRegex = &#8218;^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$&#8216;;<br \/>\n\u00a0 \u00a0 return ($email -match $EmailRegex)<br \/>\n}<br \/>\nFunction IsValidDomain($mailaddress){<br \/>\n    $domain = $mailaddress.Substring($mailaddress.IndexOf(&#8222;@&#8220;)+1)<br \/>\n    $obj = Get-AcceptedDomain -Identity $domain -ErrorAction SilentlyContinue<br \/>\n    if ($obj -eq $null){<br \/>\n        $result = $false<br \/>\n    }else{<br \/>\n        $result = $true<br \/>\n    }<\/p>\n<p>    return $result<br \/>\n}<br \/>\nFunction ConvertHaveibeenpwnedObject($obj,$mailaddress,$username){<br \/>\n    Set-ToolsLogAddLine -LogTXT &#8222;Compromised account $username, $mailaddress&#8220;<br \/>\n    $result = New-Object -TypeName PSObject<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name username -Value $username<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name mailaddress -Value $mailaddress<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPAddedDate -Value $obj.AddedDate<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPBreachDate -Value $obj.BreachDate<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPDataClasses -Value ([system.String]::Join(&#8222;;&#8220;,$obj.DataClasses))<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPDescription -Value $obj.Description<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPDomain -Value $obj.Domain<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPIsSensitive -Value $obj.IsSensitive<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPIsVerified -Value $obj.IsVerified<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPLogoType -Value $obj.LogoType<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPName -Value $obj.Name<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPPwnCount -Value $obj.PwnCount<br \/>\n    $result | Add-Member -MemberType NoteProperty -Name HIBPTitle -Value $obj.Title<br \/>\n    return $result<br \/>\n}<br \/>\nFunction main{<br \/>\n    Set-ToolsLogCreate -LogFilePath &#8222;C:\\adm\\custom\\logs\\&#8220; -LogPrefix &#8222;CompromisedAccounts&#8220;<br \/>\n    Remove-ToolsLog -olderThenDays 21<br \/>\n    Set-ToolsLogAddLine -LogTXT &#8222;Collect accounts&#8220;<\/p>\n<p>    $users = Get-ADUser -LDAPFilter &#8222;(mail=*)&#8220; -Properties mail, proxyaddresses -ResultSetSize $null<\/p>\n<p>    $i = ($users).Count<br \/>\n    Set-ToolsLogAddLine -LogTXT &#8222;Test $i accounts&#8220;<br \/>\n    $i = 0<br \/>\n    foreach($user in $users){<br \/>\n        $i ++<br \/>\n        if($i\/100 -is [int]){Set-ToolsLogAddLine -LogTXT &#8222;$i accounts tested&#8220;}<br \/>\n        $proxymail = &#8222;&#8220;<br \/>\n        $usermail = &#8222;&#8220;<br \/>\n        $usermail = $user.mail.ToLower()<br \/>\n        $output = &#8222;&#8220;<br \/>\n        if (IsValidEMail($usermail)){<br \/>\n            if (isValidDomain($usermail)){<br \/>\n                $obj = ValidateAddress($usermail)<br \/>\n                if ($obj -ne $null){<br \/>\n                    $global:results += ConvertHaveibeenpwnedObject -obj $obj -mailaddress $usermail -username $user.SamAccountName<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        Foreach($proxyAddress in $user.proxyaddresses){<br \/>\n            $proxymail = $proxyAddress.ToLower()<br \/>\n            if ($proxymail.StartsWith(&#8222;smtp:&#8220;)){<br \/>\n                $proxymail = $proxymail.Substring($proxymail.IndexOf(&#8222;smtp:&#8220;)+5)<br \/>\n                if (IsValidEMail($proxymail)){<br \/>\n                    if ($proxymail -ne $usermail){<br \/>\n                        if (isValidDomain($proxymail)){<br \/>\n                            $obj = ValidateAddress($proxymail)<br \/>\n                            if ($obj -ne $null){<br \/>\n                                 $global:results += ConvertHaveibeenpwnedObject -obj $obj -mailaddress $usermail -username $user.SamAccountName<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    Set-ToolsLogAddLine -LogTXT &#8222;Finish&#8220; -foregroundcolor &#8222;green&#8220; -backgroundcolor &#8222;darkgray&#8220;<br \/>\n    $global:results | Export-csv &#8222;$global:OutputPath\\CompromisedAccounts.csv&#8220;<br \/>\n}<\/p>\n<p>main<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Aus einem Powershell-Beispiel von &#8222;IT Pro blog&#8220; weiterentwickelt https:\/\/infracloud.wordpress.com\/2015\/10\/29\/have-you-been-pwned-use-powershell-to-find-out\/ Get-Pwned.ps1 Import-Module &#8222;C:\\adm\\custom\\BasicTools.psm1&#8220; $Global:CheckURI = &#8222;https:\/\/haveibeenpwned.com\/api\/v2\/breachedaccount&#8220; $global:OutputPath = &#8222;C:\\&#8220; $global:results = @() Function ValidateAddress($Emailaddress){ try{ $Request = Invoke-WebRequest -Uri &#8222;$global:CheckURI\/$Emailaddress&#8220; $Response = ConvertFrom-Json $Request Return $Response } catch [exception] { Return $null } } Function IsValidEMail($email){ \u00a0 \u00a0 $EmailRegex = &#8218;^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$&#8216;; \u00a0 \u00a0 return ($email -match &hellip; <a href=\"https:\/\/www.michael.tassati.at\/?p=384\" class=\"more-link\"><span class=\"screen-reader-text\">have i been pwned? Test all users of your domain<\/span> weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,60,117],"tags":[118,120,119],"_links":{"self":[{"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/posts\/384"}],"collection":[{"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=384"}],"version-history":[{"count":3,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/posts\/384\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=\/wp\/v2\/posts\/384\/revisions\/388"}],"wp:attachment":[{"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.michael.tassati.at\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}