/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
- <?php
- /**
- * Copyright (c) 2012 Endeavor Systems, Inc.
- *
- * This file is part of OpenFISMA.
- *
- * OpenFISMA is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
- * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
- * version.
- *
- * OpenFISMA is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with OpenFISMA. If not, see
- * {@link http://www.gnu.org/licenses/}.
- */
- /**
- * A scan result injection plugin for injecting Greenbone Security XML output directly into OpenFISMA.
- *
- * @author Ben Zheng <ben.zheng@reyosoft.com>
- * @copyright (c) Endeavor Systems, Inc. 2012 {@link http://www.endeavorsystems.com}
- * @license http://www.openfisma.org/content/license GPLv3
- * @package Fisma
- * @subpackage Fisma_Inject
- */
- class Fisma_Inject_Greenbone extends Fisma_Inject_Abstract
- {
- /**
- * Save assets and findings which are recorded in the report.
- *
- * @param XMLReader $oXml The full Greenbone Security report
- * @param int $uploadId The specific scanner file id
- */
- protected function _persist(XMLReader $oXml, $uploadId)
- {
- $parsedData = array();
- $hostCounter = 0;
- while ($oXml->read()) {
- // The elements of the XML that we care about don't occur until we reach a depth of 1
- if ($oXml->depth >= 1 && $oXml->nodeType == XMLReader::ELEMENT) {
- if ($oXml->name == 'scan_start') {
- $scanDate = $oXml->readString();
- }
- if ($oXml->name == 'result') {
- $parsedData[$hostCounter] = array();
- } elseif ($oXml->name == 'host') {
- $parsedData[$hostCounter]['ip'] = $oXml->readString();
- } elseif ($oXml->name == 'port') {
- $port = array();
- if (preg_match('/(\d{1,5})/', $oXml->readString(), $port)) {
- $parsedData[$hostCounter]['port'] = $port[1];
- } else {
- $parsedData[$hostCounter]['port'] = null;
- }
- } elseif ($oXml->name == 'cvss_base') {
- $parsedData[$hostCounter]['cvssBaseScore'] = $oXml->readString();
- } elseif ($oXml->name == 'risk_factor') {
- $riskFactor = $oXml->readString();
- switch($riskFactor) {
- case "Low":
- $severity = 'LOW';
- break;
- case "Medium":
- $severity = 'MODERATE';
- break;
- case "High":
- $severity = 'HIGH';
- break;
- default:
- $severity = 'NONE';
- break;
- }
- $parsedData[$hostCounter]['severity'] = $severity;
- } elseif ($oXml->name == 'cve') {
- $parsedData[$hostCounter]['cve'] = $oXml->readString();
- } elseif ($oXml->name == 'bid') {
- $parsedData[$hostCounter]['bid'] = $oXml->readString();
- } elseif ($oXml->name == 'description') {
- $parsedData[$hostCounter]['summary'] = $oXml->readString();
- }
- } elseif ($oXml->nodeType == XMLReader::END_ELEMENT) {
- if ($oXml->name == 'result') {
- $hostCounter++;
- }
- }
- }
- foreach ($parsedData as $host) {
- if (!empty($host['severity']) && 'NONE' != $host['severity']) {
- // Prepare asset
- $asset = array();
- $asset['name'] = (!empty($host['port'])) ? $host['ip'] . ':' . $host['port'] : $host['ip'];
- $asset['networkId'] = (int) $this->_networkId;
- $asset['addressIp'] = $host['ip'];
- if (!empty($host['port'])) {
- $asset['AssetServices'][]['addressPort'] = (int) $host['port'];
- }
- $asset['source'] = 'scan';
- // Prepare finding
- $findingInstance = array();
- $findingInstance['uploadId'] = (int) $uploadId;
- $discoveredDate = new Zend_Date(
- strtotime($scanDate),
- Zend_Date::TIMESTAMP
- );
- $findingInstance['discoveredDate'] = (!empty($discoveredDate)) ?
- $discoveredDate->toString(Fisma_Date::FORMAT_DATE) : NULL;
- $findingInstance['sourceId'] = (int) $this->_findingSourceId;
- $findingInstance['responsibleOrganizationId'] = (int) $this->_orgSystemId;
- $findingInstance['summary'] = (!empty($host['summary'])) ?
- Fisma_String::textToHtml($host['summary']) : NULL;
- $findingInstance['threatLevel'] = (!empty($host['severity'])) ? $host['severity']
- : NULL;
- $findingInstance['cvssBaseScore'] = (!empty($host['cvssBaseScore'])) ?
- $host['cvssBaseScore'] : NULL;
- if (!empty($host['cve']) && 'NOCVE' != $host['cve']) {
- $cves = explode(',', $host['cve']);
- foreach ($cves as $cve) {
- $findingInstance['cve'][] = trim($cve);
- }
- }
- if (!empty($host['bid']) && 'NOBID' != $host['bid']) {
- $bugtraqs = explode(',', $host['bid']);
- foreach ($bugtraqs as $bugtraq) {
- $findingInstance['bugtraq'][] = trim($bugtraq);
- }
- }
- // Save finding and asset
- $this->_save($findingInstance, $asset);
- }
- }
- // Commit all data
- $this->_commit();
- }
- }