forked from ssh-mitm/ssh-mitm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
269 lines (245 loc) · 8.06 KB
/
cli.py
File metadata and controls
269 lines (245 loc) · 8.06 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import argparse
import logging
import sys
import os
from enhancements.modules import ModuleParser
from paramiko import Transport
from rich.logging import RichHandler
from rich.highlighter import NullHighlighter
from rich import print as rich_print
from typeguard import typechecked
from ssh_proxy_server.console import sshconsole
from ssh_proxy_server.server import SSHProxyServer
from ssh_proxy_server.authentication import (
Authenticator,
AuthenticatorPassThrough
)
from ssh_proxy_server.interfaces.server import (
BaseServerInterface,
ServerInterface
)
from ssh_proxy_server.forwarders.scp import SCPBaseForwarder
from ssh_proxy_server.forwarders.ssh import SSHBaseForwarder
from ssh_proxy_server.forwarders.sftp import SFTPHandlerBasePlugin
from ssh_proxy_server.interfaces.sftp import (
BaseSFTPServerInterface,
SFTPProxyServerInterface
)
from ssh_proxy_server.forwarders.tunnel import (
ServerTunnelBaseForwarder,
ClientTunnelForwarder,
ClientTunnelBaseForwarder
)
from ssh_proxy_server.workarounds import dropbear
from ssh_proxy_server.plugins.ssh.mirrorshell import SSHMirrorForwarder
from ssh_proxy_server.plugins.scp.store_file import SCPStorageForwarder
from ssh_proxy_server.plugins.sftp.store_file import SFTPHandlerStoragePlugin
from ssh_proxy_server.plugins.tunnel.injectservertunnel import InjectableServerTunnelForwarder
from ssh_proxy_server.__version__ import version as ssh_mitm_version
from ssh_proxy_server.update import check_version
from ssh_proxy_server.session import BaseSession, Session
@typechecked
def get_parser() -> ModuleParser:
parser = ModuleParser(
description='SSH Proxy Server',
version=f"SSH-MITM {ssh_mitm_version}",
modules_from_file=True
)
parser.add_argument(
'-d',
'--debug',
dest='debug',
default=False,
action='store_true',
help='More verbose output of status information'
)
parser.add_argument(
'--listen-port',
dest='listen_port',
default=10022,
type=int,
help='listen port'
)
parser.add_argument(
'--transparent',
dest='transparent',
action='store_true',
help='enables transparent mode (requires root)'
)
parser.add_argument(
'--host-key',
dest='host_key',
help='host key file'
)
parser.add_argument(
'--host-key-algorithm',
dest='host_key_algorithm',
default='rsa',
choices=['dss', 'rsa', 'ecdsa', 'ed25519'],
help='host key algorithm (default rsa)'
)
parser.add_argument(
'--host-key-length',
dest='host_key_length',
default=2048,
type=int,
help='host key length for dss and rsa (default 2048)'
)
parser.add_module(
'--ssh-interface',
dest='ssh_interface',
default=SSHMirrorForwarder,
help='interface to handle terminal sessions',
baseclass=SSHBaseForwarder
)
parser.add_module(
'--scp-interface',
dest='scp_interface',
default=SCPStorageForwarder,
help='interface to handle scp file transfers',
baseclass=SCPBaseForwarder
)
parser.add_module(
'--sftp-interface',
dest='sftp_interface',
default=SFTPProxyServerInterface,
help='SFTP Handler to handle sftp file transfers',
baseclass=BaseSFTPServerInterface
)
parser.add_module(
'--sftp-handler',
dest='sftp_handler',
default=SFTPHandlerStoragePlugin,
help='SFTP Handler to handle sftp file transfers',
baseclass=SFTPHandlerBasePlugin
)
parser.add_module(
'--server-tunnel',
dest='server_tunnel_interface',
default=InjectableServerTunnelForwarder,
help='interface to handle tunnels from the server',
baseclass=ServerTunnelBaseForwarder
)
parser.add_module(
'--client-tunnel',
dest='client_tunnel_interface',
default=ClientTunnelForwarder,
help='interface to handle tunnels from the client',
baseclass=ClientTunnelBaseForwarder
)
parser.add_module(
'--auth-interface',
dest='auth_interface',
default=ServerInterface,
baseclass=BaseServerInterface,
help='interface for authentication'
)
parser.add_module(
'--authenticator',
dest='authenticator',
default=AuthenticatorPassThrough,
baseclass=Authenticator,
help='module for user authentication'
)
parser.add_argument(
'--request-agent-breakin',
dest='request_agent_breakin',
action='store_true',
help='enables agent forwarding and tryies to break in to the agent, if not forwarded'
)
parser.add_argument(
'--banner-name',
dest='banner_name',
default=f'SSHMITM_{ssh_mitm_version}',
help='set a custom string as server banner'
)
parser.add_argument(
'--paramiko-log-level',
dest='paramiko_log_level',
default='warning',
choices=['warning', 'info', 'debug'],
help='set paramikos log level'
)
parser.add_argument(
'--disable-workarounds',
dest='disable_workarounds',
action='store_true',
help='disable paramiko workarounds'
)
parser.add_argument(
'--check-version',
dest='check_version',
action='store_true',
help='checks if a new version is available'
)
parser.add_module(
'--session-class',
dest='session_class',
default=Session,
baseclass=BaseSession,
help=argparse.SUPPRESS
)
return parser
@typechecked
def main() -> None:
if os.environ.get('APPIMAGE', None):
# if running as appimage, remove empty arguments
if len(sys.argv) == 2 and sys.argv[-1] == '':
sys.argv = sys.argv[:-1]
parser = get_parser()
args = parser.parse_args()
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG if args.debug else logging.INFO)
root_logger.handlers.clear()
root_logger.addHandler(RichHandler(
highlighter=NullHighlighter(),
markup=False,
rich_tracebacks=True,
enable_link_path=args.debug,
show_path=args.debug
))
if args.check_version:
latest_version = check_version()
if latest_version:
logging.info(
"[yellow]:information: ssh-mitm version %s is available",
latest_version,
extra={'markup': True}
)
sys.exit(0)
if not args.disable_workarounds:
Transport.run = dropbear.transport_run # type: ignore
if args.paramiko_log_level == 'debug':
logging.getLogger("paramiko").setLevel(logging.DEBUG)
elif args.paramiko_log_level == 'info':
logging.getLogger("paramiko").setLevel(logging.INFO)
else:
logging.getLogger("paramiko").setLevel(logging.WARNING)
if args.request_agent_breakin:
args.authenticator.REQUEST_AGENT_BREAKIN = True
sshconsole.rule(f"[bold blue]SSH-MITM - ssh audits made simple", style="blue")
rich_print(f'[bold]Version:[/bold] {ssh_mitm_version}')
rich_print("[bold]Documentation:[/bold] https://docs.ssh-mitm.at")
rich_print("[bold]Issues:[/bold] https://github.com/ssh-mitm/ssh-mitm/issues")
sshconsole.rule(style="blue")
proxy = SSHProxyServer(
args.listen_port,
key_file=args.host_key,
key_algorithm=args.host_key_algorithm,
key_length=args.host_key_length,
ssh_interface=args.ssh_interface,
scp_interface=args.scp_interface,
sftp_interface=args.sftp_interface,
sftp_handler=args.sftp_handler,
server_tunnel_interface=args.server_tunnel_interface,
client_tunnel_interface=args.client_tunnel_interface,
authentication_interface=args.auth_interface,
authenticator=args.authenticator,
transparent=args.transparent,
args=args
)
if args.banner_name is not None:
Transport._CLIENT_ID = args.banner_name # type: ignore
proxy.start()
if __name__ == '__main__':
main()