forked from 0xMetr0/PowerShell-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyscripts.ps1
More file actions
374 lines (323 loc) · 13.2 KB
/
Copy pathMyscripts.ps1
File metadata and controls
374 lines (323 loc) · 13.2 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
function Get-WifiPasswords {
$profiles = (netsh wlan show profiles) | Select-String "\:(.+)$" | ForEach-Object {
$name = $_.Matches.Groups[1].Value.Trim()
$_
}
$passwords = $profiles | ForEach-Object {
(netsh wlan show profile name="$name" key=clear) | Select-String "Key Content\W+\:(.+)$" | ForEach-Object {
$pass = $_.Matches.Groups[1].Value.Trim()
[PSCustomObject]@{ PROFILE_NAME=$name;PASSWORD=$pass }
}
}
$passwords | Format-Table -AutoSize
}
Function Get-WinlogonDefault {
gp 'HKLM:\SOFTWARE\Microsoft\Windows NT\Currentversion\Winlogon' | select "Default*"
}
function Set-MAC {
Set-NetAdapter -Name "Ethernet0" -MacAddress "00-01-18-57-1B-0D"
}
function Enable-RDP {
# Allow RDP connections
(Get-WmiObject -Class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices).SetAllowTsConnections(1)
# Disable NLA
(Get-WmiObject -class "Win32_TSGeneralSetting" -Namespace root\cimv2\terminalservices -Filter "TerminalName='RDP-tcp'").SetUserAuthenticationRequired(0)
# Allow RDP on the firewall
Get-NetFirewallRule -DisplayGroup "Remote Desktop" | Set-NetFirewallRule -Enabled True
}
function Get-IpHostname {
param(
[string]$net
)
$output = 0..255 | foreach {
$ip = "$net$_"
$result = (Resolve-DNSname -ErrorAction SilentlyContinue $ip | ft NameHost -HideTableHeaders | Out-String).trim().replace("\s+","").replace("`r","").replace("`n"," ")
Write-Output "$ip $result"
$result
}
$output | tee ip_hostname.txt
}
Function Test-OpenPorts {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=0)]
[string]$IP,
[Parameter(Mandatory=$True, Position=1)]
[string]$Ports
)
$Ports.Split(" ") | ForEach-Object {
if ((new-object Net.Sockets.TcpClient).Connect($IP, $_)) {
Write-Output "Port $_ is open on $IP"
}
else {
Write-Output "Port $_ is closed on $IP"
}
}
}
function Invoke-Url {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$Url
)
$response = Invoke-WebRequest -Uri $Url
$result = Invoke-Expression -Command $response.Content
return $result
}
function Install-Git {
[CmdletBinding()]
param()
# Check if Git is already installed
if (Get-Command "git" -ErrorAction SilentlyContinue) {
Write-Host "Git is already installed."
return
}
# Download the Git installer
$url = "https://github.com/git-for-windows/git/releases/download/v2.33.1.windows.1/Git-2.33.1-64-bit.exe"
$outputFile = "$($env:TEMP)\GitInstaller.exe"
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($url, $outputFile)
# Install Git silently
$arguments = "/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS /COMPONENTS='icons,ext\reg\shellhere,assoc,assoc_sh'"
Start-Process -FilePath $outputFile -ArgumentList $arguments -Wait
# Verify that Git is installed
if (Get-Command "git" -ErrorAction SilentlyContinue) {
Write-Host "Git installed successfully."
}
else {
Write-Error "Failed to install Git."
}
# Remove the installer
Remove-Item $outputFile
}
function Uninstall-Git {
[CmdletBinding()]
param()
# Check if Git is installed
if (!(Get-Command "git" -ErrorAction SilentlyContinue)) {
Write-Host "Git is not installed."
return
}
# Uninstall Git silently
$uninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Git_is1"
$uninstallString = (Get-ItemProperty -Path $uninstallKey -Name UninstallString).UninstallString
$arguments = "/VERYSILENT /NORESTART /NOCANCEL /SP- /CLOSEAPPLICATIONS /RESTARTAPPLICATIONS"
Start-Process -FilePath $uninstallString -ArgumentList $arguments -Wait
# Verify that Git is uninstalled
if (!(Get-Command "git" -ErrorAction SilentlyContinue)) {
Write-Host "Git uninstalled successfully."
}
else {
Write-Error "Failed to uninstall Git."
}
}
function Enable-FirewallLogs {
# Enable Windows Firewall logs
Set-NetFirewallProfile -Profile Domain,Public,Private -LogAllowed True
}
#Enable-FirewallLogs
function Restore-FirewallRules {
# Disable the Windows Firewall service temporarily
Set-Service -Name MpsSvc -StartupType Disabled
# Restore the default Windows Firewall settings
netsh advfirewall reset
# Re-enable the Windows Firewall service
Set-Service -Name MpsSvc -StartupType Automatic
Start-Service -Name MpsSvc
}
#Restore-DefaultFirewall
function Import-FirewallRules {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[string]$Path
)
try {
# Import the WFAS policy file
Import-NetFirewallRule -PolicyStore $Path -ErrorAction Stop
# Activate the imported firewall rules
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True -ErrorAction Stop
Write-Host "Firewall rules imported and activated successfully."
} catch {
Write-Error "Failed to import and activate firewall rules: $_"
}
}
#Import-FirewallRules -Path "C:\temp\myfirewallrules.wfw"
function Set-Syslog {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ServerAddress,
[Parameter(Mandatory = $true)]
[int]$Port,
[Parameter(Mandatory = $true)]
[string]$Protocol
)
# Set the registry keys to enable syslog forwarding
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Security" -Name "AutoBackupLogFiles" -Value 0
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\EventLog\Security" -Name "MaxSize" -Value 0x800000
# Install the syslog agent
Install-Package -Name SyslogAgent -ProviderName 'NuGet' -Force
# Set the syslog configuration
$Config = @{
ServerAddress = $ServerAddress
Port = $Port
Protocol = $Protocol
}
Set-SyslogAgentConfig @Config
# Start the syslog service
Start-Service -Name syslogagent
}
#Set-Syslog -ServerAddress "syslog.example.com" -Port 514 -Protocol UDP
function Enable-DNSLogging {
[CmdletBinding()]
param()
# Enable DNS debug logging
$DnsServer = Get-WmiObject -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_Server"
$DnsServer.DebugLogging = $true
$DnsServer.DebugFile = "C:\Windows\System32\dns\dns.log"
$DnsServer.DebugLevel = 0
$DnsServer.Put()
# Enable DNS query logging
$RegistryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\DNS\Parameters"
$Values = @{
"LogFilePath" = "C:\Windows\System32\dns\dnsquery.log"
"EnableLogging" = 1
"LogIncomingRequests" = 1
"LogOutgoingResponses" = 1
"LogLevel" = 2
}
$Values.GetEnumerator() | ForEach-Object {
Set-ItemProperty -Path $RegistryPath -Name $_.Key -Value $_.Value -Force
}
# Restart the DNS service
Restart-Service -Name DNS -Force
}
#Enable-DNSLogging
function Enable-ADEnhancedLogging {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$DomainController
)
# Enable directory service access auditing
$AuditPolicy = Get-AuditPolicy
$AuditPolicy.DirectoryServiceAccess = "Success,Failure"
Set-AuditPolicy -AuditPolicy $AuditPolicy
# Enable directory service changes auditing
$NTDSObject = Get-ADObject "CN=NTDS Settings,CN=$DomainController,CN=Servers,CN=Default-First-Site-Name,CN=Sites,CN=Configuration,DC=example,DC=com" -Properties "options"
$Options = $NTDSObject.options
$Options[5] = $Options[5] -bor 0x20
Set-ADObject -Instance $NTDSObject
# Enable detailed tracking of security events
$GPO = Get-GPO -Name "Default Domain Controllers Policy" -Server $DomainController
$SecuritySettings = $GPO.ExtensionData.Extension.SecuritySettings
$AuditPolicy = $SecuritySettings.AuditPolicy
$AuditPolicy.AuditDetailedTracking = "Enabled"
$SecuritySettings.AuditPolicy = $AuditPolicy
Set-GPRegistryValue -Name $GPO.DisplayName -Key "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\Audit" -ValueName "ProcessCreationIncludeCmdLine_Enabled" -Type DWORD -Value 1
# Refresh the group policy on the domain controller
Invoke-GPUpdate -Computer $DomainController -Target "Computer"
# Restart the domain controller to apply the changes
Restart-Computer -ComputerName $DomainController -Force
}
#Enable-ADEnhancedLogging -DomainController "dc1.example.com"
function Set-ADUserPassword {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string[]]$UserNames,
[Parameter(Mandatory = $true)]
[string]$NewPassword
)
# Prompt for the administrator credentials
$Credential = Get-Credential -Message "Enter the administrator credentials for the domain"
# Loop through each user and set the new password
foreach ($UserName in $UserNames) {
try {
# Get the user object from Active Directory
$User = Get-ADUser -Identity $UserName -Credential $Credential -ErrorAction Stop
# Set the new password for the user
$User | Set-ADAccountPassword -NewPassword (ConvertTo-SecureString -String $NewPassword -AsPlainText -Force) -Credential $Credential -Reset -ErrorAction Stop
Write-Host "Password changed for user $UserName"
}
catch {
Write-Host "Error changing password for user $UserName: $_" -ForegroundColor Red
}
}
}
#Set-ADUserPassword -UserNames "User1", "User2", "User3" -NewPassword "NewPassword123"
function Get-ADUserNames {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$DomainController
)
# Get all user objects from Active Directory
$Users = Get-ADUser -Filter * -Server $DomainController
# Extract the usernames from the user objects
$UserNames = $Users | Select-Object -ExpandProperty SamAccountName
# Output the usernames as an array
return $UserNames
}
#Get-ADUserNames -DomainController "dc1.example.com"
function Get-DomainControllerName {
[CmdletBinding()]
param()
# Get the domain controller name for the current computer
$DomainControllerName = (Get-WmiObject -Class Win32_ComputerSystem -Property DomainControllerName).DomainControllerName
# Output the domain controller name
return $DomainControllerName
}
#Get-DomainControllerName
#-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#Get-DomainControllerName = $DCname | Get-ADUserNames -DomainController $DCname = $UserArray | Set-ADUserPassword -UserNames $UserArray -NewPassword “DeFaUlT_Ad_PaSsWoRd_2_HaRd”
function Import-FirewallRules {
param(
[Parameter(Mandatory=$true)]
[string]$FilePath
)
# Import the firewall rules from the WFW file
Import-NetFirewallRule -Path $FilePath
# Enable the newly imported firewall rules
Get-NetFirewallRule | Where-Object {$_.Enabled -eq $false} | Enable-NetFirewallRule
}
function Restore-DefaultFirewall {
# Disable the Windows Firewall service temporarily
Set-Service -Name MpsSvc -StartupType Disabled
# Restore the default Windows Firewall settings
netsh advfirewall reset
# Re-enable the Windows Firewall service
Set-Service -Name MpsSvc -StartupType Automatic
Start-Service -Name MpsSvc
}
function Enable-FirewallLogs {
# Enable Windows Firewall logs
Set-NetFirewallProfile -Profile Domain,Public,Private -LogAllowed True
}
function Set-ADUserPassword {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string[]]$UserNames,
[Parameter(Mandatory = $true)]
[string]$NewPassword
)
# Prompt for the administrator credentials
$Credential = Get-Credential -Message "Enter the administrator credentials for the domain"
# Loop through each user and set the new password
foreach ($UserName in $UserNames) {
try {
# Get the user object from Active Directory
$User = Get-ADUser -Identity $UserName -Credential $Credential -ErrorAction Stop
# Set the new password for the user
$User | Set-ADAccountPassword -NewPassword (ConvertTo-SecureString -String $NewPassword -AsPlainText -Force) -Credential $Credential -Reset -ErrorAction Stop
Write-Host "Password changed for user $UserName"
}
catch {
Write-Host "Error changing password for user $UserName: $_" -ForegroundColor Red
}
}
}
#Set-ADUserPassword -UserNames "User1", "User2", "User3" -NewPassword "NewPassword123"