-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
383 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package controllers | ||
|
||
import javax.inject.Inject | ||
|
||
import play.api.libs.json.{ JsObject, JsValue } | ||
import play.api.mvc.{ Action, AnyContent, Controller } | ||
import services.MispSrv | ||
|
||
import scala.concurrent.ExecutionContext | ||
|
||
class MispCtrl @Inject() (mispSrv: MispSrv, implicit val ec: ExecutionContext) extends Controller { | ||
|
||
def modules: Action[AnyContent] = Action { _ ⇒ | ||
Ok(mispSrv.moduleList) | ||
} | ||
|
||
def query: Action[JsValue] = Action.async(parse.json) { request ⇒ | ||
val module = (request.body \ "module").asOpt[String].getOrElse(sys.error("module not present in request")) | ||
val (mispType, dataJson) = request.body.as[JsObject].fields | ||
.collectFirst { | ||
case kv @ (k, _) if k != "module" ⇒ kv | ||
} | ||
.getOrElse(sys.error("invalid request")) | ||
val data = dataJson.asOpt[String].getOrElse(sys.error("data has invalid type (expected string)")) | ||
mispSrv.query(module, mispType, data) | ||
.map(Ok(_)) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package models | ||
|
||
import java.io._ | ||
import java.nio.file.Path | ||
import java.nio.file.Files | ||
|
||
import org.apache.commons.codec.binary.{ Base64, Base64InputStream } | ||
import play.api.libs.json.{ JsObject, JsValue, Json } | ||
import services.MispSrv | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.sys.process.{ BasicIO, Process, ProcessIO } | ||
import scala.sys.process._ | ||
|
||
case class MispModule( | ||
name: String, | ||
version: String, | ||
description: String, | ||
dataTypeList: Seq[String], | ||
author: String, | ||
modulePath: Path, | ||
loaderCommand: String)(implicit val ec: ExecutionContext) extends Analyzer { | ||
|
||
val license = "AGPL-3.0" | ||
val url = "https://github.com/MISP/misp-modules" | ||
|
||
private def stringStream(string: String): InputStream = { | ||
new ByteArrayInputStream(string.getBytes) | ||
} | ||
def analyze(artifact: Artifact): Future[JsObject] = { | ||
val input = artifact match { | ||
case DataArtifact(data, _) ⇒ | ||
stringStream(Json.obj(artifact.dataType → data).toString) | ||
case FileArtifact(data, _) ⇒ | ||
new SequenceInputStream(Iterator( | ||
stringStream("""{"file":""""), | ||
new Base64InputStream(new FileInputStream(data), true), | ||
stringStream("\"}")).asJavaEnumeration) | ||
} | ||
val output = (s"$loaderCommand --run $modulePath" #< input).!! | ||
Future { | ||
Json.parse(output).as[JsObject] | ||
} | ||
} | ||
} | ||
|
||
object MispModule { | ||
def apply( | ||
loaderCommand: String, | ||
modulePath: Path, | ||
mispSrv: MispSrv)(implicit ec: ExecutionContext): Option[MispModule] = { | ||
val moduleInfo = Json.parse(s"$loaderCommand --info $modulePath".!!) | ||
for { | ||
name ← (moduleInfo \ "name").asOpt[String] | ||
version ← (moduleInfo \ "moduleinfo" \ "version").asOpt[String] | ||
description ← (moduleInfo \ "moduleinfo" \ "description").asOpt[String] | ||
dataTypeList ← (moduleInfo \ "mispattributes" \ "input") | ||
.asOpt[Seq[String]] | ||
.map(_.map(mispSrv.mispType2dataType(_))) | ||
author ← (moduleInfo \ "moduleinfo" \ "author").asOpt[String] | ||
} yield MispModule(name, version, description, dataTypeList, author, modulePath, loaderCommand) | ||
} | ||
} | ||
|
||
//{'mispattributes': { | ||
// 'input': ['ip-src', 'ip-dst', 'domain|ip'], | ||
// 'output': ['hostname'] | ||
// }, | ||
// 'moduleinfo': { | ||
// 'version': '0.1', | ||
// 'author': 'Andreas Muehlemann', | ||
// 'description': 'Simple Reverse DNS expansion service to resolve reverse DNS from MISP attributes', 'module-type': ['expansion', 'hover']}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.