|
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 |
|
24
|
|
|
from gvm.transforms import CheckCommandTransform |
|
25
|
|
|
|
|
26
|
|
|
from gvmtools.parser import create_parser, create_connection |
|
27
|
|
|
|
|
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 main(): |
|
48
|
|
|
parser = create_parser(description=HELP_TEXT, logfilename='gvm-cli.log') |
|
49
|
|
|
|
|
50
|
|
|
parser.add_argument('-X', '--xml', help='XML request to send') |
|
51
|
|
|
parser.add_argument('-r', '--raw', help='Return raw XML', |
|
52
|
|
|
action='store_true', default=False) |
|
53
|
|
|
parser.add_argument('infile', nargs='?', type=open, default=sys.stdin) |
|
54
|
|
|
|
|
55
|
|
|
args = parser.parse_args() |
|
56
|
|
|
|
|
57
|
|
|
# If timeout value is -1, then the socket has no timeout for this session |
|
58
|
|
|
if args.timeout == -1: |
|
59
|
|
|
args.timeout = None |
|
60
|
|
|
|
|
61
|
|
|
xml = '' |
|
62
|
|
|
|
|
63
|
|
|
if args.xml is not None: |
|
64
|
|
|
xml = args.xml |
|
65
|
|
|
else: |
|
66
|
|
|
# If this returns False, then some data are in sys.stdin |
|
67
|
|
|
if not args.infile.isatty(): |
|
68
|
|
|
try: |
|
69
|
|
|
xml = args.infile.read() |
|
70
|
|
|
except (EOFError, BlockingIOError) as e: |
|
71
|
|
|
print(e) |
|
72
|
|
|
|
|
73
|
|
|
# If no command was given, program asks for one |
|
74
|
|
|
if len(xml) == 0: |
|
75
|
|
|
xml = input() |
|
76
|
|
|
|
|
77
|
|
|
# Remove all newlines if the commands come from file |
|
78
|
|
|
xml = xml.replace('\n', '').replace('\r', '') |
|
79
|
|
|
|
|
80
|
|
|
# Ask for password if none are given |
|
81
|
|
|
if args.gmp_username and not args.gmp_password: |
|
82
|
|
|
args.gmp_password = getpass.getpass('Enter password for ' + |
|
83
|
|
|
args.gmp_username + ': ') |
|
84
|
|
|
|
|
85
|
|
|
connection = create_connection(**vars(args)) |
|
86
|
|
|
|
|
87
|
|
|
if args.raw: |
|
88
|
|
|
transform = None |
|
89
|
|
|
else: |
|
90
|
|
|
transform = CheckCommandTransform() |
|
91
|
|
|
|
|
92
|
|
|
gvm = Gmp(connection, transform=transform) |
|
93
|
|
|
|
|
94
|
|
|
if args.gmp_username: |
|
95
|
|
|
gvm.authenticate(args.gmp_username, args.gmp_password) |
|
96
|
|
|
|
|
97
|
|
|
try: |
|
98
|
|
|
result = gvm.send_command(xml) |
|
99
|
|
|
|
|
100
|
|
|
print(result) |
|
101
|
|
|
except Exception as e: # pylint: disable=broad-except |
|
102
|
|
|
print(e) |
|
103
|
|
|
sys.exit(1) |
|
104
|
|
|
|
|
105
|
|
|
gvm.disconnect() |
|
106
|
|
|
|
|
107
|
|
|
sys.exit(0) |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
if __name__ == '__main__': |
|
111
|
|
|
main() |
|
112
|
|
|
|