chg: [stix-export] Code cleanup

pull/7835/head
Jakub Onderka 2021-10-13 09:55:51 +02:00
parent e96b05554c
commit 0f2ac9c1c0
3 changed files with 43 additions and 37 deletions

View File

@ -1,5 +1,4 @@
<?php
App::uses('StixExport', 'Export');
class Stix1Export extends StixExport
@ -7,21 +6,20 @@ class Stix1Export extends StixExport
protected $__attributes_limit = 15000;
protected $__default_version = '1.1.1';
protected $__sane_versions = array('1.1.1', '1.2');
private $__script_name = 'misp2stix.py ';
private $__baseurl = null;
private $__org = null;
protected function __initiate_framing_params()
{
$this->__baseurl = escapeshellarg(Configure::read('MISP.baseurl'));
$this->__org = escapeshellarg(Configure::read('MISP.org'));
return $this->pythonBin() . ' ' . $this->__framing_script . ' stix1 -v ' . $this->__version . ' -n ' . $this->__baseurl . ' -o ' . $this->__org . ' -f ' . $this->__return_format . ' ' . $this->__end_of_cmd;
$baseurl = escapeshellarg(Configure::read('MISP.baseurl'));
$org = escapeshellarg(Configure::read('MISP.org'));
return $this->pythonBin() . ' ' . $this->__framing_script . ' stix1 -v ' . $this->__version . ' -n ' . $baseurl . ' -o ' . $org . ' -f ' . $this->__return_format . ' ' . $this->__end_of_cmd;
}
protected function __parse_misp_events(array $filenames)
{
$org = escapeshellarg(Configure::read('MISP.org'));
$filenames = implode(' ', $filenames);
$scriptFile = $this->__scripts_dir . $this->__script_name;
return shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $this->__org . ' -i ' . $filenames . $this->__end_of_cmd);
$scriptFile = $this->__scripts_dir . 'misp2stix.py';
$command = $this->pythonBin() . ' ' . $scriptFile . ' -v ' . $this->__version . ' -f ' . $this->__return_format . ' -o ' . $org . ' -i ' . $filenames . $this->__end_of_cmd;
return shell_exec($command);
}
}

View File

@ -1,5 +1,4 @@
<?php
App::uses('StixExport', 'Export');
class Stix2Export extends StixExport
@ -7,7 +6,6 @@ class Stix2Export extends StixExport
protected $__attributes_limit = 15000;
protected $__default_version = '2.0';
protected $__sane_versions = array('2.0', '2.1');
private $__script_name = 'stix2/misp2stix2.py ';
protected function __initiate_framing_params()
{
@ -16,9 +14,9 @@ class Stix2Export extends StixExport
protected function __parse_misp_events(array $filenames)
{
$scriptFile = $this->__scripts_dir . $this->__script_name;
$scriptFile = $this->__scripts_dir . 'stix2/misp2stix2.py';
$filenames = implode(' ', $filenames);
$result = shell_exec($this->pythonBin() . ' ' . $scriptFile . '-v ' . $this->__version . ' -i ' . $filenames . $this->__end_of_cmd);
$result = shell_exec($this->pythonBin() . ' ' . $scriptFile . ' -v ' . $this->__version . ' -i ' . $filenames . $this->__end_of_cmd);
$result = preg_split("/\r\n|\n|\r/", trim($result));
return end($result);
}

View File

@ -16,14 +16,10 @@ abstract class StixExport
/** @var array Full paths to files to convert */
protected $__filenames = array();
protected $__default_filters = null;
protected $__version = null;
private $__current_filename = null;
private $__empty_file = null;
private $__framing = null;
/** @var TmpFileTool */
private $__stix_file;
/** @var File */
private $__tmp_file = null;
private $__n_attributes = 0;
@ -40,21 +36,21 @@ abstract class StixExport
public function handler($data, $options = array())
{
$attributes_count = count($data['Attribute']);
foreach ($data['Object'] as $_object) {
if (isset($_object['Attribute'])) {
$attributes_count += count($_object['Attribute']);
$attributesCount = count($data['Attribute']);
foreach ($data['Object'] as $object) {
if (isset($object['Attribute'])) {
$attributesCount += count($object['Attribute']);
}
}
$converter = new JSONConverterTool();
$event = json_encode($converter->convert($data, false, true)); // we don't need pretty printed JSON
if ($this->__n_attributes + $attributes_count < $this->__attributes_limit) {
if ($this->__n_attributes + $attributesCount < $this->__attributes_limit) {
$this->__tmp_file->append($this->__n_attributes === 0 ? $event : ',' . $event);
$this->__n_attributes += $attributes_count;
$this->__n_attributes += $attributesCount;
$this->__empty_file = false;
} elseif ($attributes_count > $this->__attributes_limit) {
$filePath = FileAccessTool::writeToTempFile( $event);
} elseif ($attributesCount > $this->__attributes_limit) {
$filePath = FileAccessTool::writeToTempFile($event);
$this->__filenames[] = $filePath;
} else {
$this->__tmp_file->append(']}');
@ -62,7 +58,7 @@ abstract class StixExport
$this->__filenames[] = $this->__current_filename;
$this->__initialize_misp_file();
$this->__tmp_file->append($event);
$this->__n_attributes = $attributes_count;
$this->__n_attributes = $attributesCount;
}
return '';
}
@ -75,10 +71,6 @@ abstract class StixExport
} else if ($this->__return_type === 'stix') {
$this->__return_format = 'xml';
}
$framing_cmd = $this->__initiate_framing_params();
$this->__framing = json_decode(shell_exec($framing_cmd), true);
$this->__stix_file = new TmpFileTool();
$this->__stix_file->write($this->__framing['header']);
$this->__initialize_misp_file();
return '';
}
@ -104,14 +96,18 @@ abstract class StixExport
$error = $decoded && !empty($decoded['error']) ? $decoded['error'] : $result;
throw new Exception('Error while processing your query during STIX export: ' . $error);
}
foreach ($this->__filenames as $filename) {
$stix_event = FileAccessTool::readAndDelete($filename . '.out');
$stix_event = $this->__return_type === 'stix' ? $stix_event : substr($stix_event, 1, -1);
$this->__stix_file->writeWithSeparator($stix_event, $this->__framing['separator']);
}
$this->__stix_file->write($this->__framing['footer']);
return $this->__stix_file;
$framing = $this->getFraming();
$stixFile = new TmpFileTool();
$stixFile->write($framing['header']);
foreach ($this->__filenames as $filename) {
$stixEvent = FileAccessTool::readAndDelete($filename . '.out');
$stixEvent = $this->__return_type === 'stix' ? $stixEvent : substr($stixEvent, 1, -1);
$stixFile->writeWithSeparator($stixEvent, $framing['separator']);
}
$stixFile->write($framing['footer']);
return $stixFile;
}
public function separator()
@ -134,6 +130,20 @@ abstract class StixExport
}
}
/**
* @return array
* @throws Exception
*/
private function getFraming()
{
$framingCmd = $this->__initiate_framing_params();
$framing = json_decode(shell_exec($framingCmd), true);
if ($framing === null || isset($framing['error'])) {
throw new Exception("Could not get results from framing cmd when exporting STIX file.");
}
return $framing;
}
/**
* @return string
*/