示例#1
0
文件: DB.cs 项目: rajeshwarn/NVT
        public static bool CheckDB(ref NetworkConnectionItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            using (var db = new LiteDatabase(DB_FILENAME))
            {
                var items = db.GetCollection <NetworkConnectionItem>();

                var ipAddress = item.IPAddress;

                var existingItem = items.FindOne(a => a.IPAddress == ipAddress);

                if (existingItem == null)
                {
                    return(false);
                }

                item.City      = existingItem.City;
                item.Country   = existingItem.Country;
                item.ISP       = existingItem.ISP;
                item.Latitude  = existingItem.Latitude;
                item.Longitude = existingItem.Longitude;

                return(true);
            }
        }
示例#2
0
文件: DBTests.cs 项目: rajeshwarn/NVT
        public void GetNotFoundIPAddress()
        {
            var item = new NetworkConnectionItem {
                IPAddress = "127.256.0.1"
            };

            var result = DB.CheckDB(ref item);

            Assert.IsFalse(result);
        }
示例#3
0
文件: DBTests.cs 项目: rajeshwarn/NVT
        public void AddValidToDB()
        {
            var item = new NetworkConnectionItem
            {
                IPAddress = "256.256.256.256"
            };

            DB.AddToDB(item);

            var result = DB.CheckDB(ref item);

            Assert.IsTrue(result);
        }
示例#4
0
文件: DB.cs 项目: rajeshwarn/NVT
        public static void AddToDB(NetworkConnectionItem item)
        {
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            using (var db = new LiteDatabase(DB_FILENAME))
            {
                var items = db.GetCollection <NetworkConnectionItem>();

                items.Insert(item);
            }
        }
示例#5
0
        public override List <NetworkConnectionItem> GetActiveConnections()
        {
            var activeConnections = new List <NetworkConnectionItem>();

            var pStartInfo = new ProcessStartInfo();

            pStartInfo.FileName               = "netstat.exe";
            pStartInfo.Arguments              = "-a -n -o";
            pStartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
            pStartInfo.UseShellExecute        = false;
            pStartInfo.RedirectStandardInput  = true;
            pStartInfo.RedirectStandardOutput = true;
            pStartInfo.RedirectStandardError  = true;
            pStartInfo.CreateNoWindow         = true;

            var process = new Process()
            {
                StartInfo = pStartInfo
            };

            process.Start();

            var soStream = process.StandardOutput;

            var output = soStream.ReadToEnd();

            var lines = Regex.Split(output, "\r\n").Skip(4);

            var processes = ProcessManager.GetCurrentProcesses();

            foreach (var line in lines)
            {
                try
                {
                    var parts = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var len = parts.Length;

                    if (len <= 2 || EMPTY_HOST.Contains(parts[2]) || parts[2].Contains(lib.Common.Constants.LOCALHOST) || parts[1].Count(a => a == ':') > 1 || parts[2] == "*:*")
                    {
                        continue;
                    }

                    var networkItem = new NetworkConnectionItem
                    {
                        IPAddress      = parts[2].Split(':')[0],
                        Port           = Convert.ToInt32(parts[2].Split(':')[1]),
                        ProcessId      = int.Parse(parts[len - 1]),
                        ConnectionType = parts[0]
                    };

                    if (processes.ContainsKey(networkItem.ProcessId))
                    {
                        var processItem = processes[networkItem.ProcessId];

                        networkItem.ProcessName = processItem.Name;

                        try
                        {
                            networkItem.ProcessFileName = processItem.FileName;
                        }
                        catch (Win32Exception wex)
                        {
                            Log.Error($"Failed to get filename from process due to: {wex}");

                            networkItem.ProcessFileName = $"{networkItem.ProcessName} (Name)";
                        }
                    }

                    activeConnections.Add(networkItem);
                }
                catch (Exception ex)
                {
                    Log.Error($"Failed to parse line {line} due to: {ex}");
                }
            }

            return(activeConnections);
        }