Advertisement
afalahi

Enable Secure Autologon

Jan 30th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. invoke-command -ComputerName smoke -ScriptBlock {
  2. begin {
  3.    
  4.     [string] $WinlogonPath = "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
  5.     [string] $WinlogonBannerPolicyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
  6.     [string] $Username ="ENTER USERNAME"
  7.     $Password = ConvertTo-SecureString "ENTER PASSWORD HERE" -AsPlainText -Force
  8.     [string] $Domain = "YOUR DOMAIN"
  9.     [Int] $AutoLogonCount = 1
  10.     [string] $Enable = 1
  11.     [string] $Disable = 0
  12.    
  13.     #region C# Code to P-invoke LSA LsaStorePrivateData function.
  14.     Add-Type @"
  15.         using System;
  16.         using System.Collections.Generic;
  17.         using System.Text;
  18.         using System.Runtime.InteropServices;
  19.  
  20.         namespace ComputerSystem
  21.         {
  22.             public class LSAutil
  23.             {
  24.                 [StructLayout(LayoutKind.Sequential)]
  25.                 private struct LSA_UNICODE_STRING
  26.                 {
  27.                     public UInt16 Length;
  28.                     public UInt16 MaximumLength;
  29.                     public IntPtr Buffer;
  30.                 }
  31.  
  32.                 [StructLayout(LayoutKind.Sequential)]
  33.                 private struct LSA_OBJECT_ATTRIBUTES
  34.                 {
  35.                     public int Length;
  36.                     public IntPtr RootDirectory;
  37.                     public LSA_UNICODE_STRING ObjectName;
  38.                     public uint Attributes;
  39.                     public IntPtr SecurityDescriptor;
  40.                     public IntPtr SecurityQualityOfService;
  41.                 }
  42.  
  43.                 private enum LSA_AccessPolicy : long
  44.                 {
  45.                     POLICY_VIEW_LOCAL_INFORMATION = 0x00000001L,
  46.                     POLICY_VIEW_AUDIT_INFORMATION = 0x00000002L,
  47.                     POLICY_GET_PRIVATE_INFORMATION = 0x00000004L,
  48.                     POLICY_TRUST_ADMIN = 0x00000008L,
  49.                     POLICY_CREATE_ACCOUNT = 0x00000010L,
  50.                     POLICY_CREATE_SECRET = 0x00000020L,
  51.                     POLICY_CREATE_PRIVILEGE = 0x00000040L,
  52.                     POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080L,
  53.                     POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100L,
  54.                     POLICY_AUDIT_LOG_ADMIN = 0x00000200L,
  55.                     POLICY_SERVER_ADMIN = 0x00000400L,
  56.                     POLICY_LOOKUP_NAMES = 0x00000800L,
  57.                     POLICY_NOTIFICATION = 0x00001000L
  58.                 }
  59.  
  60.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  61.                 private static extern uint LsaRetrievePrivateData(
  62.                             IntPtr PolicyHandle,
  63.                             ref LSA_UNICODE_STRING KeyName,
  64.                             out IntPtr PrivateData
  65.                 );
  66.  
  67.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  68.                 private static extern uint LsaStorePrivateData(
  69.                         IntPtr policyHandle,
  70.                         ref LSA_UNICODE_STRING KeyName,
  71.                         ref LSA_UNICODE_STRING PrivateData
  72.                 );
  73.  
  74.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  75.                 private static extern uint LsaOpenPolicy(
  76.                     ref LSA_UNICODE_STRING SystemName,
  77.                     ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
  78.                     uint DesiredAccess,
  79.                     out IntPtr PolicyHandle
  80.                 );
  81.  
  82.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  83.                 private static extern uint LsaNtStatusToWinError(
  84.                     uint status
  85.                 );
  86.  
  87.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  88.                 private static extern uint LsaClose(
  89.                     IntPtr policyHandle
  90.                 );
  91.  
  92.                 [DllImport("advapi32.dll", SetLastError = true, PreserveSig = true)]
  93.                 private static extern uint LsaFreeMemory(
  94.                     IntPtr buffer
  95.                 );
  96.  
  97.                 private LSA_OBJECT_ATTRIBUTES objectAttributes;
  98.                 private LSA_UNICODE_STRING localsystem;
  99.                 private LSA_UNICODE_STRING secretName;
  100.  
  101.                 public LSAutil(string key)
  102.                 {
  103.                     if (key.Length == 0)
  104.                     {
  105.                         throw new Exception("Key lenght zero");
  106.                     }
  107.  
  108.                     objectAttributes = new LSA_OBJECT_ATTRIBUTES();
  109.                     objectAttributes.Length = 0;
  110.                     objectAttributes.RootDirectory = IntPtr.Zero;
  111.                     objectAttributes.Attributes = 0;
  112.                     objectAttributes.SecurityDescriptor = IntPtr.Zero;
  113.                     objectAttributes.SecurityQualityOfService = IntPtr.Zero;
  114.  
  115.                     localsystem = new LSA_UNICODE_STRING();
  116.                     localsystem.Buffer = IntPtr.Zero;
  117.                     localsystem.Length = 0;
  118.                     localsystem.MaximumLength = 0;
  119.  
  120.                     secretName = new LSA_UNICODE_STRING();
  121.                     secretName.Buffer = Marshal.StringToHGlobalUni(key);
  122.                     secretName.Length = (UInt16)(key.Length * UnicodeEncoding.CharSize);
  123.                     secretName.MaximumLength = (UInt16)((key.Length + 1) * UnicodeEncoding.CharSize);
  124.                 }
  125.  
  126.                 private IntPtr GetLsaPolicy(LSA_AccessPolicy access)
  127.                 {
  128.                     IntPtr LsaPolicyHandle;
  129.  
  130.                     uint ntsResult = LsaOpenPolicy(ref this.localsystem, ref this.objectAttributes, (uint)access, out LsaPolicyHandle);
  131.  
  132.                     uint winErrorCode = LsaNtStatusToWinError(ntsResult);
  133.                     if (winErrorCode != 0)
  134.                     {
  135.                         throw new Exception("LsaOpenPolicy failed: " + winErrorCode);
  136.                     }
  137.  
  138.                     return LsaPolicyHandle;
  139.                 }
  140.  
  141.                 private static void ReleaseLsaPolicy(IntPtr LsaPolicyHandle)
  142.                 {
  143.                     uint ntsResult = LsaClose(LsaPolicyHandle);
  144.                     uint winErrorCode = LsaNtStatusToWinError(ntsResult);
  145.                     if (winErrorCode != 0)
  146.                     {
  147.                         throw new Exception("LsaClose failed: " + winErrorCode);
  148.                     }
  149.                 }
  150.  
  151.                 public void SetSecret(string value)
  152.                 {
  153.                     LSA_UNICODE_STRING lusSecretData = new LSA_UNICODE_STRING();
  154.  
  155.                     if (value.Length > 0)
  156.                     {
  157.                         //Create data and key
  158.                         lusSecretData.Buffer = Marshal.StringToHGlobalUni(value);
  159.                         lusSecretData.Length = (UInt16)(value.Length * UnicodeEncoding.CharSize);
  160.                         lusSecretData.MaximumLength = (UInt16)((value.Length + 1) * UnicodeEncoding.CharSize);
  161.                     }
  162.                     else
  163.                     {
  164.                         //Delete data and key
  165.                         lusSecretData.Buffer = IntPtr.Zero;
  166.                         lusSecretData.Length = 0;
  167.                         lusSecretData.MaximumLength = 0;
  168.                     }
  169.  
  170.                     IntPtr LsaPolicyHandle = GetLsaPolicy(LSA_AccessPolicy.POLICY_CREATE_SECRET);
  171.                     uint result = LsaStorePrivateData(LsaPolicyHandle, ref secretName, ref lusSecretData);
  172.                     ReleaseLsaPolicy(LsaPolicyHandle);
  173.  
  174.                     uint winErrorCode = LsaNtStatusToWinError(result);
  175.                     if (winErrorCode != 0)
  176.                     {
  177.                         throw new Exception("StorePrivateData failed: " + winErrorCode);
  178.                     }
  179.                 }
  180.             }
  181.         }
  182. "@
  183.     #endregion
  184. }
  185.  
  186. process {
  187.  
  188.     try {
  189.         $ErrorActionPreference = "Stop"
  190.        
  191.         $decryptedPass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
  192.             [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
  193.         )
  194.  
  195.                
  196.             # Store the password securely.
  197.         $lsaUtil = New-Object ComputerSystem.LSAutil -ArgumentList "DefaultPassword"
  198.         $lsaUtil.SetSecret($decryptedPass)
  199.  
  200.             # Store the autologon registry settings.
  201.         Set-ItemProperty -Path $WinlogonPath -Name AutoAdminLogon -Value $Enable -Force
  202.  
  203.         Set-ItemProperty -Path $WinlogonPath -Name DefaultUserName -Value $Username -Force
  204.         Set-ItemProperty -Path $WinlogonPath -Name DefaultDomainName -Value $Domain -Force
  205.  
  206.         if ($AutoLogonCount) {
  207.             Set-ItemProperty -Path $WinlogonPath -Name AutoLogonCount -Value $AutoLogonCount -Force
  208.         } else {
  209.             Remove-ItemProperty -Path $WinlogonPath -Name AutoLogonCount -ErrorAction SilentlyContinue
  210.         }
  211.  
  212.     } catch {
  213.         throw 'Failed to set auto logon. The error was: "{0}".' -f $_
  214.     }
  215.  
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement