Skip to content

Commit

Permalink
#52 Export case into MISP event
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Aug 14, 2017
1 parent f239da6 commit 5379580
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 24 deletions.
6 changes: 5 additions & 1 deletion thehive-backend/app/controllers/AlertCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ class AlertCtrl @Inject() (

@Timed
def create(): Action[Fields] = authenticated(Role.write).async(fieldsBodyParser) { implicit request
alertSrv.create(request.body)
alertSrv.create(request.body
.unset("lastSyncDate")
.unset("case")
.unset("status")
.unset("follow"))
.map(alert renderer.toOutput(CREATED, alert))
}

Expand Down
2 changes: 1 addition & 1 deletion thehive-backend/app/models/Alert.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class AlertModel @Inject() (dblists: DBLists)
val sourceRef = (attrs \ "sourceRef").asOpt[String].getOrElse("<null>")
val _id = hasher.fromString(s"$tpe|$source|$sourceRef").head.toString()
attrs + ("_id" JsString(_id))
} - "lastSyncDate" - "case" - "status" - "follow"
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions thehive-misp/app/connectors/misp/JsonFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ object JsonFormat {
comment,
value,
tags :+ s"MISP:category$category" :+ s"MISP:type=$tpe"))

implicit val exportedAttributeWrites: Writes[ExportedMispAttribute] = Writes[ExportedMispAttribute] { attribute
Json.obj(
"category" attribute.category,
"type" attribute.tpe,
"value" attribute.value.fold[String](identity, _.name),
"comment" attribute.comment,
"status" attribute.status)
}
}
25 changes: 20 additions & 5 deletions thehive-misp/app/connectors/misp/MispCtrl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import play.api.libs.json.Json
import play.api.mvc.{ Action, AnyContent, Controller }
import play.api.routing.SimpleRouter
import play.api.routing.sird.{ GET, UrlContext }
import services.AlertTransformer
import services.{ AlertTransformer, CaseSrv }
import connectors.misp.JsonFormat.exportedAttributeWrites

import scala.concurrent.{ ExecutionContext, Future }

@Singleton
class MispCtrl @Inject() (
mispSrv: MispSrv,
caseSrv: CaseSrv,
authenticated: Authenticated,
eventSrv: EventSrv,
implicit val ec: ExecutionContext) extends Controller with Connector with Status with AlertTransformer {
Expand All @@ -30,10 +32,11 @@ class MispCtrl @Inject() (

private[MispCtrl] lazy val logger = Logger(getClass)
val router = SimpleRouter {
case GET(p"/_syncAlerts") syncAlerts
case GET(p"/_syncAllAlerts") syncAllAlerts
case GET(p"/_syncArtifacts") syncArtifacts
case r throw NotFoundError(s"${r.uri} not found")
case GET(p"/_syncAlerts") syncAlerts
case GET(p"/_syncAllAlerts") syncAllAlerts
case GET(p"/_syncArtifacts") syncArtifacts
case GET(p"/export/$caseId/to/$mispName") exportCase(mispName, caseId)
case r throw NotFoundError(s"${r.uri} not found")
}

@Timed
Expand All @@ -54,6 +57,18 @@ class MispCtrl @Inject() (
Ok("")
}

@Timed
def exportCase(mispName: String, caseId: String): Action[AnyContent] = authenticated(Role.write).async { implicit request
caseSrv
.get(caseId)
.flatMap { caze mispSrv.export(mispName, caze) }
.map {
case (eventId, exportedAttributes) Ok(Json.obj(
"eventId" eventId,
"attributes" exportedAttributes))
}
}

override def createCase(alert: Alert, customCaseTemplate: Option[String])(implicit authContext: AuthContext): Future[Case] = {
mispSrv.createCase(alert, customCaseTemplate)
}
Expand Down
Loading

0 comments on commit 5379580

Please sign in to comment.