-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.ps1
168 lines (145 loc) · 6.45 KB
/
script.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
Function Convert-EventLogRecord {
[cmdletbinding()]
[alias("clr")]
Param(
[Parameter(Position = 0, Mandatory, ValueFromPipeline)]
[ValidateNotNullorEmpty()]
[System.Diagnostics.Eventing.Reader.EventLogRecord[]]$LogRecord
)
Begin {
Write-Verbose "[BEGIN ] Starting: $($MyInvocation.Mycommand)"
} #begin
Process {
foreach ($record in $LogRecord) {
Write-Verbose "[PROCESS] Processing event id $($record.ID) from $($record.logname) log on $($record.machinename)"
Write-Verbose "[PROCESS] Creating XML data"
[xml]$r = $record.ToXml()
$h = [ordered]@{
LogName = $record.LogName
RecordType = $record.LevelDisplayName
TimeCreated = $record.TimeCreated
ID = $record.Id
}
if ($r.Event.EventData.Data.Count -gt 0) {
Write-Verbose "[PROCESS] Parsing event data"
if ($r.Event.EventData.Data -is [array]) {
<#
I only want to enumerate with the For loop if the data is an array of objects
If the data is just a single string like Foo, then when using the For loop,
the data value will be the F and not the complete string, Foo.
#>
for ($i = 0; $i -lt $r.Event.EventData.Data.count; $i++) {
$data = $r.Event.EventData.data[$i]
#test if there is structured data or just text
if ($data.name) {
$Name = $data.name
$Value = $data.'#text'
}
else {
Write-Verbose "[PROCESS] No data property name detected"
$Name = "RawProperties"
#data will likely be an array of strings
[string[]]$Value = $data
}
if ($h.Contains("RawProperties")) {
Write-Verbose "[PROCESS] Appending to RawProperties"
$h.RawProperties += $value
}
else {
Write-Verbose "[PROCESS] Adding $name"
$h.add($name, $Value)
}
} #for data
} #data is an array
else {
$data = $r.Event.EventData.data
if ($data.name) {
$Name = $data.name
$Value = $data.'#text'
}
else {
Write-Verbose "[PROCESS] No data property name detected"
$Name = "RawProperties"
#data will likely be an array of strings
[string[]]$Value = $data
}
if ($h.Contains("RawProperties")) {
Write-Verbose "[PROCESS] Appending to RawProperties"
$h.RawProperties += $value
}
else {
Write-Verbose "[PROCESS] Adding $name"
$h.add($name, $Value)
}
}
} #if data
else {
Write-Verbose "[PROCESS] No event data to process"
}
$h.Add("Message", $record.Message)
$h.Add("Keywords", $record.KeywordsDisplayNames)
$h.Add("Source", $record.ProviderName)
$h.Add("Computername", $record.MachineName)
Write-Verbose "[PROCESS] Creating custom object"
New-Object -TypeName PSObject -Property $h
} #foreach record
} #process
End {
Write-Verbose "[END ] Ending: $($MyInvocation.Mycommand)"
} #end
}
Function ADAuth {
param(
$username,
$password)
$password = $password | ConvertTo-SecureString -asPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential($username, $password)
Start-Process powershell "calc.exe" -Credential $creds
}
$ErrorActionPreference= 'silentlycontinue'
# Configuration variables
$PSEmailServer="10.11.1.184"
$from = "kerberoasting <[email protected]>"
$to = "soc <[email protected]>"
$name = 'svc_backup'
$serviceName = "BackupService"
$serviceAccountPassword = 'ecfd1f2055b07c306ed3bc2709b5de6'
$schedulerInterval = 5
$schedulerTaskName = "BackupTask"
$binaryPathName = "C:\Windows\System32\calc.exe"
if(Get-ScheduledTaskInfo -TaskName BackupTask) {
try {
$ip = Get-WinEvent -FilterHashtable @{Logname="Security";ID=4769;starttime=(date).AddSeconds(($schedulerInterval * 60) * -1);endtime=date} | Convert-EventLogRecord | Where-Object -Property ServiceName -Contains $name | select IPAddress
$ip = $ip.IpAddress.split(':')[-1]
} catch [Exception] {
echo "Null"
}
If ($ip -ne $null) {
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = "SPN Honeypot has been triggered from $ip"
$balloon.BalloonTipTitle = "Kerberoasting Detected!"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
Send-MailMessage -From $from -To $to -Subject "Kerberoasting Alert!" -Body "SPN Honeypot has been triggered from $ip"
} else {
echo "Null"
}
} else {
if (!(Get-ADUser -Filter "Name -eq '$name'")) {
New-ADUser -Name $name -SamAccountName $sam -AccountPassword(ConvertTo-SecureString $serviceAccountPassword -AsPlainText -force) -Enabled $true
New-Service -Name "$serviceName" -BinaryPathName $binaryPathName -DisplayName "$serviceName"
setspn -S "HOST/$serviceName" $name
ADAuth -username $name -password $serviceAccountPassword
}
$scriptPath = Get-Location
$scriptName = $MyInvocation.MyCommand.Name
$scriptPath = "$scriptPath\$scriptName"
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-File $scriptPath"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes $schedulerInterval)
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $schedulerTaskName
}