2022-05-24 14:11:49 +02:00
|
|
|
<?php
|
|
|
|
include_once APP . 'Model/WorkflowModules/WorkflowBaseModule.php';
|
|
|
|
|
2022-07-20 15:06:41 +02:00
|
|
|
class Module_misp_module extends WorkflowBaseActionModule
|
2022-05-24 14:11:49 +02:00
|
|
|
{
|
2022-07-19 09:52:38 +02:00
|
|
|
public $blocking = false;
|
2022-06-01 08:22:26 +02:00
|
|
|
public $is_misp_module = true;
|
2022-05-24 14:11:49 +02:00
|
|
|
public $id = 'misp-module';
|
|
|
|
public $name = 'misp-module';
|
|
|
|
public $description = 'misp-module';
|
|
|
|
public $icon = 'python';
|
|
|
|
public $icon_class = 'fab';
|
|
|
|
public $inputs = 1;
|
2022-07-05 15:50:54 +02:00
|
|
|
public $outputs = 1;
|
|
|
|
public $support_filters = false;
|
2022-05-24 14:11:49 +02:00
|
|
|
public $params = [];
|
|
|
|
|
|
|
|
/** @var Module */
|
|
|
|
private $Module;
|
|
|
|
private $misp_module_config;
|
|
|
|
|
|
|
|
public function __construct($misp_module_config)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2022-05-30 14:34:45 +02:00
|
|
|
$this->id = Inflector::underscore($misp_module_config['name']);
|
2022-05-24 14:11:49 +02:00
|
|
|
$this->name = $misp_module_config['name'];
|
|
|
|
$this->description = $misp_module_config['meta']['description'];
|
|
|
|
if (!empty($misp_module_config['meta']['icon'])) {
|
|
|
|
$this->icon = $misp_module_config['meta']['icon'];
|
|
|
|
}
|
2022-06-01 09:24:20 +02:00
|
|
|
if (!empty($misp_module_config['meta']['icon_class'])) {
|
|
|
|
$this->icon_class = $misp_module_config['meta']['icon_class'];
|
|
|
|
}
|
2022-05-24 14:11:49 +02:00
|
|
|
if (!empty($misp_module_config['meta']['inputs'])) {
|
|
|
|
$this->inputs = (int)$misp_module_config['meta']['inputs'];
|
|
|
|
}
|
|
|
|
if (!empty($misp_module_config['meta']['outputs'])) {
|
|
|
|
$this->inputs = (int)$misp_module_config['meta']['outputs'];
|
|
|
|
}
|
2022-07-26 08:45:33 +02:00
|
|
|
if (!empty($misp_module_config['meta']['config']['blocking'])) {
|
|
|
|
$this->blocking = !empty($misp_module_config['meta']['config']['blocking']);
|
2022-06-24 17:45:12 +02:00
|
|
|
}
|
2022-07-26 08:45:33 +02:00
|
|
|
if (!empty($misp_module_config['meta']['config']['expect_misp_core_format'])) {
|
|
|
|
$this->expect_misp_core_format = !empty($misp_module_config['meta']['config']['expect_misp_core_format']);
|
2022-07-20 15:19:33 +02:00
|
|
|
}
|
2022-07-26 08:45:33 +02:00
|
|
|
if (!empty($misp_module_config['meta']['config']['support_filters'])) {
|
|
|
|
$this->support_filters = !empty($misp_module_config['meta']['config']['support_filters']);
|
2022-07-05 15:50:54 +02:00
|
|
|
}
|
2023-05-03 14:56:38 +02:00
|
|
|
if (!empty($misp_module_config['meta']['config']['params'])) {
|
2022-07-26 08:45:33 +02:00
|
|
|
foreach ($misp_module_config['meta']['config']['params'] as $paramName => $moduleParam) {
|
2022-06-01 09:24:20 +02:00
|
|
|
$this->params[] = $this->translateParams($paramName, $moduleParam);
|
|
|
|
}
|
2022-05-24 14:11:49 +02:00
|
|
|
}
|
|
|
|
$this->Module = ClassRegistry::init('Module');
|
|
|
|
}
|
|
|
|
|
2022-05-30 14:34:45 +02:00
|
|
|
public function exec(array $node, WorkflowRoamingData $roamingData, array &$errors=[]): bool
|
2022-05-24 14:11:49 +02:00
|
|
|
{
|
2022-05-25 16:25:00 +02:00
|
|
|
parent::exec($node, $roamingData);
|
2022-05-24 15:25:58 +02:00
|
|
|
$postData = ['module' => $this->name];
|
2022-07-05 15:50:54 +02:00
|
|
|
$rData = $roamingData->getData();
|
|
|
|
$postData['data'] = $rData;
|
|
|
|
if ($this->support_filters) {
|
|
|
|
$filters = $this->getFilters($node);
|
|
|
|
$extracted = $this->extractData($rData, $filters['selector']);
|
|
|
|
if ($extracted === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$filteredItems = $this->getItemsMatchingCondition($extracted, $filters['value'], $filters['operator'], $filters['path']);
|
|
|
|
$postData['filteredItems'] = !empty($filteredItems) ? $filteredItems : $rData;
|
|
|
|
}
|
|
|
|
|
2023-10-25 10:52:59 +02:00
|
|
|
$rData = $roamingData->getData();
|
|
|
|
$indexedParams = $this->getParamsWithValues($node, $rData);
|
2022-07-27 15:41:21 +02:00
|
|
|
$postData['params'] = Hash::combine($indexedParams, '{s}.id', '{s}.value');
|
2022-07-05 15:50:54 +02:00
|
|
|
$matchingData = [];
|
2023-10-25 10:52:59 +02:00
|
|
|
foreach ($indexedParams as $param) {
|
2022-07-05 15:50:54 +02:00
|
|
|
if (!empty($param['_isHashPath'])) {
|
|
|
|
$matchingData[$param['label']] = !empty($param['value']) ? $this->extractData($rData, $param['value']) : $rData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty($matchingData)) {
|
|
|
|
$postData['matchingData'] = $matchingData;
|
|
|
|
}
|
|
|
|
|
2022-07-05 11:19:18 +02:00
|
|
|
$query = $this->Module->queryModuleServer($postData, false, 'Action', false, $postData['data']);
|
2022-06-01 09:24:20 +02:00
|
|
|
if (!empty($query['error'])) {
|
|
|
|
$errors[] = $query['error'];
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-24 15:59:41 +02:00
|
|
|
return true;
|
2022-05-24 14:11:49 +02:00
|
|
|
}
|
2022-06-01 09:24:20 +02:00
|
|
|
|
|
|
|
// FIXME: We might want to align the module config with what's currently supported
|
|
|
|
protected function translateParams($paramName, $moduleParam): array
|
|
|
|
{
|
2022-07-27 15:41:21 +02:00
|
|
|
$param = [
|
2022-07-29 10:26:14 +02:00
|
|
|
'id' => Inflector::slug(Inflector::underscore($paramName)),
|
2022-07-27 15:41:21 +02:00
|
|
|
'label' => Inflector::humanize($paramName),
|
|
|
|
'placeholder' => $moduleParam['value'] ?? '',
|
|
|
|
];
|
2022-07-05 15:50:54 +02:00
|
|
|
if ($moduleParam['type'] == 'hash_path') {
|
|
|
|
$param['type'] = 'input';
|
|
|
|
$param['_isHashPath'] = true;
|
2022-07-26 08:45:33 +02:00
|
|
|
} elseif ($moduleParam['type'] == 'large_string') {
|
2022-07-26 10:00:30 +02:00
|
|
|
$param['type'] = 'textarea';
|
2022-07-05 15:50:54 +02:00
|
|
|
} else {
|
|
|
|
$param['type'] = 'input';
|
|
|
|
}
|
2023-10-25 10:52:59 +02:00
|
|
|
if (isset($moduleParam['jinja_supported'])) {
|
|
|
|
$param['jinja_supported'] = !empty($moduleParam['jinja_supported']);
|
|
|
|
}
|
2022-06-01 09:24:20 +02:00
|
|
|
return $param;
|
|
|
|
}
|
2022-05-24 14:11:49 +02:00
|
|
|
}
|