-
Notifications
You must be signed in to change notification settings - Fork 0
/
AzureAegis.ps1
247 lines (230 loc) · 12 KB
/
AzureAegis.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
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
# Define the function to display the Help message
function Show-Help {
Write-Host "==========================" -ForegroundColor Cyan
Write-Host " Welcome to AzureAegis!" -ForegroundColor Green
Write-Host "==========================" -ForegroundColor Cyan
Write-Host " Version : 1.0" -ForegroundColor White
Write-Host ""
Write-Host "- Important: Always keep an eye on colored text" -ForegroundColor Yellow
Write-Host "- Filtered most common Vault names print always." -ForegroundColor White
Write-Host "- Type 'x' and Press ENTER for a list of all options." -ForegroundColor White
Write-Host "- Type 'number' to enter a valid number seen in the console." -ForegroundColor White
Write-Host "- Type 'name' to refer to 'part or full' secret name." -ForegroundColor White
Write-Host "- If 'name' given with 'part', there will be multiple secrets printed." -ForegroundColor White
Write-Host "- 'Permission Error' means you don't have access to the selected vault." -ForegroundColor Red
Write-Host "- Type 'H' and Press ENTER to print this message" -ForegroundColor White
Write-Host ""
Write-Host "** Security Alert **" -ForegroundColor Red
Write-Host ""
Write-Host "Please ensure you follow these security best practices:" -ForegroundColor White
Write-Host "- **Do not share your password** with anyone." -ForegroundColor Yellow
Write-Host "- **Keep script updated** to guard against bugs/vulnerabilities." -ForegroundColor Yellow
Write-Host "- **Report any suspicious/bug activity** immediately." -ForegroundColor Yellow
Write-Host ""
Write-Host "Happy exploring!`n" -ForegroundColor Green
}
$currentUsername = $Env:USERNAME
$filePath = "C:\Users\$currentUsername\KeyVaults.txt"
$number = 1
$oldVault = $null
$KeyVaults = $null
$KeyVaultsx = $null
function Show-Progress {
param (
[int]$Duration
)
$spinner = @("|", "/", "-", "\")
$startTime = Get-Date
$i = 0
while ((Get-Date) -lt $startTime.AddSeconds($Duration)) {
Write-Host -NoNewline "`rFetching secrets... $($spinner[$i % $spinner.Length])"
Start-Sleep -Milliseconds 200
$i++
}
Write-Host "`rFetching secrets... Done!`n" -NoNewline
}
function Get-SecretWithProgress {
param (
[string]$VaultName
)
# Start a background job to get the secrets
$job = Start-Job -ScriptBlock {
param ($VaultName)
try {
$result = Get-AzKeyVaultSecret -VaultName $VaultName -ErrorAction Stop | Select-Object -ExpandProperty Name
[PSCustomObject]@{ Success = $true; Result = $result }
} catch {
[PSCustomObject]@{ Success = $false; ErrorMessage = $_.Exception.Message; StatusCode = $_.Exception.Response.StatusCode }
}
} -ArgumentList $VaultName
# Display progress while job is running
$startTime = Get-Date
$spinner = @("|", "/", "-", "\")
$i = 0
while ($job.State -eq 'Running') {
Write-Host -NoNewline "`rFetching secrets... $($spinner[$i % $spinner.Length])"
Start-Sleep -Milliseconds 200
$i++
}
# Wait for the job to complete and get results
$jobResult = Receive-Job -Job $job -Wait
Remove-Job -Job $job
Write-Host "`rFetching secrets... Done!`n" -NoNewline
return $jobResult
}
function keyz {
if (Test-Path $filePath) {
$KeyVaults = Get-Content -Path $filePath
$KeyVaultsx = $KeyVaults | findstr /i "PATTERN" #<<<<<<< CHANGE HERE IN CODE WITH PATTERN
if ($KeyVaults) {
function Print-Option {
param([string[]]$opts)
cls
Write-Host "`n Welcome To AzureAegis, " -NoNewline -ForegroundColor Green
Write-Host "$currentUsername !" -ForegroundColor Cyan
Write-Host "`n ===================== `n" -ForegroundColor DarkMagenta
for ($i = 0; $i -lt $opts.Length; $i++) {
Write-Host "` $($opts[$i])"
}
}
function Read-Option {
$choice = 0
while ($true) {
Write-Host "`n(Press 'x' and Enter to list all VaultName | Press 'H' help)`n" -ForegroundColor Magenta
#Write-Host "`tImportant: Read All Colored Text`n" -ForegroundColor Red
$choice = Read-Host "Enter vault 'number'"
# Check if the input is 'x'
if ($choice -eq 'x') {
Print-Option -opts $KeyVaults
continue
}
$allowedConditions = @("h", "H", "help", "Help")
if ($allowedConditions -contains $choice) {
# Fetch text from the URL
cls
Show-Help
Read-Host -Prompt "Press Enter to continue/Clr+C to Cancel"
Print-Option -opts $KeyVaultsx
continue
}
# Try to convert the input to an integer
if ([int]::TryParse($choice, [ref]$null)) {
$choice = [int]$choice
if ($choice -gt 0 -and $choice -le $KeyVaults.Length) {
break
} else {
Write-Host "Please enter a number between 1 and $($KeyVaults.Length)." -ForegroundColor Red
}
} else {
Write-Host "Invalid input. Please enter a number or 'x'." -ForegroundColor Red
}
}
return $KeyVaults[$choice - 1]
}
function Search-Option {
param([string[]]$opts)
$pattern = $null
$taker = 0
$selectedVault = $selectedOption -split ' '
$filePathx = "C:\Users\$currentUsername\KeySecrets.txt"
$KeySecrets = Get-Content -Path $filePathx
if ($KeySecrets) {
if ($oldVault -ne $selectedVault[1]) {
$result = Get-SecretWithProgress -VaultName $selectedVault[1]
if ($result.Success) {
$result.Result > C:\Users\$currentUsername\KeySecrets.txt
$oldVault = $selectedVault[1]
Search-Option -opts $selectedOption
} else {
Write-Host "`n!! Permission Error !! You don't have access to this vault" -ForegroundColor Red
#Write-Host "Error Message: $($result.ErrorMessage)" -ForegroundColor Red
#Write-Host "Status Code: $($result.StatusCode)" -ForegroundColor Red
Read-Host -Prompt "Press Enter Key to continue/Clr+C to Cancel"
$selectedVault = $null
keyz
}
} else {
cls
Write-Host "Selected Location:" -NoNewline -ForegroundColor Magenta
Write-Host "$selectedVault" -NoNewline -ForegroundColor Green
Write-Host "|| (Press 'x' and Enter Key to list all SecretName)`n" -ForegroundColor Magenta
$pattern = Read-Host "Enter\Paste secret 'name'"
$ScList = Get-content "C:\Users\$currentUsername\KeySecrets.txt" | findstr $pattern
if ($pattern -match 'x') {
Write-Host "$selectedVault Secrets `n========================`n" -ForegroundColor Magenta
Get-content "C:\Users\$currentUsername\KeySecrets.txt"
Write-Host "`nPlease copy SecretName," -NoNewline -ForegroundColor Magenta
Read-Host -Prompt "Press Enter key to continue"
Search-Option -opts $selectedOption
} else {
if ($ScList.Count -eq 1) {
$opts = Get-AzKeyVaultSecret -VaultName $selectedVault[1] -Name $ScList -AsPlainText
Write-Host "`n=====================" -ForegroundColor DarkMagenta
Write-Host "SecretName : $ScList" -ForegroundColor Green
Write-Host "Password : $opts" -ForegroundColor DarkGray
Write-Host "===================== `n" -ForegroundColor DarkMagenta
Read-Host -Prompt "Press Enter Key to continue/Clr+C to Cancel"
keyz
}
if ($ScList.Count -gt 1) {
$taker = 0
Write-Host "`n"
for ($i = 0; $i -lt $ScList.Count; $i++) {
Write-Host "$($i + 1). $($ScList[$i])"
}
Write-Host "`nSorry multiple entries !!`n" -ForegroundColor Red
[int]$taker = Read-Host "Enter secret number"
$opts = Get-AzKeyVaultSecret -VaultName $selectedVault[1] -Name $ScList[$taker -1] -AsPlainText
$SecretN = $ScList[$taker -1]
Write-Host "`n=====================" -ForegroundColor DarkMagenta
Write-Host "SecretName : $SecretN" -ForegroundColor Green
Write-Host "Password : $opts" -ForegroundColor DarkGray
Write-Host "===================== `n" -ForegroundColor DarkMagenta
Read-Host -Prompt "Press Enter to continue/Clr+C to Cancel"
keyz
}
if ($ScList.Count -lt 1) {
Write-Host "`nSorry no such host!`n" -ForegroundColor Red
Search-Option -opts $selectedOption
}
}
}
} else {
$result = Get-SecretWithProgress -VaultName $selectedVault[1]
if ($result.Success) {
$result.Result > C:\Users\$currentUsername\KeySecrets.txt
$oldVault = $selectedVault[1]
Search-Option -opts $selectedOption
} else {
Write-Host "`n!! Permission Error !! You don't have access to this vault" -ForegroundColor Red
Write-Host "Error Message: $($result.ErrorMessage)" -ForegroundColor Red
Write-Host "Status Code: $($result.StatusCode)" -ForegroundColor Red
Read-Host -Prompt "Press Enter to continue/Clr+C to Cancel"
$selectedVault = $null
keyz
}
}
}
Print-Option -opts $KeyVaultsx
$selectedOption = Read-Option -opts $KeyVaultsx
Search-Option -opts $selectedOption
} else {
$uniqueValue = Get-AzKeyVault | findstr /r "^Vault" | Select-String -Pattern 'PATTERN' -AllMatches #<<<<<<< CHANGE HERE IN CODE WITH PATTERN
foreach ($match in $uniqueValue.Matches) {
$contentToAdd = "$number $match";
Add-Content -Path "C:\Users\$currentUsername\KeyVaults.txt" -Value $contentToAdd;
$number++
}
$filePath = "C:\Users\$currentUsername\KeyVaults.txt"
$KeyVaults = Get-content $filePath
$KeyVaultsx = $KeyVaults | findstr /i "PATTERN" #<<<<<<< CHANGE HERE IN CODE WITH PATTERN
keyz
}
} else {
New-Item -Path C:\Users\$currentUsername\KeyVaults.txt -ItemType File
New-Item -Path C:\Users\$currentUsername\KeySecrets.txt -ItemType File
cls
keyz
}
}
keyz