Find out which service is being used in port tcp C#

Asked

Viewed 558 times

1

I created a Scan Port in C# (winforms), which is checking a range of ports, previously set, I can verify that the door is open, but my problem is this: I know the door is open, but I don’t know what kind of service is running on that door, there is some function or library that I can verify which service is running on these open doors??

public void StartScan(object o)
{
    IPAddress ipAddress = o as IPAddress;
    gridPortaAberta.Rows.Clear();


    for (int i = startPort; i <= endPort; i++)
    {
        lock (consoleLock)
        {

            label5.Text = "Scaneando Porta: " + i;
        }

        while (waitingForResponses >= maxQueriesAtOneTime)
            Thread.Sleep(0);

        if (stop)
            break;

        try
        {
            Socket s = new Socket(AddressFamily.InterNetwork,
                                  SocketType.Stream, ProtocolType.Tcp);


            //Representa um ponto de extremidade de rede como um endereço IP e um número de porta.
            s.BeginConnect(new IPEndPoint(ipAddress, i), EndConnect, s);

            Interlocked.Increment(ref waitingForResponses);
        }
        catch (Exception)
        {

        }
    }
}

public void EndConnect(IAsyncResult ar)
{
    try
    {
        DecrementResponses();

        List<string> items = new List<string>();

        Socket s = ar.AsyncState as Socket;

        s.EndConnect(ar);

        if (s.Connected)
        {
            int openPort = Convert.ToInt32(s.RemoteEndPoint.ToString().Split(':')[1]);

            openPorts.Add(openPort);


            gridPortaAberta.Rows.Add(openPort.ToString());

            lock (consoleLock)
            {
                Console.WriteLine("TCP conectado na porta: { 0}", openPort);
            }

            s.Disconnect(true);
        }

    }
    catch (Exception)
    {

    }
}

public void IncrementResponses()
{
    Interlocked.Increment(ref waitingForResponses);

    PrintWaitingForResponses();
}

public void DecrementResponses()
{
    Interlocked.Decrement(ref waitingForResponses);

    PrintWaitingForResponses();
}

public void PrintWaitingForResponses()
{
    lock (consoleLock)
    {

        label6.Text = "Esperando respostas de " + waitingForResponses + " threads";


    }
}
  • if you can show the code you are working on... help

  • I posted the code friend, sera q there is some way to make this check??

  • 2

    You’re basically trying to connect to the door... if it goes wrong, it’s because you’re being used. I recommend viewing this code, use the windows executable to read the ports and you can get more information: http://www.cheynewallace.com/get-active-ports-and-associated-process-names-in-c/

  • 1

    there is this other way by doing the application by hand using the windows API: https://www.codeproject.com/Articles/4298/Getting-active-TCP-UDP-connections-on-a-box

  • 1

    obg friend, I saw the projects here, I think it will give right to what I need, very obggg

1 answer

1

You can use the netstat -b to obtain this information. You can execute this command with the Process.Start and read the output returned by nestat for the process + port pair information. I made a small example.

public class UsedPort
{
    public int Port { get; set; }
    public string Process { get; set; }
}
public static IEnumerable<UsedPort> GetUsedPorts()
{
    var process = Process.Start(new ProcessStartInfo("netstat", "-anb")
    {
        RedirectStandardOutput = true,
        UseShellExecute = false
    });
    string line = null;
    while ((line = process.StandardOutput.ReadLine()) != null)
    {
        if (line.IndexOf("TCP") >= 0 || line.IndexOf("UDP") >= 0)
        {
            var row = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            var localAddress = row[1];
            var processName = process.StandardOutput.ReadLine();
            if(!processName.Contains("Can not obtain ownership information")){
                yield return new UsedPort
                {
                    Port = int.Parse(localAddress.Split(':').Last()),
                    Process = processName
                };
            }
        }
    }
}
  • Very obg friend, the afternoon I will implement and put the result here... very obg really, have a good day

Browser other questions tagged

You are not signed in. Login or sign up in order to post.