targetgroupmgmt.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
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
# Setup APIs
function New-ICTargetGroup {
  param(
    [parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$Name,

    [parameter(Mandatory=$false, Position=1)]
    [String]$ControllerGroupId
  )

  if (-NOT $ControllerGroupId) {
    $g = Get-ICControllerGroup
    if (($g.id.count -gt 1) -AND (-NOT $ControllerGroupId)) {
        Write-Error "More than one Controller Group. Please specify ControllerGroupId."
    }
    $ControllerGroupId = $g.id
  }

  $Endpoint = "targets"
  $body = @{
    name = $Name
    controllerGroupId = $ControllerGroupId
  }
  Write-Host "Creating new target group: $Name [$HuntServerAddress/api/$Endpoint]"
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -body $body -method 'POST'
}

function New-ICControllerGroup {
  param(
    [parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$Name
  )

  $Endpoint = "controllergroups"
  $body = @{
    name = $Name;
  }
  Write-Host "Creating new Controller Group: $Name [$HuntServerAddress/api/$Endpoint]"
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -body $body -method 'POST'
}

function Get-ICTargetGroup ([String]$TargetGroupId) {
  $Endpoint = "targets"
  $filter =  @{
    order = @("name", "id")
    limit = $resultlimit
    skip = 0
  }
  if ($TargetGroupId) {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$true | where { $_.id -eq $TargetGroupId}
  } else {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$true
  }
}

function Get-ICControllerGroup ([String]$ControllerGroupId) {
  $Endpoint = "controllergroups"
  $filter =  @{
    order = @("name", "id")
    limit = $resultlimit
    skip = 0
  }
  if ($TargetGroupId) {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$true | where { $_.id -eq $ControllerGroupId}
  } else {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$true
  }
}


function Remove-ICTargetGroup {
  param(
    [parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$TargetGroupId
  )

  $Endpoint = "targets/$TargetGroupId"
  Write-Warning "Removing target group [$HuntServerAddress/api/$Endpoint]."
  Write-Warning "This will remove access to all scan data within this target group and is only reversible for the next 7 days"
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -method 'DELETE'
}

function Remove-ICControllerGroup {
  param(
    [parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$ControllerGroupId
  )

  $Endpoint = "controllergroups/$ControllerGroupId"
  Write-Warning "Removing Controller Group [$HuntServerAddress/api/$Endpoint]."
 # Write-Warning "This will remove access to all scan data within this target group and is only reversible for the next 7 days"
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -method 'DELETE'
}


function New-ICCredential {
  Param(
    [parameter(Mandatory=$True, Position=0)]
    [String]
    $Name,

    [parameter(Mandatory=$True)]
    [PSCredential]$Cred
  )

  $Endpoint = "credentials"
  $data = @{
    name = $Name
    username = $Cred.Username
    password = $Cred.GetNetworkCredential().Password
  }
  $body = @{
    data = $data
  }
  Write-Host "Adding new Credential $Name [$($Cred.Username)] to the Credential Manager"
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -body $body -method POST
}

function Get-ICCredential ($CredentialId) {
    Write-Verbose "Getting Credential Objects from Infocyte HUNT: $HuntServerAddress"
  $Endpoint = "credentials"
  $filter =  @{
    limit = $resultlimit
    skip = 0
  }
  if ($CredentialId) {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter | where { $_.id -eq $CredentialId }
  } else {
    _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter
  }
}

function Remove-ICCredential {
  param(
    [parameter(Mandatory=$true, Position=0)]
    [ValidateNotNullOrEmpty()]
    [String]$CredentialId
  )

  $Endpoint = "credentials/$CredentialId"
  Write-Warning "Removing credential [$HuntServerAddress/api/$Endpoint]."
  _ICRestMethod -url $HuntServerAddress/api/$Endpoint -method 'DELETE'

}


function Get-ICAddress ([String]$TargetGroupId, [HashTable]$Where, [Switch]$NoLimit) {
  $Endpoint = "Addresses"
    $filter =  @{
        order = "lastAccessedOn"
        limit = $resultlimit
        skip = 0
    where = @{
      and = @()
    }
    }
  if ($where.count -gt 0) {
    $where.GetEnumerator() | % {
      $filter['where']['and'] += @{ $($_.key) = $($_.value) }
    }
  }

  if ($TargetGroupId) {
    $filter['where']['and'] += @{ targetId = $TargetGroupId }
    Write-Verbose "Getting all addresses from TargetGroup $TargetGroupId"
  }
  _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$NoLimit
}

function Remove-ICAddress {
  Param(
    [ValidateNotNullorEmpty()]
    [String]$TargetGroupId
  )

    Write-Warning "Clearing all Addresses from TargetGroup $TargetGroupId"
  $Endpoint = "Addresses"
  $where = @{
    targetId = $TargetGroupId
  }
  $body = @{
    where = $where
  }
    _ICRestMethod -url $HuntServerAddress/api/$Endpoint -body $body -method DELETE
}


function Get-ICScan ([String]$TargetGroupId, $TargetGroupName, [HashTable]$Where, [Switch]$NoLimit) {
  $Endpoint = "IntegrationScans"
  $filter =  @{
    order = "scanCompletedOn desc"
    limit = $resultlimit
    skip = 0
    where = @{
      and = @()
    }
  }
  if ($where.count -gt 0) {
    $where | % {
      $filter['where']['and'] += $_
    }
  }

  if ($TargetGroupId) {
    $tgname = (Get-ICTargetGroup -TargetGroupId $TargetGroupId).name
    $filter['where']['and'] += @{ targetList = $tgname }
    Write-Verbose "Getting Scans against Target Group $TargetGroup [$TargetGroupId] from $HuntServerAddress"
  } elseif ($TargetGroupName) {
      $filter['where']['and'] += @{ targetList = $TargetGroupName }
      Write-Verbose "Getting Scans against $TargetGroupName from $HuntServerAddress"
  } else {
    Write-Verbose "Getting Scans from $HuntServerAddress"
  }
  _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$NoLimit
}

function Get-ICBox ([Switch]$Last90, [Switch]$Last7, [Switch]$Last30, [Switch]$IncludeDeleted, [Switch]$Global, [String]$targetGroupId, [Switch]$NoLimit) {
  $Endpoint = "Boxes"
  $filter =  @{
    limit = $resultlimit
    skip = 0
    where = @{ and = @() }
  }


  if ($Last90) {
    $filter.where['and'] += @{ name = "Last 90 days" }
  }
  elseif ($Last30) {
    $filter.where['and'] += @{ name = "Last 30 days" }
  }
  elseif ($Last7) {
    $filter.where['and'] += @{ name = "Last 7 days" }
  }

  if ($targetGroupId) {
    $filter.where['and'] += @{ targetId = $targetGroupId }
  }
  elseif ($Global) {
    $filter.where['and'] += @{ targetId = $null }
  }

  $boxes = _ICGetMethod -url $HuntServerAddress/api/$Endpoint -filter $filter -NoLimit:$NoLimit
  $TargetGroups = Get-ICTargetGroup
  $boxes | % {
    if ($_.targetId) {
       $tgid = $_.targetId
       $tg = $TargetGroups | where { $_.id -eq $tgid }
       if ($tg) {
         $_ | Add-Member -MemberType "NoteProperty" -name "targetGroup" -value $tg.name
       } else {
         $_ | Add-Member -MemberType "NoteProperty" -name "targetGroup" -value "Deleted"
       }
    } else {
      $_ | Add-Member -MemberType "NoteProperty" -name "targetGroup" -value "All"
    }
  }
  if ($IncludeDeleted) {
    $boxes
  } else {
    $boxes | where { $_.targetGroup -ne "Deleted" }
  }

}