Ejemplo n.º 1
0
        private void ActionButton_Click(object sender, RoutedEventArgs e)
        {
            var notif = (NotificationItem)((FrameworkElement)sender).DataContext;

            if (notif.Type == (int)CustomNotification.ActionType.BlockAllow)
            {
                Firewall.SetRule(notif.ApplicationName, notif.ApplicationPath, false);
            }
            else if (notif.Type == (int)CustomNotification.ActionType.UnblockAllow)
            {
                Firewall.RemoveRule(notif.ApplicationName, notif.ApplicationPath);
            }
            else if (notif.Type == (int)CustomNotification.ActionType.SuspendWhitelist ||
                     notif.Type == (int)CustomNotification.ActionType.TerminateWhitelist)
            {
                Controller.AddToWhitelist(notif.ApplicationName, notif.ApplicationPath);
            }

            Task.Run(() =>
            {
                Controller.NotificationList.Find(x => x == notif).NotActivated = false;

                using (var db = new ArgonDB())
                    db.NotificationsList
                    .Where(x => x.Type == notif.Type &&
                           x.ApplicationPath == notif.ApplicationPath &&
                           x.Time == notif.Time.Ticks)
                    .Set(x => x.NotActivated, 0)
                    .Update();

                UpdateViewSource();
            });
        }
Ejemplo n.º 2
0
 public FirewallUI()
 {
     InitializeComponent();
     RulesListViewSource = new CollectionViewSource();
     RefreshRuleList();
     LockdownState.IsChecked = !Firewall.GetLockdownState();
     FirewallState.IsChecked = Firewall.GetEnabledState();
     DataContext             = this;
 }
Ejemplo n.º 3
0
 private void LockdownState_IsCheckedChanged(object sender, EventArgs e)
 {
     if (!((ToggleSwitch)sender).IsChecked ?? false)
     {
         Firewall.Lockdown();
     }
     else
     {
         Firewall.LockdownRelease();
     }
 }
Ejemplo n.º 4
0
 public static void Initialize()
 {
     ReadNotifications();
     Task.Run(() => { ReadConfig(); });
     Task.Run(() => { TotalCpuLoadCounter.NextValue(); });
     Firewall.Initialize();
     NetworkProcessList = GetNetworkProcessList();
     GetServices();
     GetCurrentProcesses();
     InitProcessDataList();
     EtwMonitor.Initialize();
     timer.Elapsed += Timer_Elapsed;
     timer.Start();
 }
Ejemplo n.º 5
0
        private void ChangeAppFirewallRule(object sender, System.Windows.RoutedEventArgs e)
        {
            var rule = (FirewallRule)((FrameworkElement)sender).DataContext;

            switch (((ToggleButton)sender).IsChecked)
            {
            case true:
                Firewall.RemoveRule(rule.Name + "__" + rule.Path);
                Firewall.SetRule(rule.Name, rule.Path, true);
                break;

            case false:
                Firewall.RemoveRule(rule.Name + "__" + rule.Path);
                Firewall.SetRule(rule.Name, rule.Path, false);
                break;

            default:
                Firewall.RemoveRule(rule.Name + "__" + rule.Path);
                break;
            }
        }
Ejemplo n.º 6
0
        public void RefreshRuleList()
        {
            Task.Run(() =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    btnRefresh.IsEnabled       = false;
                    RulesListViewSource.Source = null;
                    ProgressBar1.Visibility    = Visibility.Visible;
                }));

                List <FirewallRule> rules = Firewall.GetFirewallRules();

                Dispatcher.BeginInvoke(new Action(() =>
                {
                    btnRefresh.IsEnabled       = true;
                    ProgressBar1.Visibility    = Visibility.Collapsed;
                    RulesListViewSource.Source = rules;
                }));
            });
        }
Ejemplo n.º 7
0
 private void FirewallState_IsCheckedChanged(object sender, EventArgs e)
 {
     Firewall.EnableFirewall(FirewallState.IsChecked ?? true);
 }
