public static string GetLocalIpUsedByProcess(this Process process)
        {
            var tcpRow = ManagedIpHelper.GetExtendedTcpTable(true)
                         .FirstOrDefault(t => t.RemoteEndPoint.Address.ToString() == Static.Datacenter.Servers[0].Ip && t.ProcessId == process.Id);

            return(tcpRow?.LocalEndPoint.ToString());
        }
Exemple #2
0
        static void Main(string[] args)
        {
            bool loop = true;

            while (loop)
            {
                Console.WriteLine("Active Connections");
                Console.WriteLine();
                Console.WriteLine("  Proto  Local Address          Foreign Address        State         PID");
                foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                {
                    Process process = Process.GetProcessById(tcpRow.ProcessId);
                    Console.WriteLine("  {0,-7}{1,-23}{2, -23}{3,-14}{4,-7}{5}", "TCP", tcpRow.LocalEndPoint, tcpRow.RemoteEndPoint, tcpRow.State, tcpRow.ProcessId, process.ProcessName);

                    try
                    {
                        /*if (process.ProcessName != "System")
                         * {
                         *  foreach (ProcessModule processModule in process.Modules)
                         *  {
                         *      Console.WriteLine("  {0}", processModule.FileName);
                         *  }
                         *
                         *  Console.WriteLine("  [{0}]", Path.GetFileName(process.MainModule.FileName));
                         * }
                         * else
                         * {
                         *  Console.WriteLine("  -- unknown component(s) --");
                         *  Console.WriteLine("  [{0}]", "System");
                         * }*/
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(process.ProcessName + " error'd");
                    }

                    Console.WriteLine();
                }

                foreach (UdpRow udpRow in ManagedIpHelper.GetExtendedUdpTable(true))
                {
                    Process process = Process.GetProcessById(udpRow.ProcessId);
                    try
                    {
                        Console.WriteLine("  {0,-7}{1,-23}{2, -23}{3,-14}{4,-7}{5}", "UDP", udpRow.LocalEndPoint, "", "", udpRow.ProcessId, process.ProcessName);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error'd at {0}", process.ProcessName);
                        Console.WriteLine("{0}", ex.ToString());
                    }
                }

                Console.Write("{0}Press any key to continue...", Environment.NewLine);
                Console.ReadKey();

                loop = false;
            }
        }
