Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate observable taxonomies and fix for analyzer reports migration #2206

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.thp.thehive.migration.dto

import org.thp.thehive.models.Observable
import org.thp.thehive.models.{Observable, ReportTag}

case class InputObservable(
metaData: MetaData,
observable: Observable,
organisations: Set[String],
dataOrAttachment: Either[String, InputAttachment]
dataOrAttachment: Either[String, InputAttachment],
reportTags: List[ReportTag]
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import org.thp.thehive.dto.v1.InputCustomFieldValue
import org.thp.thehive.migration.dto._
import org.thp.thehive.models._
import play.api.libs.functional.syntax._
import play.api.libs.json.JsValue.jsValueToJsLookup
import play.api.libs.json._

import java.util.{Base64, Date}
import scala.collection.mutable

case class Attachment(name: String, hashes: Seq[Hash], size: Long, contentType: String, id: String)
trait Conversion {
Expand Down Expand Up @@ -101,6 +102,40 @@ trait Conversion {
)
}

def getTaxonomies(input: Map[String, JsValue]): List[ReportTag] = {
val taxonomies = mutable.MutableList[ReportTag]()
input.foreach{ case (origin: String, value: JsValue) =>
(jsValueToJsLookup(value) \ "taxonomies").asOpt[List[Map[String, JsValue]]].getOrElse(List.empty).foreach(taxonomy => {
taxonomies += ReportTag(
origin,
taxonomy.get("level") match {
case Some(x) => x.asOpt[String].getOrElse("") match {
case "malicious" => ReportTagLevel.malicious
case "suspicious" => ReportTagLevel.suspicious
case "safe" => ReportTagLevel.safe
case "info" => ReportTagLevel.info
}
case None => throw new Exception
},
taxonomy.get("namespace") match {
case Some(x) => x.asOpt[String].getOrElse("")
case None => throw new Exception
},
taxonomy.get("predicate") match {
case Some(x) => x.asOpt[String].getOrElse("")
case None => throw new Exception
},
taxonomy.get("value") match {
case Some(x) => x
case None => throw new Exception
}
)
})
}

return taxonomies.toList
}

implicit val observableReads: Reads[InputObservable] = Reads[InputObservable] { json =>
for {
metaData <- json.validate[MetaData]
Expand All @@ -109,7 +144,9 @@ trait Conversion {
ioc <- (json \ "ioc").validate[Boolean]
sighted <- (json \ "sighted").validate[Boolean]
dataType <- (json \ "dataType").validate[String]

tags = (json \ "tags").asOpt[Set[String]].getOrElse(Set.empty)
taxonomiesList = getTaxonomies(Json.parse((json \ "reports").asOpt[String].getOrElse("{}")).as[Map[String, JsValue]])
dataOrAttachment <-
(json \ "data")
.validate[String]
Expand All @@ -131,7 +168,8 @@ trait Conversion {
tags = tags.toSeq
),
Set(mainOrganisation),
dataOrAttachment
dataOrAttachment,
taxonomiesList
)
}

Expand Down Expand Up @@ -258,7 +296,8 @@ trait Conversion {
tags = tags.toSeq
),
Set(mainOrganisation),
dataOrAttachment
dataOrAttachment,
List()
)

}
Expand Down Expand Up @@ -429,8 +468,8 @@ trait Conversion {
for {
metaData <- json.validate[MetaData]
workerId <- (json \ "analyzerId").validate[String]
workerName <- (json \ "analyzerId").validate[String]
workerDefinition <- (json \ "analyzerId").validate[String]
workerName <- (json \ "analyzerName").validate[String]
workerDefinition <- (json \ "analyzerDefinition").validate[String]
status <- (json \ "status").validate[JobStatus.Value]
startDate <- (json \ "createdAt").validate[Date]
endDate <- (json \ "endDate").validate[Date]
Expand Down Expand Up @@ -484,7 +523,8 @@ trait Conversion {
tags = tags.toSeq
),
Set(mainOrganisation),
dataOrAttachment
dataOrAttachment,
List()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Output @Inject() (
caseSrv: CaseSrv,
observableSrvProvider: Provider[ObservableSrv],
dataSrv: DataSrv,
reportTagSrv: ReportTagSrv,
userSrv: UserSrv,
tagSrv: TagSrv,
caseTemplateSrv: CaseTemplateSrv,
Expand Down Expand Up @@ -697,6 +698,7 @@ class Output @Inject() (
for {
organisations <- inputObservable.organisations.toTry(getOrganisation)
richObservable <- createObservable(caseId, inputObservable, organisations.map(_._id).toSet)
_ <- reportTagSrv.updateTags(richObservable, "not_existing_origin", inputObservable.reportTags)
case0 <- getCase(caseId)
_ <- organisations.toTry(o => shareSrv.shareObservable(RichObservable(richObservable, None, None, Nil), case0, o._id))
} yield IdMapping(inputObservable.metaData.id, richObservable._id)
Expand Down Expand Up @@ -730,6 +732,7 @@ class Output @Inject() (
authTransaction(inputAlert.metaData.createdBy) { implicit graph => implicit authContext =>
logger.debug(s"Create alert ${inputAlert.alert.`type`}:${inputAlert.alert.source}:${inputAlert.alert.sourceRef}")
val `case` = inputAlert.caseId.flatMap(c => getCase(EntityId.read(c)).toOption)

for {
organisation <- getOrganisation(inputAlert.organisation)
createdAlert <- alertSrv.createEntity(inputAlert.alert.copy(organisationId = organisation._id, caseId = `case`.fold(EntityId.empty)(_._id)))
Expand Down