Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

c# socket or tcpclient over with socks5 proxy

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  🤝
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.
[Image: Syr3DlV.png]
.Net not have SOCKS4/SOCKS5 implementation, you have to write your own SOCKS5 client. See https://tools.ietf.org/html/rfc1928


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by elmagekOct 24, 2021 02:50 PM 7 290 Oct 24, 2021 02:50 PM
Last Post: #Kat
[C#] by 0x0_Sep 23, 2018 03:22 AM 2 334 Sep 23, 2018 03:22 AM
Last Post: Zephyros
[.NET] by huntersJul 3, 2017 05:07 AM 2 201 Jul 3, 2017 05:07 AM
Last Post: ubbelol