Ejemplo n.º 8
0
        static void StartEtwSession()
        {
            try {
                using (EtwSession = new TraceEventSession("ArgonTraceEventSession")) {
                    EtwSession.EnableKernelProvider(KernelTraceEventParser.Keywords.NetworkTCPIP |
                                                    KernelTraceEventParser.Keywords.Process);

                    EtwSession.Source.Kernel.ProcessStart += data =>
                    {
                        lock (Controller.NewProcesses)
                            Controller.NewProcesses.Add(data.ProcessID);
                    };

                    EtwSession.Source.Kernel.ProcessStop += data =>
                    {
                        lock (Controller.NewProcesses)
                            Controller.NewProcesses.RemoveAll(x => x == data.ProcessID);
                        lock (Controller.ProcessDataList)
                            Controller.ProcessDataList.RemoveAll(p => p.ID == data.ProcessID);
                        lock (Controller.Services)
                            Controller.Services.TryRemove(data.ProcessID, out string x);
                    };

                    EtwSession.Source.Kernel.TcpIpSend += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID
                                                                          & x.Time == data.TimeStamp.Ticks.NextSecond()
                                                                          & x.Type == 1
                                                                          & x.SourceAddr == data.saddr.ToString()
                                                                          & x.SourcePort == data.sport
                                                                          & x.DestAddr == data.daddr.ToString()
                                                                          & x.DestPort == data.dport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID
                                                                            & x.Time == data.TimeStamp.Ticks.NextSecond()
                                                                            & x.Type == 1
                                                                            & x.SourceAddr == data.saddr.ToString()
                                                                            & x.SourcePort == data.sport
                                                                            & x.DestAddr == data.daddr.ToString()
                                                                            & x.DestPort == data.dport)
                                        .First().Sent += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = data.size,
                                            Recv            = 0,
                                            SourceAddr      = data.saddr.ToString(),
                                            SourcePort      = data.sport,
                                            DestAddr        = data.daddr.ToString(),
                                            DestPort        = data.dport,
                                            Type            = 1,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.TcpIpRecv += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 1 &&
                                                                          x.SourceAddr == data.daddr.ToString() &&
                                                                          x.SourcePort == data.dport &&
                                                                          x.DestAddr == data.saddr.ToString() &&
                                                                          x.DestPort == data.sport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 1 &&
                                                                            x.SourceAddr == data.daddr.ToString() &&
                                                                            x.SourcePort == data.dport &&
                                                                            x.DestAddr == data.saddr.ToString() &&
                                                                            x.DestPort == data.sport)
                                        .First().Recv += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = 0,
                                            Recv            = data.size,
                                            DestAddr        = data.saddr.ToString(),
                                            DestPort        = data.sport,
                                            SourceAddr      = data.daddr.ToString(),
                                            SourcePort      = data.dport,
                                            Type            = 1,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.TcpIpSendIPV6 += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 2 &&
                                                                          x.SourceAddr == data.saddr.ToString() &&
                                                                          x.SourcePort == data.sport &&
                                                                          x.DestAddr == data.daddr.ToString() &&
                                                                          x.DestPort == data.dport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 2 &&
                                                                            x.SourceAddr == data.saddr.ToString() &&
                                                                            x.SourcePort == data.sport &&
                                                                            x.DestAddr == data.daddr.ToString() &&
                                                                            x.DestPort == data.dport)
                                        .First().Sent += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = data.size,
                                            Recv            = 0,
                                            SourceAddr      = data.saddr.ToString(),
                                            SourcePort      = data.sport,
                                            DestAddr        = data.daddr.ToString(),
                                            DestPort        = data.dport,
                                            Type            = 2,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.TcpIpRecvIPV6 += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 2 &&
                                                                          x.SourceAddr == data.daddr.ToString() &&
                                                                          x.SourcePort == data.dport &&
                                                                          x.DestAddr == data.saddr.ToString() &&
                                                                          x.DestPort == data.sport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 2 &&
                                                                            x.SourceAddr == data.daddr.ToString() &&
                                                                            x.SourcePort == data.dport &&
                                                                            x.DestAddr == data.saddr.ToString() &&
                                                                            x.DestPort == data.sport)
                                        .First().Recv += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = 0,
                                            Recv            = data.size,
                                            DestAddr        = data.saddr.ToString(),
                                            DestPort        = data.sport,
                                            SourceAddr      = data.daddr.ToString(),
                                            SourcePort      = data.dport,
                                            Type            = 2,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.UdpIpSend += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 3 &&
                                                                          x.SourceAddr == data.saddr.ToString() &&
                                                                          x.SourcePort == data.sport &&
                                                                          x.DestAddr == data.daddr.ToString() &&
                                                                          x.DestPort == data.dport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 3 &&
                                                                            x.SourceAddr == data.saddr.ToString() &&
                                                                            x.SourcePort == data.sport &&
                                                                            x.DestAddr == data.daddr.ToString() &&
                                                                            x.DestPort == data.dport)
                                        .First().Sent += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = data.size,
                                            Recv            = 0,
                                            SourceAddr      = data.saddr.ToString(),
                                            SourcePort      = data.sport,
                                            DestAddr        = data.daddr.ToString(),
                                            DestPort        = data.dport,
                                            Type            = 3,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.UdpIpRecv += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 3 &&
                                                                          x.SourceAddr == data.daddr.ToString() &&
                                                                          x.SourcePort == data.dport &&
                                                                          x.DestAddr == data.saddr.ToString() &&
                                                                          x.DestPort == data.sport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 3 &&
                                                                            x.SourceAddr == data.daddr.ToString() &&
                                                                            x.SourcePort == data.dport &&
                                                                            x.DestAddr == data.saddr.ToString() &&
                                                                            x.DestPort == data.sport)
                                        .First().Recv += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = 0,
                                            Recv            = data.size,
                                            DestAddr        = data.saddr.ToString(),
                                            DestPort        = data.sport,
                                            SourceAddr      = data.daddr.ToString(),
                                            SourcePort      = data.dport,
                                            Type            = 3,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.UdpIpSendIPV6 += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 4 &&
                                                                          x.SourceAddr == data.saddr.ToString() &&
                                                                          x.SourcePort == data.sport &&
                                                                          x.DestAddr == data.daddr.ToString() &&
                                                                          x.DestPort == data.dport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 4 &&
                                                                            x.SourceAddr == data.saddr.ToString() &&
                                                                            x.SourcePort == data.sport &&
                                                                            x.DestAddr == data.daddr.ToString() &&
                                                                            x.DestPort == data.dport)
                                        .First().Sent += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = data.size,
                                            Recv            = 0,
                                            SourceAddr      = data.saddr.ToString(),
                                            SourcePort      = data.sport,
                                            DestAddr        = data.daddr.ToString(),
                                            DestPort        = data.dport,
                                            Type            = 4,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Kernel.UdpIpRecvIPV6 += data =>
                    {
                        try {
                            lock (Controller.NetworkTrafficList)
                                lock (Controller.ProcessDataList)
                                    if (Controller.NetworkTrafficList.Any(x => x.ProcessID == data.ProcessID &&
                                                                          x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                          x.Type == 4 &&
                                                                          x.SourceAddr == data.daddr.ToString() &&
                                                                          x.SourcePort == data.dport &&
                                                                          x.DestAddr == data.saddr.ToString() &&
                                                                          x.DestPort == data.sport))
                                    {
                                        Controller.NetworkTrafficList.Where(x => x.ProcessID == data.ProcessID &&
                                                                            x.Time == data.TimeStamp.Ticks.NextSecond() &&
                                                                            x.Type == 4 &&
                                                                            x.SourceAddr == data.daddr.ToString() &&
                                                                            x.SourcePort == data.dport &&
                                                                            x.DestAddr == data.saddr.ToString() &&
                                                                            x.DestPort == data.sport)
                                        .First().Recv += data.size;
                                    }
                                    else
                                    {
                                        ProcessData p = Controller.ProcessDataList.Where(x => x.ID == data.ProcessID).First();
                                        Controller.NetworkTrafficList.Add(new NetworkTraffic
                                        {
                                            Time            = data.TimeStamp.Ticks.NextSecond(),
                                            ApplicationName = p.Name,
                                            ProcessName     = data.ProcessName,
                                            FilePath        = p.Path,
                                            Sent            = 0,
                                            Recv            = data.size,
                                            DestAddr        = data.saddr.ToString(),
                                            DestPort        = data.sport,
                                            SourceAddr      = data.daddr.ToString(),
                                            SourcePort      = data.dport,
                                            Type            = 4,
                                            ProcessID       = data.ProcessID
                                        });

                                        lock (Controller.NetworkProcessList)
                                            if (!Controller.NetworkProcessList.Contains(p.Path))
                                            {
                                                Controller.NetworkProcessList.Add(p.Path);
                                                if (Controller.BlockNewConnections)
                                                {
                                                    Firewall.SetRule(p.Name, p.Path, false);
                                                    if (Controller.NotifyNewApplication)
                                                    {
                                                        Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.UnblockAllow);
                                                    }
                                                }
                                                else if (Controller.NotifyNewApplication)
                                                {
                                                    Controller.ShowNotification(p.ID, p.Name, p.Path, CustomNotification.ActionType.BlockAllow);
                                                }
                                            }
                                    }
                        }
                        catch (NullReferenceException) {
                            Controller.UpdateProcessDataList();
                            NullRef += 1;
                            if (NullRef > 10)
                            {
                                Controller.InitProcessDataList();
                                NullRef = 0;
                            }
                        }
                        catch { }
                    };

                    EtwSession.Source.Process();
                }
            }
            catch {
                if (++Failed > 3)
                {
                    EtwSession.Dispose();
                    throw new Exception("Unable to start ETW Session.");
                }
                else
                {
                    EtwSession.Dispose();
                    Thread.Sleep(1000);
                    Initialize();
                }
            }
        }
