Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
351 lines (302 loc) · 14.4 KB
/
Copy pathinstall.ps1
File metadata and controls
351 lines (302 loc) · 14.4 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
<#
.SYNOPSIS
Install Synapse on Windows.
.DESCRIPTION
Downloads the Synapse binary from GitHub releases, creates the required
directory structure, writes a default configuration file, and optionally
installs Synapse as a Windows Service.
Must be run as Administrator (the script will attempt to self-elevate if not).
.PARAMETER ApiKey
Your Gen0Sec API key. Obtain from https://dash.gen0sec.com/settings/api-keys
If omitted, edit C:\ProgramData\Gen0Sec\Synapse\config.yaml after install.
.PARAMETER Version
Synapse version to install (default: latest release).
.PARAMETER InstallDir
Binary installation directory.
Default: C:\Program Files\Gen0Sec\Synapse
.PARAMETER ConfigDir
Configuration and data directory.
Default: C:\ProgramData\Gen0Sec\Synapse
.PARAMETER NoService
Skip Windows Service installation. The binary is installed and configured
but not registered with the Service Control Manager.
.PARAMETER WithEBPF
Download and install eBPF for Windows (requires test signing mode).
See: https://github.com/microsoft/ebpf-for-windows
.PARAMETER Uninstall
Stop and remove the Synapse service, then delete all installed files.
Configuration and logs in ConfigDir are preserved unless -PurgeConfig is set.
.PARAMETER PurgeConfig
When used with -Uninstall, also removes ConfigDir (config, logs, data).
.EXAMPLE
# One-liner (PowerShell 5+)
iwr -useb https://raw.githubusercontent.com/gen0sec/synapse/main/install.ps1 | iex
.EXAMPLE
# With API key
powershell -ExecutionPolicy Bypass -File install.ps1 -ApiKey "your-key"
.EXAMPLE
# Uninstall
powershell -ExecutionPolicy Bypass -File install.ps1 -Uninstall
#>
[CmdletBinding(DefaultParameterSetName = 'Install')]
param(
[Parameter(ParameterSetName = 'Install')]
[string]$ApiKey = "",
[Parameter(ParameterSetName = 'Install')]
[string]$Version = "",
[Parameter(ParameterSetName = 'Install')]
[string]$InstallDir = "C:\Program Files\Gen0Sec\Synapse",
[string]$ConfigDir = "C:\ProgramData\Gen0Sec\Synapse",
[Parameter(ParameterSetName = 'Install')]
[switch]$NoService,
[Parameter(ParameterSetName = 'Install')]
[switch]$WithEBPF,
[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
[switch]$Uninstall,
[Parameter(ParameterSetName = 'Uninstall')]
[switch]$PurgeConfig
)
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# ── Colours ──────────────────────────────────────────────────────────────────
function Write-Step { param($msg) Write-Host " ==> $msg" -ForegroundColor Cyan }
function Write-OK { param($msg) Write-Host " ✓ $msg" -ForegroundColor Green }
function Write-Warn { param($msg) Write-Host " ⚠ $msg" -ForegroundColor Yellow }
function Write-Fail { param($msg) Write-Host " ✗ $msg" -ForegroundColor Red }
# ── Admin check / self-elevate ────────────────────────────────────────────────
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(
[Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Warn "Not running as Administrator — attempting to self-elevate..."
$argList = "-ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`""
foreach ($p in $PSBoundParameters.GetEnumerator()) {
$argList += " -$($p.Key)"
if ($p.Value -isnot [switch]) { $argList += " `"$($p.Value)`"" }
}
Start-Process powershell -ArgumentList $argList -Verb RunAs
exit 0
}
# ─────────────────────────────────────────────────────────────────────────────
Write-Host ""
Write-Host " Synapse — Active NDR for Windows" -ForegroundColor White
Write-Host " https://gen0sec.com" -ForegroundColor DarkGray
Write-Host ""
# ── Uninstall path ────────────────────────────────────────────────────────────
if ($Uninstall) {
Write-Step "Stopping Synapse service..."
$svc = Get-Service -Name "Synapse" -ErrorAction SilentlyContinue
if ($svc) {
if ($svc.Status -eq "Running") { Stop-Service -Name "Synapse" -Force }
Write-Step "Unregistering service..."
$exe = Join-Path $InstallDir "synapse.exe"
if (Test-Path $exe) {
& $exe --service uninstall 2>$null
} else {
sc.exe delete Synapse | Out-Null
}
Write-OK "Service removed."
} else {
Write-Warn "Synapse service not found — skipping."
}
Write-Step "Removing binary from $InstallDir..."
if (Test-Path $InstallDir) {
Remove-Item $InstallDir -Recurse -Force
Write-OK "Removed $InstallDir"
}
# Remove from PATH
$machinePath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
if ($machinePath -like "*$InstallDir*") {
$newPath = ($machinePath -split ";" | Where-Object { $_ -ne $InstallDir }) -join ";"
[Environment]::SetEnvironmentVariable("PATH", $newPath, "Machine")
Write-OK "Removed from system PATH."
}
if ($PurgeConfig) {
Write-Step "Removing configuration and data ($ConfigDir)..."
if (Test-Path $ConfigDir) {
Remove-Item $ConfigDir -Recurse -Force
Write-OK "Removed $ConfigDir"
}
} else {
Write-Warn "Configuration and logs preserved at $ConfigDir"
Write-Warn "Run with -Uninstall -PurgeConfig to remove them."
}
Write-Host ""
Write-OK "Synapse uninstalled."
Write-Host ""
exit 0
}
# ── Resolve version ───────────────────────────────────────────────────────────
if ($Version -eq "") {
Write-Step "Resolving latest release..."
try {
$rel = Invoke-RestMethod "https://api.github.com/repos/gen0sec/synapse/releases/latest"
$Version = $rel.tag_name -replace '^v', ''
Write-OK "Latest version: $Version"
} catch {
Write-Fail "Could not query GitHub releases. Specify -Version explicitly."
exit 1
}
}
$DOWNLOAD_URL = "https://github.com/gen0sec/synapse/releases/download/v${Version}/synapse-x86_64-pc-windows-msvc.zip"
$CONFIG_URL = "https://raw.githubusercontent.com/gen0sec/synapse/refs/heads/main/config/config_windows.yaml"
# ── eBPF for Windows (optional) ───────────────────────────────────────────────
if ($WithEBPF) {
$ebpfSvc = Get-Service -Name "eBPFCore" -ErrorAction SilentlyContinue
if ($ebpfSvc) {
Write-OK "eBPF for Windows already installed."
} else {
Write-Step "Installing eBPF for Windows..."
$ebpfUrl = "https://github.com/microsoft/ebpf-for-windows/releases/latest/download/ebpf-for-windows.msi"
$ebpfMsi = Join-Path $env:TEMP "ebpf-for-windows.msi"
Invoke-WebRequest -Uri $ebpfUrl -OutFile $ebpfMsi -UseBasicParsing
Start-Process msiexec.exe -ArgumentList "/i `"$ebpfMsi`" /quiet /norestart" -Wait -Verb RunAs
Write-OK "eBPF for Windows installed."
Write-Warn "Test signing may be required: bcdedit /set testsigning on (then reboot)"
}
}
# ── Download binary ───────────────────────────────────────────────────────────
Write-Step "Downloading Synapse v${Version}..."
$zipPath = Join-Path $env:TEMP "synapse-windows.zip"
try {
Invoke-WebRequest -Uri $DOWNLOAD_URL -OutFile $zipPath -UseBasicParsing
Write-OK "Downloaded."
} catch {
Write-Fail "Download failed: $_"
Write-Fail "URL: $DOWNLOAD_URL"
exit 1
}
# ── Create directories ────────────────────────────────────────────────────────
Write-Step "Creating directories..."
foreach ($dir in @(
$InstallDir,
$ConfigDir,
(Join-Path $ConfigDir "logs"),
(Join-Path $ConfigDir "data"),
(Join-Path $ConfigDir "certs")
)) {
if (-not (Test-Path $dir)) {
New-Item -ItemType Directory -Path $dir -Force | Out-Null
}
}
Write-OK "Directories created."
# ── Extract binary ────────────────────────────────────────────────────────────
Write-Step "Extracting synapse.exe to $InstallDir..."
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($zipPath)
foreach ($entry in $zip.Entries) {
if ($entry.Name -eq "synapse.exe") {
$dest = Join-Path $InstallDir "synapse.exe"
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $dest, $true)
break
}
}
$zip.Dispose()
Remove-Item $zipPath -Force
Write-OK "synapse.exe extracted."
# ── Add to PATH ───────────────────────────────────────────────────────────────
$machinePath = [Environment]::GetEnvironmentVariable("PATH", "Machine")
if ($machinePath -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$machinePath;$InstallDir", "Machine")
Write-OK "Added to system PATH."
}
# ── Write configuration ───────────────────────────────────────────────────────
$configFile = Join-Path $ConfigDir "config.yaml"
if (-not (Test-Path $configFile)) {
Write-Step "Downloading default configuration..."
try {
Invoke-WebRequest -Uri $CONFIG_URL -OutFile $configFile -UseBasicParsing
Write-OK "Config written to $configFile"
} catch {
Write-Warn "Could not download default config. Writing minimal config..."
@"
mode: "agent"
network:
iface: "auto"
ip_version: "both"
firewall:
mode: "auto"
platform:
api_key: ""
base_url: "https://api.gen0sec.com/v1"
log_sending_enabled: true
logging:
level: "info"
file_logging_enabled: true
log_directory: "$($ConfigDir.Replace('\','\\'))\\logs"
etw:
enabled: false
"@ | Set-Content $configFile -Encoding UTF8
}
} else {
Write-Warn "Config already exists at $configFile — not overwritten."
}
# Inject API key
if ($ApiKey -ne "") {
Write-Step "Setting API key..."
$cfg = Get-Content $configFile -Raw
$cfg = $cfg -replace '(api_key:\s*")[^"]*(")', "api_key: `"$ApiKey`""
Set-Content $configFile $cfg -Encoding UTF8 -NoNewline
Write-OK "API key configured."
}
# ── Windows Firewall rule ─────────────────────────────────────────────────────
$fwRule = Get-NetFirewallRule -DisplayName "Synapse" -ErrorAction SilentlyContinue
if (-not $fwRule) {
Write-Step "Adding Windows Firewall inbound rule..."
New-NetFirewallRule `
-DisplayName "Synapse" `
-Direction Inbound `
-Program (Join-Path $InstallDir "synapse.exe") `
-Action Allow `
-Protocol Any | Out-Null
Write-OK "Firewall rule added."
}
# ── Install Windows Service ───────────────────────────────────────────────────
if (-not $NoService) {
Write-Step "Installing Synapse as Windows Service..."
$exe = Join-Path $InstallDir "synapse.exe"
$svc = Get-Service -Name "Synapse" -ErrorAction SilentlyContinue
if ($svc) {
Write-Warn "Synapse service already exists. Skipping service install."
} else {
& $exe -c (Join-Path $ConfigDir "config.yaml") --service install
Write-OK "Service installed."
}
}
# ── Summary ───────────────────────────────────────────────────────────────────
$svc = Get-Service -Name "Synapse" -ErrorAction SilentlyContinue
$svcStatus = if ($svc) { $svc.Status } else { "not installed" }
Write-Host ""
Write-Host " ─────────────────────────────────────────" -ForegroundColor DarkGray
Write-Host " Synapse v${Version} installed" -ForegroundColor White
Write-Host " ─────────────────────────────────────────" -ForegroundColor DarkGray
Write-Host ""
Write-Host " Binary: $InstallDir\synapse.exe" -ForegroundColor Gray
Write-Host " Config: $configFile" -ForegroundColor Gray
Write-Host " Logs: $ConfigDir\logs\" -ForegroundColor Gray
Write-Host " Service: $svcStatus" -ForegroundColor Gray
Write-Host ""
if ($ApiKey -eq "") {
Write-Warn "No API key set. Edit $configFile and add your key:"
Write-Host " https://dash.gen0sec.com/settings/api-keys" -ForegroundColor DarkCyan
Write-Host ""
}
if (-not $NoService) {
if ($ApiKey -ne "") {
Write-Host " Start the service:" -ForegroundColor White
Write-Host " sc start Synapse" -ForegroundColor DarkCyan
} else {
Write-Host " After setting your API key, start the service:" -ForegroundColor White
Write-Host " sc start Synapse" -ForegroundColor DarkCyan
}
Write-Host ""
Write-Host " Other service commands:" -ForegroundColor DarkGray
Write-Host " sc stop Synapse # stop" -ForegroundColor DarkGray
Write-Host " sc control Synapse paramchange # reload config" -ForegroundColor DarkGray
Write-Host " synapse.exe --service uninstall # uninstall service" -ForegroundColor DarkGray
} else {
Write-Host " Run manually:" -ForegroundColor White
Write-Host " synapse.exe -c `"$configFile`"" -ForegroundColor DarkCyan
}
Write-Host ""
Write-Host " Docs: https://github.com/gen0sec/synapse/blob/main/docs/INSTALLATION.md" -ForegroundColor DarkGray
Write-Host ""