Advertisement
afalahi

Bypass-ActiveDirectory-PasswordHistory

Jun 16th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Function to generate password
  2. Function Get-Password ($length=10)
  3. {
  4.     Add-Type -AssemblyName System.Web
  5.     $password = [System.Web.Security.Membership]::GeneratePassword($length,4)
  6.     return $password
  7. }
  8. Function Secure-Password
  9.     {
  10.         $password= Read-Host -AsSecureString "Enter your password"
  11.         $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)  
  12.         $password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
  13.         return $password
  14.     }
  15.  
  16. #Define user variables
  17. $userName=$env:USERNAME
  18. $currentPassword=Secure-Password
  19. #$preferredPassword=$true              #comment out if your current password is your preferred one
  20. $preferredPassword=$currentPassword
  21. $passwordHistory=24
  22.  
  23. #Initialize the user context
  24. Add-Type -AssemblyName System.DirectoryServices.AccountManagement
  25. $principalContext=New-Object System.DirectoryServices.AccountManagement.PrincipalContext("Domain")
  26. $userPrincipal=[System.DirectoryServices.AccountManagement.UserPrincipal]::FindByIdentity($principalContext,"SamAccountName", $userName)
  27. $passwordList=@{}
  28.  
  29. for($i=0;$i-le $passwordHistory;$i++)
  30.     {
  31.         $newPassword=Get-Password
  32.         Write-Host "`nAttemping password change number"$i
  33.         $passwordList.Add("Password-$i",$newPassword)
  34.         try
  35.             {
  36.                 $userPrincipal.ChangePassword($currentPassword, $newPassword)
  37.             }
  38.         catch [PasswordException]
  39.             {
  40.                 $Error[0].Exception.Message
  41.             }
  42.         Write-Host "Password Changed successfuly"
  43.         $currentPassword = $newPassword
  44.         Sleep -Milliseconds 1500
  45.        
  46.     }
  47.  
  48. if($preferredPassword -eq $true)
  49.     {
  50.         $newPassword=Secure-Password
  51.         $userPrincipal.ChangePassword($currentPassword, $newPassword)
  52.     }
  53. else
  54.     {
  55.         $userPrincipal.ChangePassword($currentPassword, $preferredPassword)
  56.         $currentPassword=$preferredPassword
  57.     }
  58.  
  59. $validate=$principalContext.ValidateCredentials($userName,$currentPassword)
  60. Write-Host "`nValidating password change"
  61. if($validate)
  62.     {
  63.         Return "Password validation succeeded"
  64.  
  65.     }
  66. Else
  67.     {
  68.         $passwordList | Out-File (((Get-ChildItem).DirectoryName | select -First 1)+"\passwordList.txt")
  69.         throw $Error[0].Exception.Message + "Please check the password list file in" + (((Get-ChildItem).DirectoryName | select -First 1)+"\passwordList.txt")
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement