Beispiel #1
0
def test_client_greeting_assert_socks5():
    raw = tutils.treader(b"\x00\x00")
    msg = socks.ClientGreeting.from_file(raw)
    with pytest.raises(socks.SocksError):
        msg.assert_socks5()

    raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
    msg = socks.ClientGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" not in str(e)
    else:
        assert False

    raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
    msg = socks.ClientGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" in str(e)
    else:
        assert False

    raw = tutils.treader(b"XX")
    with pytest.raises(socks.SocksError):
        socks.ClientGreeting.from_file(raw, fail_early=True)
Beispiel #2
0
def test_client_greeting_assert_socks5():
    raw = tutils.treader(b"\x00\x00")
    msg = socks.ClientGreeting.from_file(raw)
    with pytest.raises(socks.SocksError):
        msg.assert_socks5()

    raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
    msg = socks.ClientGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" not in str(e)
    else:
        assert False

    raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
    msg = socks.ClientGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" in str(e)
    else:
        assert False

    raw = tutils.treader(b"XX")
    with pytest.raises(socks.SocksError):
        socks.ClientGreeting.from_file(raw, fail_early=True)
    def test_socks_connect(self):
        to = ("foobar", 80)
        c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None)
        c.rfile, c.wfile = tutils.treader(b""), io.BytesIO()
        with pytest.raises(pathoc.PathocError):
            c.socks_connect(to)

        c.rfile = tutils.treader(
            b"\x05\xEE"
        )
        with pytest.raises(Exception, match="SOCKS without authentication"):
            c.socks_connect(("example.com", 0xDEAD))

        c.rfile = tutils.treader(
            b"\x05\x00" +
            b"\x05\xEE\x00\x03\x0bexample.com\xDE\xAD"
        )
        with pytest.raises(Exception, match="SOCKS server error"):
            c.socks_connect(("example.com", 0xDEAD))

        c.rfile = tutils.treader(
            b"\x05\x00" +
            b"\x05\x00\x00\x03\x0bexample.com\xDE\xAD"
        )
        c.socks_connect(("example.com", 0xDEAD))
Beispiel #4
0
    def test_socks_connect(self):
        to = ("foobar", 80)
        c = pathoc.Pathoc(("127.0.0.1", self.d.port), fp=None)
        c.rfile, c.wfile = tutils.treader(b""), io.BytesIO()
        with pytest.raises(pathoc.PathocError):
            c.socks_connect(to)

        c.rfile = tutils.treader(
            b"\x05\xEE"
        )
        with pytest.raises(Exception, match="SOCKS without authentication"):
            c.socks_connect(("example.com", 0xDEAD))

        c.rfile = tutils.treader(
            b"\x05\x00" +
            b"\x05\xEE\x00\x03\x0bexample.com\xDE\xAD"
        )
        with pytest.raises(Exception, match="SOCKS server error"):
            c.socks_connect(("example.com", 0xDEAD))

        c.rfile = tutils.treader(
            b"\x05\x00" +
            b"\x05\x00\x00\x03\x0bexample.com\xDE\xAD"
        )
        c.socks_connect(("example.com", 0xDEAD))
Beispiel #5
0
def test_message_unknown_atyp():
    raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError):
        socks.Message.from_file(raw)

    m = socks.Message(5, 1, 0x02, ("example.com", 5050))
    with pytest.raises(socks.SocksError):
        m.to_file(BytesIO())
Beispiel #6
0
def test_message_unknown_atyp():
    raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError):
        socks.Message.from_file(raw)

    m = socks.Message(5, 1, 0x02, ("example.com", 5050))
    with pytest.raises(socks.SocksError):
        m.to_file(BytesIO())
Beispiel #7
0
def test_server_greeting():
    raw = tutils.treader(b"\x05\x02")
    out = BytesIO()
    msg = socks.ServerGreeting.from_file(raw)
    msg.assert_socks5()
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()
    assert msg.ver == 5
    assert msg.method == 0x02
Beispiel #8
0
def test_server_greeting():
    raw = tutils.treader(b"\x05\x02")
    out = BytesIO()
    msg = socks.ServerGreeting.from_file(raw)
    msg.assert_socks5()
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()
    assert msg.ver == 5
    assert msg.method == 0x02
Beispiel #9
0
def test_message_ipv4():
    # Test ATYP=0x01 (IPV4)
    raw = tutils.treader(b"\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    left = raw.read(2)
    assert left == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.addr == ("127.0.0.1", 0xDEAD)
Beispiel #10
0
def test_username_password_auth_response():
    raw = tutils.treader(b"\x01\x00\xBE\xEF")
    out = BytesIO()
    auth = socks.UsernamePasswordAuthResponse.from_file(raw)
    auth.assert_authver1()
    assert raw.read(2) == b"\xBE\xEF"
    auth.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert auth.ver == socks.USERNAME_PASSWORD_VERSION.DEFAULT
    assert auth.status == 0
Beispiel #11
0
def test_username_password_auth_response():
    raw = tutils.treader(b"\x01\x00\xBE\xEF")
    out = BytesIO()
    auth = socks.UsernamePasswordAuthResponse.from_file(raw)
    auth.assert_authver1()
    assert raw.read(2) == b"\xBE\xEF"
    auth.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert auth.ver == socks.USERNAME_PASSWORD_VERSION.DEFAULT
    assert auth.status == 0
Beispiel #12
0
def test_server_greeting_assert_socks5():
    raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
    msg = socks.ServerGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" in str(e)
    else:
        assert False

    raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
    msg = socks.ServerGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" not in str(e)
    else:
        assert False
Beispiel #13
0
def test_server_greeting_assert_socks5():
    raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
    msg = socks.ServerGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" in str(e)
    else:
        assert False

    raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
    msg = socks.ServerGreeting.from_file(raw)
    try:
        msg.assert_socks5()
    except socks.SocksError as e:
        assert "Invalid SOCKS version" in str(e)
        assert "HTTP" not in str(e)
    else:
        assert False
Beispiel #14
0
def test_message_ipv4():
    # Test ATYP=0x01 (IPV4)
    raw = tutils.treader(b"\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    left = raw.read(2)
    assert left == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.addr == ("127.0.0.1", 0xDEAD)
Beispiel #15
0
def test_username_password_auth():
    raw = tutils.treader(b"\x01\x03usr\x03psd\xBE\xEF")
    out = BytesIO()
    auth = socks.UsernamePasswordAuth.from_file(raw)
    auth.assert_authver1()
    assert raw.read(2) == b"\xBE\xEF"
    auth.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert auth.ver == socks.USERNAME_PASSWORD_VERSION.DEFAULT
    assert auth.username == "usr"
    assert auth.password == "psd"
Beispiel #16
0
def test_client_greeting():
    raw = tutils.treader(b"\x05\x02\x00\xBE\xEF")
    out = BytesIO()
    msg = socks.ClientGreeting.from_file(raw)
    msg.assert_socks5()
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-1]
    assert msg.ver == 5
    assert len(msg.methods) == 2
    assert 0xBE in msg.methods
    assert 0xEF not in msg.methods
Beispiel #17
0
def test_client_greeting():
    raw = tutils.treader(b"\x05\x02\x00\xBE\xEF")
    out = BytesIO()
    msg = socks.ClientGreeting.from_file(raw)
    msg.assert_socks5()
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-1]
    assert msg.ver == 5
    assert len(msg.methods) == 2
    assert 0xBE in msg.methods
    assert 0xEF not in msg.methods
Beispiel #18
0
def test_username_password_auth():
    raw = tutils.treader(b"\x01\x03usr\x03psd\xBE\xEF")
    out = BytesIO()
    auth = socks.UsernamePasswordAuth.from_file(raw)
    auth.assert_authver1()
    assert raw.read(2) == b"\xBE\xEF"
    auth.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert auth.ver == socks.USERNAME_PASSWORD_VERSION.DEFAULT
    assert auth.username == "usr"
    assert auth.password == "psd"
Beispiel #19
0
def test_message():
    raw = tutils.treader(b"\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    msg.assert_socks5()
    assert raw.read(2) == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.ver == 5
    assert msg.msg == 0x01
    assert msg.atyp == 0x03
    assert msg.addr == ("example.com", 0xDEAD)
Beispiel #20
0
def test_message():
    raw = tutils.treader(b"\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    msg.assert_socks5()
    assert raw.read(2) == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.ver == 5
    assert msg.msg == 0x01
    assert msg.atyp == 0x03
    assert msg.addr == ("example.com", 0xDEAD)
Beispiel #21
0
def test_message_ipv6():
    # Test ATYP=0x04 (IPV6)
    ipv6_addr = "2001:db8:85a3:8d3:1319:8a2e:370:7344"

    raw = tutils.treader(b"\x05\x01\x00\x04" +
                         ipaddress.IPv6Address(ipv6_addr).packed +
                         b"\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    assert raw.read(2) == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.addr[0] == ipv6_addr
Beispiel #22
0
def test_message_ipv6():
    # Test ATYP=0x04 (IPV6)
    ipv6_addr = u"2001:db8:85a3:8d3:1319:8a2e:370:7344"

    raw = tutils.treader(
        b"\x05\x01\x00\x04" +
        ipaddress.IPv6Address(ipv6_addr).packed +
        b"\xDE\xAD\xBE\xEF")
    out = BytesIO()
    msg = socks.Message.from_file(raw)
    assert raw.read(2) == b"\xBE\xEF"
    msg.to_file(out)

    assert out.getvalue() == raw.getvalue()[:-2]
    assert msg.addr[0] == ipv6_addr
Beispiel #23
0
 def round(*args, **kwargs):
     f = websockets.Frame(*args, **kwargs)
     raw = bytes(f)
     f2 = websockets.Frame.from_file(tutils.treader(raw))
     assert f == f2
Beispiel #24
0
 def test_funky(self):
     f = websockets.FrameHeader(masking_key=b"test", mask=False)
     raw = bytes(f)
     f2 = websockets.FrameHeader.from_file(tutils.treader(raw))
     assert not f2.mask
Beispiel #25
0
def test_message_unknown_atyp():
    raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    tutils.raises(socks.SocksError, socks.Message.from_file, raw)

    m = socks.Message(5, 1, 0x02, tcp.Address(("example.com", 5050)))
    tutils.raises(socks.SocksError, m.to_file, BytesIO())
Beispiel #26
0
def test_message_assert_socks5():
    raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
    msg = socks.Message.from_file(raw)
    with pytest.raises(socks.SocksError):
        msg.assert_socks5()
Beispiel #27
0
def test_message_unknown_atyp():
    raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    tutils.raises(socks.SocksError, socks.Message.from_file, raw)

    m = socks.Message(5, 1, 0x02, tcp.Address(("example.com", 5050)))
    tutils.raises(socks.SocksError, m.to_file, BytesIO())
Beispiel #28
0
def test_username_password_auth_assert_ver1():
    raw = tutils.treader(b"\x02\x03usr\x03psd\xBE\xEF")
    auth = socks.UsernamePasswordAuth.from_file(raw)
    tutils.raises(socks.SocksError, auth.assert_authver1)
Beispiel #29
0
def test_message_invalid_rsv():
    raw = tutils.treader(b"\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError):
        socks.Message.from_file(raw)
Beispiel #30
0
def test_username_password_auth_response_auth_assert_ver1():
    raw = tutils.treader(b"\x02\x00\xBE\xEF")
    auth = socks.UsernamePasswordAuthResponse.from_file(raw)
    with pytest.raises(socks.SocksError):
        auth.assert_authver1()
Beispiel #31
0
def test_message_invalid_rsv():
    raw = tutils.treader(b"\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError):
        socks.Message.from_file(raw)
Beispiel #32
0
 def round(*args, **kwargs):
     h = websockets.FrameHeader(*args, **kwargs)
     h2 = websockets.FrameHeader.from_file(tutils.treader(bytes(h)))
     assert h == h2
Beispiel #33
0
 def test_deserialization_length(self, input, expected):
     h = websockets.FrameHeader.from_file(tutils.treader(codecs.decode(input, 'hex')))
     assert h.payload_length == expected
Beispiel #34
0
def test_message_assert_socks5():
    raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
    msg = socks.Message.from_file(raw)
    with pytest.raises(socks.SocksError):
        msg.assert_socks5()
Beispiel #35
0
def test_username_password_auth_response_auth_assert_ver1():
    raw = tutils.treader(b"\x02\x00\xBE\xEF")
    auth = socks.UsernamePasswordAuthResponse.from_file(raw)
    with pytest.raises(socks.SocksError):
        auth.assert_authver1()
Beispiel #36
0
 def test_deserialization_masking(self, input, expected):
     h = websockets.FrameHeader.from_file(tutils.treader(codecs.decode(input, 'hex')))
     assert h.mask == expected[0]
     if h.mask:
         assert h.masking_key == codecs.decode(expected[1], 'hex')
Beispiel #37
0
def test_message_invalid_host():
    raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample@com\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError,
                       match="Invalid hostname: b'example@com'"):
        socks.Message.from_file(raw)
Beispiel #38
0
def test_username_password_auth_assert_ver1():
    raw = tutils.treader(b"\x02\x03usr\x03psd\xBE\xEF")
    auth = socks.UsernamePasswordAuth.from_file(raw)
    tutils.raises(socks.SocksError, auth.assert_authver1)
Beispiel #39
0
def test_message_invalid_host():
    raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample@com\xDE\xAD\xBE\xEF")
    with pytest.raises(socks.SocksError, match="Invalid hostname: b'example@com'"):
        socks.Message.from_file(raw)