| Howto - Use ADSI to Managing Service Account Passwords |
|
|
|
|
Most enterprises expend significant effort to ensure that when employees leave the firm, all computer access has been disabled by the time they make their final exit. When IT staff members leave the firm, some enterprises might even change the Administrator account passwords for the domains to prevent unauthorized access through a shared account. Unfortunately, those same IT staff members are likely to have set up Microsoft Systems Management Server (SMS), Microsoft Exchange Server, and other applications that require an administrative account in the domain. But few enterprises modify the service account passwords when IT staff members leave. Adding to the security risk that service accounts pose, consider that setting the Password Never Expires flag for service accounts is typically considered good practice. Although setting this flag typically reduces the probability of a service logon failure, it provides password hash-comparison utilities such as L0phtCrack an infinite amount of time to obtain the password for a privileged account. The solution to mitigate both these risks seems quite simple in theory: Change the service account passwords regularly to an unknown value. In practice, however, most IT shops are unwilling to do so because of the potential risk of compromising the functionality of their core infrastructure services. However, you can automate the process of managing service account passwords. By writing a service-account-password management script, you not only reduce the security risk that privileged accounts pose but also reduce the risk of incurring downtime as a result of changing service account passwords. To automate the process for managing service account passwords, you can use Active Directory Service Interfaces (ADSI) to write a script that
Enumerating Machines Binding to a Service When you customize the ServiceProgrammaticName entry, make sure you use the service's programmatic name and not its display name. Typically, systems administrators are most familiar with the display name, which appears in the Control Panel Services applet (in NT) or the Microsoft Management Console (MMC) Computer Management snap-in (in Windows 2000). Unfortunately, you can't use a display name to bind to a service. Instead, you must use the service's programmatic name. Quite often, this name matches the relative name of the executable (without the extension) in the host computer's file system. However, this isn't always the case, so you might want to use an alternative way to find the programmatic name. A fail-safe approach is to bind to the computer, enumerate all services, and display the IADs::Name and IADsService::DisplayName properties for each service. The IADs::Name property contains the programmatic name.
Changing the Service Logon Password To use this code, you need to customize it in several places. First, you need to customize the TargetDomain and TargetComputer entries in the GetObject function's ADsPath. Next, you need to replace domain\administrator with the name of the domain containing the user account and the relative name of the user account. Finally, you need to replace New_Password with your new service logon password. Changing the Account Password in the Namespace You can use the WinNT: ADSI service provider to bind to the SAM on NT 4.0 and NT 3.51 machines and to bind to the local SAM on Win2K workstations and standalone servers. (Because service definitions aren't stored in AD, you use the WinNT: ADSI service provider to bind to services on Win2K machines as well.) For user accounts in AD, you still use the IADsUser::SetPassword or IADsUser::ChangePassword methods to change the password for the account. However, the ADsPath you use to bind the User object differs based on where you defined the account. For accounts in AD, the ADsPath must specify the exact location of the object in the namespace. For example, Listing 5 shows how to bind to an object called svc_exchange_server1 that's located in the ServiceAccounts organizational unit (OU). Cycling the Service to Activate the Changes When you use this script, note that you need to customize an additional entry in the ADsPath. As Listing 6 shows, you need to replace ServiceProgrammaticName with the service's programmatic name. Enhancing Security The code uses the WinNT: provider, so it works with accounts defined in the SAM. You can easily adapt the code to work with AD by changing the binding string. (The code describes how to change the string.) Excel Service Account Management Tool I created a fully functional application that performs the password-administration tasks I've discussed here and generates a report that details the results. You can download this application from the Code Library on the Windows Scripting Solutions Web site. To view the application's code, open the application in Excel 2000, then press Alt+F11 to access the Visual Basic Editor. To use the Excel application, follow the directions in the workbook's Instructions sheet. Prevent Downtime; Save Time and Money
Listing 1 ' Listing 1. Code to Enumerate Services on All Machines in a Domain to Identify the Domain Accounts Dim oDomain Dim oComputer Set oDomain = GetObject("WinNT://TargetDomain") oDomain.Filter = Array("Computer") For Each oComputer in oDomain oComputer.Filter = Array("service") For Each oService in oComputer If oService.ServiceAccountName<>"LocalSystem" Then Wscript.Echo oService.ADsPath & " " & oService.ServiceAccountName End If Next Next
Listing 2 ' Listing 2. Code to Bind to a Specific Service Dim oService Set oService = GetObject("WinNT://TargetDomain/TargetComputer/" & "ServiceProgrammaticName,service")
Listing 3 ' Listing 3. Code to Change the Password That the Service Uses for Authentication Dim oComputer Dim oService Set oComputer = GetObject("WinNT://TargetDomain/TargetComputer,computer") oComputer.Filter=Array("service") For Each oService in oComputer If lcase(oService.ServiceAccountName) = lcase("domain\administrator") Then oService.SetPassword "New_Password" If Err.Number=0 Then Wscript.Echo "Password changed for " & oService.ADsPath Else Wscript.Echo "Password could not be changed on " & oService.ADsPath End If End If Next
Listing 4 ' Listing 4. Code to Change the Password in the SAM Dim oUser Set oUser = GetObject("WinNT://User_Domain/svc_exchange_server1,user") oUser.SetPassword "New_Password"
Listing 5 ' Listing 5. Code to Change the Password in AD Dim oRootDSE Set oRootDSE = GetObject("LDAP://RootDSE") Dim oUser Set oUser = GetObject("LDAP://cn=svc_exchange_server1,OU=ServiceAccounts," & oRootDSE.Get("defaultNamingContext") oUser.SetPassword "New_Password"
Listing 6 ' Listing 6. Excerpt from the CycleService Script Dim oService Set oService = GetObject("WinNT://TargetDomain/TargetComputer/" & "ServiceProgrammaticName,service") |
| < Prev | Next > |
|---|








