How do I find unused service accounts in Active Directory

0 votes

Our AD environment contains several service accounts, but I suspect that some of them are no longer in use.

  • How can I check if a service account is inactive?
  • Is there a way to track last login times for service accounts?
  • What are the best practices for disabling or removing unused service accounts?

Looking for PowerShell scripts or AD queries to identify unused service accounts.

Feb 7 in Cyber Security & Ethical Hacking by Nidhi
• 11,580 points
51 views

1 answer to this question.

0 votes

Identifying and managing unused service accounts in Active Directory (AD) is crucial for maintaining security and ensuring efficient resource utilization. Here's how you can approach this task:

1. Checking if a Service Account is Inactive

To determine if a service account is inactive, you can examine its last logon timestamp. In AD, the lastLogonTimestamp attribute indicates the last time an account authenticated against the domain. However, it's important to note that this attribute is updated periodically and may not reflect real-time activity.

Using PowerShell:

You can use the Get-ADUser cmdlet to retrieve service accounts and check their last logon dates. Assuming your service accounts follow a specific naming convention (e.g., starting with "svc_"), you can run:

$threshold = (Get-Date).AddDays(-90)
Get-ADUser -Filter 'Name -like "svc_*"' -Properties lastLogonTimestamp | Where-Object {
    $_.lastLogonTimestamp -lt $threshold
} | Select-Object Name, @{Name="LastLogonDate";Expression={[datetime]::FromFileTime($_.lastLogonTimestamp)}}

This script lists service accounts that haven't logged in during the past 90 days. Adjust the -90 to your desired threshold.

2. Tracking Last Login Times for Service Accounts

The lastLogonTimestamp attribute is useful for identifying potentially inactive accounts. However, be aware that this attribute is replicated across domain controllers and may not update with every logon, leading to potential discrepancies. For more precise tracking, you might consider auditing logon events or using specialized monitoring tools.

3. Best Practices for Disabling or Removing Unused Service Accounts

  • Review and Documentation: Before taking action, document all service accounts, their purposes, and dependencies.

  • Disable Before Deletion: Initially, disable the account rather than deleting it. Monitor for any issues that arise, which might indicate the account was still in use.

  • Monitor for Impact: After disabling, observe system and application behavior to ensure no critical services are affected.

  • Deletion: If no issues are detected after a predetermined period (e.g., 30 days), consider deleting the account.

  • Regular Audits: Implement a routine audit process to identify and manage inactive accounts proactively.

4. PowerShell Scripts and AD Queries to Identify Unused Service Accounts

In addition to the earlier script, you can use the Search-ADAccount cmdlet to find inactive accounts:

Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 90.00:00:00 | Where-Object {
    $_.Name -like "svc_*"
} | Select-Object Name, LastLogonDate

This command searches for user accounts (which can include service accounts) that have been inactive for the past 90 days. Ensure your service accounts are identifiable, either through naming conventions or specific organizational units (OUs), to filter them appropriately.

Additional Considerations

  • Service Account Identification: If your service accounts are managed service accounts (MSAs), you can list them using:
Get-ADServiceAccount -Filter *

For standard user accounts used as service accounts, ensure they are distinguishable by naming conventions or group memberships.

  • Audit Policies: Enable auditing on domain controllers to track logon events, providing more granular data on account activity.

  • Third-Party Tools: Consider using specialized tools or scripts that provide more detailed analysis and reporting capabilities for service account management.

By following these steps and best practices, you can effectively identify, manage, and secure service accounts within your Active Directory environment.

answered Feb 14 by CaLLmeDaDDY
• 22,940 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
0 answers

How do I find and exploit an insecure API endpoint in a mobile app?

How do I find and exploit an ...READ MORE

Oct 14, 2024 in Cyber Security & Ethical Hacking by Anupam
• 12,620 points
126 views
+1 vote
1 answer

How do I find and exploit an insecure API endpoint in a mobile app?

In order to locate and test insecure ...READ MORE

answered Oct 24, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
298 views
0 votes
1 answer

How do i check a ip address range whether it falls in Class A,Class B,Class C

class NetworkId{ static String findClass(String str){ int index = ...READ MORE

answered Feb 16, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
1,024 views
+1 vote
1 answer

How do you decrypt a ROT13 encryption on the terminal itself?

Yes, it's possible to decrypt a ROT13 ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
453 views
+1 vote
1 answer

How does the LIMIT clause in SQL queries lead to injection attacks?

The LIMIT clause in SQL can indeed ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
423 views
+1 vote
1 answer

Is it safe to use string concatenation for dynamic SQL queries in Python with psycopg2?

The use of string concatenation while building ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
277 views
+1 vote
1 answer
0 votes
1 answer

How do I get a list of service accounts in Active Directory?

To list all service accounts in your ...READ MORE

answered Feb 14 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
90 views
0 votes
1 answer

How do I remove a service connection point in Active Directory?

Removing a Service Connection Point (SCP) from ...READ MORE

answered Feb 13 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 22,940 points
40 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP