Skip to content

Instantly share code, notes, and snippets.

@mvelazc0
Last active October 21, 2023 03:11
Show Gist options
  • Save mvelazc0/223a711c3ecd4edcabcb4447709d5d4f to your computer and use it in GitHub Desktop.
Save mvelazc0/223a711c3ecd4edcabcb4447709d5d4f to your computer and use it in GitHub Desktop.
Queries AD to identify computers and creates three network shares with 'Everyone' permission on the identified computers. Written for network share enumeration simulations.
Import-Module ActiveDirectory
$shareNames = @("Backup", "WebServer", "Finance", "HR", "Logs", "Database", "Reports", "Media", "Temp", "Archive", "Projects", "Dev", "Testing", "Marketing", "Sales", "Support", "Operations", "Engineering", "QA", "Legal", "Compliance", "Audit", "Design", "Research", "Training", "Analytics", "Security", "Content", "Maintenance", "Migration", "Production", "Inventory", "Services", "Retail", "Consulting", "Governance", "Planning", "Documentation", "Management", "Metrics", "Recruitment", "Networking", "Administration", "Collaboration", "Integration", "Automation", "Monitoring", "Facilities")
$sharePath = "C:\Data"
$dateCutoff = (Get-Date).AddDays(-10)
$computers = Get-ADComputer -Filter { LastLogonTimestamp -gt $dateCutoff } | Select-Object -ExpandProperty DNSHostName
foreach ($computer in $computers) {
Write-Host "Connecting to $computer..."
$session = New-PSSession -ComputerName $computer
if ($session) {
1..3 | ForEach-Object {
$shareName = Get-Random -InputObject $shareNames
#Write-Host "Creating share named '$shareName' on $computer..."
Invoke-Command -Session $session -ScriptBlock {
# Check if the path exists; if not, create it
if (-Not (Test-Path $using:sharePath)) {
New-Item -Path $using:sharePath -ItemType Directory
}
# Check if the share name already exists
if (Get-SmbShare -Name $using:shareName -ErrorAction SilentlyContinue) {
Write-Host "Share name '$using:shareName' already exists on this computer."
return
}
# Create the share
New-SmbShare -Name $using:shareName -Path $using:sharePath -FullAccess "Everyone" | Out-Null
}
Write-Host "Share '$shareName' created successfully on $computer."
}
Remove-PSSession -Session $session
} else {
Write-Host "Failed to connect to $computer."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment