CyGlobalLists.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
<#
.SYNOPSIS
    Gets the global list
 
.PARAMETER API
    Optional. API Handle (use only when not using session scope).
 
.PARAMETER List
    The type of global list to retrieve.
#>

function Get-CyGlobalList {
    Param (
        [parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [Parameter(Mandatory=$true)]
        [ValidateSet ("GlobalSafeList", "GlobalQuarantineList")]
        [String]$List
        )

    $APIListType = 0

    switch ($List) {
        "GlobalQuarantine" {
            $APIListType = 0
        }
        "GlobalSafeList" {
            $APIListType = 1
        }
    }

    Read-CyData -API $API -Uri "$($API.BaseUrl)/globallists/v2" -QueryParams @{ "listTypeId" = $APIListType }
}

<#
.SYNOPSIS
    Adds file hash to global list
 
.PARAMETER API
    Optional. API Handle (use only when not using session scope).
 
.PARAMETER List
    The global list type to add to
 
.PARAMETER SHA256
    The file hash to add
 
.PARAMETER Category
    The category of the file to add to the list
 
.PARAMETER Reason
    The reason for adding the file.
#>

function Add-CyHashToGlobalList {
    Param (
        [parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [Parameter(Mandatory=$true)]
        [ValidateSet ("GlobalSafeList", "GlobalQuarantineList")]
        [String]$List,
        [Parameter(
            Mandatory=$true, 
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [String]$SHA256,
        [Parameter(Mandatory=$false)]
        [ValidateSet ("Admin Tool", "Commercial Software", "Drivers", "Internal Application", "Operating System", "Security Software", "None")]
        [String]$Category,
        [Parameter(Mandatory=$true)]
        [String]$Reason
    )

    Begin {
        $headers = @{
            "Accept" = "application/json"
            "Authorization" = "Bearer $($API.AccessToken)"
        }
        $APIListType = "GlobalQuarantine";
        if ($List -eq "GlobalSafeList") { $APIListType = "GlobalSafe" }
    }

    Process {
        switch ($APIListType) {
            "GlobalQuarantine" {
                $updateMap = @{
                    "sha256" = $SHA256
                    "list_type" = $APIListType
                    "reason" = $Reason
                }
            }
            "GlobalSafe" {
                $updateMap = @{
                    "sha256" = $SHA256
                    "list_type" = $APIListType
                    "reason" = $Reason
                    "category" = $Category
                }
            }
        }

        $json = $updateMap | ConvertTo-Json
        # remain silent
        $null = Invoke-RestMethod -Method POST -Uri "$($API.BaseUrl)/globallists/v2" -ContentType "application/json; charset=utf-8" -Header $headers -UserAgent "" -Body $json
    }
}

<#
.SYNOPSIS
    Removes a hash from a global list
 
.PARAMETER API
    Optional. API Handle (use only when not using session scope).
 
.PARAMETER List
    The list type
 
.PARAMETER SHA256
    The file hash to remove.
#>

function Remove-CyHashFromGlobalList {
    Param (
        [parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [Parameter(Mandatory=$true)]
        [ValidateSet ("GlobalSafeList", "GlobalQuarantineList")]
        [String]$List,
        [Parameter(
            Mandatory=$true, 
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
        [String]$SHA256
    )

    Begin {
        $headers = @{
            "Accept" = "application/json"
            "Authorization" = "Bearer $($API.AccessToken)"
        }
        $APIListType = "GlobalQuarantine";
        if ($List -eq "GlobalSafeList") { $APIListType = "GlobalSafe" }
    }

    Process {
        $updateMap = @{
            "sha256" = $SHA256
            "list_type" = $APIListType
        }

        $json = $updateMap | ConvertTo-Json
        $null = Invoke-RestMethod -Method DELETE -Uri "$($API.BaseUrl)/globallists/v2" -ContentType "application/json; charset=utf-8" -Header $headers -UserAgent "" -Body $json
    }
}