mitmproxy/test/netlib/test_socks.py

189 lines
5.3 KiB
Python
Raw Normal View History

2015-09-20 17:40:09 +00:00
import ipaddress
2015-09-17 14:31:50 +00:00
from io import BytesIO
2014-06-25 18:31:28 +00:00
import socket
2015-08-01 12:49:15 +00:00
from netlib import socks, tcp, tutils
2014-06-25 18:31:28 +00:00
def test_client_greeting():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x02\x00\xBE\xEF")
out = BytesIO()
2014-06-25 18:31:28 +00:00
msg = socks.ClientGreeting.from_file(raw)
2015-07-03 00:01:30 +00:00
msg.assert_socks5()
2014-06-25 18:31:28 +00:00
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
2015-07-03 00:01:30 +00:00
def test_client_greeting_assert_socks5():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x00\x00")
2015-07-03 00:01:30 +00:00
msg = socks.ClientGreeting.from_file(raw)
tutils.raises(socks.SocksError, msg.assert_socks5)
2015-09-20 16:12:55 +00:00
raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
2015-07-03 00:01:30 +00:00
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
2015-09-20 17:40:09 +00:00
raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
2015-07-03 00:01:30 +00:00
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
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"XX")
2015-08-10 18:44:36 +00:00
tutils.raises(
socks.SocksError,
socks.ClientGreeting.from_file,
raw,
fail_early=True)
2015-07-03 00:45:12 +00:00
2015-07-03 00:01:30 +00:00
2014-06-25 18:31:28 +00:00
def test_server_greeting():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x02")
out = BytesIO()
2014-06-25 18:31:28 +00:00
msg = socks.ServerGreeting.from_file(raw)
2015-07-03 00:01:30 +00:00
msg.assert_socks5()
2014-06-25 18:31:28 +00:00
msg.to_file(out)
assert out.getvalue() == raw.getvalue()
assert msg.ver == 5
assert msg.method == 0x02
2015-07-03 00:01:30 +00:00
def test_server_greeting_assert_socks5():
2015-09-20 17:40:09 +00:00
raw = tutils.treader(b"HTTP/1.1 200 OK" + b" " * 100)
2015-07-03 00:01:30 +00:00
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
2015-09-20 17:40:09 +00:00
raw = tutils.treader(b"GET / HTTP/1.1" + b" " * 100)
2015-07-03 00:01:30 +00:00
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
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"
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)
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
def test_username_password_auth_response_auth_assert_ver1():
raw = tutils.treader(b"\x02\x00\xBE\xEF")
auth = socks.UsernamePasswordAuthResponse.from_file(raw)
tutils.raises(socks.SocksError, auth.assert_authver1)
2014-06-25 18:31:28 +00:00
def test_message():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
out = BytesIO()
2014-06-25 18:31:28 +00:00
msg = socks.Message.from_file(raw)
2015-07-03 00:01:30 +00:00
msg.assert_socks5()
2015-09-17 14:31:50 +00:00
assert raw.read(2) == b"\xBE\xEF"
2014-06-25 18:31:28 +00:00
msg.to_file(out)
assert out.getvalue() == raw.getvalue()[:-2]
assert msg.ver == 5
assert msg.msg == 0x01
assert msg.atyp == 0x03
2015-09-20 17:40:09 +00:00
assert msg.addr == ("example.com", 0xDEAD)
2014-06-25 18:31:28 +00:00
2014-06-25 19:45:45 +00:00
2015-07-03 00:01:30 +00:00
def test_message_assert_socks5():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\xEE\x01\x00\x03\x0bexample.com\xDE\xAD\xBE\xEF")
2015-07-03 00:01:30 +00:00
msg = socks.Message.from_file(raw)
tutils.raises(socks.SocksError, msg.assert_socks5)
2014-06-25 19:45:45 +00:00
def test_message_ipv4():
2014-06-25 18:31:28 +00:00
# Test ATYP=0x01 (IPV4)
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x01\x00\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
out = BytesIO()
2014-06-25 18:31:28 +00:00
msg = socks.Message.from_file(raw)
2015-09-17 14:31:50 +00:00
left = raw.read(2)
assert left == b"\xBE\xEF"
2014-06-25 19:45:45 +00:00
msg.to_file(out)
assert out.getvalue() == raw.getvalue()[:-2]
2015-09-20 17:40:09 +00:00
assert msg.addr == ("127.0.0.1", 0xDEAD)
2014-06-25 18:31:28 +00:00
2014-06-25 19:16:47 +00:00
def test_message_ipv6():
2014-06-25 18:31:28 +00:00
# Test ATYP=0x04 (IPV6)
2015-09-20 17:56:45 +00:00
ipv6_addr = u"2001:db8:85a3:8d3:1319:8a2e:370:7344"
2014-06-25 19:45:45 +00:00
2015-05-30 00:02:58 +00:00
raw = tutils.treader(
2015-09-17 14:31:50 +00:00
b"\x05\x01\x00\x04" +
2015-09-20 17:40:09 +00:00
ipaddress.IPv6Address(ipv6_addr).packed +
2015-09-17 14:31:50 +00:00
b"\xDE\xAD\xBE\xEF")
out = BytesIO()
2014-06-25 18:31:28 +00:00
msg = socks.Message.from_file(raw)
2015-09-17 14:31:50 +00:00
assert raw.read(2) == b"\xBE\xEF"
2014-06-25 19:45:45 +00:00
msg.to_file(out)
assert out.getvalue() == raw.getvalue()[:-2]
assert msg.addr.host == ipv6_addr
def test_message_invalid_rsv():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x01\xFF\x01\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
2014-06-25 19:45:45 +00:00
tutils.raises(socks.SocksError, socks.Message.from_file, raw)
def test_message_unknown_atyp():
2015-09-17 14:31:50 +00:00
raw = tutils.treader(b"\x05\x02\x00\x02\x7f\x00\x00\x01\xDE\xAD\xBE\xEF")
2014-06-25 19:45:45 +00:00
tutils.raises(socks.SocksError, socks.Message.from_file, raw)
m = socks.Message(5, 1, 0x02, tcp.Address(("example.com", 5050)))
2015-09-17 14:31:50 +00:00
tutils.raises(socks.SocksError, m.to_file, BytesIO())