This repository was archived by the owner on Dec 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathMispSrv.scala
More file actions
339 lines (312 loc) · 13.3 KB
/
MispSrv.scala
File metadata and controls
339 lines (312 loc) · 13.3 KB
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
package connectors.misp
import java.util.Date
import javax.inject.{Inject, Provider, Singleton}
import scala.concurrent.{ExecutionContext, Future}
import play.api.Logger
import play.api.libs.json.JsLookupResult.jsLookupResultToJsLookup
import play.api.libs.json.JsValue.jsValueToJsLookup
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import play.api.libs.json._
import play.api.libs.ws.WSBodyWritables.writeableOf_JsValue
import akka.NotUsed
import akka.stream.Materializer
import akka.stream.scaladsl.{FileIO, Sink, Source}
import connectors.misp.JsonFormat._
import models._
import net.lingala.zip4j.core.ZipFile
import net.lingala.zip4j.exception.ZipException
import net.lingala.zip4j.model.FileHeader
import services._
import org.elastic4play.controllers.{Fields, FileInputValue}
import org.elastic4play.services.{Attachment, AuthContext, TempSrv}
import org.elastic4play.{InternalError, NotFoundError}
@Singleton
class MispSrv @Inject()(
mispConfig: MispConfig,
alertSrvProvider: Provider[AlertSrv],
caseSrv: CaseSrv,
artifactSrv: ArtifactSrv,
tempSrv: TempSrv,
implicit val ec: ExecutionContext,
implicit val mat: Materializer
) extends MispConverter {
private[misp] lazy val logger = Logger(getClass)
private[misp] lazy val alertSrv = alertSrvProvider.get
private[misp] def getInstanceConfig(name: String): Future[MispConnection] =
mispConfig
.connections
.find(_.name == name)
.fold(Future.failed[MispConnection](NotFoundError(s"""Configuration of MISP server "$name" not found"""))) { instanceConfig ⇒
Future.successful(instanceConfig)
}
def getEvent(mispConnection: MispConnection, eventId: String): Future[MispAlert] = {
logger.debug(s"Get MISP event $eventId")
require(!eventId.isEmpty)
mispConnection(s"events/$eventId")
.get()
.map { e ⇒
(e.json \ "Event")
.as[MispAlert]
.copy(source = mispConnection.name)
}
}
def getEventsFromDate(mispConnection: MispConnection, fromDate: Date): Source[MispAlert, NotUsed] = {
logger.debug(s"Get MISP events from $fromDate")
val date = fromDate.getTime / 1000
Source
.fromFuture {
mispConnection("events/index")
.post(Json.obj("searchpublish_timestamp" → date))
}
.mapConcat { response ⇒
val eventJson = Json
.parse(response.body)
.asOpt[Seq[JsValue]]
.getOrElse {
logger.warn(s"Invalid MISP event format:\n${response.body}")
Nil
}
val events = eventJson
.flatMap { j ⇒
j.asOpt[MispAlert]
.orElse {
logger.warn(s"MISP event can't be parsed\n$j")
None
}
.filterNot(mispConnection.isExcluded)
.map(_.copy(source = mispConnection.name))
}
val eventJsonSize = eventJson.size
val eventsSize = events.size
if (eventJsonSize != eventsSize)
logger.warn(s"MISP returns $eventJsonSize events but only $eventsSize contain valid data")
events.filter(_.lastSyncDate after fromDate).toList
}
}
def getAttributesFromCase(caze: Case): Future[Seq[ExportedMispAttribute]] = {
import org.elastic4play.services.QueryDSL._
artifactSrv
.find(and(withParent(caze), "status" ~= "Ok", "ioc" ~= true), Some("all"), Nil)
._1
.map { artifact ⇒
val (category, tpe) = fromArtifact(artifact.dataType(), artifact.data())
val value = (artifact.data(), artifact.attachment()) match {
case (Some(data), None) ⇒ Left(data)
case (None, Some(attachment)) ⇒ Right(attachment)
case _ ⇒
logger.error(s"Artifact $artifact has neither data nor attachment")
sys.error("???")
}
ExportedMispAttribute(artifact, tpe, category, artifact.tags(), artifact.tlp(), value, artifact.message())
}
.runWith(Sink.seq)
}
def getAttributesFromMisp(mispConnection: MispConnection, eventId: String, fromDate: Option[Date]): Future[Seq[MispArtifact]] = {
val date = fromDate.fold(0L)(_.getTime / 1000)
mispConnection(s"attributes/restSearch/json")
.post(Json.obj("request" → Json.obj("timestamp" → date, "eventid" → eventId)))
// add ("deleted" → 1) to see also deleted attributes
// add ("deleted" → "only") to see only deleted attributes
.map(_.body)
.map {
case body if mispConnection.maxSize.fold(false)(body.length > _) ⇒
logger.debug(s"Size of event exceeds (${body.length}) the configured limit")
JsObject.empty
case body ⇒ Json.parse(body)
}
.map { jsBody ⇒
val refDate = fromDate.getOrElse(new Date(0))
val artifactTags = s"src:${mispConnection.name}" +: mispConnection.artifactTags
(jsBody \ "response" \\ "Attribute")
.flatMap(_.as[Seq[MispAttribute]])
.filter(_.date after refDate)
.flatMap(convertAttribute)
.groupBy {
case MispArtifact(SimpleArtifactData(data), dataType, _, _, _, _) ⇒ dataType → Right(data)
case MispArtifact(RemoteAttachmentArtifact(filename, _, _), dataType, _, _, _, _) ⇒ dataType → Left(filename)
case MispArtifact(AttachmentArtifact(Attachment(filename, _, _, _, _)), dataType, _, _, _, _) ⇒ dataType → Left(filename)
}
.values
.map { mispArtifact ⇒
mispArtifact.head.copy(tags = (mispArtifact.head.tags ++ artifactTags).distinct, tlp = 2L)
}
.toSeq
}
}
def attributeToArtifact(mispConnection: MispConnection, attr: JsObject, defaultTlp: Long)(
implicit authContext: AuthContext
): Option[Future[Fields]] =
(for {
dataType ← (attr \ "dataType").validate[String]
data ← (attr \ "data").validateOpt[String]
message ← (attr \ "message").validate[String]
startDate ← (attr \ "startDate").validate[Date]
attachmentReference ← (attr \ "remoteAttachment" \ "reference").validateOpt[String]
attachmentType ← (attr \ "remoteAttachment" \ "type").validateOpt[String]
attachment = attachmentReference
.flatMap {
case ref if dataType == "file" ⇒ Some(downloadAttachment(mispConnection, ref))
case _ ⇒ None
}
.map {
case f if attachmentType.contains("malware-sample") ⇒ f.map(extractMalwareAttachment)
case f ⇒ f
}
tags = (attr \ "tags").asOpt[Seq[String]].getOrElse(Nil)
tlp = tags
.map(_.toLowerCase)
.collectFirst {
case "tlp:white" ⇒ JsNumber(0)
case "tlp:green" ⇒ JsNumber(1)
case "tlp:amber" ⇒ JsNumber(2)
case "tlp:red" ⇒ JsNumber(3)
}
.getOrElse(JsNumber(defaultTlp))
fields = Fields
.empty
.set("dataType", dataType)
.set("message", message)
.set("startDate", Json.toJson(startDate))
.set(
"tags",
JsArray(
tags
.filterNot(_.toLowerCase.startsWith("tlp:"))
.map(JsString)
)
)
.set("tlp", tlp)
if attachment.isDefined != data.isDefined
} yield attachment.fold(Future.successful(fields.set("data", data.get)))(_.map { fiv ⇒
fields.set("attachment", fiv)
})) match {
case JsSuccess(r, _) ⇒ Some(r)
case e: JsError ⇒
logger.warn(s"Invalid attribute format: $e\n$attr")
None
}
def createCase(alert: Alert, customCaseTemplate: Option[String])(implicit authContext: AuthContext): Future[Case] =
alert.caze() match {
case Some(id) ⇒ caseSrv.get(id)
case None ⇒
for {
caseTemplate ← alertSrv.getCaseTemplate(alert, customCaseTemplate)
caze ← caseSrv.create(Fields(alert.toCaseJson), caseTemplate)
_ ← importArtifacts(alert, caze)
} yield caze
}
def importArtifacts(alert: Alert, caze: Case)(implicit authContext: AuthContext): Future[Case] =
for {
instanceConfig ← getInstanceConfig(alert.source())
artifacts ← Future.sequence(alert.artifacts().flatMap(attributeToArtifact(instanceConfig, _, alert.tlp())))
_ ← artifactSrv.create(caze, artifacts)
} yield caze
def mergeWithCase(alert: Alert, caze: Case)(implicit authContext: AuthContext): Future[Case] =
for {
_ ← importArtifacts(alert, caze)
description = caze.description() + s"\n \n#### Merged with MISP event ${alert.title()}\n\n${alert.description().trim}"
updatedCase ← caseSrv.update(caze, Fields.empty.set("description", description))
} yield updatedCase
def updateMispAlertArtifact()(implicit authContext: AuthContext): Future[Unit] = {
import org.elastic4play.services.QueryDSL._
logger.info("Update MISP attributes in alerts")
val (alerts, _) = alertSrv.find("type" ~= "misp", Some("all"), Nil)
alerts
.mapAsyncUnordered(5) { alert ⇒
if (alert.artifacts().nonEmpty) {
logger.info(s"alert ${alert.id} has artifacts, ignore it")
Future.successful(alert → Nil)
} else {
getInstanceConfig(alert.source())
.flatMap { mcfg ⇒
getAttributesFromMisp(mcfg, alert.sourceRef(), None)
}
.map(alert → _)
.recover {
case NotFoundError(m) ⇒
logger.error(s"Retrieve MISP attribute of event ${alert.id} error: $m")
alert → Nil
case error ⇒
logger.error(s"Retrieve MISP attribute of event ${alert.id} error", error)
alert → Nil
}
}
}
.filterNot(_._2.isEmpty)
.mapAsyncUnordered(5) {
case (alert, artifacts) ⇒
logger.info(s"Updating alert ${alert.id}")
alertSrv
.update(alert.id, Fields.empty.set("artifacts", Json.toJson(artifacts)))
.recover {
case t ⇒ logger.error(s"Update alert ${alert.id} fail", t)
}
}
.runWith(Sink.ignore)
.map(_ ⇒ ())
}
def extractMalwareAttachment(file: FileInputValue)(implicit authContext: AuthContext): FileInputValue = {
import scala.collection.JavaConverters._
try {
val zipFile = new ZipFile(file.filepath.toFile)
if (zipFile.isEncrypted)
zipFile.setPassword("infected")
// Get the list of file headers from the zip file
val fileHeaders = zipFile.getFileHeaders.asScala.toList.asInstanceOf[List[FileHeader]]
val (fileNameHeaders, contentFileHeaders) = fileHeaders.partition { fileHeader ⇒
fileHeader.getFileName.endsWith(".filename.txt")
}
(for {
fileNameHeader ← fileNameHeaders
.headOption
.orElse {
logger.warn(s"Format of malware attribute ${file.name} is invalid : file containing filename not found")
None
}
buffer = Array.ofDim[Byte](128)
len = zipFile.getInputStream(fileNameHeader).read(buffer)
filename = new String(buffer, 0, len)
contentFileHeader ← contentFileHeaders
.headOption
.orElse {
logger.warn(s"Format of malware attribute ${file.name} is invalid : content file not found")
None
}
tempFile = tempSrv.newTemporaryFile("misp", "malware")
_ = logger.info(s"Extract malware file ${file.filepath} in file $tempFile")
_ = zipFile.extractFile(contentFileHeader, tempFile.getParent.toString, null, tempFile.getFileName.toString)
} yield FileInputValue(filename, tempFile, "application/octet-stream")).getOrElse(file)
} catch {
case e: ZipException ⇒
logger.warn(s"Format of malware attribute ${file.name} is invalid : zip file is unreadable", e)
file
}
}
private[MispSrv] val fileNameExtractor = """attachment; filename="(.*)"""".r
def downloadAttachment(mispConnection: MispConnection, attachmentId: String)(implicit authContext: AuthContext): Future[FileInputValue] =
mispConnection(s"attributes/download/$attachmentId")
.withMethod("GET")
.stream()
.flatMap {
case response if response.status != 200 ⇒
val status = response.status
logger.warn(s"MISP attachment $attachmentId can't be downloaded (status $status) : ${response.body}")
Future.failed(InternalError(s"MISP attachment $attachmentId can't be downloaded (status $status)"))
case response ⇒
val tempFile = tempSrv.newTemporaryFile("misp_attachment", attachmentId)
response
.bodyAsSource
.runWith(FileIO.toPath(tempFile))
.map { ioResult ⇒
if (!ioResult.wasSuccessful) // throw an exception if transfer failed
throw ioResult.getError
val contentType = response.headers.getOrElse("Content-Type", Seq("application/octet-stream")).head
val filename = response
.headers
.get("Content-Disposition")
.flatMap(_.collectFirst { case fileNameExtractor(name) ⇒ name })
.getOrElse("noname")
FileInputValue(filename, tempFile, contentType)
}
}
}