Skip to content

SOCKS proxy support, like 'ssh -D' #955

Open

Description

@Hoeze

Hi, I'd like to create a SOCKS proxy from an accessible SSH server, in order to pass Python Requests over that server.
Is it possible to do this using paramiko?

Activity

  1. bitprophet commented on May 9, 2017

    @bitprophet
    Member

    Thought there was an open feature req for SOCKS but all I see is #508 which mentions it in passing. Let's make it this ticket. Thanks for the report!

    I also saw some offhand references to using ProxyCommand support to talk to a SOCKS process; I don't use SOCKS myself so not sure how workable that is, but if you haven't already looked into that I'd suggest doing so.

  2. changed the title [-]Is there an equivalent to "ssh -D"?[/-] [+]SOCKS proxy support, like 'ssh -D'[/+] on May 9, 2017
  3. Cellebyte commented on Nov 11, 2017

    @Cellebyte
    • is their a way to hack it myself?
    • and if so, how can I achieve it.
    • need this feature purely dew to firewall restrictions.
  4. maxim-smirnov commented on Feb 11, 2018

    @maxim-smirnov

    Any updates here?

  5. Cellebyte commented on Feb 11, 2018

    @Cellebyte

    Look for your needs.

    • Do you really need Dynamic Forward?
    • I achieve my solution by using something like ssh -L in paramiko

    Problem

    • ssl certificate verification does not work with Local Forward because all your requests go to localhost.
  6. maxim-smirnov commented on Feb 11, 2018

    @maxim-smirnov

    @Cellebyte Yes, I really need socks functionality...

  7. utkonos commented on Jan 8, 2019

    @utkonos

    Support for dynamic port forwarding would be great!

  8. obikao commented on Apr 17, 2019

    @obikao

    Same

  9. ksantr commented on Jun 3, 2019

    @ksantr

    We need it

  10. hamadrehman commented on Jul 28, 2019

    @hamadrehman

    we need it

  11. linkpwd commented on Aug 11, 2019

    @linkpwd

    i need it .

  12. anarchy9388 commented on Aug 13, 2019

    @anarchy9388

    I need it!

  13. 15 remaining items

  14. Dunedan commented on May 30, 2021

    @Dunedan

    @bitprophet Sounds good. I'll do. 👍

    If anybody is wondering about the current status: I'm still working on getting the code into an upstreamable state, whenever I find some time. It might still take a few more weeks before I get to a state I'm satisfied with.

  15. anarchy9388 commented on Jun 3, 2021

    @anarchy9388

    @Dunedan Thanks!

  16. Dunedan commented on Jul 2, 2021

    @Dunedan

    I've finally found time to put the SOCKS proxy code into a PR, so here it is: #1873
    @bitprophet I'm looking forward to your review. 🙂

  17. FredAmouzgar commented on Sep 19, 2021

    @FredAmouzgar

    Hi @Dunedan,

    I wrote a small python app using this new branch, and it's ok for the most part. It connects to an SSH server and creates a socks proxy. It works fine when I set my OS network proxy setting to manual (Ubuntu 20) using ('localhost', 1080). But when I try to use a PAC file for automatic proxy, it raises the error below. I should mention that the same PAC file is completely functional when I use an 'ssh -D' command. It seems to me that it's unhappy about the struct.unpack on line 143, but I can't spot the issue. Would you please look into this and help me solve it? Many thanks.

    Exception happened during processing of request from ('127.0.0.1', 57916)
    Traceback (most recent call last):
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
    super(SOCKS5RequestHandler, self).init(*args, **kwargs)
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
    self.handle()
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 257, in handle
    version = m.get_char()
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 143, in get_char
    return byte_ord(struct.unpack("!c", self.get_bytes(1))[0])
    struct.error: unpack requires a buffer of 1 bytes

    PS: The contents of the PAC file which is accessed via a SimpleHttpServer:
    function FindProxyForURL(url, host)
    {
    return 'SOCKS localhost:1080; DIRECT';
    }

    Here's my code snippet for firing up the connection:

    self.server = paramiko.SSHClient()
    self.server.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    if using_password:
        self.server.connect(ip, username=user, password=pass, allow_agent=True)
    if using_key:
        k = paramiko.RSAKey.from_private_key_file(self.keyfile)
        self.server.connect(ip, username=user, pkey=k, allow_agent=True)
    self.server.open_socks_proxy(bind_address="localhost", port=1080)
  18. FredAmouzgar commented on Sep 19, 2021

    @FredAmouzgar

    I'm getting another exception that is not affecting the connection, but I thought there might be some value in pointing it out. What do you think is going wrong here?

    Secsh channel 29 open FAILED: open failed: Connect failed

    Exception happened during processing of request from ('127.0.0.1', 60522)
    Traceback (most recent call last):
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 239, in init
    super(SOCKS5RequestHandler, self).init(*args, **kwargs)
    File "/home/fred/anaconda3/lib/python3.8/socketserver.py", line 720, in init
    self.handle()
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 318, in handle
    channel, af, addr = self._open_channel(af_and_addrs)
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/socks_proxy.py", line 361, in _open_channel
    channel = self.server.ssh_transport.open_channel(
    File "/home/fred/anaconda3/lib/python3.8/site-packages/paramiko/transport.py", line 1017, in open_channel
    raise e
    paramiko.ssh_exception.ChannelException: ChannelException(2, 'Connect failed')

  19. Dunedan commented on Sep 19, 2021

    @Dunedan

    @FredAmouzgar To debug this, can you please provide the PAC file you're using and a recording of the TCP packets between the browser and the proxy?

  20. FredAmouzgar commented on Sep 19, 2021

    @FredAmouzgar

    I guess it'd be easier if I provided my code and you see it for yourself.
    Here's its Git repo. It's a simple GUI app. The primary purpose was to access our university server and IP address.

    https://github.com/FredAmouzgar/SshTunneller

  21. Dunedan commented on Sep 19, 2021

    @Dunedan

    You're using the branch git+https://github.com/yomagroup/paramiko.git@wip-socks-proxy, which is an older branch to test viability of the whole approach. The branch proposed for inclusion in paramiko as referenced in #1873 (yomagroup:add-socks-proxy) includes various improvements related to stability. Can you please try with the add-socks-proxy branch and check whether you get the same problem with it as well?

  22. FredAmouzgar commented on Sep 19, 2021

    @FredAmouzgar

    Thanks for your help and your quick reply. I tested this new branch, and it led to the same issues. I should also clarify that I'm not a network guru, and I may have made a mistake in setting up the PAC file through the HttpServer. After all, the SSH proxy is working fine when I manually access it. So, I play around with my code and let you know if I can resolve the issue.

  23. added a commit that references this issue on Oct 7, 2022
  24. su115 commented on Jan 22, 2025

    @su115

    What the hell is going on here? Why is this feature still here? I thought it was in the prod. True shame. I need this feature!!!!

  25. mfat commented on Aug 3, 2025

    @mfat

    Was this ever implemented?

  26. TX7083 commented on Nov 26, 2025

    @TX7083

    @Dunedan did you ever finish working on this feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions