2018-04-26 13:51:01 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# Copyright (C) 2003-2007 Robey Pointer <robeypointer@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of paramiko.
|
|
|
|
#
|
|
|
|
# Paramiko is free software; you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU Lesser General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 2.1 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# Paramiko 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 Lesser General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with Paramiko; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
|
|
|
|
2018-08-18 09:39:50 +00:00
|
|
|
import random
|
2018-04-26 13:51:01 +00:00
|
|
|
import socket
|
|
|
|
# import sys
|
|
|
|
import threading
|
|
|
|
# import traceback
|
|
|
|
import paramiko
|
2018-08-18 09:39:50 +00:00
|
|
|
|
|
|
|
from binascii import hexlify
|
2018-04-26 13:51:01 +00:00
|
|
|
from paramiko.py3compat import u, decodebytes
|
2018-08-24 07:11:21 +00:00
|
|
|
from tests.utils import make_tests_data_path
|
2018-04-26 13:51:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
# setup logging
|
2018-08-24 07:11:21 +00:00
|
|
|
paramiko.util.log_to_file(make_tests_data_path('sshserver.log'))
|
2018-04-26 13:51:01 +00:00
|
|
|
|
2018-08-24 07:11:21 +00:00
|
|
|
host_key = paramiko.RSAKey(filename=make_tests_data_path('test_rsa.key'))
|
2018-04-26 13:51:01 +00:00
|
|
|
# host_key = paramiko.DSSKey(filename='test_dss.key')
|
|
|
|
|
|
|
|
print('Read key: ' + u(hexlify(host_key.get_fingerprint())))
|
|
|
|
|
2018-05-30 13:29:44 +00:00
|
|
|
banner = u'\r\n\u6b22\u8fce\r\n'
|
2018-05-30 14:02:17 +00:00
|
|
|
event_timeout = 5
|
2018-04-26 13:51:01 +00:00
|
|
|
|
2018-05-30 13:29:44 +00:00
|
|
|
|
|
|
|
class Server(paramiko.ServerInterface):
|
2018-04-26 13:51:01 +00:00
|
|
|
# 'data' is the output of base64.b64encode(key)
|
|
|
|
# (using the "user_rsa_key" files)
|
|
|
|
data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp'
|
|
|
|
b'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC'
|
|
|
|
b'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT'
|
|
|
|
b'UWT10hcuO4Ks8=')
|
|
|
|
good_pub_key = paramiko.RSAKey(data=decodebytes(data))
|
|
|
|
|
2018-08-28 12:25:07 +00:00
|
|
|
encodings = ['UTF-8', 'GBK', 'UTF-8\r\n', 'GBK\r\n']
|
2018-05-30 13:29:44 +00:00
|
|
|
|
2018-04-26 13:51:01 +00:00
|
|
|
def __init__(self):
|
2018-05-30 13:29:44 +00:00
|
|
|
self.shell_event = threading.Event()
|
|
|
|
self.exec_event = threading.Event()
|
2018-08-27 23:55:41 +00:00
|
|
|
self.encoding = random.choice(self.encodings)
|
2018-04-26 13:51:01 +00:00
|
|
|
|
|
|
|
def check_channel_request(self, kind, chanid):
|
|
|
|
if kind == 'session':
|
|
|
|
return paramiko.OPEN_SUCCEEDED
|
|
|
|
return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
|
|
|
|
|
|
|
|
def check_auth_password(self, username, password):
|
2018-08-18 08:33:21 +00:00
|
|
|
print('Auth attempt with username: {!r} & password: {!r}'.format(username, password)) # noqa
|
2018-09-01 03:00:01 +00:00
|
|
|
if (username in ['robey', 'bar', 'foo']) and (password == 'foo'):
|
2018-04-26 13:51:01 +00:00
|
|
|
return paramiko.AUTH_SUCCESSFUL
|
|
|
|
return paramiko.AUTH_FAILED
|
|
|
|
|
|
|
|
def check_auth_publickey(self, username, key):
|
2018-08-18 08:33:21 +00:00
|
|
|
print('Auth attempt with username: {!r} & key: {!r}'.format(username, u(hexlify(key.get_fingerprint())))) # noqa
|
2018-08-26 13:53:28 +00:00
|
|
|
if (username in ['robey', 'keyonly']) and (key == self.good_pub_key):
|
2018-04-26 13:51:01 +00:00
|
|
|
return paramiko.AUTH_SUCCESSFUL
|
|
|
|
return paramiko.AUTH_FAILED
|
|
|
|
|
|
|
|
def get_allowed_auths(self, username):
|
2018-08-26 13:53:28 +00:00
|
|
|
if username == 'keyonly':
|
|
|
|
return 'publickey'
|
2018-04-26 13:51:01 +00:00
|
|
|
return 'password,publickey'
|
|
|
|
|
2018-05-30 13:29:44 +00:00
|
|
|
def check_channel_exec_request(self, channel, command):
|
2018-08-27 23:55:41 +00:00
|
|
|
if command != b'locale charmap':
|
2018-05-30 13:29:44 +00:00
|
|
|
ret = False
|
|
|
|
else:
|
|
|
|
ret = True
|
2018-08-27 23:55:41 +00:00
|
|
|
channel.send(self.encoding)
|
2018-05-30 13:29:44 +00:00
|
|
|
channel.shutdown(1)
|
|
|
|
self.exec_event.set()
|
|
|
|
return ret
|
|
|
|
|
2018-04-26 13:51:01 +00:00
|
|
|
def check_channel_shell_request(self, channel):
|
2018-05-30 13:29:44 +00:00
|
|
|
self.shell_event.set()
|
2018-04-26 13:51:01 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
def check_channel_pty_request(self, channel, term, width, height,
|
|
|
|
pixelwidth, pixelheight, modes):
|
|
|
|
return True
|
|
|
|
|
2018-05-30 09:35:51 +00:00
|
|
|
def check_channel_window_change_request(self, channel, width, height,
|
|
|
|
pixelwidth, pixelheight):
|
|
|
|
channel.send('resized')
|
|
|
|
return True
|
|
|
|
|
2018-04-26 13:51:01 +00:00
|
|
|
|
2018-04-27 01:49:48 +00:00
|
|
|
def run_ssh_server(port=2200, running=True):
|
2018-04-26 13:51:01 +00:00
|
|
|
# now connect
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
2018-04-27 01:49:48 +00:00
|
|
|
sock.bind(('127.0.0.1', port))
|
2018-04-26 13:51:01 +00:00
|
|
|
sock.listen(100)
|
|
|
|
|
2018-04-27 01:31:52 +00:00
|
|
|
while running:
|
2018-04-26 13:51:01 +00:00
|
|
|
client, addr = sock.accept()
|
|
|
|
print('Got a connection!')
|
|
|
|
|
|
|
|
t = paramiko.Transport(client)
|
|
|
|
t.load_server_moduli()
|
|
|
|
t.add_server_key(host_key)
|
|
|
|
server = Server()
|
|
|
|
try:
|
|
|
|
t.start_server(server=server)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# wait for auth
|
2018-06-01 01:32:14 +00:00
|
|
|
chan = t.accept(2)
|
2018-04-26 13:51:01 +00:00
|
|
|
if chan is None:
|
|
|
|
print('*** No channel.')
|
|
|
|
continue
|
|
|
|
|
2018-04-27 06:34:06 +00:00
|
|
|
username = t.get_username()
|
|
|
|
print('{} Authenticated!'.format(username))
|
2018-04-26 13:51:01 +00:00
|
|
|
|
2018-05-30 14:02:17 +00:00
|
|
|
server.shell_event.wait(timeout=event_timeout)
|
2018-05-30 13:29:44 +00:00
|
|
|
if not server.shell_event.is_set():
|
2018-04-26 13:51:01 +00:00
|
|
|
print('*** Client never asked for a shell.')
|
|
|
|
continue
|
|
|
|
|
2018-05-30 14:02:17 +00:00
|
|
|
server.exec_event.wait(timeout=event_timeout)
|
2018-05-30 13:29:44 +00:00
|
|
|
if not server.exec_event.is_set():
|
|
|
|
print('*** Client never asked for a command.')
|
|
|
|
continue
|
|
|
|
|
|
|
|
# chan.send('\r\n\r\nWelcome!\r\n\r\n')
|
|
|
|
print(server.encoding)
|
2018-08-28 12:25:07 +00:00
|
|
|
chan.send(banner.encode(server.encoding.strip()))
|
2018-04-27 06:34:06 +00:00
|
|
|
if username == 'bar':
|
2018-05-26 07:51:53 +00:00
|
|
|
msg = chan.recv(1024)
|
|
|
|
chan.send(msg)
|
2018-09-01 03:00:01 +00:00
|
|
|
elif username == 'foo':
|
|
|
|
lst = []
|
|
|
|
while True:
|
|
|
|
msg = chan.recv(32 * 1024)
|
|
|
|
lst.append(msg)
|
|
|
|
if msg.endswith(b'\r\n\r\n'):
|
|
|
|
break
|
|
|
|
data = b''.join(lst)
|
|
|
|
while data:
|
|
|
|
s = chan.send(data)
|
|
|
|
data = data[s:]
|
2018-09-13 10:44:13 +00:00
|
|
|
else:
|
|
|
|
chan.close()
|
|
|
|
t.close()
|
|
|
|
client.close()
|
2018-04-26 13:51:01 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
sock.close()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-04-27 01:31:52 +00:00
|
|
|
run_ssh_server()
|