-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenvas_plugin_communicator.php
More file actions
130 lines (112 loc) · 3.89 KB
/
openvas_plugin_communicator.php
File metadata and controls
130 lines (112 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* Copyright (C) 2014-2015 Dustin Demuth
* Westfälische Wilhelms-Universität Münster
* Zentrum für Informationsverarbeitung - CERT
*
* This program 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 2 of the License, or
* (at your option) any later version.
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
include_once 'openvas_plugin_config.php';
/*
* This file holds the functions which are required to communicate with the openvas-manager
*/
/*
* Sends the commands to openvas
* @param $commands the list of OMP commands
* @return the Response of openvas
*/
function send_Commands($commands) {
global $omp_host;
global $omp_port;
return _sendToOpenVAS(cmd_Commands($commands), $omp_host, $omp_port);
}
/*
* Connects to OpenVAS-Manager and sends the configured XML
* @return The Answer of OpenVas as a String or null in case of errors
* @exception
* @todo Exception Handling
* @todo Verfify if SSL/TLS Connection is handled properly and safe
*/
function _sendToOpenVAS($content, $host, $port) {
/*
* Verify if the content is valid,
*/
try {
$valid = _verifyOMP($content);
} catch (Exception $ex) {
throw new Exception("sendToOpenvas: I will not even try to connect to Openvas, as the content which shall be send is not valid.", null, $ex);
}
if (!$valid) {
throw new Exception("sendToOpenvas: I will not even try to connect to Openvas, as the content which shall be send is not valid.");
}
/*
* @todo Verify Host and Port
*/
/*
* Set Stream Context
* @see http://php.net/manual/en/function.stream-context-create.php
*/
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'allow_self_signed' => true
)
));
// Response and Errors
$response = null;
$errno = null;
$errstr = null;
/*
* Connect to OpenVAS with SSL/TLS
* @todo does this work with TLS?
*/
$fp = stream_socket_client('ssl://' . $host . ':' . $port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if ($errno) {
throw new Exception("sendToOpenvas: The connection to openVAS failed, because of Error: (" . $errno . ") " . $errstr);
}
if ($fp) {
try {
/*
* Send content to OpenVAS
*/
fwrite($fp, $content);
$response = _readStreamToBuffer($fp);
} catch (Exception $ex) {
throw new Exception("sendToOpenvas: The connection to openVAS failed, because of Error: (" . $errno . ") " . $errstr);
}
}
return $response;
}
/* Read the Response into a buffer
* @todo length of buffer?
*/
function _readStreamToBuffer($fp, $length = 8192) {
$response = "";
do {
$response.=$buf = fread($fp, $length);
} while (strlen($buf) == $length);
return $response;
}
/*
* This function should verify if a given XML-String matches the OMP-Schema definition
* @see http://openvas.org/protocol-doc.html
* @return true if matches else if false
* @exception throws exception if $omp is not a string
* @todo this method is a simple stub, the verification against the schema has to be done,
* right now it only checks whether $omp is a String
*/
function _verifyOMP($omp) {
if (!is_String($omp)) {
throw new Exception('OMP-Verification: Content is not a String.');
}
return true;
}