Hack Forums
[C#] c# socket or tcpclient over with socks5 proxy - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] c# socket or tcpclient over with socks5 proxy (/showthread.php?tid=6017875)



c# socket or tcpclient over with socks5 proxy - asdox - Oct 20, 2019

Im currently using this snippet that lets me rout the connection over http proxy

Code
public static TcpClient ProxyTcpClient(string targetHost, int targetPort, string httpProxyHost, int httpProxyPort, string proxyUserName, string proxyPassword)
       {

           const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
           Uri proxyUri = new UriBuilder
           {
               Scheme = Uri.UriSchemeHttp,
               Host = httpProxyHost,
               Port = httpProxyPort
           }.Uri;
           Uri targetUri = new UriBuilder
           {
               Scheme = Uri.UriSchemeHttp,
               Host = targetHost,
               Port = targetPort
           }.Uri;

           WebProxy webProxy = new WebProxy(proxyUri, true);
           webProxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword);
           WebRequest request = WebRequest.Create(targetUri);
           request.Proxy = webProxy;
           request.Method = "CONNECT";
           HttpWebResponse response = (HttpWebResponse)request.GetResponse();
           Stream responseStream = response.GetResponseStream();
           Type responseType = responseStream.GetType();
           PropertyInfo connectionProperty = responseType.GetProperty("Connection", Flags);
           var connection = connectionProperty.GetValue(responseStream, null);
           Type connectionType = connection.GetType();
           PropertyInfo networkStreamProperty = connectionType.GetProperty("NetworkStream", Flags);
           NetworkStream networkStream = (NetworkStream)networkStreamProperty.GetValue(connection, null);
           Type nsType = networkStream.GetType();
           PropertyInfo socketProperty = nsType.GetProperty("Socket", Flags);
           Socket socket = (Socket)socketProperty.GetValue(networkStream, null);

           return new TcpClient { Client = socket };
       }

I need to do the same thing but only with socks5 proxy.
Thanks  🤝


RE: c# socket or tcpclient over with socks5 proxy - gigajew - Oct 24, 2019

Haven't seen anyone do it this way before, that's pretty cool. However you would probably come out better using a library that supports different types of proxies for sockets.


RE: c# socket or tcpclient over with socks5 proxy - REEND - Oct 13, 2020

.Net not have SOCKS4/SOCKS5 implementation, you have to write your own SOCKS5 client. See https://tools.ietf.org/html/rfc1928