1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018 Greenbone Networks GmbH |
3
|
|
|
# |
4
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later |
5
|
|
|
# |
6
|
|
|
# This program is free software: you can redistribute it and/or modify |
7
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
# the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
# (at your option) any later version. |
10
|
|
|
# |
11
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
# GNU General Public License for more details. |
15
|
|
|
# |
16
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
|
19
|
|
|
import getpass |
20
|
|
|
import logging |
21
|
|
|
import sys |
22
|
|
|
|
23
|
|
|
from gvm.protocols.latest import Gmp, Osp |
24
|
|
|
from gvm.transforms import CheckCommandTransform |
25
|
|
|
|
26
|
|
|
from gvmtools.helper import do_not_run_as_root |
27
|
|
|
from gvmtools.parser import create_parser, create_connection, PROTOCOL_OSP |
28
|
|
|
|
29
|
|
|
logger = logging.getLogger(__name__) |
30
|
|
|
|
31
|
|
|
HELP_TEXT = """ |
32
|
|
|
Command line tool to access services via GMP (Greenbone Management Protocol) and OSP (Open Scanner Protocol) |
33
|
|
|
|
34
|
|
|
Examples: |
35
|
|
|
gvm-cli socket --help |
36
|
|
|
gvm-cli tls --help |
37
|
|
|
gvm-cli ssh --help |
38
|
|
|
|
39
|
|
|
gvm-cli socket --xml "<get_version/>" |
40
|
|
|
gvm-cli socket --xml "<commands><authenticate><credentials><username>myuser</username><password>mypass</password></credentials></authenticate><get_tasks/></commands>" |
41
|
|
|
gvm-cli socket --gmp-username foo --gmp-password foo < myfile.xml |
42
|
|
|
|
43
|
|
|
The protocol specifications for GMP and OSP are available at: |
44
|
|
|
https://docs.greenbone.net/index.html#api_documentation""" |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
def _load_infile(filename=None): |
48
|
|
|
if not filename: |
49
|
|
|
return None |
50
|
|
|
|
51
|
|
|
with open(filename) as f: |
52
|
|
|
return f.read() |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
def main(): |
56
|
|
|
do_not_run_as_root() |
57
|
|
|
|
58
|
|
|
parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log') |
59
|
|
|
|
60
|
|
|
parser.add_protocol_argument() |
61
|
|
|
|
62
|
|
|
parser.add_argument('-X', '--xml', help='XML request to send') |
63
|
|
|
parser.add_argument( |
64
|
|
|
'-r', '--raw', help='Return raw XML', action='store_true', default=False |
65
|
|
|
) |
66
|
|
|
parser.add_argument( |
67
|
|
|
'infile', nargs='?', help='File to read XML commands from.' |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
args = parser.parse_args() |
71
|
|
|
|
72
|
|
|
# If timeout value is -1, then the socket has no timeout for this session |
73
|
|
|
if args.timeout == -1: |
74
|
|
|
args.timeout = None |
75
|
|
|
|
76
|
|
|
if args.xml is not None: |
77
|
|
|
xml = args.xml |
78
|
|
|
else: |
79
|
|
|
try: |
80
|
|
|
xml = _load_infile(args.infile) |
81
|
|
|
except IOError as e: |
82
|
|
|
print(e) |
83
|
|
|
sys.exit(1) |
84
|
|
|
|
85
|
|
|
# If no command was given, program asks for one |
86
|
|
|
if len(xml) == 0: |
87
|
|
|
xml = input() |
88
|
|
|
|
89
|
|
|
connection = create_connection(**vars(args)) |
90
|
|
|
|
91
|
|
|
if args.raw: |
92
|
|
|
transform = None |
93
|
|
|
else: |
94
|
|
|
transform = CheckCommandTransform() |
95
|
|
|
|
96
|
|
|
if args.protocol == PROTOCOL_OSP: |
97
|
|
|
protocol = Osp(connection, transform=transform) |
98
|
|
|
else: |
99
|
|
|
protocol = Gmp(connection, transform=transform) |
100
|
|
|
|
101
|
|
|
# Ask for password if none are given |
102
|
|
|
if args.gmp_username and not args.gmp_password: |
103
|
|
|
args.gmp_password = getpass.getpass( |
104
|
|
|
'Enter password for ' + args.gmp_username + ': ' |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
if args.gmp_username: |
108
|
|
|
protocol.authenticate(args.gmp_username, args.gmp_password) |
109
|
|
|
|
110
|
|
|
try: |
111
|
|
|
result = protocol.send_command(xml) |
112
|
|
|
|
113
|
|
|
print(result) |
114
|
|
|
except Exception as e: # pylint: disable=broad-except |
115
|
|
|
print(e) |
116
|
|
|
sys.exit(1) |
117
|
|
|
|
118
|
|
|
protocol.disconnect() |
119
|
|
|
|
120
|
|
|
sys.exit(0) |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
if __name__ == '__main__': |
124
|
|
|
main() |
125
|
|
|
|