chg: [stix export] Merging all the differents changes at different places to support every type of collection export as STIX 1 & 2

misp-stix
chrisr3d 2021-12-09 16:52:50 +01:00
parent db44f1a105
commit 8af7a00073
No known key found for this signature in database
GPG Key ID: 6BBED1B63A6D639F
3 changed files with 51 additions and 24 deletions

View File

@ -16,13 +16,33 @@ class Stix1Export extends StixExport
$this->__baseurl = escapeshellarg(Configure::read('MISP.baseurl'));
$this->__org = escapeshellarg(Configure::read('MISP.org'));
$my_server = ClassRegistry::init('Server');
return $my_server->getPythonVersion() . ' ' . $this->__framing_script . ' stix1 -v ' . $this->__version . ' -n ' . $this->__baseurl . ' -o ' . $this->__org . ' -f ' . $this->__return_format . ' ' . $this->__end_of_cmd;
return $my_server->getPythonVersion() . ' ' . $this->__framing_script . ' stix1 -s ' . $this->__scope . ' -v ' . $this->__version . ' -n ' . $this->__baseurl . ' -o ' . $this->__org . ' -f ' . $this->__return_format . ' ' . $this->__end_of_cmd;
}
protected function __parse_misp_data($filenames)
{
$scriptFile = $this->__scripts_dir . $this->__script_name;
$my_server = ClassRegistry::init('Server');
return shell_exec($my_server->getPythonVersion() . ' ' . $scriptFile . '-v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $this->__org . ' -i ' . $this->__tmp_dir . $filenames . ' -s ' . $this->__scope . $this->__end_of_cmd);
$result = shell_exec($my_server->getPythonVersion() . ' ' . $scriptFile . '-v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $this->__org . ' -i ' . $this->__tmp_dir . $filenames . ' -s ' . $this->__scope . $this->__end_of_cmd);
$decoded = json_decode($result, true);
if (!isset($decoded['success']) || !$decoded['success']) {
$this->__delete_temporary_files();
$error = !empty($decoded['error']) ? $decoded['error'] : $result;
return 'Error while processing your query: ' . $error;
}
if (!empty($decoded['filenames'])) {
foreach ($this->__filenames as $f => $filename) {
@unlink($this->__tmp_dir . $filename);
}
foreach ($decoded['filenames'] as $filename) {
$this->__write_stix_content($filename);
}
} else {
foreach ($this->__filenames as $f => $filename) {
$content_filename = $this->__tmp_dir . $filename;
$this->__write_stix_content($content_filename . '.out');
@unlink($content_filename);
}
}
}
}

View File

@ -22,6 +22,16 @@ class Stix2Export extends StixExport
$my_server = ClassRegistry::init('Server');
$result = shell_exec($my_server->getPythonVersion() . ' ' . $scriptFile . '-v ' . $this->__version . ' -i ' . $this->__tmp_dir . $filenames . $this->__end_of_cmd);
$result = preg_split("/\r\n|\n|\r/", trim($result));
return end($result);
$decoded = json_decode(end($result), true);
if (!isset($decoded['success']) || !$decoded['success']) {
$this->__delete_temporary_files();
$error = !empty($decoded['error']) ? $decoded['error'] : $result;
return 'Error while processing your query: ' . $error;
}
foreach ($this->__filenames as $f => $filename) {
$content_filename = $this->__tmp_dir . $filename;
$this->__write_stix_content($content_filename . '.out');
@unlink($content_filename);
}
}
}

View File

@ -16,14 +16,14 @@ class StixExport
protected $__default_filters = null;
protected $__version = null;
protected $__scope = null;
protected $__stix_file = null;
protected $__framing = null;
private $__cluster_uuids = array();
private $__converter = null;
private $__current_filename = null;
private $__empty_file = true;
private $__event_galaxies = array();
private $__framing = null;
private $__stix_file = null;
private $__tmp_file = null;
private $__n_attributes = 0;
@ -96,28 +96,15 @@ class StixExport
$this->__filenames[] = $this->__current_filename;
}
$filenames = implode(' ' . $this->__tmp_dir, $this->__filenames);
$result = $this->__parse_misp_data($filenames);
$decoded = json_decode($result, true);
if (!isset($decoded['success']) || !$decoded['success']) {
$this->__delete_temporary_files();
$error = !empty($decoded['error']) ? $decoded['error'] : $result;
return 'Error while processing your query: ' . $error;
}
foreach ($this->__filenames as $f => $filename) {
$file = new File($this->__tmp_dir . $filename . '.out');
$stix_event = ($this->__return_type == 'stix') ? $file->read() : substr($file->read(), 1, -1);
$file->close();
$file->delete();
@unlink($this->__tmp_dir . $filename);
$this->__stix_file->append($stix_event . $this->__framing['separator']);
unset($stix_event);
}
$this->__parse_misp_data($filenames);
$stix_event = $this->__stix_file->read();
$this->__stix_file->close();
$this->__stix_file->delete();
$sep_len = strlen($this->__framing['separator']);
$stix_event = (empty($this->__filenames) ? $stix_event : substr($stix_event, 0, -$sep_len)) . $this->__framing['footer'];
return $stix_event;
if ($sep_len != 0 && !empty($this->__filenames)) {
$stix_event = substr($stix_event, 0, -$sep_len);
}
return $stix_event . $this->__framing['footer'];
}
public function separator()
@ -318,7 +305,7 @@ class StixExport
return (new RandomTool())->random_str(false, 12);
}
private function __delete_temporary_files()
protected function __delete_temporary_files()
{
foreach ($this->__filenames as $f => $filename) {
@unlink($this->__tmp_dir . $filename);
@ -357,4 +344,14 @@ class StixExport
$this->__tmp_file->append(implode(', ', $galaxies));
$this->__event_galaxies = array();
}
protected function __write_stix_content($filename)
{
$file = new File($filename);
$stix_content = ($this->__return_type == 'stix') ? $file->read() : substr($file->read(), 1, -1);
$file->close();
$file->delete();
$this->__stix_file->append($stix_content . $this->__framing['separator']);
unset($stix_content);
}
}