/
demo-malware-detection-api.ps1
136 lines (116 loc) · 3.93 KB
/
demo-malware-detection-api.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
Param(
[Switch]$Trigger
)
Clear-Host
# General Variables - Please change where necessary
$APIversion = "v1"
$utcNow = Get-Date -UFormat "%Y-%m-%dT%H:%M:%S.%333Z"
$veeamAPI = "https://localhost:9419"
# Function for getting the Bearer Token
function Connect-VeeamRestAPI {
[CmdletBinding()]
param (
[string] $AppUri,
[pscredential] $Cred
)
begin {
$header = @{
"Content-Type" = "application/x-www-form-urlencoded"
"x-api-version" = "1.1-rev1"
"accept" = "application/json"
}
$body = @{
"grant_type" = "password"
"username" = $cred.UserName
"password" = $cred.GetNetworkCredential().password
"refresh_token" = " "
"rememberMe" = " "
}
$requestURI = $veeamAPI + $appUri
$tokenRequest = Invoke-RestMethod -Uri $requestURI -Headers $header -Body $body -Method Post
Write-Output $tokenRequest.access_token
}
}
# Function GET RestAPI data
function Get-VeeamRestAPI {
[CmdletBinding()]
param (
[string] $AppUri,
[string] $Token
)
begin {
$header = @{
"accept" = "application/json"
"x-api-version" = "1.1-rev1"
"Authorization" = "Bearer $Token"
}
$requestURI = $veeamAPI + $AppUri
$results = Invoke-RestMethod -Method GET -Uri $requestUri -Headers $header
Write-Output $results
}
}
# Function POST Veeam RestAPI
function Post-VeeamRestAPI {
[CmdletBinding()]
param (
[string] $AppUri,
[string] $token,
[string] $body
)
begin {
$header = @{
"accept" = "application/json"
"x-api-version" = "1.1-rev1"
"Content-Type" = "application/json"
"Authorization" = "Bearer $Token"
}
$requestURI = $veeamAPI + $appUri
$tokenRequest = Invoke-RestMethod -Uri $requestURI -Headers $header -Body $body -Method Post
Write-Output $tokenRequest
}
}
# Get credentials - The ones you need to login to the Management Console
$cred = Get-Credential -Message "Please enter your VBR credentials" -UserName Administrator
#Ignore any self-signed certificate
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
#Request Bearer Token
Write-Host "Get Bearer Token...."
Write-Host ""
$appURI = "/api/oauth2/token"
$token = Connect-VeeamRestAPI -AppUri $appURI -Cred $cred
# Get all malware detection events
Write-Host "Getting malware detection events...." -ForegroundColor White
Write-Host ""
Write-Host "Last 10 events"
$appURI = "/api/$APIversion/malwareDetection/events/"
$vbrMalwareEvt = Get-VeeamRestAPI -AppUri $appURI -Token $token
$tableData = @()
foreach ($entry in $vbrMalwareEvt.data) {
$rowData = New-Object PSObject -property @{
'TimeUtc' = $entry.detectionTimeUtc
'Severity' = $entry.severity
'Source' = $entry.source
'Details' = $entry.Details
}
$tableData += $rowData
}
$tableData = $tableData | Sort-Object TimeUtc -Descending
$tableData | Select-Object -Last 10 | Format-Table -Property TimeUtc, Severity, Source, Details -AutoSize
# Trigger a test event
if($Trigger){
#Building Body trigger a malware alert - Mimimun 2. Others are uuid or ipv6
$body_data = @{
detectionTimeUtc = "$($utcNow)"
machine = @{
fqdn = "<your host name here>"
ipv4 = "<your IPv4 address here>"
}
details = "Malware Detection Test Trigger"
engine = "YAMT Test Script"
}
Write-Host "Trigger malware detection event...." -ForegroundColor White
Write-Host ""
$appURI = "/api/$APIversion/malwareDetection/events"
$body_json = $body_data | ConvertTo-Json
Post-VeeamRestAPI -AppUri $appURI -Token $token -body $body_json
}