|
|
3ef272 |
diff --git a/paramiko/common.py b/paramiko/common.py
|
|
|
3ef272 |
index 0b0cc2a..50355f6 100644
|
|
|
3ef272 |
--- a/paramiko/common.py
|
|
|
3ef272 |
+++ b/paramiko/common.py
|
|
|
3ef272 |
@@ -32,6 +32,7 @@ MSG_USERAUTH_INFO_REQUEST, MSG_USERAUTH_INFO_RESPONSE = range(60, 62)
|
|
|
3ef272 |
MSG_USERAUTH_GSSAPI_RESPONSE, MSG_USERAUTH_GSSAPI_TOKEN = range(60, 62)
|
|
|
3ef272 |
MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, MSG_USERAUTH_GSSAPI_ERROR,\
|
|
|
3ef272 |
MSG_USERAUTH_GSSAPI_ERRTOK, MSG_USERAUTH_GSSAPI_MIC = range(63, 67)
|
|
|
3ef272 |
+HIGHEST_USERAUTH_MESSAGE_ID = 79
|
|
|
3ef272 |
MSG_GLOBAL_REQUEST, MSG_REQUEST_SUCCESS, MSG_REQUEST_FAILURE = range(80, 83)
|
|
|
3ef272 |
MSG_CHANNEL_OPEN, MSG_CHANNEL_OPEN_SUCCESS, MSG_CHANNEL_OPEN_FAILURE, \
|
|
|
3ef272 |
MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_DATA, MSG_CHANNEL_EXTENDED_DATA, \
|
|
|
3ef272 |
diff --git a/paramiko/transport.py b/paramiko/transport.py
|
|
|
3ef272 |
index 7906c9f..31df82a 100644
|
|
|
3ef272 |
--- a/paramiko/transport.py
|
|
|
3ef272 |
+++ b/paramiko/transport.py
|
|
|
3ef272 |
@@ -49,7 +49,8 @@ from paramiko.common import xffffffff, cMSG_CHANNEL_OPEN, cMSG_IGNORE, \
|
|
|
3ef272 |
MSG_CHANNEL_SUCCESS, MSG_CHANNEL_FAILURE, MSG_CHANNEL_DATA, \
|
|
|
3ef272 |
MSG_CHANNEL_EXTENDED_DATA, MSG_CHANNEL_WINDOW_ADJUST, MSG_CHANNEL_REQUEST, \
|
|
|
3ef272 |
MSG_CHANNEL_EOF, MSG_CHANNEL_CLOSE, MIN_WINDOW_SIZE, MIN_PACKET_SIZE, \
|
|
|
3ef272 |
- MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE
|
|
|
3ef272 |
+ MAX_WINDOW_SIZE, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_PACKET_SIZE, \
|
|
|
3ef272 |
+ HIGHEST_USERAUTH_MESSAGE_ID
|
|
|
3ef272 |
from paramiko.compress import ZlibCompressor, ZlibDecompressor
|
|
|
3ef272 |
from paramiko.dsskey import DSSKey
|
|
|
3ef272 |
from paramiko.kex_gex import KexGex, KexGexSHA256
|
|
|
3ef272 |
@@ -1720,6 +1721,43 @@ class Transport (threading.Thread, ClosingContextManager):
|
|
|
3ef272 |
max_packet_size = self.default_max_packet_size
|
|
|
3ef272 |
return clamp_value(MIN_PACKET_SIZE, max_packet_size, MAX_WINDOW_SIZE)
|
|
|
3ef272 |
|
|
|
3ef272 |
+ def _ensure_authed(self, ptype, message):
|
|
|
3ef272 |
+ """
|
|
|
3ef272 |
+ Checks message type against current auth state.
|
|
|
3ef272 |
+
|
|
|
3ef272 |
+ If server mode, and auth has not succeeded, and the message is of a
|
|
|
3ef272 |
+ post-auth type (channel open or global request) an appropriate error
|
|
|
3ef272 |
+ response Message is crafted and returned to caller for sending.
|
|
|
3ef272 |
+
|
|
|
3ef272 |
+ Otherwise (client mode, authed, or pre-auth message) returns None.
|
|
|
3ef272 |
+ """
|
|
|
3ef272 |
+ if (
|
|
|
3ef272 |
+ not self.server_mode
|
|
|
3ef272 |
+ or ptype <= HIGHEST_USERAUTH_MESSAGE_ID
|
|
|
3ef272 |
+ or self.is_authenticated()
|
|
|
3ef272 |
+ ):
|
|
|
3ef272 |
+ return None
|
|
|
3ef272 |
+ # WELP. We must be dealing with someone trying to do non-auth things
|
|
|
3ef272 |
+ # without being authed. Tell them off, based on message class.
|
|
|
3ef272 |
+ reply = Message()
|
|
|
3ef272 |
+ # Global requests have no details, just failure.
|
|
|
3ef272 |
+ if ptype == MSG_GLOBAL_REQUEST:
|
|
|
3ef272 |
+ reply.add_byte(cMSG_REQUEST_FAILURE)
|
|
|
3ef272 |
+ # Channel opens let us reject w/ a specific type + message.
|
|
|
3ef272 |
+ elif ptype == MSG_CHANNEL_OPEN:
|
|
|
3ef272 |
+ kind = message.get_text()
|
|
|
3ef272 |
+ chanid = message.get_int()
|
|
|
3ef272 |
+ reply.add_byte(cMSG_CHANNEL_OPEN_FAILURE)
|
|
|
3ef272 |
+ reply.add_int(chanid)
|
|
|
3ef272 |
+ reply.add_int(OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED)
|
|
|
3ef272 |
+ reply.add_string('')
|
|
|
3ef272 |
+ reply.add_string('en')
|
|
|
3ef272 |
+ # NOTE: Post-open channel messages do not need checking; the above will
|
|
|
3ef272 |
+ # reject attemps to open channels, meaning that even if a malicious
|
|
|
3ef272 |
+ # user tries to send a MSG_CHANNEL_REQUEST, it will simply fall under
|
|
|
3ef272 |
+ # the logic that handles unknown channel IDs (as the channel list will
|
|
|
3ef272 |
+ # be empty.)
|
|
|
3ef272 |
+ return reply
|
|
|
3ef272 |
|
|
|
3ef272 |
def run(self):
|
|
|
3ef272 |
# (use the exposed "run" method, because if we specify a thread target
|
|
|
3ef272 |
@@ -1779,7 +1817,11 @@ class Transport (threading.Thread, ClosingContextManager):
|
|
|
3ef272 |
continue
|
|
|
3ef272 |
|
|
|
3ef272 |
if ptype in self._handler_table:
|
|
|
3ef272 |
- self._handler_table[ptype](self, m)
|
|
|
3ef272 |
+ error_msg = self._ensure_authed(ptype, m)
|
|
|
3ef272 |
+ if error_msg:
|
|
|
3ef272 |
+ self._send_message(error_msg)
|
|
|
3ef272 |
+ else:
|
|
|
3ef272 |
+ self._handler_table[ptype](self, m)
|
|
|
3ef272 |
elif ptype in self._channel_handler_table:
|
|
|
3ef272 |
chanid = m.get_int()
|
|
|
3ef272 |
chan = self._channels.get(chanid)
|
|
|
3ef272 |
diff --git a/tests/test_transport.py b/tests/test_transport.py
|
|
|
3ef272 |
index d81ad8f..1305cd5 100644
|
|
|
3ef272 |
--- a/tests/test_transport.py
|
|
|
3ef272 |
+++ b/tests/test_transport.py
|
|
|
3ef272 |
@@ -32,7 +32,7 @@ from hashlib import sha1
|
|
|
3ef272 |
import unittest
|
|
|
3ef272 |
|
|
|
3ef272 |
from paramiko import Transport, SecurityOptions, ServerInterface, RSAKey, DSSKey, \
|
|
|
3ef272 |
- SSHException, ChannelException, Packetizer
|
|
|
3ef272 |
+ SSHException, ChannelException, Packetizer, Channel
|
|
|
3ef272 |
from paramiko import AUTH_FAILED, AUTH_SUCCESSFUL
|
|
|
3ef272 |
from paramiko import OPEN_SUCCEEDED, OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
|
|
|
3ef272 |
from paramiko.common import MSG_KEXINIT, cMSG_CHANNEL_WINDOW_ADJUST, \
|
|
|
3ef272 |
@@ -87,7 +87,11 @@ class NullServer (ServerInterface):
|
|
|
3ef272 |
|
|
|
3ef272 |
def check_global_request(self, kind, msg):
|
|
|
3ef272 |
self._global_request = kind
|
|
|
3ef272 |
- return False
|
|
|
3ef272 |
+ # NOTE: for w/e reason, older impl of this returned False always, even
|
|
|
3ef272 |
+ # tho that's only supposed to occur if the request cannot be served.
|
|
|
3ef272 |
+ # For now, leaving that the default unless test supplies specific
|
|
|
3ef272 |
+ # 'acceptable' request kind
|
|
|
3ef272 |
+ return kind == 'acceptable'
|
|
|
3ef272 |
|
|
|
3ef272 |
def check_channel_x11_request(self, channel, single_connection, auth_protocol, auth_cookie, screen_number):
|
|
|
3ef272 |
self._x11_single_connection = single_connection
|
|
|
3ef272 |
@@ -125,7 +129,9 @@ class TransportTest(unittest.TestCase):
|
|
|
3ef272 |
self.socks.close()
|
|
|
3ef272 |
self.sockc.close()
|
|
|
3ef272 |
|
|
|
3ef272 |
- def setup_test_server(self, client_options=None, server_options=None):
|
|
|
3ef272 |
+ def setup_test_server(
|
|
|
3ef272 |
+ self, client_options=None, server_options=None, connect_kwargs=None,
|
|
|
3ef272 |
+ ):
|
|
|
3ef272 |
host_key = RSAKey.from_private_key_file(test_path('test_rsa.key'))
|
|
|
3ef272 |
public_host_key = RSAKey(data=host_key.asbytes())
|
|
|
3ef272 |
self.ts.add_server_key(host_key)
|
|
|
3ef272 |
@@ -139,8 +145,13 @@ class TransportTest(unittest.TestCase):
|
|
|
3ef272 |
self.server = NullServer()
|
|
|
3ef272 |
self.assertTrue(not event.is_set())
|
|
|
3ef272 |
self.ts.start_server(event, self.server)
|
|
|
3ef272 |
- self.tc.connect(hostkey=public_host_key,
|
|
|
3ef272 |
- username='slowdive', password='pygmalion')
|
|
|
3ef272 |
+ if connect_kwargs is None:
|
|
|
3ef272 |
+ connect_kwargs = dict(
|
|
|
3ef272 |
+ hostkey=public_host_key,
|
|
|
3ef272 |
+ username='slowdive',
|
|
|
3ef272 |
+ password='pygmalion',
|
|
|
3ef272 |
+ )
|
|
|
3ef272 |
+ self.tc.connect(**connect_kwargs)
|
|
|
3ef272 |
event.wait(1.0)
|
|
|
3ef272 |
self.assertTrue(event.is_set())
|
|
|
3ef272 |
self.assertTrue(self.ts.is_active())
|
|
|
3ef272 |
@@ -846,3 +857,37 @@ class TransportTest(unittest.TestCase):
|
|
|
3ef272 |
self.assertEqual([chan], r)
|
|
|
3ef272 |
self.assertEqual([], w)
|
|
|
3ef272 |
self.assertEqual([], e)
|
|
|
3ef272 |
+
|
|
|
3ef272 |
+ def test_server_rejects_open_channel_without_auth(self):
|
|
|
3ef272 |
+ try:
|
|
|
3ef272 |
+ self.setup_test_server(connect_kwargs={})
|
|
|
3ef272 |
+ self.tc.open_session()
|
|
|
3ef272 |
+ except ChannelException as e:
|
|
|
3ef272 |
+ assert e.code == OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
|
|
|
3ef272 |
+ else:
|
|
|
3ef272 |
+ assert False, "Did not raise ChannelException!"
|
|
|
3ef272 |
+
|
|
|
3ef272 |
+ def test_server_rejects_arbitrary_global_request_without_auth(self):
|
|
|
3ef272 |
+ self.setup_test_server(connect_kwargs={})
|
|
|
3ef272 |
+ # NOTE: this dummy global request kind would normally pass muster
|
|
|
3ef272 |
+ # from the test server.
|
|
|
3ef272 |
+ self.tc.global_request('acceptable')
|
|
|
3ef272 |
+ # Global requests never raise exceptions, even on failure (not sure why
|
|
|
3ef272 |
+ # this was the original design...ugh.) Best we can do to tell failure
|
|
|
3ef272 |
+ # happened is that the client transport's global_response was set back
|
|
|
3ef272 |
+ # to None; if it had succeeded, it would be the response Message.
|
|
|
3ef272 |
+ err = "Unauthed global response incorrectly succeeded!"
|
|
|
3ef272 |
+ assert self.tc.global_response is None, err
|
|
|
3ef272 |
+
|
|
|
3ef272 |
+ def test_server_rejects_port_forward_without_auth(self):
|
|
|
3ef272 |
+ # NOTE: at protocol level port forward requests are treated same as a
|
|
|
3ef272 |
+ # regular global request, but Paramiko server implements a special-case
|
|
|
3ef272 |
+ # method for it, so it gets its own test. (plus, THAT actually raises
|
|
|
3ef272 |
+ # an exception on the client side, unlike the general case...)
|
|
|
3ef272 |
+ self.setup_test_server(connect_kwargs={})
|
|
|
3ef272 |
+ try:
|
|
|
3ef272 |
+ self.tc.request_port_forward('localhost', 1234)
|
|
|
3ef272 |
+ except SSHException as e:
|
|
|
3ef272 |
+ assert "forwarding request denied" in str(e)
|
|
|
3ef272 |
+ else:
|
|
|
3ef272 |
+ assert False, "Did not raise SSHException!"
|