MISP (core software) - Open Source Threat Intelligence and Sharing Platform (formely known as Malware Information Sharing Platform) https://www.misp-project.org/
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
2.6 KiB

  1. <?php
  2. class JsonExport
  3. {
  4. private $__converter = false;
  5. public $non_restrictive_export = true;
  6. /**
  7. * @param $data
  8. * @param array $options
  9. * @return false|Generator|string
  10. */
  11. public function handler($data, $options = array())
  12. {
  13. if ($options['scope'] === 'Attribute') {
  14. return $this->__attributeHandler($data, $options);
  15. } else if($options['scope'] === 'Event') {
  16. return $this->__eventHandler($data, $options);
  17. } else if($options['scope'] === 'Object') {
  18. return $this->__objectHandler($data, $options);
  19. } else if($options['scope'] === 'Sighting') {
  20. return $this->__sightingsHandler($data, $options);
  21. }
  22. }
  23. /**
  24. * @param array $event
  25. * @param array $options
  26. * @return Generator
  27. */
  28. private function __eventHandler($event, $options = array())
  29. {
  30. if ($this->__converter === false) {
  31. App::uses('JSONConverterTool', 'Tools');
  32. $this->__converter = new JSONConverterTool();
  33. }
  34. return $this->__converter->streamConvert($event);
  35. }
  36. private function __objectHandler($object, $options = array()) {
  37. if ($this->__converter === false) {
  38. App::uses('JSONConverterTool', 'Tools');
  39. $this->__converter = new JSONConverterTool();
  40. }
  41. return json_encode($this->__converter->convertObject($object, false, true));
  42. }
  43. private function __attributeHandler($attribute, $options = array())
  44. {
  45. $attribute = array_merge($attribute['Attribute'], $attribute);
  46. unset($attribute['Attribute']);
  47. if (isset($attribute['Object']) && empty($attribute['Object']['id'])) {
  48. unset($attribute['Object']);
  49. }
  50. $tagTypes = array('AttributeTag', 'EventTag');
  51. foreach($tagTypes as $tagType) {
  52. if (isset($attribute[$tagType])) {
  53. foreach ($attribute[$tagType] as $tk => $tag) {
  54. if ($tagType === 'EventTag') {
  55. $attribute[$tagType][$tk]['Tag']['inherited'] = 1;
  56. }
  57. $attribute['Tag'][] = $attribute[$tagType][$tk]['Tag'];
  58. }
  59. unset($attribute[$tagType]);
  60. }
  61. }
  62. unset($attribute['value1']);
  63. unset($attribute['value2']);
  64. return json_encode($attribute);
  65. }
  66. private function __sightingsHandler($sighting, $options = array())
  67. {
  68. return json_encode($sighting);
  69. }
  70. public function header($options = array())
  71. {
  72. if ($options['scope'] === 'Attribute') {
  73. return '{"response": {"Attribute": [';
  74. } else {
  75. return '{"response": [';
  76. }
  77. }
  78. public function footer($options = array())
  79. {
  80. if ($options['scope'] === 'Attribute') {
  81. return ']}}' . PHP_EOL;
  82. } else {
  83. return ']}' . PHP_EOL;
  84. }
  85. }
  86. public function separator()
  87. {
  88. return ',';
  89. }
  90. }