-
Notifications
You must be signed in to change notification settings - Fork 17
/
Get-CsToken.psm1
116 lines (105 loc) · 3.45 KB
/
Get-CsToken.psm1
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
function Get-CsToken {
<#
.SYNOPSIS
Request an OAuth2 access token
.PARAMETER ID
Client Id
.PARAMETER SECRET
Client Secret
.PARAMETER CID
Specific CID to target in MSSP configurations
.PARAMETER CLOUD
CrowdStrike destination cloud [default: 'US']
.PARAMETER PROXY
Web proxy address
#>
[CmdletBinding()]
[OutputType([psobject])]
param(
[string]
$Id,
[string]
$Secret,
[string]
$CID,
[ValidateSet('EU', 'US', 'US-2', 'USFed')]
[string]
$Cloud = 'US',
[string]
$Proxy
)
begin{
# Create $Falcon for logging and caching credentials
if (-not($Falcon)) {
[System.Collections.Hashtable] $Global:Falcon = @{}
}
# Set $Falcon.host based on $Cloud
switch ($Cloud) {
'EU' { $Falcon['host'] = 'https://api.eu-1.crowdstrike.com' }
'US' { $Falcon['host'] = 'https://api.crowdstrike.com' }
'US-2' { $Falcon['host'] = 'https://api.us-2.crowdstrike.com' }
'USFed' { $Falcon['host'] = 'https://api.laggar.gcw.crowdstrike.com' }
}
# Capture parameter input
switch ($PSBoundParameters.Keys) {
'Id' { $Falcon['id'] = $Id }
'Secret' { $Falcon['secret'] = $Secret | ConvertTo-SecureString -AsPlainText -Force }
'CID' { $Falcon['cid'] = [string] $CID }
'Proxy' { $Falcon['proxy'] = $Proxy }
}
# If missing, prompt for Id/Secret
if (-not($Falcon.id)) {
$Falcon['id'] = Read-Host 'Client Id'
}
if (-not($Falcon.secret)) {
$Falcon['secret'] = Read-Host 'Client Secret' -AsSecureString
}
# Clear existing member CID if $CID was not defined
if ((-not($CID)) -and ($Falcon.cid)) {
$Falcon.remove('cid')
}
# Clear existing proxy if $Proxy was not defined
if ((-not($Proxy)) -and ($Falcon.proxy)) {
$Falcon.remove('proxy')
}
}
process{
# Set base parameters
$Param = @{
Uri = '/oauth2/token'
Method = 'post'
Header = @{
accept = 'application/json'
}
Body = 'client_id=' + [string] $Falcon.id + '&client_secret='
}
# Add secret to token request
if ($PSVersionTable.PSVersion.Major -gt 6) {
$Param.Body += ($Falcon.secret | ConvertFrom-SecureString -AsPlainText)
} else {
$Param.Body += ([System.Runtime.InteropServices.Marshal]::PtrToStringAuto(
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Falcon.secret)))
}
# Add member CID, if defined
if ($Falcon.cid) {
$Param.Body += '&member_cid=' + [string] $Falcon.cid
}
$Request = Invoke-CsAPI @Param
# Save token and expiration time to $Falcon
if ($Request.access_token) {
$Falcon['expires'] = ((Get-Date).addSeconds($Request.expires_in))
$Falcon['token'] = [string] $Request.token_type + ' ' + [string] $Request.access_token
}
else {
# Erase $Falcon if token request fails
Remove-Variable -Name Falcon -Scope Global
# Output error
if ($Request.errors) {
$Request.errors
}
else {
$Request
}
}
}
}