/library/Fisma/Inject/Greenbone.php

https://bitbucket.org/khuongduybui/openfisma · PHP · 151 lines · 99 code · 16 blank · 36 comment · 30 complexity · 98c178183d03e6537565b44f1f74f1be MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2012 Endeavor Systems, Inc.
  4. *
  5. * This file is part of OpenFISMA.
  6. *
  7. * OpenFISMA is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  8. * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
  9. * version.
  10. *
  11. * OpenFISMA is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  12. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  13. * details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with OpenFISMA. If not, see
  16. * {@link http://www.gnu.org/licenses/}.
  17. */
  18. /**
  19. * A scan result injection plugin for injecting Greenbone Security XML output directly into OpenFISMA.
  20. *
  21. * @author Ben Zheng <ben.zheng@reyosoft.com>
  22. * @copyright (c) Endeavor Systems, Inc. 2012 {@link http://www.endeavorsystems.com}
  23. * @license http://www.openfisma.org/content/license GPLv3
  24. * @package Fisma
  25. * @subpackage Fisma_Inject
  26. */
  27. class Fisma_Inject_Greenbone extends Fisma_Inject_Abstract
  28. {
  29. /**
  30. * Save assets and findings which are recorded in the report.
  31. *
  32. * @param XMLReader $oXml The full Greenbone Security report
  33. * @param int $uploadId The specific scanner file id
  34. */
  35. protected function _persist(XMLReader $oXml, $uploadId)
  36. {
  37. $parsedData = array();
  38. $hostCounter = 0;
  39. while ($oXml->read()) {
  40. // The elements of the XML that we care about don't occur until we reach a depth of 1
  41. if ($oXml->depth >= 1 && $oXml->nodeType == XMLReader::ELEMENT) {
  42. if ($oXml->name == 'scan_start') {
  43. $scanDate = $oXml->readString();
  44. }
  45. if ($oXml->name == 'result') {
  46. $parsedData[$hostCounter] = array();
  47. } elseif ($oXml->name == 'host') {
  48. $parsedData[$hostCounter]['ip'] = $oXml->readString();
  49. } elseif ($oXml->name == 'port') {
  50. $port = array();
  51. if (preg_match('/(\d{1,5})/', $oXml->readString(), $port)) {
  52. $parsedData[$hostCounter]['port'] = $port[1];
  53. } else {
  54. $parsedData[$hostCounter]['port'] = null;
  55. }
  56. } elseif ($oXml->name == 'cvss_base') {
  57. $parsedData[$hostCounter]['cvssBaseScore'] = $oXml->readString();
  58. } elseif ($oXml->name == 'risk_factor') {
  59. $riskFactor = $oXml->readString();
  60. switch($riskFactor) {
  61. case "Low":
  62. $severity = 'LOW';
  63. break;
  64. case "Medium":
  65. $severity = 'MODERATE';
  66. break;
  67. case "High":
  68. $severity = 'HIGH';
  69. break;
  70. default:
  71. $severity = 'NONE';
  72. break;
  73. }
  74. $parsedData[$hostCounter]['severity'] = $severity;
  75. } elseif ($oXml->name == 'cve') {
  76. $parsedData[$hostCounter]['cve'] = $oXml->readString();
  77. } elseif ($oXml->name == 'bid') {
  78. $parsedData[$hostCounter]['bid'] = $oXml->readString();
  79. } elseif ($oXml->name == 'description') {
  80. $parsedData[$hostCounter]['summary'] = $oXml->readString();
  81. }
  82. } elseif ($oXml->nodeType == XMLReader::END_ELEMENT) {
  83. if ($oXml->name == 'result') {
  84. $hostCounter++;
  85. }
  86. }
  87. }
  88. foreach ($parsedData as $host) {
  89. if (!empty($host['severity']) && 'NONE' != $host['severity']) {
  90. // Prepare asset
  91. $asset = array();
  92. $asset['name'] = (!empty($host['port'])) ? $host['ip'] . ':' . $host['port'] : $host['ip'];
  93. $asset['networkId'] = (int) $this->_networkId;
  94. $asset['addressIp'] = $host['ip'];
  95. if (!empty($host['port'])) {
  96. $asset['AssetServices'][]['addressPort'] = (int) $host['port'];
  97. }
  98. $asset['source'] = 'scan';
  99. // Prepare finding
  100. $findingInstance = array();
  101. $findingInstance['uploadId'] = (int) $uploadId;
  102. $discoveredDate = new Zend_Date(
  103. strtotime($scanDate),
  104. Zend_Date::TIMESTAMP
  105. );
  106. $findingInstance['discoveredDate'] = (!empty($discoveredDate)) ?
  107. $discoveredDate->toString(Fisma_Date::FORMAT_DATE) : NULL;
  108. $findingInstance['sourceId'] = (int) $this->_findingSourceId;
  109. $findingInstance['responsibleOrganizationId'] = (int) $this->_orgSystemId;
  110. $findingInstance['summary'] = (!empty($host['summary'])) ?
  111. Fisma_String::textToHtml($host['summary']) : NULL;
  112. $findingInstance['threatLevel'] = (!empty($host['severity'])) ? $host['severity']
  113. : NULL;
  114. $findingInstance['cvssBaseScore'] = (!empty($host['cvssBaseScore'])) ?
  115. $host['cvssBaseScore'] : NULL;
  116. if (!empty($host['cve']) && 'NOCVE' != $host['cve']) {
  117. $cves = explode(',', $host['cve']);
  118. foreach ($cves as $cve) {
  119. $findingInstance['cve'][] = trim($cve);
  120. }
  121. }
  122. if (!empty($host['bid']) && 'NOBID' != $host['bid']) {
  123. $bugtraqs = explode(',', $host['bid']);
  124. foreach ($bugtraqs as $bugtraq) {
  125. $findingInstance['bugtraq'][] = trim($bugtraq);
  126. }
  127. }
  128. // Save finding and asset
  129. $this->_save($findingInstance, $asset);
  130. }
  131. }
  132. // Commit all data
  133. $this->_commit();
  134. }
  135. }