-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.ps1
118 lines (99 loc) · 4.15 KB
/
popup.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Check if the script is running with administrative privileges
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
# Check if the script is running in non-administration mode and the choice is to apply only for admin PowerShell
if (-not $isAdmin -and $args[0] -eq "1") {
Write-Host "This script is configured to apply only for admin PowerShell sessions."
Exit
}
# Define variables
$registryKey = "HKCU:\Software\MyApplication"
$registryValueName = "DisableCMD"
# Function to set registry value
function Set-RegistryValue {
param (
[string]$key,
[string]$valueName,
[int]$valueData
)
try {
# Check if the registry key exists, if not, create it
if (-not (Test-Path -Path $key)) {
New-Item -Path $key -Force | Out-Null
}
Set-ItemProperty -Path $key -Name $valueName -Value $valueData -ErrorAction Stop
Write-Host "Registry value set successfully."
} catch {
Write-Host "Error setting registry value: $_"
}
}
# Function to close the PowerShell window
function Close-PowerShellWindow {
$host.SetShouldExit(0)
}
# Function to prompt for credentials
function Prompt-ForCredentials {
param (
[int]$attempt
)
# Get the directory where the script is located using $PSScriptRoot
$scriptDirectory = $PSScriptRoot
# Construct the path to the credentials file relative to the script's location
$credentialsFile = Join-Path -Path $scriptDirectory -ChildPath "credentials.xml"
# Load credentials from XML file
try {
$xml = [xml](Get-Content $credentialsFile)
$storedUsername = $xml.credentials.username
$storedPassword = $xml.credentials.password
# Prompt user for credentials
if ($attempt -lt 3) {
$usernameInput = Read-Host "Username for Windows:"
$passwordInput = Read-Host -Prompt "Passowrd for windows:" -AsSecureString
$credential = New-Object System.Management.Automation.PSCredential ($usernameInput, $passwordInput)
} else {
$credential = Get-Credential -Message "Credintial for windows"
}
# Check if the user canceled the prompt
if ($credential -eq $null) {
Write-Host "Credential prompt canceled. PowerShell remains usable. if not close click CTRL+C to close "
Close-PowerShellWindow
}
# Validate user credentials
elseif (($credential.UserName -eq $storedUsername) -and ($credential.GetNetworkCredential().Password -eq ($storedPassword))) {
Write-Host "Authentication successful. Access granted."
return $true
} else {
Write-Host "Authentication failed. Access denied."
return $false
}
} catch {
Write-Host "Error occurred while loading credentials: $_"
}
}
# Enable command prompt and PowerShell with user authentication
try {
# If the choice is to apply for the whole PowerShell or admin PowerShell
if ($args[0] -eq "0" -or $args[0] -eq "1") {
# Set registry value to enable command prompt
Set-RegistryValue -Key $registryKey -ValueName $registryValueName -ValueData 0
}
$authenticated = $false
$attempts = 0
while (-not $authenticated) {
$authenticated = Prompt-ForCredentials -attempt $attempts
# Increment attempt count if authentication fails
if (-not $authenticated) {
$attempts++
if ($attempts -ge 3) {
Write-Host "Maximum attempts reached. Opening popup window for credential entry."
$authenticated = Prompt-ForCredentials -attempt $attempts
}
if ($attempts -ge 3) {
Write-Host "Maximum attempts reached. Exiting script."
Close-PowerShellWindow
}
}
}
# Additional operations can be performed here if needed after successful authentication
} catch {
Write-Host "Error occurred: $_"
}