forked from thinkst/canary-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTokens.ps1
59 lines (53 loc) · 2.13 KB
/
CreateTokens.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
# Script to create Canary tokens for a list of hosts.
# We force TLS1.2 since our API doesn't support lower.
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
Set-StrictMode -Version 2.0
# Connect to API
$ApiHost = [string]::Empty
Do {
$ApiHost = Read-Host -Prompt "Enter your Canary API domain"
} Until (($ApiHost.Length -gt 0) -and ((Resolve-DnsName -DnsOnly -NoHostsFile -Name $ApiHost -Type A -ErrorAction SilentlyContinue)[0].IPAddress))
$ApiTokenSecure = New-Object System.Security.SecureString
Do {
$ApiTokenSecure = Read-Host -AsSecureString -Prompt "Enter your Canary API key"
} Until ($ApiTokenSecure.Length -gt 0)
$ApiToken = (New-Object System.Management.Automation.PSCredential "user",$ApiTokenSecure).GetNetworkCredential().Password
$ApiBaseURL = '/api/v1'
$PingResult = Invoke-RestMethod -Method Get -Uri "https://$ApiHost$ApiBaseURL/ping?auth_token=$ApiToken"
$Result = $PingResult.result
If ($Result -ne 'success') {
Write-Host "Cannot ping Canary API. Bad token?"
Exit
} Else {
Write-Host "Canary API available for service!"
}
$Targets = (
'HOST1',
'HOST2',
'HOST3')
ForEach ($TargetHostname in $Targets) {
# Check whether token already exists
$OutputFileName = "$TargetHostname-MSWORD.docx"
If (Test-Path $OutputFileName) {
Write-Host Skipping $TargetHostname, file already exists.
Continue
}
# Create token
$TokenName = "$TargetHostname-MSWORD"
$PostData = @{
auth_token = "$ApiToken"
kind = "doc-msword"
memo = "$TokenName"
}
$CreateResult = Invoke-RestMethod -Method Post -Uri "https://$ApiHost$ApiBaseURL/canarytoken/create" -Body $PostData
$Result = $CreateResult.result
If ($Result -ne 'success') {
Write-Host "Creation of $TokenName failed."
Exit
} Else {
$WordTokenID = $($CreateResult).canarytoken.canarytoken
Write-Host "$TokenName created (ID: $WordTokenID)."
}
# Download token
Invoke-RestMethod -Method Get -Uri "https://$ApiHost$ApiBaseURL/canarytoken/download?auth_token=$ApiToken&canarytoken=$WordTokenID" -OutFile "$OutputFileName"
}