0

So, I am writing some code and am pulling in 3rd party projects to achieve a specific set of functionality. Namely, talking to AD, getting kerberos tickets, doing user impersonation if a username/password is supplied to the program etc.

The issue I've run into is that many of these things depend on Windows kernel libraries (netapi.dll, win32.dll to name a few). I need to be able to run this one Linux as well. So I tried .NET core. Well, being core I was never able to find an alternative to these libraries, and of course WINE doesn't work for them either. So I am left with one option. Socks proxies.

So how in the world, can I proxy an entire application (console app) through a socks4 or socks5 proxy. PS, I am NOT talking HTTP. An example would be the following (just a simple AD lookup):

public void PrintAllUsers()
        {
            Console.WriteLine("[+] Domain Users");
            Console.WriteLine("-----------------");
            GroupPrincipal gp = GetGroup("Domain Users");
            Console.WriteLine("[+] Count (" + gp.Members.Count + ")");
            foreach (Principal pc in gp.Members)
            {
                if (pc.StructuralObjectClass.ToLower() == "user")
                {
                    PrintUserData(pc as UserPrincipal);
                }

                if (pc.StructuralObjectClass.ToLower() == "computer")
                {
                    PrintComputerData(pc as ComputerPrincipal);

                }
            }

            Console.WriteLine("-----------------");
        }
raithedavion
  • 127
  • 1
  • 12

2 Answers2

0

Unfortunately, .NET Core itself doesn't support SOCKS proxies, neither 4 or 5, it only supports HTTP proxies.

However there's an open source library that can add SOCKS5 proxy support to your application. Try this: https://github.com/MihaZupan/HttpToSocks5Proxy

And with the two methods above combined, your application should be able to access Internet resources through a SOCKS5 proxy.

Melonee
  • 36
  • 1
  • 4
0

Now .NET 6 supports SOCKS Proxy (answered here):

var proxy = new WebProxy
{
    Address = new Uri("socks5://localhost:8080")
};
//proxy.Credentials = new NetworkCredential(); //Used to set Proxy logins. 
var handler = new HttpClientHandler
{
    Proxy = proxy
};
var httpClient = new HttpClient(handler);

or when you can inject IHttpClientFactory and wanna create HttpClient instances using it:

Services.AddHttpClient("WithProxy")
    .ConfigurePrimaryHttpMessageHandler(() =>
    {
        var proxy = new WebProxy
        {
            Address = new Uri("socks5://localhost:8080")
        };
        return new HttpClientHandler
        {
            Proxy = proxy
        };
    });

and when you injected IHttpClientFactory object:

httpClient = httpClientFactory.CreateClient("WithProxy");
Majid
  • 3,128
  • 1
  • 26
  • 31