-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_vulnAD.ps1
102 lines (81 loc) · 3.24 KB
/
gen_vulnAD.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
param(
[Parameter(Mandatory=$true)] $Jsonfile,
[switch]$Revert
)
function CreateADGroup() {
param([Parameter(Mandatory=$true)] $groupObject)
$groups = $groupObject.split(" ")
foreach ($group in $groups) {
New-ADGroup -name $group -GroupScope Global
}
}
function RemoveADGroup(){
param([Parameter(Mandatory=$true)] $groupObject)
$groups = $groupObject.split(" ")
foreach ($group in $groups) {
Remove-ADGroup -Identity $group -Confirm:$false
}
}
function CreateADUser(){
param([Parameter(Mandatory=$true)] $userObject)
# Pull required info from config file
$name = $userObject.name
$password = $userObject.password
# Carve out a first initial, last name structure for username
$firstname, $lastname = $name.Split(" ")
$username = ($firstname[0] + $lastname).ToLower()
$samAccountName = $username
$principalname = $username
# Create the AD user
Write-Host "Creating $username User..."
New-ADUser -Name "$name" -GivenName $firstname -Surname $lastname -SamAccountName $SamAccountName -UserPrincipalName $principalname@$Global:Domain -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -PassThru | Enable-ADAccount
# Add the user to an appropriate group
foreach ($group_name in $userObject.groups) {
try {
Get-ADGroup -Identity "$group_name"
Add-ADGroupMember -Identity $group_name -Members $username
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]
{
Write-Warning “WARNING! AD Group $group_name not found while creating user $name”
}
}
}
function RemoveADUser(){
param([Parameter(Mandatory=$true)] $userObject)
$name = $userObject.name
$firstname, $lastname = $name.Split(" ")
$username = ($firstname[0] + $lastname).ToLower()
$samAccountName = $username
Remove-ADUser -Identity $samAccountName -Confirm:$false
}
function NerfPasswordPolicy(){
secedit /export /cfg c:\secpol.cfg
(Get-Content C:\secpol.cfg).replace("PasswordComplexity = 1", "PasswordComplexity = 0").replace("MinimumPasswordLength = 7", "MinimumPasswordLength = 1") | Out-File C:\secpol.cfg
secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY
Remove-Item -force c:\secpol.cfg -confirm:$false
}
function BuffPasswordPolicy(){
secedit /export /cfg c:\secpol.cfg
(Get-Content C:\secpol.cfg).replace("PasswordComplexity = 0", "PasswordComplexity = 1").replace("MinimumPasswordLength = 1", "MinimumPasswordLength = 7") | Out-File C:\secpol.cfg
secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY
Remove-Item -force c:\secpol.cfg -confirm:$false
}
$jsonData = (Get-Content $Jsonfile | ConvertFrom-JSON)
$Global:Domain = $jsonData.domain
if ( -not $Revert) {
NerfPasswordPolicy
# Create the groups for the domain
CreateADGroup $jsonData.groups
# Create the users for the domain
foreach ( $user in $jsonData.users){
CreateADUser $user
}
} else {
# Revert given -> roll back
BuffPasswordPolicy
foreach ( $user in $jsonData.users){
RemoveADUser $user
}
RemoveADGroup $jsonData.groups
}