-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathslack-cna-parser_windows.ps1
180 lines (157 loc) · 5.85 KB
/
slack-cna-parser_windows.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
$global:version = "1.0"
$ascii = @"
██████ ███ ██ █████ ██████ █████ ██████ ███████ ███████ ██████
██ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ███████ █████ ██████ ███████ ██████ ███████ █████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ████ ██ ██ ██ ██ ██ ██ ██ ███████ ███████ ██ ██
~ Created with <3 by @nickvourd
~ Special thanks to @sec_groundzero
~ Version: $global:version
~ Type: Slack CNA Parser Tool (Windows Edition)
"@
Write-Host $ascii
# Initialize variables with default values
$hostname = ""
$channel = ""
$webhook = ""
# Function to display usage information
function Show-Help {
Write-Host ""
Write-Host "Usage: .\slack-cna-parser_windows.ps1 -hostname <hostname> -channel <channel> -webhook <url>`n"
Write-Host "Options:`n"
Write-Host " -h, --help Display this help message"
Write-Host " -hostname <hostname> Set hostname"
Write-Host " -channel <#channel> Set channel(Staring with #)"
Write-Host " -webhook <url> Set webhook url`n"
}
# Check if no arguments are provided
if ($args.Count -eq 0) {
Show-Help
exit 0
}
# Function to validate if the channel starts with '#'
function ChannelValidation {
param([string]$channel)
if ($channel -match '^#') {
return $true # Channel starts with '#'
} else {
return $false # Channel does not start with '#'
}
}
# Find-CnaFile function
function Find-CnaFile {
param (
[string]$rootPath = "C:\" # Specify the root directory where you want to start the search
)
# Use the Get-ChildItem cmdlet to recursively search for the cna file
$cnaFile = Get-ChildItem -Path $rootPath -Recurse -Filter "slack-alerts_windows.cna" -File -ErrorAction SilentlyContinue
if ($null -ne $cnaFile) {
return $cnaFile.FullName
} else {
Write-Host "CNA file not found in $rootPath or its subdirectories."
return $null
}
}
# ReplaceValues function
function ReplaceValues {
param (
[string]$channel,
[string]$webhook,
[string]$hostname
)
# Find the CNA file
$cnaFilePath = Find-CnaFile
if ($null -eq $cnaFilePath) {
Write-Host "Cannot proceed. CNA file not found."
return
}
# Replace placeholders with provided values in the cna file
(Get-Content $cnaFilePath) | ForEach-Object {$_ -replace '#XXXXXX', $channel -replace 'https://hooks.slack.com/services/XXXXXXXXXXX/XXXXXXXXXXXXXx', $webhook -replace 'XXXXXX', $hostname} | Set-Content $cnaFilePath
Write-Host "`n[+] The $cnaFilePath has been successfully edited!`n"
}
# Function to validate webhook URL
function ValidateUrl {
param([string]$url)
# Regex pattern to check if the URL matches the basic format
$urlRegex = '^https://hooks.slack.com/services/.+'
if ($url -match $urlRegex) {
# Check if the URL contains "https://hooks.slack.com/services/"
if ($url -like '*https://hooks.slack.com/services/*') {
return $true # Valid URL
} else {
return $false # Invalid URL (doesn't contain required string)
}
} else {
return $false # Invalid URL
}
}
# Parse command-line options
for ($i = 0; $i -lt $args.Count; $i++) {
switch ($args[$i]) {
'-hostname' {
if ($i + 1 -lt $args.Count) {
$hostname = $args[$i + 1]
$i++
} else {
Write-Host "`n[!] Error: Argument for -hostname is missing" -ForegroundColor Red
Show-Help
exit 1
}
break
}
'-channel' {
if ($i + 1 -lt $args.Count) {
$channel = $args[$i + 1]
if (-not (ChannelValidation $channel)) {
Write-Host "`n[!] Error: Channel should start with '#' symbol" -ForegroundColor Red
Show-Help
exit 1
}
$i++
} else {
Write-Host "`n[!] Error: Argument for -channel is missing" -ForegroundColor Red
Show-Help
exit 1
}
break
}
'-webhook' {
if ($i + 1 -lt $args.Count) {
$webhook = $args[$i + 1]
if (-not (ValidateUrl $webhook)) {
Write-Host "`n[!] Error: Invalid webhook URL or missing 'https://hooks.slack.com/services/'" -ForegroundColor Red
Show-Help
exit 1
}
$i++
} else {
Write-Host "`n[!] Error: Argument for -webhook is missing" -ForegroundColor Red
Show-Help
exit 1
}
break
}
'-h' {
Show-Help
exit 0
}
'--help' {
Show-Help
exit 0
}
default {
Write-Host "`n[!] Error: Invalid option $($args[$i])" -ForegroundColor Red
Show-Help
exit 1
}
}
}
# Check if required arguments are missing
if (-not $hostname -or -not $channel -or -not $webhook) {
Write-Host "`n[!] Error: Required argument(s) missing" -ForegroundColor Red
Show-Help
exit 1
}
# Replace placeholders in the cna file
ReplaceValues $channel $webhook $hostname