-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
391 lines (342 loc) · 17.8 KB
/
Program.cs
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Amazon;
using Amazon.GuardDuty;
using Amazon.GuardDuty.Model;
using Amazon.Runtime;
using Amazon.Runtime.CredentialManagement;
using CommandLine;
using Newtonsoft.Json;
namespace guardduty_stix
{
class Program
{
private static readonly SHA256Managed sha256managed = new SHA256Managed();
public class Options
{
[Option('p', "profile", Required = false, HelpText = "Connect to the AWS account with the credential stored in a named profile")]
public string Profile { get; set; }
[Option('k', "key", Required = false, HelpText = "Instead of a profile, use the specified AWS access key")]
public string AccessKeyId { get; set; }
[Option('s', "secret", Required = false, HelpText = "Instead of a profile, use the specified AWS access secret")]
public string AccessKeySecret { get; set; }
[Option('r', "region", Required = false, HelpText = "Instead of a profile, use the specified AWS region", Default = "us-east-1")]
public string Region { get; set; }
[Option('o', "output", Required = false, HelpText = "Instead of dumping to stdout, save to the specified file")]
public string OutputFile { get; set; }
}
private static string[] titleBanner = new string[] {
@" ______ ______ __ ",
@" / ____/_ ______ __________/ / __ \__ __/ /___ __",
@" / / __/ / / / __ `/ ___/ __ / / / / / / / __/ / / /",
@"/ /_/ / /_/ / /_/ / / / /_/ / /_/ / /_/ / /_/ /_/ / ",
@"\____/\________________\____/_____/\________/\__, / ",
@" / ___/_ __/ _/ |/ / |__ \ / __ \ /____/ ",
@" \__ \ / / / / | / __/ / / / / / ",
@" ___/ // / _/ / / | / __/_/ /_/ / ",
@" /____//_/ /___//_/|_| /____(_)____/ ver 1.0.1 ",
@"",
@"A program to turn GuardDuty findings from the AWS API",
@" into compliant STIX 2.0 ",
@"",
@"Copyright Sean McElroy 2018 <me@seanmcelroy.com> ",
@" Released under the terms of the MIT License. ",
@""
};
private static string[] helpScreen = new string[] {
@"This program converts AWS GuardDuty findings into STIX 2.0",
@"",
@" Command line arguments: ",
@" --profile=PROFILE_NAME Connect to the AWS account with the credential stored in a named profile",
@" --key=ACCESS_KEY_ID Instead of a profile, use the specified AWS access key",
@" --secret=ACCESS_KEY_SECRET Instead of a profile, use the specified AWS access secret",
@" --region=AWS-REGION-1 Specify the region for the connection. Required if profile not specified",
@"",
@" --output=FILE_PATH If specified, will save output to specified file; otherwise, to stdout",
@"",
};
static int Main(string[] args)
{
foreach (var line in titleBanner)
Console.WriteLine(line);
if (args == null || args.Length == 0)
{
// Help screen
foreach (var line in helpScreen)
Console.WriteLine(line);
System.Environment.Exit(-1);
}
Options options = null;
Parser.Default.ParseArguments<Options>(args)
.WithParsed(o => options = o)
.WithNotParsed(errors =>
{
foreach (var error in errors)
Console.WriteLine(error);
System.Environment.Exit(-2);
});
// Setup AWS credentials
var chain = new CredentialProfileStoreChain();
AWSCredentials awsCredentials;
RegionEndpoint awsRegion;
if (!string.IsNullOrWhiteSpace(options.Profile))
{
if (!chain.TryGetAWSCredentials(options.Profile, out awsCredentials))
{
Console.WriteLine($"Unable to retrieve credentials for profile {options.Profile}");
System.Environment.Exit(-3);
return -3;
}
CredentialProfile credentialProfile;
if (!chain.TryGetProfile(options.Profile, out credentialProfile))
{
Console.WriteLine($"Unable to retrieve credential profile for {options.Profile}");
System.Environment.Exit(-4);
return -4;
}
awsRegion = credentialProfile.Region ?? RegionEndpoint.GetBySystemName(options.Region);
}
else
{
if (string.IsNullOrWhiteSpace(options.AccessKeyId))
{
Console.Error.WriteLine("No profile was specified, but an access key ID was not provided either.");
System.Environment.Exit(-5);
return -5;
}
if (string.IsNullOrWhiteSpace(options.AccessKeySecret))
{
Console.Error.WriteLine("No profile was specified, but an access key secret was not provided either.");
System.Environment.Exit(-6);
return -6;
}
awsCredentials = new BasicAWSCredentials(options.AccessKeyId, options.AccessKeySecret);
awsRegion = RegionEndpoint.GetBySystemName(options.Region);
}
var cts = new CancellationTokenSource();
var getFindingsTask = Task.Run(new Func<Task<Tuple<object, Exception>>>(async () =>
{
var client = new AmazonGuardDutyClient(awsCredentials, awsRegion);
var detectorRequest = new ListDetectorsRequest();
var detectorResponse = await client.ListDetectorsAsync(detectorRequest, cts.Token);
dynamic bundle = new ExpandoObject();
bundle.type = "bundle";
bundle.id = $"guardduty-stix-{DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture)}";
bundle.spec_version = "2.0";
var objects = new List<object>();
foreach (var detectorId in detectorResponse.DetectorIds)
{
var listFindingsRequest = new ListFindingsRequest()
{
DetectorId = detectorId,
/*FindingCriteria = new FindingCriteria
{
Criterion = { { "service.archived", new Condition { Eq = { "FALSE" } } } }
}*/
};
try
{
// Get list of findings
var listFindingsResponse = await client.ListFindingsAsync(listFindingsRequest, cts.Token);
// For the list, get the details
var getFindingsRequest = new GetFindingsRequest()
{
DetectorId = detectorId,
FindingIds = listFindingsResponse.FindingIds
};
var getFindingsResponse = await client.GetFindingsAsync(getFindingsRequest, cts.Token);
foreach (var finding in getFindingsResponse.Findings)
{
var sdo = await ConvertFindingToStixAsync(finding);
objects.Add(sdo);
}
}
catch (Exception e)
{
await Console.Error.WriteLineAsync(e.ToString());
return new Tuple<object, Exception>(null, e);
}
}
bundle.objects = objects;
return new Tuple<object, Exception>(bundle, null);
}));
if (!Task.WaitAll(new[] { getFindingsTask }, 60000, cts.Token))
{
Console.Error.WriteLine("Failed to complete within 60 seconds, aborted.");
System.Environment.Exit(-7);
return -7;
}
var result = getFindingsTask.Result;
if (result.Item2 != null)
{
Console.Error.WriteLine($"Unable to parse output: {result.Item2.ToString()}");
System.Environment.Exit(-8);
return -8;
}
if (string.IsNullOrWhiteSpace(options.OutputFile))
Console.Out.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(result.Item1));
else
{
try
{
using (var fs = new FileStream(options.OutputFile, FileMode.Create, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
sw.Write(Newtonsoft.Json.JsonConvert.SerializeObject(result.Item1));
}
Console.Out.WriteLine($"Output saved to file {options.OutputFile}");
}
catch (Exception e)
{
Console.Error.WriteLine($"Unable to write file: {e.ToString()}");
System.Environment.Exit(-9);
return -9;
}
}
return 0;
}
private static async Task<object> ConvertFindingToStixAsync(Finding finding)
{
// UUID should be deterministically determined from finding.Id
var bytes = Encoding.UTF8.GetBytes(finding.Id);
var hash = sha256managed.ComputeHash(bytes);
var uuid = new Guid(hash.Take(16).ToArray());
var labels = new object[0];
dynamic ret = new ExpandoObject();
ret.id = $"indicator--{uuid}";
ret.type = "indicator";
if (finding.Title != null)
ret.name = finding.Title;
if (finding.Description != null)
ret.description = finding.Description;
if (finding.CreatedAt != null && DateTime.TryParse(finding.CreatedAt, out DateTime dateCreatedAt))
{
ret.valid_from = dateCreatedAt.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture);
ret.created = ret.valid_from;
}
if (finding.UpdatedAt != null && DateTime.TryParse(finding.UpdatedAt, out DateTime dateUpdatedAt))
ret.modified = dateUpdatedAt.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", System.Globalization.CultureInfo.InvariantCulture);
ret.external_references = new[]
{
new {
source_name = "guardduty",
description = "Amazon Web Services GuardDuty Finding ID",
external_id = finding.Id
},
new {
source_name = "guardduty",
description = "Amazon Web Services GuardDuty Finding ARN",
external_id = finding.Arn
}
};
if (finding.Description != null && finding.Description.IndexOf("registered to an unusual organization") > -1)
ret.labels = new[] { "anomalous-activity" };
else if (finding.Description != null && finding.Description.IndexOf("under unusual circumstances") > -1)
ret.labels = new[] { "anomalous-activity" };
else if (finding.Description != null && finding.Description.IndexOf("is performing outbound port scans against remote host") > -1)
ret.labels = new[] { "compromised" };
else if (finding.Description != null && finding.Description.IndexOf("known malicious") > -1)
ret.labels = new[] { "malicious-activity" };
else if (finding.Description != null && finding.Description.IndexOf("specific location has not been seen before") > -1)
ret.labels = new[] { "benign" };
else
{
await Console.Error.WriteLineAsync("No known label for description.");
}
if (finding.Type != null && finding.Type.StartsWith("Recon:"))
{
ret.kill_chain_phases = new[] {
new {
kill_chain_name = "lockheed-martin-cyber-kill-chain",
phase_name = "reconnaissance"
}
};
}
var sbPattern = new StringBuilder("[");
if (finding.Resource.AccessKeyDetails != null)
sbPattern.Append($"(user-account:account_type = 'aws' AND user-account:user_id = '{finding.Resource.AccessKeyDetails.AccessKeyId ?? finding.Resource.AccessKeyDetails.UserName}' AND user-account:account_login = '{finding.Resource.AccessKeyDetails.UserName.Replace("\'", "")}')");
if (finding.Resource.InstanceDetails != null)
{
if (finding.Service.Action.PortProbeAction != null)
{
var subPattern = new StringBuilder();
subPattern.Append('(');
var nicCount = 0;
foreach (var nic in finding.Resource.InstanceDetails.NetworkInterfaces)
{
nicCount++;
if (nicCount > 1)
subPattern.Append(" OR ");
subPattern.Append($"({(subPattern.Length > 2 ? " AND " : string.Empty)}(network-traffic:dst_ref.type = 'ipv4-addr' AND network-traffic:dst_ref.value = '{nic.PublicIp ?? nic.PrivateIpAddress}/32'))");
}
if (nicCount < 2)
{
subPattern.Remove(0, 2);
subPattern.Remove(subPattern.Length - 2, 1);
}
else
subPattern.Append(')');
sbPattern.Append(subPattern);
}
else if (finding.Title.StartsWith("Outbound portscan from EC2 instance"))
{
var subPattern = new StringBuilder();
subPattern.Append('(');
var nicCount = 0;
foreach (var nic in finding.Resource.InstanceDetails.NetworkInterfaces)
{
nicCount++;
if (nicCount > 1)
subPattern.Append(" OR ");
subPattern.Append($"({(subPattern.Length > 2 ? " AND " : string.Empty)}(network-traffic:src_ref.type = 'ipv4-addr' AND network-traffic:src_ref.value = '{nic.PublicIp ?? nic.PrivateIpAddress}/32'))");
}
if (nicCount < 2)
{
subPattern.Remove(0, 2);
subPattern.Remove(subPattern.Length - 2, 1);
}
else
subPattern.Append(')');
sbPattern.Append(subPattern);
}
else
await Console.Error.WriteLineAsync("No known pattern for instance details.");
}
if (finding.Service.Action.AwsApiCallAction != null)
{
var remote = finding.Service.Action.AwsApiCallAction.RemoteIpDetails;
if (remote != null)
sbPattern.Append($"{(sbPattern.Length > 1 ? " AND " : string.Empty)}(network-traffic:src_ref.type = 'ipv4-addr' AND network-traffic:src_ref.value = '{remote.IpAddressV4}/32')");
else
await Console.Error.WriteLineAsync("No known pattern for AWS API call action.");
}
if (finding.Service.Action.NetworkConnectionAction != null)
{
var nca = finding.Service.Action.NetworkConnectionAction;
if (nca.RemoteIpDetails != null)
sbPattern.Append($"{(sbPattern.Length > 1 ? " AND " : string.Empty)}(network-traffic:src_ref.type = 'ipv4-addr' AND network-traffic:src_ref.value = '{nca.RemoteIpDetails.IpAddressV4}/32')");
else
await Console.Error.WriteLineAsync("No known pattern for network connection action.");
}
if (finding.Service.Action.PortProbeAction != null)
{
foreach (var probDetail in finding.Service.Action.PortProbeAction.PortProbeDetails)
{
var org = probDetail.RemoteIpDetails.Organization;
var asn = org == null ? "" : $"autonomous-system:number = {org.Asn} AND autonomous-system:name = '{org.AsnOrg.Replace("\'", "")}' AND ";
sbPattern.Append($"{(sbPattern.Length > 1 ? " AND " : string.Empty)}({asn}network-traffic:src_ref.type = 'ipv4-addr' AND network-traffic:src_ref.value = '{probDetail.RemoteIpDetails.IpAddressV4}/32')");
}
}
if (sbPattern.Length > 1)
ret.pattern = sbPattern.Append("]").ToString();
return ret;
}
}
}