๐PowerShell and .NET
Active Directory Penetration Testing - PowerShell & .NET Tradecraft
Table of Contents
PowerShell Fundamentals
PowerShell Security Bypasses
AV Signature Bypasses
Offensive .NET
Domain Enumeration
Active Directory Trusts
PowerShell Fundamentals
Loading PowerShell Scripts and Modules
Load a PowerShell script using dot sourcing:
. C:\AD\Tools\PowerView.ps1Import a module:
Import-Module C:\AD\Tools\ADModule-master\ActiveDirectory\ActiveDirectory.psd1List all commands in a module:
PowerShell Download-Execute Cradles
Basic web client method:
Internet Explorer COM object method:
PowerShell v3+ method:
XMLHTTP method:
WebRequest method:
PowerShell Security Bypasses
Execution Policy Bypass
Execution Policy is NOT a security measure - it only prevents accidental script execution.
Bypass methods:
Invisi-Shell - Bypassing PowerShell Logging
Tool: https://github.com/OmerYa/Invisi-Shell
Invisi-Shell hooks .NET assemblies (System.Management.Automation.dll and System.Core.dll) to bypass logging using the CLR Profiler API.
Usage:
With admin privileges:
With non-admin privileges:
Type
exitfrom the new PowerShell session to complete cleanup
AV Signature Bypasses
Identifying Flagged Code
Tools for detection:
AMSITrigger: https://github.com/RythmStick/AMSITrigger
DefenderCheck: https://github.com/t3hbb/DefenderCheck
Scan a script:
Bypass Process
Scan using AMSITrigger
Modify the detected code snippet
Rescan using AMSITrigger
Repeat steps 2 & 3 until result shows "AMSI_RESULT_NOT_DETECTED" or "Blank"
Script Modification Techniques
Finding line numbers from byte offsets:
Use helper script to match byte offsets to line numbers
Remove or obfuscate the detected portions
Common culprits: Base64 encoded strings, specific function names
Full obfuscation:
Use Invoke-Obfuscation: https://github.com/danielbohannon/Invoke-Obfuscation
Offensive .NET
Introduction
.NET lacks some security features present in System.Management.Automation.dll
Many red teams have adopted .NET for offensive operations
Numerous open-source offensive .NET tools available
Challenges with .NET Tradecraft
Detection by AV/EDR solutions
Delivery of compiled payloads
Logging (process creation, command line logging)
Source Code Obfuscation - Codecepticon
Tool: https://github.com/Accenture/Codecepticon
Command example:
Binary Obfuscation - ConfuserEx
Tool: https://mkaring.github.io/ConfuserEx/
Steps:
Run ConfuserEx GUI
Add the Release folder of the compiled binary
Add a new Rule in the settings page
Double click the rule and set preset to "Maximum"
In the protect page, click "Protect" to produce obfuscated binary
Verify with DefenderCheck
Domain Enumeration
Enumeration Tools
ActiveDirectory PowerShell Module (Microsoft signed, works in CLM):
BloodHound - Graph-based AD analysis:
https://github.com/BloodHoundAD/BloodHound
PowerView - PowerShell enumeration:
SharpView - C# version (no pipeline filtering):
https://github.com/tevora-threat/SharpView/
Domain Information
Get current domain:
Get another domain:
Get domain SID:
Domain Policy Enumeration
Get domain policy:
Domain Controllers
Get domain controllers:
User Enumeration
List all users:
Get specific user properties:
Search by attribute:
Computer Enumeration
List computers:
Group Enumeration
List all groups:
Find groups with "admin":
Get group members:
Get user's group membership:
Local Group Enumeration
Note: Requires administrator privileges on non-DC machines
List local groups:
Get local group members:
Logged On User Enumeration
Get actively logged users (requires local admin):
Get locally logged users (requires remote registry):
Get last logged user (requires admin rights and remote registry):
Share Enumeration
PowerHuntShares - https://github.com/NetSPI/PowerHuntShares
PowerView share commands:
SOAPHound - Stealthy Enumeration
Tool: Uses Active Directory Web Services (ADWS - Port 9389) instead of LDAP queries
Build cache:
Collect BloodHound data:
BloodHound
Versions:
BloodHound Legacy: https://github.com/BloodHoundAD/BloodHound
BloodHound CE: https://github.com/SpecterOps/BloodHound
Stealthy collection (excludes noisy methods and DCs):
Full collection:
ACL Enumeration
Find interesting ACEs:
Get ACLs for specific objects:
ActiveDirectory Module:
GPO Enumeration
List GPOs:
GPO local group mapping:
OU Enumeration
List OUs:
Get GPO applied on OU:
Active Directory Trusts
Trust Fundamentals
Trust Definition: A relationship between domains/forests allowing users to access resources across boundaries.
Trust Types:
Automatic Trusts: Created automatically (parent-child, tree-root)
Established Trusts: Manually created (forest, external)
Trust Direction
One-Way Trust:
Unidirectional access
Direction: Trusting (resource) domain โ Trusted (account) domain
Users in trusted domain can access resources in trusting domain
Two-Way Trust:
Bidirectional access
Common in parent-child, tree-root, and forest trusts
Trust Transitivity
Transitive Trust:
Can be extended to other domains in the forest
All intra-forest trusts are transitive and two-way
Example: If A trusts B and B trusts C, then A trusts C
Non-Transitive Trust:
Cannot be extended to other domains
Example: External trusts between different forests
Default/Automatic Trusts
Parent-Child Trust:
Created automatically when new domain added under parent
Always two-way transitive
Example:
dollarcorp.moneycorp.localis child ofmoneycorp.local
Tree-Root Trust:
Created automatically when new domain tree added to forest root
Always two-way transitive
External Trusts
Between two domains in different forests
Can be one-way or two-way
Always non-transitive
Forest Trusts
Trust relationship between two forests
Allows all domains in one forest to trust all domains in another
Enables cross-forest resource access
Trust Enumeration Commands
Forest enumeration:
Domain trust enumeration:
Key Trust Concepts
Transitivity is crucial for trust exploitation
External trusts are non-transitive and often targeted for lateral movement
Parent-child and tree-root trusts are automatic, transitive, and two-way
Trust enumeration is critical for mapping attack paths in Active Directory
Learning Objectives Summary
Learning Objective 1 - Basic Domain Enumeration
Enumerate all users in dollarcorp domain
Enumerate all computers in dollarcorp domain
Identify Domain Administrators
Identify Enterprise Administrators
Use BloodHound to find shortest path to Domain Admins
Find file shares with write permissions
Learning Objective 2 - ACL Analysis
Enumerate ACL for Domain Admins group
Identify ACLs where user has interesting permissions
Analyze permissions in BloodHound UI
Learning Objective 3 - OU and GPO Analysis
List all OUs
List computers in specific OU (e.g., DevOps OU)
List all GPOs
Enumerate GPO applied on specific OU
Enumerate ACLs for specific GPOs
Note: These notes are for educational purposes in authorized penetration testing environments only.
Last updated