CyOpticsInstaQuery.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
<#
.SYNOPSIS
    Creates a new InstaQuery.
 
.PARAMETER API
    Optional. API Handle (use only when not using session scope).
#>

function New-CyInstaQuery {
    Param (
        [parameter(Mandatory=$false)]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [parameter(Mandatory=$true)]
        [String]$Name,
        [parameter(Mandatory=$false)]
        [String]$Description,
        [parameter(Mandatory=$true)]
        [ValidateSet ("File_Path", 
                        "File_MD5", 
                        "File_SHA256", 
                        "File_Owner", 
                        "File_CreationDateTime", 
                        "Process_Name", 
                        "Process_CommandLine", 
                        "Process_PrimaryImagePath", 
                        "Process_PrimaryImageMd5", 
                        "Process_StartDateTime", 
                        "NetworkConnection_DestAddr", 
                        "NetworkConnection_DestPort", 
                        "RegistryKey_ProcessName", 
                        "RegistryKey_ProcessPrimaryImagePath", 
                        "RegistryKey_ValueName", 
                        "RegistryKey_FilePath", 
                        "RegistryKey_FileMd5", 
                        "RegistryKey_IsPersistencePoint")]
        [String]$QueryType,
        [parameter(Mandatory=$false)]
        [bool]$CaseSensitive = $false,
        [parameter(Mandatory=$false)]
        [ValidateSet ("Fuzzy", "Exact")]
        [String]$MatchType = "Fuzzy",
        [parameter(Mandatory=$true)]
        [String[]]$Value,
        [parameter(Mandatory=$true)]
        [object[]]$Zones
    )

    Begin {
    }

    Process {
        $qt = $QueryType.Split("_")
        $params = @{
            name = $Name;
            case_sensitive = $CaseSensitive;
            match_type = $MatchType;
            match_values = @( $Value );
            artifact = $qt[0];
            match_value_type = $qt[1];
            zones = @( $Zones.id | ForEach-Object { $_.ToUpper() -replace "-" } )
# filters = @{
                #aspect = "OS";
                #value = "Windows"
            #}
        }

        if (![String]::IsNullOrEmpty($Description)) {
            $params.description = $Description
        }


        $json = '{"name":"powershe- Proc Name","description":"","artifact":"Process","match_value_type":"Name","match_values":["powershell.exe"],"case_sensitive":false,"match_type":"Fuzzy","zones":["979951FC8E724A51B31105AC19BC1C8B","2D567BB4B1144F77BD4EB4D2D111AB70"]}' | ConvertFrom-Json
        $json = ConvertTo-Json $json

        $json = ConvertTo-Json $params
        $json

        Invoke-CyRestMethod -Method POST -API $API -Uri "$($API.BaseUrl)/instaqueries/v2" -Body $json
    }
}

<#
.SYNOPSIS
    Gets an InstaQuery status
 
    .PARAMETER API
    Optional. API Handle (use only when not using session scope).
#>

function Get-CyInstaQueryResults {
    Param (
        [parameter(Mandatory=$false)]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [parameter(Mandatory=$false)]
        [Object]$InstaQuery
    )

    $queryId = $InstaQuery.id

    Invoke-CyRestMethod -API $API -Method GET -Uri "$($API.BaseUrl)/instaqueries/v2/$($queryId)/results"
}

<#
.SYNOPSIS
    Gets all InstaQuery queries in the tenant
 
    .PARAMETER API
    Optional. API Handle (use only when not using session scope).
#>

function Get-CyInstaQueries {
    Param (
        [parameter(Mandatory=$false)]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [parameter(Mandatory=$false)]
        [bool]$IncludeArchived = $false,
        [parameter(Mandatory=$false)]
        [String]$Query = "",
        [parameter(Mandatory=$false)]
        [ValidateSet ("name", "description", "artifact", "match_value_type")]
        [string]$Sort
    )

    $params = @{
        archived = $IncludeArchived;
        q = $Query
    }

    if (![String]::IsNullOrEmpty($Sort)) {
        $params.sort = $Sort
    }

    Read-CyData -API $API -Uri "$($API.BaseUrl)/instaqueries/v2" -QueryParams $params
}

<#
.SYNOPSIS
    Gets the device lockdown status and history.
 
.PARAMETER API
    Optional. API Handle (use only when not using session scope).
 
.PARAMETER Device
    The device object to query for.
#>

function Get-CyLockdownStatus {
    Param (
        [parameter(Mandatory=$false)]
        [CylanceAPIHandle]$API = $GlobalCyAPIHandle,
        [parameter(Mandatory=$false)]
        [object]$Device
    )
    Invoke-CyRestMethod -API $API -Method GET -Uri "$($API.BaseUrl)/devicecommands/v2/$($Device.id.ToUpper() -replace "-" )/lockdown" | Convert-CyObject
}