romis2012 / python-socks

Core proxy client (SOCKS4, SOCKS5, HTTP) functionality for Python
Apache License 2.0
101 stars 18 forks source link

unstable SOCKS5 parsing causes ValueError #32

Closed RTUnreal closed 4 months ago

RTUnreal commented 4 months ago

Hello, I am using this package for doing my SOCKS5 stuff, but sporadically I get random ValueErrors:

  File \"/home/checker/.cache/pypoetry/virtualenvs/onlyflags-checker-SS4sQMzU-py3.11/lib/python3.11/site-packages/python_socks/async_/asyncio/_proxy.py\", line 59, in connect
    return await self._connect(
           ^^^^^^^^^^^^^^^^^^^^
  File \"/home/checker/.cache/pypoetry/virtualenvs/onlyflags-checker-SS4sQMzU-py3.11/lib/python3.11/site-packages/python_socks/async_/asyncio/_proxy.py\", line 93, in _connect
    await connector.connect(
  File \"/home/checker/.cache/pypoetry/virtualenvs/onlyflags-checker-SS4sQMzU-py3.11/lib/python3.11/site-packages/python_socks/_connectors/socks5_async.py\", line 69, in connect
    reply: socks5.ConnectReply = conn.receive(data)
                                 ^^^^^^^^^^^^^^^^^^
  File \"/home/checker/.cache/pypoetry/virtualenvs/onlyflags-checker-SS4sQMzU-py3.11/lib/python3.11/site-packages/python_socks/_protocols/socks5.py\", line 347, in receive
    reply = ConnectReply.loads(data)
            ^^^^^^^^^^^^^^^^^^^^^^^^
  File \"/home/checker/.cache/pypoetry/virtualenvs/onlyflags-checker-SS4sQMzU-py3.11/lib/python3.11/site-packages/python_socks/_protocols/socks5.py\", line 227, in loads
    bnd_host = socket.inet_ntop(socket.AF_INET, bnd_host_data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid length of packed IP address string

https://github.com/romis2012/python-socks/blob/ffd330bc1fdbeed48bbbf8b1e0da5e9713726ff0/python_socks/_protocols/socks5.py#L227

I, personally, blame the unstable parsing beforehand and would recommend using struct to make parsing better.

https://github.com/romis2012/python-socks/blob/ffd330bc1fdbeed48bbbf8b1e0da5e9713726ff0/python_socks/_protocols/socks5.py#L219-L224

I don't know, how I would easily reproduce it, as the server it connects to is bespoke and I can't reveal the source code atm. If there are test I should run, then I could do that.

Thank you