-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathAlertSrv.scala
475 lines (427 loc) · 19.5 KB
/
AlertSrv.scala
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package services
import java.nio.file.Files
import scala.collection.immutable
import scala.concurrent.{ExecutionContext, Future}
import scala.util.matching.Regex
import scala.util.{Failure, Success, Try}
import play.api.libs.json._
import play.api.{Configuration, Logger}
import akka.NotUsed
import akka.stream.Materializer
import akka.stream.scaladsl.{Sink, Source}
import connectors.ConnectorRouter
import javax.inject.{Inject, Singleton}
import models._
import org.elastic4play.controllers.{Fields, FileInputValue}
import org.elastic4play.database.ModifyConfig
import org.elastic4play.services.JsonFormat.attachmentFormat
import org.elastic4play.services.QueryDSL.{groupByField, parent, selectCount, withId}
import org.elastic4play.services._
import org.elastic4play.utils.Collection
import org.elastic4play.{ConflictError, InternalError}
trait AlertTransformer {
def createCase(alert: Alert, customCaseTemplate: Option[String])(implicit authContext: AuthContext): Future[Case]
def mergeWithCase(alert: Alert, caze: Case)(implicit authContext: AuthContext): Future[Case]
}
case class CaseSimilarity(caze: Case, similarIOCCount: Int, iocCount: Int, similarArtifactCount: Int, artifactCount: Int)
object AlertSrv {
val dataExtractor: Regex = "^(.*);(.*);(.*)".r
}
@Singleton
class AlertSrv(
templates: Map[String, String],
alertModel: AlertModel,
createSrv: CreateSrv,
getSrv: GetSrv,
updateSrv: UpdateSrv,
deleteSrv: DeleteSrv,
findSrv: FindSrv,
caseSrv: CaseSrv,
artifactSrv: ArtifactSrv,
caseTemplateSrv: CaseTemplateSrv,
attachmentSrv: AttachmentSrv,
connectors: ConnectorRouter,
hashAlg: Seq[String],
implicit val ec: ExecutionContext,
implicit val mat: Materializer
) extends AlertTransformer {
@Inject() def this(
configuration: Configuration,
alertModel: AlertModel,
createSrv: CreateSrv,
getSrv: GetSrv,
updateSrv: UpdateSrv,
deleteSrv: DeleteSrv,
findSrv: FindSrv,
caseSrv: CaseSrv,
artifactSrv: ArtifactSrv,
caseTemplateSrv: CaseTemplateSrv,
attachmentSrv: AttachmentSrv,
connectors: ConnectorRouter,
ec: ExecutionContext,
mat: Materializer
) =
this(
Map.empty[String, String],
alertModel: AlertModel,
createSrv,
getSrv,
updateSrv,
deleteSrv,
findSrv,
caseSrv,
artifactSrv,
caseTemplateSrv,
attachmentSrv,
connectors,
(configuration.get[String]("datastore.hash.main") +: configuration.get[Seq[String]]("datastore.hash.extra")).distinct,
ec,
mat
)
private[AlertSrv] lazy val logger = Logger(getClass)
import AlertSrv._
def create(fields: Fields)(implicit authContext: AuthContext): Future[Alert] = {
val artifactsFields =
Future.traverse(fields.getValues("artifacts")) {
case a: JsObject if (a \ "dataType").asOpt[String].contains("file") ⇒
(a \ "data").asOpt[String] match {
case Some(dataExtractor(filename, contentType, data)) ⇒
attachmentSrv
.save(filename, contentType, java.util.Base64.getDecoder.decode(data))
.map(attachment ⇒ a - "data" + ("attachment" → Json.toJson(attachment)))
case _ ⇒ Future.successful(a)
}
case a ⇒ Future.successful(a)
}
artifactsFields.flatMap { af ⇒
/* remove duplicate artifacts */
val distinctArtifacts = Collection.distinctBy(af) { a ⇒
val data = (a \ "data").asOpt[String]
val attachment = (a \ "attachment" \ "id").asOpt[String]
val dataType = (a \ "dataType").asOpt[String]
data.orElse(attachment).map(_ → dataType).getOrElse(a)
}
createSrv[AlertModel, Alert](alertModel, fields.set("artifacts", JsArray(distinctArtifacts)))
}
}
def bulkCreate(fieldSet: Seq[Fields])(implicit authContext: AuthContext): Future[Seq[Try[Alert]]] =
createSrv[AlertModel, Alert](alertModel, fieldSet)
def get(id: String): Future[Alert] =
getSrv[AlertModel, Alert](alertModel, id)
def get(tpe: String, source: String, sourceRef: String): Future[Option[Alert]] = {
import org.elastic4play.services.QueryDSL._
findSrv[AlertModel, Alert](alertModel, and("type" ~= tpe, "source" ~= source, "sourceRef" ~= sourceRef), Some("0-1"), Nil)
._1
.runWith(Sink.headOption)
}
def update(id: String, fields: Fields)(implicit authContext: AuthContext): Future[Alert] =
update(id, fields, ModifyConfig.default)
def update(id: String, fields: Fields, modifyConfig: ModifyConfig)(implicit authContext: AuthContext): Future[Alert] =
for {
alert ← get(id)
updatedAlert ← update(alert, fields, modifyConfig)
} yield updatedAlert
def update(alert: Alert, fields: Fields)(implicit authContext: AuthContext): Future[Alert] =
update(alert, fields, ModifyConfig.default)
def update(alert: Alert, fields: Fields, modifyConfig: ModifyConfig)(implicit authContext: AuthContext): Future[Alert] = {
val follow = fields.getBoolean("follow").getOrElse(alert.follow())
val newStatus = if (follow && alert.status() != AlertStatus.New) AlertStatus.Updated else alert.status()
val updatedAlert = updateSrv(alert, fields.set("status", Json.toJson(newStatus)), modifyConfig)
alert.caze() match {
case Some(caseId) if follow ⇒
for {
caze ← caseSrv.get(caseId)
a ← updatedAlert
_ ← importArtifacts(a, caze)
_ ← caseSrv.update(caze, Fields.empty.set("status", CaseStatus.Open.toString))
} yield a
case _ ⇒ updatedAlert
}
}
def bulkUpdate(ids: Seq[String], fields: Fields)(implicit authContext: AuthContext): Future[Seq[Try[Alert]]] =
bulkUpdate(ids, fields, ModifyConfig.default)
def bulkUpdate(ids: Seq[String], fields: Fields, modifyConfig: ModifyConfig)(implicit authContext: AuthContext): Future[Seq[Try[Alert]]] =
updateSrv[AlertModel, Alert](alertModel, ids, fields, modifyConfig)
def bulkUpdate(updates: Seq[(Alert, Fields)])(implicit authContext: AuthContext): Future[Seq[Try[Alert]]] =
bulkUpdate(updates, ModifyConfig.default)
def bulkUpdate(updates: Seq[(Alert, Fields)], modifyConfig: ModifyConfig)(implicit authContext: AuthContext): Future[Seq[Try[Alert]]] =
updateSrv[Alert](updates, modifyConfig)
def markAsRead(alert: Alert, modifyConfig: ModifyConfig = ModifyConfig.default)(implicit authContext: AuthContext): Future[Alert] =
alert.caze() match {
case Some(_) ⇒ updateSrv[AlertModel, Alert](alertModel, alert.id, Fields.empty.set("status", "Imported"), modifyConfig)
case None ⇒ updateSrv[AlertModel, Alert](alertModel, alert.id, Fields.empty.set("status", "Ignored"), modifyConfig)
}
def markAsUnread(alert: Alert, modifyConfig: ModifyConfig = ModifyConfig.default)(implicit authContext: AuthContext): Future[Alert] =
alert.caze() match {
case Some(_) ⇒ updateSrv[AlertModel, Alert](alertModel, alert.id, Fields.empty.set("status", "Updated"), modifyConfig)
case None ⇒ updateSrv[AlertModel, Alert](alertModel, alert.id, Fields.empty.set("status", "New"), modifyConfig)
}
def getCaseTemplate(alert: Alert, customCaseTemplate: Option[String]): Future[Option[CaseTemplate]] =
customCaseTemplate.fold[Future[Option[CaseTemplate]]](Future.successful(None)) { templateName ⇒
caseTemplateSrv
.getByName(templateName)
.map { ct ⇒
Some(ct)
}
.recover { case _ ⇒ None }
}
def createCase(alert: Alert, customCaseTemplate: Option[String])(implicit authContext: AuthContext): Future[Case] =
alert.caze() match {
case Some(id) ⇒ caseSrv.get(id)
case None ⇒
connectors.get(alert.tpe()) match {
case Some(connector: AlertTransformer) ⇒
for {
caze ← connector.createCase(alert, customCaseTemplate)
_ ← setCase(alert, caze)
} yield caze
case _ ⇒
for {
caseTemplate ← getCaseTemplate(alert, customCaseTemplate)
caze ← caseSrv.create(
Fields
.empty
.set("title", alert.title())
.set("description", alert.description())
.set("severity", JsNumber(alert.severity()))
.set("tags", JsArray(alert.tags().map(JsString)))
.set("tlp", JsNumber(alert.tlp()))
.set("status", CaseStatus.Open.toString)
.set("startDate", Json.toJson(alert.date()))
.set("customFields", alert.customFields()),
caseTemplate
)
_ ← importArtifacts(alert, caze)
_ ← setCase(alert, caze)
} yield caze
}
}
override def mergeWithCase(alert: Alert, caze: Case)(implicit authContext: AuthContext): Future[Case] =
alert.caze() match {
case Some(id) ⇒ caseSrv.get(id)
case None ⇒
connectors.get(alert.tpe()) match {
case Some(connector: AlertTransformer) ⇒
for {
updatedCase ← connector.mergeWithCase(alert, caze)
_ ← setCase(alert, updatedCase)
} yield updatedCase
case _ ⇒
for {
_ ← importArtifacts(alert, caze)
newDescription = caze
.description() + s"\n \n#### Merged with alert #${alert.sourceRef()} ${alert.title()}\n\n${alert.description().trim}"
newTags = (caze.tags() ++ alert.tags()).distinct.map(JsString.apply)
updatedCase ← caseSrv.update(
caze,
Fields
.empty
.set("description", newDescription)
.set("tags", JsArray(newTags))
)
_ ← setCase(alert, caze)
} yield updatedCase
}
}
def bulkMergeWithCase(alerts: Seq[Alert], caze: Case)(implicit authContext: AuthContext): Future[Case] =
Future
.traverse(alerts) { alert ⇒
for {
_ ← importArtifacts(alert, caze)
_ ← setCase(alert, caze)
} yield ()
}
.flatMap { _ ⇒ // then merge all tags
val newTags = (caze.tags() ++ alerts.flatMap(_.tags())).distinct.map(JsString.apply)
val newDescription = caze.description() + alerts
.map(alert ⇒ s"\n \n#### Merged with alert #${alert.sourceRef()} ${alert.title()}\n\n${alert.description().trim}")
.mkString("")
caseSrv.update(
caze,
Fields
.empty
.set("description", newDescription)
.set("tags", JsArray(newTags))
)
}
def importArtifacts(alert: Alert, caze: Case)(implicit authContext: AuthContext): Future[Case] = {
val artifactsFields = alert
.artifacts()
.map { artifact ⇒
val tags = (artifact \ "tags").asOpt[Seq[JsString]].getOrElse(Nil) :+ JsString("src:" + alert.tpe())
val message = (artifact \ "message").asOpt[JsString].getOrElse(JsString(""))
val artifactFields = Fields(
artifact +
("tags" → JsArray(tags)) +
("message" → message)
)
if (artifactFields.getString("dataType").contains("file")) {
artifactFields
.getString("data")
.map {
case dataExtractor(filename, contentType, data) ⇒
val f = Files.createTempFile("alert-", "-attachment")
Files.write(f, java.util.Base64.getDecoder.decode(data))
artifactFields
.set("attachment", FileInputValue(filename, f, contentType))
.unset("data")
case data ⇒
logger.warn(s"Invalid data format for file artifact: $data")
artifactFields
}
.getOrElse(artifactFields)
} else {
artifactFields
}
}
val updatedCase = artifactSrv
.create(caze, artifactsFields)
.flatMap { artifacts ⇒
Future.traverse(artifacts) {
case Success(_) ⇒ Future.successful(())
case Failure(ConflictError(_, attributes)) ⇒ // if it already exists, add tags from alert
import org.elastic4play.services.QueryDSL._
(for {
dataType ← (attributes \ "dataType").asOpt[String]
data = (attributes \ "data").asOpt[String]
attachment = (attributes \ "attachment").asOpt[Attachment]
tags ← (attributes \ "tags").asOpt[Seq[String]]
_ ← data orElse attachment
dataOrAttachment = data.toLeft(attachment.get)
} yield artifactSrv
.find(artifactSrv.similarArtifactFilter(dataType, dataOrAttachment, withParent(caze)), None, Nil)
._1
.mapAsyncUnordered(1) { artifact ⇒
artifactSrv.update(artifact.id, Fields.empty.set("tags", JsArray((artifact.tags() ++ tags).distinct.map(JsString.apply))))
}
.map(_ ⇒ caze)
.runWith(Sink.ignore)
.map(_ ⇒ caze))
.getOrElse {
logger.warn(s"A conflict error occurs when creating the artifact $attributes but it doesn't exist")
Future.successful(())
}
case Failure(e) ⇒
logger.warn("Create artifact error", e)
Future.successful(())
}
}
.map(_ ⇒ caze)
updatedCase.onComplete { _ ⇒
// remove temporary files
artifactsFields
.flatMap(_.get("Attachment"))
.foreach {
case FileInputValue(_, file, _) ⇒ Files.delete(file)
case _ ⇒
}
}
updatedCase
}
def setCase(alert: Alert, caze: Case, modifyConfig: ModifyConfig = ModifyConfig.default)(implicit authContext: AuthContext): Future[Alert] =
updateSrv(alert, Fields(Json.obj("case" → caze.id, "status" → AlertStatus.Imported)), modifyConfig)
def unsetCase(alert: Alert, modifyConfig: ModifyConfig = ModifyConfig.default)(implicit authContext: AuthContext): Future[Alert] = {
val status = alert.status match {
case AlertStatus.New ⇒ AlertStatus.New
case AlertStatus.Updated ⇒ AlertStatus.New
case AlertStatus.Ignored ⇒ AlertStatus.Ignored
case AlertStatus.Imported ⇒ AlertStatus.Ignored
}
logger.debug(s"Remove case association in alert ${alert.id} (${alert.title}")
updateSrv(alert, Fields(Json.obj("case" → JsNull, "status" → status)), modifyConfig)
}
def delete(id: String, force: Boolean)(implicit authContext: AuthContext): Future[Unit] =
if (force) deleteSrv.realDelete[AlertModel, Alert](alertModel, id)
else get(id).flatMap(alert ⇒ markAsUnread(alert)).map(_ ⇒ ())
def find(queryDef: QueryDef, range: Option[String], sortBy: Seq[String]): (Source[Alert, NotUsed], Future[Long]) =
findSrv[AlertModel, Alert](alertModel, queryDef, range, sortBy)
def stats(queryDef: QueryDef, aggs: Seq[Agg]): Future[JsObject] = findSrv(alertModel, queryDef, aggs: _*)
def setFollowAlert(alertId: String, follow: Boolean, modifyConfig: ModifyConfig = ModifyConfig.default)(
implicit authContext: AuthContext
): Future[Alert] =
updateSrv[AlertModel, Alert](alertModel, alertId, Fields(Json.obj("follow" → follow)), modifyConfig)
def similarCases(alert: Alert): Future[Seq[CaseSimilarity]] = {
def similarArtifacts(artifact: JsObject): Option[Source[Artifact, NotUsed]] =
for {
dataType ← (artifact \ "dataType").asOpt[String]
data ← if (dataType == "file")
(artifact \ "attachment").asOpt[Attachment].map(Right.apply)
else
(artifact \ "data").asOpt[String].map(Left.apply)
} yield artifactSrv.findSimilar(dataType, data, None, Some("all"), Nil)._1
Source(alert.artifacts().to[immutable.Iterable])
.flatMapConcat { artifact ⇒
similarArtifacts(artifact)
.getOrElse(Source.empty)
}
.fold(Map.empty[String, (Int, Int)]) { (similarCases, artifact) ⇒
val caseId = artifact.parentId.getOrElse(sys.error(s"Artifact ${artifact.id} has no case !"))
val (iocCount, artifactCount) = similarCases.getOrElse(caseId, (0, 0))
if (artifact.ioc())
similarCases + (caseId → ((iocCount + 1, artifactCount)))
else
similarCases + (caseId → ((iocCount, artifactCount + 1)))
}
.mapConcat(identity)
.mapAsyncUnordered(5) {
case (caseId, (similarIOCCount, similarArtifactCount)) ⇒
caseSrv.get(caseId).map((_, similarIOCCount, similarArtifactCount))
}
.filter {
case (caze, _, _) ⇒ caze.status() != CaseStatus.Deleted && !caze.resolutionStatus().contains(CaseResolutionStatus.Duplicated)
}
.mapAsyncUnordered(5) {
case (caze, similarIOCCount, similarArtifactCount) ⇒
for {
artifactCountJs ← artifactSrv.stats(parent("case", withId(caze.id)), Seq(groupByField("ioc", selectCount)))
iocCount = (artifactCountJs \ "1" \ "count").asOpt[Int].getOrElse(0)
artifactCount = (artifactCountJs \\ "count").map(_.as[Int]).sum
} yield CaseSimilarity(caze, similarIOCCount, iocCount, similarArtifactCount, artifactCount)
case _ ⇒ Future.failed(InternalError("Case not found"))
}
.runWith(Sink.seq)
}
def getArtifactSeen(artifact: JsObject): Future[Long] = {
val maybeArtifactSeen = for {
dataType ← (artifact \ "dataType").asOpt[String]
data ← dataType match {
case "file" ⇒ (artifact \ "attachment").asOpt[Attachment].map(Right.apply)
case _ ⇒ (artifact \ "data").asOpt[String].map(Left.apply)
}
numberOfSimilarArtifacts = artifactSrv.findSimilar(dataType, data, None, None, Nil)._2
} yield numberOfSimilarArtifacts
maybeArtifactSeen.getOrElse(Future.successful(0L))
}
def alertArtifactsWithSeen(alert: Alert): Future[Seq[JsObject]] =
Future.traverse(alert.artifacts()) { artifact ⇒
getArtifactSeen(artifact).map(seen ⇒ artifact + ("seen" → JsNumber(seen)))
}
def fixStatus()(implicit authContext: AuthContext): Future[Unit] = {
import org.elastic4play.services.QueryDSL._
val updatedStatusFields = Fields.empty.set("status", "Updated")
val (updateAlerts, updateAlertCount) = find("status" ~= "Update", Some("all"), Nil)
updateAlertCount.foreach(c ⇒ logger.info(s"Updating $c alert with Update status"))
val updateAlertProcess = updateAlerts
.mapAsyncUnordered(3) { alert ⇒
logger.debug(s"Updating alert ${alert.id} (status: Update → Updated)")
update(alert, updatedStatusFields)
.andThen {
case Failure(error) ⇒ logger.warn(s"""Fail to set "Updated" status to alert ${alert.id}""", error)
}
}
val ignoredStatusFields = Fields.empty.set("status", "Ignored")
val (ignoreAlerts, ignoreAlertCount) = find("status" ~= "Ignore", Some("all"), Nil)
ignoreAlertCount.foreach(c ⇒ logger.info(s"Updating $c alert with Ignore status"))
val ignoreAlertProcess = ignoreAlerts
.mapAsyncUnordered(3) { alert ⇒
logger.debug(s"Updating alert ${alert.id} (status: Ignore → Ignored)")
update(alert, ignoredStatusFields)
.andThen {
case Failure(error) ⇒ logger.warn(s"""Fail to set "Ignored" status to alert ${alert.id}""", error)
}
}
(updateAlertProcess ++ ignoreAlertProcess)
.runWith(Sink.ignore)
.map(_ ⇒ ())
}
}