Exemple #3
0
        private void metroButton3_Click(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            //this.listBox1.Items.Clear();
            //MyAPI.GetTcpConnexions();
            //for(int i=0;i<MyAPI.TcpConnexion.dwNumEntries;i++)
            //{
            //    this.listBox1.Items.Add(string.Format("{0} \t\t {1} \t\t\t  {2}",
            //        MyAPI.TcpConnexion.table[i].Local.Address.ToString()+":"+MyAPI.TcpConnexion.table[i].Local.Port.ToString(),
            //        MyAPI.TcpConnexion.table[i].Remote.Address.ToString()+":"+MyAPI.TcpConnexion.table[i].Remote.Port.ToString(),
            //        MyAPI.TcpConnexion.table[i].StrgState.ToString()));
            //}
            this.listBox1.Items.Add("Active Connections\n\n");
            this.listBox1.Items.Add("  Proto \t  Local Address          Foreign Address        State         PID\n");
            foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
            {
                this.listBox1.Items.Add(string.Format("  {0,-7}\t{1,-23}{2, -23}{3,-14}{4}", "TCP", tcpRow.LocalEndPoint, tcpRow.RemoteEndPoint, tcpRow.State, tcpRow.ProcessId));
                try
                {
                    Process process = Process.GetProcessById(tcpRow.ProcessId);
                    if (process.ProcessName != "System")
                    {
                        //foreach (ProcessModule processModule in process.Modules)
                        //{
                        //    Console.WriteLine("  {0}", processModule.FileName);
                        //}
                        try
                        {
                            this.listBox1.Items.Add(string.Format("  [{0}]", Path.GetFileName(process.MainModule.FileName)));
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                    else
                    {
                        this.listBox1.Items.Add(string.Format("  -- unknown component(s) --"));
                        this.listBox1.Items.Add(string.Format("  [{0}]", "System"));
                    }

                    this.listBox1.Items.Add("");
                }
                catch (Exception ex)
                {
                    listBox1.Items.Add("unknown components");
                }
            }
        }
        private void UpdateTcpTables()
        {
            TcpTable eTcpData = ManagedIpHelper.GetExtendedTcpTable(true);

            foreach (TcpRow currentRow in eTcpData)
            {
                if (chkBoxOnlyInternet.IsChecked != null && chkBoxOnlyInternet.IsChecked.Value)
                {
                    bool canWeAdd = !IPAddress.IsLoopback(currentRow.LocalEndPoint.Address);

                    if (currentRow.LocalEndPoint.Address.ToString() == "0.0.0.0")
                    {
                        canWeAdd = false;
                    }

                    if (currentRow.State == TcpState.Listen)
                    {
                        canWeAdd = false;
                    }

                    if (canWeAdd)
                    {
                        if (currentRow.ProcessId != 0)
                        {
                            _latestTcpRowData.Add(currentRow);
                        }
                    }
                }
                else
                {
                    if (currentRow.ProcessId != 0)
                    {
                        _latestTcpRowData.Add(currentRow);
                    }
                }
            }
        }
        private void UpdateTcpTables()
        {
            TcpTable eTcpData    = ManagedIpHelper.GetExtendedTcpTable(true);
            var      currentList = new List <Int32>();

            foreach (TcpRow currentRow in eTcpData.Where(currentRow => !currentList.Contains(currentRow.ProcessId)))
            {
                if (currentRow.ProcessId != 0 && currentRow.ProcessId != 4)
                {
                    currentList.Add(currentRow.ProcessId);
                }
            }

            List <int> removeList = _pids.Where(item => !currentList.Contains(item)).ToList();

            foreach (int item in removeList)
            {
                _pids.Remove(item);
            }
            foreach (int item in currentList.Where(item => !_pids.Contains(item)))
            {
                _pids.Add(item);
            }
        }
 private static TcpTable ActiveConnections()
 {
     return(ManagedIpHelper.GetExtendedTcpTable(true));
 }
Exemple #7
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Clear ListView
            processesList.Clear();
            // Create hash table for ports (so we don't add the same port twice)
            HashSet <int> ports = new HashSet <int>();

            // TEMP?
            toBeLookedUp = new List <string>();

            // For each magic row in the magic table (don't ask me)
            // [Ref] timvw.be/2007/09/09/build-your-own-netstatexe-with-c/
            foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
            {
                // Only look at ports that are listening for requests
                string process_port_state = tcpRow.State.ToString();
                if (process_port_state.StartsWith("Listen") ||
                    process_port_state.StartsWith("Established"))
                {
                    // Get the executable name from PID, and the port it's using
                    int    process_id   = tcpRow.ProcessId;
                    string process_name = Process.GetProcessById(process_id).ProcessName;
                    int    process_port = tcpRow.LocalEndPoint.Port;
                    // If the port is already on the hash table, skip this entry
                    if (ports.Contains(process_port))
                    {
                        continue;
                    }
                    // Otherwise, add it to the hash table
                    ports.Add(process_port);

                    // TEMP?
                    string process_host = "";
                    foreach (string ip in toBeLookedUp)
                    {
                        if (reverseLookup.ContainsKey(ip))
                        {
                            if (ip.Equals(""))
                            {
                                process_host = ip;
                            }
                            else
                            {
                                process_host = reverseLookup[ip];
                            }
                            continue;
                        }

                        string reversed = DoReverseIpLookup(ip);
                        reverseLookup.Add(ip, reversed);
                        if (reversed.Equals(""))
                        {
                            process_host = ip;
                        }
                        else
                        {
                            process_host = reversed;
                        }
                    }

                    // And at last, insert this entry in the ListView
                    processesList.Add(new VisualProcess {
                        Port  = process_port,
                        Name  = process_name,
                        ID    = process_id,
                        State = process_port_state,
                        Host  = process_host
                    });

                    // TEMP?
                    toBeLookedUp.Add(tcpRow.RemoteEndPoint.Address.ToString());
                }
            }

            // Being honest, I don't fully understand how this works
            // [Ref] wpf-tutorial.com/listview-control/listview-sorting/
            listBoxView.SortDescriptions.Clear();
            listBoxView.SortDescriptions.Add(new SortDescription("Port", ListSortDirection.Ascending));
        }
Exemple #8
0
        public int getFreeBotPort(bool first = false)
        {
            if (checkBox_manualPortOverride.Checked && textBox_manualPortOverride.Text != "")
            {
                int tmpport = 0;
                if (!int.TryParse(textBox_manualPortOverride.Text, out tmpport))
                {
                    MainLog("Manual Port set failed, please only use a valid port from 1-65535");
                }
                return(tmpport);
            }

            int manualPID = 0;

            if (radioButton_manual.Checked)
            {
                if (listBox_botClients.SelectedItem != null)
                {
                    BotList bl = listBox_botClients.SelectedItem as BotList;
                    manualPID = bl.PID;
                }
                else
                {
                    manualPID = -1;
                }
            }

            int x    = 0;
            int port = 0;

            foreach (Process procesInfo in Process.GetProcesses())
            {
                if ((procesInfo.ProcessName == "phBot" || procesInfo.ProcessName.Contains("mBot")) && procesInfo.MainWindowTitle != "")
                {
                    int PID = procesInfo.Id;
                    if (radioButton_autoSelect.Checked || manualPID == 0 || manualPID == PID)
                    {
                        foreach (TcpRow tcpRow in ManagedIpHelper.GetExtendedTcpTable(true))
                        {
                            if (tcpRow.ProcessId != 0)
                            {
                                Process process = Process.GetProcessById(tcpRow.ProcessId);
                                if (process.ProcessName != "System")
                                {
                                    try
                                    {
                                        if (process.ProcessName.Contains("phBot") && process.Id == PID)
                                        {
                                            if ((tcpRow.LocalEndPoint.ToString().Contains("127.0.0.1")) && (!process.MainWindowTitle.Contains("Connected") || process.MainWindowTitle.Contains("N/A") || (checkBox_ignoreState.Checked && !radioButton_autoSelect.Checked)))
                                            {
                                                x++;
                                                port = 0;
                                                int.TryParse(tcpRow.LocalEndPoint.ToString().Replace("127.0.0.1:", ""), out port);
                                                mBot = false;
                                            }
                                        }
                                        else if (process.ProcessName.Contains("mBot") && process.Id == PID)
                                        {
                                            if ((tcpRow.LocalEndPoint.ToString().Contains("0.0.0.0")) && (!process.MainWindowTitle.Contains("[") || process.MainWindowTitle.Contains("DC") || (checkBox_ignoreState.Checked && !radioButton_autoSelect.Checked)))
                                            {
                                                x++;
                                                port = 0;
                                                int.TryParse(tcpRow.LocalEndPoint.ToString().Replace("0.0.0.0:", ""), out port);
                                                mBot = true;
                                                if (!first)
                                                {
                                                    return(port);
                                                }
                                            }
                                        }
                                    }
                                    catch
                                    { }
                                }
                            }
                        }
                    }
                }
            }
            return(port);
        }