Ejemplo n.º 9
0
        public CustomNotification(int PID, string applicationName, string applicationPath, ActionType actionType, DateTime time)
        {
            Time = time.ToLongTimeString();
            string title = actionType == ActionType.BlockAllow ? "First connection: " :
                           actionType == ActionType.UnblockAllow ? "Blocked connection: " :
                           actionType == ActionType.SuspendWhitelist ? "High CPU load: " :
                           actionType == ActionType.TerminateWhitelist ? "Suspended: " :
                           "";

            Title   = title + applicationName;
            Message = applicationPath;

            Button1 = actionType == ActionType.BlockAllow ? "BLOCK" :
                      actionType == ActionType.UnblockAllow ? "UNBLOCK" :
                      actionType == ActionType.SuspendWhitelist ? "SUSPEND" :
                      actionType == ActionType.TerminateWhitelist ? "TERMINATE" :
                      null;

            Button2 = actionType == ActionType.BlockAllow ? "ALLOW" :
                      actionType == ActionType.UnblockAllow ? "ALLOW" :
                      actionType == ActionType.SuspendWhitelist ? "WHITELIST" :
                      actionType == ActionType.TerminateWhitelist ? "WHITELIST" :
                      null;

            BackgroundColor = actionType == ActionType.BlockAllow ?
                              new SolidColorBrush(Color.FromArgb(255, 0, 182, 0)) :
                              actionType == ActionType.UnblockAllow ?
                              new SolidColorBrush(Color.FromArgb(255, 182, 0, 0)) :
                              actionType == ActionType.SuspendWhitelist ?
                              new SolidColorBrush(Color.FromArgb(255, 0, 112, 128)) :
                              actionType == ActionType.TerminateWhitelist ?
                              new SolidColorBrush(Color.FromArgb(255, 204, 80, 0)) :
                              new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));

            Action _button1Action = actionType == ActionType.BlockAllow ?
                                    new Action(() => Firewall.SetRule(applicationName, applicationPath, false)) :
                                    actionType == ActionType.UnblockAllow ?
                                    new Action(() => Firewall.RemoveRule(applicationName + "__" + applicationPath)) :
                                    actionType == ActionType.SuspendWhitelist ?
                                    new Action(() => Controller.SuspendProcess(PID)) :
                                    actionType == ActionType.TerminateWhitelist ?
                                    new Action(() => Controller.TerminateProcess(PID)) :
                                    new Action(() => { });

            Action _button2Action = actionType == ActionType.BlockAllow ?
                                    new Action(() => Firewall.SetRule(applicationName, applicationPath, true)) :
                                    actionType == ActionType.UnblockAllow ?
                                    new Action(() => Firewall.SetRule(applicationName, applicationPath, true)) :
                                    actionType == ActionType.SuspendWhitelist ?
                                    new Action(() =>
            {
                Controller.AddToWhitelist(PID, applicationName, applicationPath);
                ((MainWindow)Application.Current.MainWindow).SuspendedProcesses.UpdateViewSource();
            }) :
                                    actionType == ActionType.TerminateWhitelist ?
                                    new Action(() =>
            {
                Controller.ResumeProcess(PID);
                Controller.AddToWhitelist(PID, applicationName, applicationPath);
                ((MainWindow)Application.Current.MainWindow).SuspendedProcesses.UpdateViewSource();
            }) :
                                    new Action(() => { });

            var _closeAction = new Action <CustomNotification>(n => n.Close());

            Action setNotificationActivated = new Action(() =>
            {
                Controller.NotificationList.Find(x => x.Type == (int)actionType && x.ApplicationPath == applicationPath && x.Time == time).NotActivated = false;
                ((MainWindow)Application.Current.MainWindow).Notifications.UpdateViewSource();

                using (var db = new ArgonDB())
                    db.NotificationsList
                    .Where(x => x.Type == (int)actionType &&
                           x.ApplicationPath == applicationPath &&
                           x.Time == time.Ticks)
                    .Set(x => x.NotActivated, 0)
                    .Update();
            });

            Button1Command = new RelayCommand(x => { _button1Action(); setNotificationActivated(); _closeAction(this); });
            Button2Command = new RelayCommand(x => { _button2Action(); setNotificationActivated(); _closeAction(this); });
            CloseCommand   = new RelayCommand(x => _closeAction(this));
        }