-
Notifications
You must be signed in to change notification settings - Fork 2
/
DotNet.Provider.fs
313 lines (262 loc) · 13.1 KB
/
DotNet.Provider.fs
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//Licensed to the Apache Software Foundation (ASF) under one
//or more contributor license agreements. See the NOTICE file
//distributed with this work for additional information
//regarding copyright ownership. The ASF licenses this file
//to you under the Apache License, Version 2.0 (the
//"License"); you may not use this file except in compliance
//with the License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//Unless required by applicable law or agreed to in writing,
//software distributed under the License is distributed on an
//"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
//KIND, either express or implied. See the License for the
//specific language governing permissions and limitations
//under the License.
module Fetters.DotNet.Provider
open System
open System.Net
open System.Text
open Fetters.Lists
open Fetters.DomainTypes
open Fetters.DotNet.Common
let getBasicInfo () =
let currentUser =
{username = Environment.UserName
cwd = Environment.CurrentDirectory
isHighIntegrity = isHighIntegrity ()
isLocalAdmin = isLocalAdmin ()
}
let rKey = getRegistryKey HKEY_LOCAL_MACHINE "Software\\Microsoft\\Windows NT\\CurrentVersion" |> Option.get
let windowsDetails =
{productName = getRegistryValue "ProductName" rKey
releaseId = getRegistryValue "ReleaseId" rKey
currentBuild = getRegistryValue "CurrentBuild" rKey
arch = Environment.GetEnvironmentVariable "PROCESSOR_ARCHITECTURE"
buildBranch = getRegistryValue "BuildBranch" rKey
currentSession = currentUser
}
let pc = {hostname = (NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName + "\\" + Dns.GetHostName())
processorCount = Environment.ProcessorCount}
windowsDetails |> FettersSpecialRecord.WindowsDetails, pc |> FettersSpecialRecord.PC
//////////////////////////
//Browser Data Enumeration
//////////////////////////
let private extractChromeHistory (path: string) : ChromeHistory =
let cPath = path + "\\" + "AppData\\Local\\Google\\Chrome\\User Data\\Default\\History"
match fileExistsAtLocation cPath with
|true ->
let rgx = createMatchRegex @"(http|ftp|https|file)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-]\s)?"
let res = yieldLineSequence cPath |> Seq.map (matchStringRegex rgx) |> Seq.toList |> List.filter(fun l -> not(l = ""))
{path = path; url = res}
|false -> {path = ""; url = []}
let private extractChromeBookmarks (path: string) : ChromeBookmark list =
let cPath = path + "\\" + "AppData\\Local\\Google\\Chrome\\User Data\\Default\\Bookmarks"
match fileExistsAtLocation cPath with
|true ->
let bookmarks = ChromeBookmarkJ.Parse(yieldWholeFile cPath)
bookmarks.Roots.BookmarkBar.Children
|> Array.map(fun b -> {name = b.Name; url = b.Url})
|> Array.toList
|false -> []
let triageChrome (path: string) : FettersFilesystemRecord =
//Fed by a collection of user paths
let b = extractChromeBookmarks path
let h = extractChromeHistory path
{bookmarks = b; history = h} |> FettersFilesystemRecord.ChromeInfo
let private getFirefoxProfiles (path: string) =
//Because multiple Firefox profiles could reside in one user directory,
//this is implemented differently than the Chrome enumeration.
let cPath = path + "\\" + "AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\"
match dirExistsAtLocation cPath with
|true -> listChildDirectories cPath
|> Array.map (fun d -> d + "\\places.sqlite")
|> Array.filter(fun d -> fileExistsAtLocation d)
|false -> [||]
let private extractFirefoxHistory (path: string) : FirefoxHistory =
match fileExistsAtLocation path with //Leaving the check because whatever
|true ->
let rgx = createMatchRegex @"(http|ftp|https|file)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-]\s)?"
let res = yieldLineSequence path |> Seq.map (matchStringRegex rgx) |> Seq.toList |> List.filter(fun l -> not(l = ""))
{path = path; url = res}
|false -> {path = ""; url = []}
let triageFirefox path : FettersFilesystemRecord =
let fpath = getFirefoxProfiles path |> Array.map extractFirefoxHistory |> Array.toList
{history = fpath} |> FettersFilesystemRecord.FirefoxInfo
///////////////////
//Event Enumeration
///////////////////
let getEventLog4624 (week: DateTime) (now: DateTime) : FettersSpecialRecord list =
let query =
sprintf "*[System/EventID=4624] and *[System[TimeCreated[@SystemTime >= '%s']]] and *[System[TimeCreated[@SystemTime <= '%s']]]"
(week.ToUniversalTime().ToString("o"))
(now.ToUniversalTime().ToString("o"))
createEventQuery "Security" query
|> createEventLogReader
|> extractEventLogs
|> Seq.filter(fun f -> not((snd f).[8] = "7")) // Don't really care about unlock events
|> Seq.map(fun ev ->
let tstamp, e = ev
{eventId = 4624us
timeStamp = tstamp
subjectSID = e.[0]
subjectUsername = e.[1]
subjectDomainname = e.[2]
subjectLogonId = e.[3]
targetUserSID = e.[4]
targetUsername = e.[5]
targetDomainname = e.[6]
logonType = e.[8]
workstationName = e.[11]
processName = e.[17]
ipAddress = e.[18]
} |> FettersSpecialRecord.Event4624)
|> Seq.toList
let getEventLog4648 (week: DateTime) (now: DateTime) : FettersSpecialRecord list =
let query =
sprintf "*[System/EventID=4648] and *[System[TimeCreated[@SystemTime >= '%s']]] and *[System[TimeCreated[@SystemTime <= '%s']]]"
(week.ToUniversalTime().ToString("o"))
(now.ToUniversalTime().ToString("o"))
createEventQuery "Security" query
|> createEventLogReader
|> extractEventLogs
|> Seq.map(fun ev ->
let tstamp, e = ev
{eventId = 4648us
timeStamp = tstamp
subjectSID = e.[0]
subjectUsername = e.[1]
subjectDomainname = e.[2]
subjectLogonId = e.[3]
targetUsername = e.[5]
targetDomainname = e.[6]
targetServername = e.[7]
processName = e.[10]
ipAddress = e.[11]
} |> FettersSpecialRecord.Event4648)
|> Seq.toList
///////////////////////////
//Firewall Rule Enumeration
///////////////////////////
let private retrieveFirewallRules (onlyDeny: bool) : FirewallRule list =
let rawRules = getRawRules ()
let filteredRules =
match onlyDeny with
|true -> denyOnlyFilter rawRules
|false -> allowFilter rawRules
filteredRules
|> List.map(fun fR ->
let pList = firewallPropertyNames |> List.map (getFirewallAttr fR)
{name = pList.[0]
description = pList.[1]
protocol = pList.[2]
applicationName = pList.[3]
localAddresses = pList.[4]
localPorts = pList.[5]
remoteAddresses = pList.[6]
remotePorts = pList.[7]
direction = pList.[8]
profiles = pList.[9]
})
let getFirewallRules denyOnly : FettersSpecialRecord =
match denyOnly with
|true -> {profile = createFirewallObj() |> getFProfileProperty |> string
rules = retrieveFirewallRules true} |> FettersSpecialRecord.Firewall
|false -> {profile = createFirewallObj() |> getFProfileProperty |> string
rules = retrieveFirewallRules false} |> FettersSpecialRecord.Firewall
/////////////////////
//Secrets Enumeration
/////////////////////
let getDPAPIMasterKeys userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Roaming\\Microsoft\\Protect\\")
|> Array.filter dirExistsAtLocation
|> Array.map listChildDirectories
|> Array.concat
|> Array.map listChildFiles
|> Array.concat
|> Array.filter(fun f ->
let r = f.Split '\\'
not(r |> Array.last = "Preferred"))
|> Array.map(fun d ->
let token = d.Split('\\')
let sid = token.[token.Length-2]
{userSID = sid; encodedBlob = encodeEntireFileB64 d}
|> Credential.DPAPIMasterKey |> FettersFilesystemRecord.Credential)
|> Array.toList
let getDPAPICredFiles userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Local\\Microsoft\\Credentials\\")
|> Array.filter dirExistsAtLocation
|> Array.map listChildFiles
|> Array.concat
|> Array.map(fun p ->
let mguid = new Guid(getByteSection 36L 16 p)
let strlen = BitConverter.ToInt32((getByteSection 56L 4 p), 0)
let credtype = Encoding.Unicode.GetString(getByteSection 60L (strlen - 6) p)
{path = p; description = credtype; encodedBlob = encodeEntireFileB64 p }
|> Credential.DPAPICredFile |> FettersFilesystemRecord.Credential)
|> Array.toList
let detectRDCManFile userFolders : string list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Local\\Microsoft\\Remote Desktop Connection Manager\\RDCMan.settings")
|> Array.filter fileExistsAtLocation
|> Array.map (sprintf "Remote Desktop Manager connection file exists at %s")
|> Array.toList
let getGoogleCloudCreds userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Roaming\\gcloud\\credentials.db")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{GoogleCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.GoogleCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getGoogleCloudCredsL userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Roaming\\gcloud\\legacy_credentials.db")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{GoogleCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.GoogleCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getGoogleAccessTokens userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\AppData\\Roaming\\gcloud\\access_tokens.db")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{GoogleCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.GoogleCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getAzureTokens userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\.azure\\accessTokens.json")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{AzureCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.AzureCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getAzureProfile userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\.azure\\accessProfile.json")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{AzureCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.AzureCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getAWSCreds userFolders : FettersFilesystemRecord list =
userFolders
|> Array.map (sprintf "%s\\.aws\\credentials")
|> Array.filter fileExistsAtLocation
|> Array.map(fun f ->
{AWSCredential.path = f; encodedFile = encodeEntireFileB64 f}
|> Credential.AWSCredential |> FettersFilesystemRecord.Credential)
|> Array.toList
let getSystemEnvVariables () : FettersSpecialRecord list =
let evDict = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine)
[for key in evDict.Keys do
{environmentKey = unbox<string> key; environmentVal = unbox<string> evDict.[key]}
|> FettersSpecialRecord.EnvironmentVar]
let getUserEnvVariables () : FettersSpecialRecord list =
let evDict = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)
[for key in evDict.Keys do
{environmentKey = unbox<string> key; environmentVal = unbox<string> evDict.[key]}
|> FettersSpecialRecord.EnvironmentVar]