2016-10-17 19:31:13 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2016-10-25 16:19:08 +02:00
|
|
|
import socket
|
2016-11-02 12:18:03 +01:00
|
|
|
import errno
|
2016-10-25 16:19:08 +02:00
|
|
|
import pytest
|
2016-10-17 19:31:13 +02:00
|
|
|
|
|
|
|
|
2016-10-25 16:19:08 +02:00
|
|
|
# pylint: disable=attribute-defined-outside-init
|
2016-10-17 19:31:13 +02:00
|
|
|
class TestErrors(object):
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def init(self, ssh_audit):
|
|
|
|
self.AuditConf = ssh_audit.AuditConf
|
|
|
|
self.audit = ssh_audit.audit
|
|
|
|
|
|
|
|
def _conf(self):
|
|
|
|
conf = self.AuditConf('localhost', 22)
|
2016-10-19 19:53:47 +02:00
|
|
|
conf.colors = False
|
2016-10-17 19:31:13 +02:00
|
|
|
conf.batch = True
|
|
|
|
return conf
|
|
|
|
|
2016-11-02 12:00:24 +01:00
|
|
|
def _audit(self, spy, conf=None, sysexit=True):
|
|
|
|
if conf is None:
|
|
|
|
conf = self._conf()
|
|
|
|
spy.begin()
|
|
|
|
if sysexit:
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
self.audit(conf)
|
|
|
|
else:
|
|
|
|
self.audit(conf)
|
|
|
|
lines = spy.flush()
|
|
|
|
return lines
|
|
|
|
|
2016-11-02 18:28:16 +01:00
|
|
|
def test_connection_unresolved(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.gsock.addrinfodata['localhost#22'] = []
|
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'has no DNS records' in lines[-1]
|
|
|
|
|
2016-10-17 19:31:13 +02:00
|
|
|
def test_connection_refused(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
2016-11-02 12:18:03 +01:00
|
|
|
vsocket.errors['connect'] = socket.error(errno.ECONNREFUSED, 'Connection refused')
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'Connection refused' in lines[-1]
|
|
|
|
|
2016-11-02 12:00:24 +01:00
|
|
|
def test_connection_timeout(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.errors['connect'] = socket.timeout('timed out')
|
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'timed out' in lines[-1]
|
|
|
|
|
|
|
|
def test_recv_empty(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
|
|
|
|
|
|
|
def test_recv_timeout(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(socket.timeout('timed out'))
|
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
|
|
|
assert 'timed out' in lines[-1]
|
|
|
|
|
|
|
|
def test_recv_retry_till_timeout(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
2016-11-02 12:18:03 +01:00
|
|
|
vsocket.rdata.append(socket.error(errno.EAGAIN, 'Resource temporarily unavailable'))
|
|
|
|
vsocket.rdata.append(socket.error(errno.EWOULDBLOCK, 'Resource temporarily unavailable'))
|
|
|
|
vsocket.rdata.append(socket.error(errno.EAGAIN, 'Resource temporarily unavailable'))
|
2016-11-02 12:00:24 +01:00
|
|
|
vsocket.rdata.append(socket.timeout('timed out'))
|
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
|
|
|
assert 'timed out' in lines[-1]
|
|
|
|
|
|
|
|
def test_recv_retry_till_reset(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
2016-11-02 12:18:03 +01:00
|
|
|
vsocket.rdata.append(socket.error(errno.EAGAIN, 'Resource temporarily unavailable'))
|
|
|
|
vsocket.rdata.append(socket.error(errno.EWOULDBLOCK, 'Resource temporarily unavailable'))
|
|
|
|
vsocket.rdata.append(socket.error(errno.EAGAIN, 'Resource temporarily unavailable'))
|
|
|
|
vsocket.rdata.append(socket.error(errno.ECONNRESET, 'Connection reset by peer'))
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
|
|
|
assert 'reset by peer' in lines[-1]
|
|
|
|
|
2016-10-17 19:31:13 +02:00
|
|
|
def test_connection_closed_before_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
2016-11-02 12:18:03 +01:00
|
|
|
vsocket.rdata.append(socket.error(errno.ECONNRESET, 'Connection reset by peer'))
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 1
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
2016-11-02 12:00:24 +01:00
|
|
|
assert 'reset by peer' in lines[-1]
|
2016-10-17 19:31:13 +02:00
|
|
|
|
|
|
|
def test_connection_closed_after_header(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'header line 1\n')
|
2016-11-02 18:28:16 +01:00
|
|
|
vsocket.rdata.append(b'\n')
|
2016-10-17 19:31:13 +02:00
|
|
|
vsocket.rdata.append(b'header line 2\n')
|
2016-11-02 12:18:03 +01:00
|
|
|
vsocket.rdata.append(socket.error(errno.ECONNRESET, 'Connection reset by peer'))
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 3
|
|
|
|
assert 'did not receive banner' in lines[-1]
|
2016-11-02 12:00:24 +01:00
|
|
|
assert 'reset by peer' in lines[-1]
|
2016-10-17 19:31:13 +02:00
|
|
|
|
|
|
|
def test_connection_closed_after_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-2.0-ssh-audit-test\r\n')
|
|
|
|
vsocket.rdata.append(socket.error(54, 'Connection reset by peer'))
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 2
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert 'reset by peer' in lines[-1]
|
|
|
|
|
|
|
|
def test_empty_data_after_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-2.0-ssh-audit-test\r\n')
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 2
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert 'empty' in lines[-1]
|
|
|
|
|
|
|
|
def test_wrong_data_after_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-2.0-ssh-audit-test\r\n')
|
|
|
|
vsocket.rdata.append(b'xxx\n')
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 2
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert 'xxx' in lines[-1]
|
|
|
|
|
2016-10-19 19:53:47 +02:00
|
|
|
def test_non_ascii_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-2.0-ssh-audit-test\xc3\xbc\r\n')
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-19 19:53:47 +02:00
|
|
|
assert len(lines) == 3
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert 'ASCII' in lines[-2]
|
|
|
|
assert lines[-3].endswith('SSH-2.0-ssh-audit-test?')
|
|
|
|
|
2016-10-18 08:45:03 +02:00
|
|
|
def test_nonutf8_data_after_banner(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-2.0-ssh-audit-test\r\n')
|
|
|
|
vsocket.rdata.append(b'\x81\xff\n')
|
2016-11-02 12:00:24 +01:00
|
|
|
lines = self._audit(output_spy)
|
2016-10-18 08:45:03 +02:00
|
|
|
assert len(lines) == 2
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert '\\x81\\xff' in lines[-1]
|
|
|
|
|
2016-10-17 19:31:13 +02:00
|
|
|
def test_protocol_mismatch_by_conf(self, output_spy, virtual_socket):
|
|
|
|
vsocket = virtual_socket
|
|
|
|
vsocket.rdata.append(b'SSH-1.3-ssh-audit-test\r\n')
|
|
|
|
vsocket.rdata.append(b'Protocol major versions differ.\n')
|
2016-11-02 12:00:24 +01:00
|
|
|
conf = self._conf()
|
|
|
|
conf.ssh1, conf.ssh2 = True, False
|
|
|
|
lines = self._audit(output_spy, conf)
|
2016-10-17 19:31:13 +02:00
|
|
|
assert len(lines) == 3
|
|
|
|
assert 'error reading packet' in lines[-1]
|
|
|
|
assert 'major versions differ' in lines[-1]
|