1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# Copyright (C) 2018-2021 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 code |
20
|
|
|
import logging |
21
|
|
|
import os |
22
|
|
|
import sys |
23
|
|
|
|
24
|
|
|
from argparse import Namespace |
25
|
|
|
|
26
|
|
|
from gvm import get_version as get_gvm_version |
27
|
|
|
from gvm.xml import pretty_print |
28
|
|
|
from gvm.protocols.gmp import Gmp |
29
|
|
|
from gvm.protocols.latest import Osp |
30
|
|
|
from gvm.transforms import EtreeCheckCommandTransform |
31
|
|
|
|
32
|
|
|
from gvmtools import get_version |
33
|
|
|
from gvmtools.helper import authenticate, run_script, do_not_run_as_root |
34
|
|
|
from gvmtools.parser import ( |
35
|
|
|
create_parser, |
36
|
|
|
create_connection, |
37
|
|
|
PROTOCOL_OSP, |
38
|
|
|
PROTOCOL_GMP, |
39
|
|
|
) |
40
|
|
|
|
41
|
|
|
__version__ = get_version() |
42
|
|
|
__api_version__ = get_gvm_version() |
43
|
|
|
|
44
|
|
|
logger = logging.getLogger(__name__) |
45
|
|
|
|
46
|
|
|
HELP_TEXT = """ |
47
|
|
|
Command line tool to access services via GMP (Greenbone Management |
48
|
|
|
Protocol) and OSP (Open Scanner Protocol) |
49
|
|
|
|
50
|
|
|
gvm-pyshell provides an interactive shell for GMP and OSP services. |
51
|
|
|
|
52
|
|
|
Example: |
53
|
|
|
>>> tasks = gmp.get_tasks() |
54
|
|
|
>>> task_names = tasks.xpath('task/name/text()') |
55
|
|
|
>>> print(task_names) |
56
|
|
|
['Scan Task'] |
57
|
|
|
>>> pretty_print('<xml/>') |
58
|
|
|
<xml/> |
59
|
|
|
|
60
|
|
|
The interactive shell can be exited with: |
61
|
|
|
Ctrl + D on Linux or |
62
|
|
|
Ctrl + Z on Windows |
63
|
|
|
|
64
|
|
|
The protocol specifications for GMP and OSP are available at: |
65
|
|
|
https://docs.greenbone.net/index.html#api_documentation""" |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
class Help(object): |
69
|
|
|
"""Help class to overwrite the help function from python itself.""" |
70
|
|
|
|
71
|
|
|
def __call__(self): |
72
|
|
|
return print(HELP_TEXT) |
73
|
|
|
|
74
|
|
|
def __repr__(self): |
75
|
|
|
# do pwd command |
76
|
|
|
return HELP_TEXT |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
def main(): |
80
|
|
|
do_not_run_as_root() |
81
|
|
|
|
82
|
|
|
parser = create_parser(description=HELP_TEXT, logfilename='gvm-pyshell.log') |
83
|
|
|
|
84
|
|
|
parser.add_protocol_argument() |
85
|
|
|
|
86
|
|
|
parser.add_argument( |
87
|
|
|
'-i', |
88
|
|
|
'--interactive', |
89
|
|
|
action='store_true', |
90
|
|
|
default=False, |
91
|
|
|
help='Start an interactive Python shell', |
92
|
|
|
) |
93
|
|
|
|
94
|
|
|
parser.add_argument( |
95
|
|
|
'scriptname', |
96
|
|
|
nargs='?', |
97
|
|
|
metavar="SCRIPT", |
98
|
|
|
help='Path to script to be preloaded (example: myscript.gmp.py)', |
99
|
|
|
) |
100
|
|
|
parser.add_argument( |
101
|
|
|
'scriptargs', |
102
|
|
|
nargs='*', |
103
|
|
|
metavar="ARG", |
104
|
|
|
help='Arguments for preloaded script', |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
args = parser.parse_args() |
108
|
|
|
|
109
|
|
|
connection = create_connection(**vars(args)) |
110
|
|
|
|
111
|
|
|
transform = EtreeCheckCommandTransform() |
112
|
|
|
|
113
|
|
|
global_vars = { |
114
|
|
|
'help': Help(), |
115
|
|
|
'pretty_print': pretty_print, |
116
|
|
|
'__version__': __version__, |
117
|
|
|
'__api_version__': __api_version__, |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
username = None |
121
|
|
|
password = None |
122
|
|
|
|
123
|
|
|
if args.protocol == PROTOCOL_OSP: |
124
|
|
|
protocol_class = Osp |
125
|
|
|
name = 'osp' |
126
|
|
|
else: |
127
|
|
|
protocol_class = Gmp |
128
|
|
|
name = 'gmp' |
129
|
|
|
|
130
|
|
|
with protocol_class(connection, transform=transform) as protocol: |
131
|
|
|
global_vars[name] = protocol |
132
|
|
|
global_vars['__name__'] = '__{}__'.format(name) |
133
|
|
|
|
134
|
|
|
if args.protocol == PROTOCOL_GMP: |
135
|
|
|
if args.gmp_username: |
136
|
|
|
(username, password) = authenticate( |
137
|
|
|
protocol, |
138
|
|
|
username=args.gmp_username, |
139
|
|
|
password=args.gmp_password, |
140
|
|
|
) |
141
|
|
|
|
142
|
|
|
shell_args = Namespace(username=username, password=password) |
143
|
|
|
|
144
|
|
|
global_vars['args'] = shell_args |
145
|
|
|
|
146
|
|
|
with_script = args.scriptname and len(args.scriptname) > 0 |
147
|
|
|
|
148
|
|
|
if with_script: |
149
|
|
|
argv = [os.path.abspath(args.scriptname), *args.scriptargs] |
150
|
|
|
shell_args.argv = argv |
151
|
|
|
# for backwards compatibility we add script here |
152
|
|
|
shell_args.script = argv |
153
|
|
|
|
154
|
|
|
no_script_no_interactive = not args.interactive and not with_script |
155
|
|
|
script_and_interactive = args.interactive and with_script |
156
|
|
|
only_interactive = not with_script and args.interactive |
157
|
|
|
only_script = not args.interactive and with_script |
158
|
|
|
|
159
|
|
|
if only_interactive or no_script_no_interactive: |
160
|
|
|
enter_interactive_mode(global_vars) |
161
|
|
|
|
162
|
|
|
if script_and_interactive or only_script: |
163
|
|
|
if only_script: |
164
|
|
|
print( |
165
|
|
|
'Using gvm-pyshell for running scripts only is deprecated. ' |
166
|
|
|
'Please use gvm-script instead', |
167
|
|
|
file=sys.stderr, |
168
|
|
|
) |
169
|
|
|
|
170
|
|
|
script_name = args.scriptname |
171
|
|
|
run_script(script_name, global_vars) |
172
|
|
|
|
173
|
|
|
if not only_script: |
174
|
|
|
enter_interactive_mode(global_vars) |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
def enter_interactive_mode(global_vars): |
178
|
|
|
code.interact( |
179
|
|
|
banner='GVM Interactive Console {} API {}. Type "help" to get ' |
180
|
|
|
'information about functionality.'.format(__version__, __api_version__), |
181
|
|
|
local=dict(global_vars), |
182
|
|
|
) |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
if __name__ == '__main__': |
186
|
|
|
main() |
187
|
|
|
|