From d346c53fdde16b040b0678556645845147be7d82 Mon Sep 17 00:00:00 2001 From: DavidXanatos Date: Wed, 7 Oct 2020 18:32:14 +0200 Subject: [PATCH] Build v0.80 --- CHANGELOG.md | 31 +- .../O365.Security.Native.ETW.vcxproj | 164 +++++ FirewallCore/FirewallCore.vcxproj | 163 +++++ FirewallCore/FirewallCore.vcxproj.filters | 49 ++ MiscHelpers/API/AppManager.cs | 318 +++++++++ MiscHelpers/API/AuditPolicy.cs | 290 ++++++++ .../API/Network => MiscHelpers/API}/DnsApi.cs | 8 +- .../API}/DnsConfigurator.cs | 5 +- MiscHelpers/API/FileOps.cs | 236 +++++++ MiscHelpers/API/IPHelper.cs | 484 +++++++++++++ .../API/MiscStats.cs | 2 +- MiscHelpers/API/NetFunc.cs | 443 ++++++++++++ MiscHelpers/API/ProcFunc.cs | 166 +++++ MiscHelpers/API/ProcessUtilities.cs | 325 +++++++++ MiscHelpers/API/RegistryMonitor.cs | 396 +++++++++++ MiscHelpers/API/ServiceHelper.cs | 657 ++++++++++++++++++ MiscHelpers/API/TokenManipulator.cs | 123 ++++ MiscHelpers/API/UwpFunc.cs | 105 +++ {PrivateWin10 => MiscHelpers}/API/WinVer.cs | 6 +- .../API/WindowsFirewall.cs | 619 +++++++++-------- MiscHelpers/Common/AppLog.cs | 274 ++++++++ MiscHelpers/Common/ClonableDictionary.cs | 21 + MiscHelpers/Common/ClonableList.cs | 21 + .../Common/DataGridExt.cs | 2 +- MiscHelpers/Common/HttpTask.cs | 365 ++++++++++ MiscHelpers/Common/IconExtractor.cs | 383 ++++++++++ MiscHelpers/Common/ImgFunc.cs | 94 +++ MiscHelpers/Common/MiscFunc.cs | 420 +++++++++++ MiscHelpers/Common/MultiValueDictionary.cs | 123 ++++ MiscHelpers/Common/TextHelpers.cs | 88 +++ MiscHelpers/Common/WinConsole.cs | 102 +++ MiscHelpers/Common/WpfFunc.cs | 247 +++++++ MiscHelpers/MiscHelpers.csproj | 98 +++ MiscHelpers/Properties/AssemblyInfo.cs | 36 + .../PipeIPC => PrivateAPI/IPC}/PipeClient.cs | 85 ++- .../PipeIPC => PrivateAPI/IPC}/PipeHost.cs | 45 +- .../PipeIPC => PrivateAPI/IPC}/PipeIPC.cs | 97 ++- PrivateAPI/IPC/Priv10Conv.cs | 258 +++++++ PrivateAPI/IPC/Priv10Logger.cs | 86 +++ PrivateAPI/PrivateAPI.csproj | 60 ++ PrivateAPI/Properties/AssemblyInfo.cs | 36 + .../API/EtwLogger.cs | 0 PrivateService/App.config | 6 + PrivateService/Common/AdminFunc.cs | 34 + .../Core/DnsInspector.cs | 13 +- .../Core/DnsInspector/DnsCacheMonitor.cs | 11 + .../Core/DnsInspector/DnsQueryWatcher.cs | 7 +- .../Core/DnsInspector/HostNameResolver.cs | 4 +- .../Core/DnsProxy/DnsBlockList.cs | 37 +- .../Core/DnsProxy/DnsProxyServer.cs | 7 +- .../Core/NetworkMonitor.cs | 5 +- .../Core/NetworkSocket.cs | 55 +- .../Core/Priv10Engine.cs | 134 ++-- PrivateService/Core/Priv10Service.cs | 81 +++ .../Core/ProcessMonitor.cs | 7 +- .../Core/Program.cs | 166 +++-- PrivateService/Core/ProgramID.cs | 251 +++++++ .../Core/ProgramList.cs | 11 +- .../Core/ProgramSet.cs | 112 +-- .../Core/WindowsFirewall/FirewallGuard.cs | 1 + .../Core/WindowsFirewall/FirewallManager.cs | 17 +- .../Core/WindowsFirewall/FirewallMonitor.cs | 3 +- .../Core/WindowsFirewall/FirewallRule.cs | 7 +- .../Core/WindowsFirewall/FirewallRuleEx.cs | 0 .../Core/WindowsFirewall/WinNetFwAPI.cs | 19 +- .../Core/WindowsFirewall/WinNetFwCom.cs | 12 +- PrivateService/IPC/Priv10Host.cs | 441 ++++++++++++ PrivateService/PrivateService.csproj | 133 ++++ PrivateService/Properties/AssemblyInfo.cs | 36 + PrivateService/Service.cs | 308 ++++++++ PrivateWin10.sln | 107 +++ PrivateWin10/API/AppManager.cs | 317 --------- PrivateWin10/API/AuditPolicy.cs | 288 -------- PrivateWin10/API/FileOps.cs | 232 ------- PrivateWin10/API/Network/IPHelper.cs | 481 ------------- PrivateWin10/API/Network/NetFunc.cs | 440 ------------ PrivateWin10/API/ProcFunc.cs | 163 ----- PrivateWin10/API/ProcessUtilities.cs | 321 --------- PrivateWin10/API/RegistryMonitor.cs | 392 ----------- PrivateWin10/API/ServiceHelper.cs | 652 ----------------- PrivateWin10/API/TokenManipulator.cs | 121 ---- PrivateWin10/API/UwpFunc.cs | 96 --- PrivateWin10/App.xaml.cs | 367 ++++------ PrivateWin10/Common/AdminFunc.cs | 1 + PrivateWin10/Common/AppLog.cs | 271 -------- PrivateWin10/Common/ClonableDictionary.cs | 18 - PrivateWin10/Common/ClonableList.cs | 18 - PrivateWin10/Common/HttpTask.cs | 359 ---------- PrivateWin10/Common/IconExtractor.cs | 380 ---------- PrivateWin10/Common/ImgFunc.cs | 89 --- PrivateWin10/Common/MiscFunc.cs | 393 ----------- PrivateWin10/Common/MultiValueDictionary.cs | 120 ---- PrivateWin10/Common/TextHelpers.cs | 87 --- PrivateWin10/Common/WinConsole.cs | 100 --- PrivateWin10/Common/WpfFunc.cs | 286 -------- PrivateWin10/Controls/AddressControl.xaml.cs | 1 + PrivateWin10/Controls/ControlList.cs | 211 ++++++ PrivateWin10/Controls/Converters.cs | 3 +- .../{ => Dns}/DnsBlockListsControl.xaml | 0 .../{ => Dns}/DnsBlockListsControl.xaml.cs | 1 + .../{ => Dns}/DnsFilterListControl.xaml | 0 .../{ => Dns}/DnsFilterListControl.xaml.cs | 1 + .../Controls/{ => Dns}/DnsLogList.xaml | 0 .../Controls/{ => Dns}/DnsLogList.xaml.cs | 1 + .../{ => Dns}/DnsQueryLogControl.xaml | 0 .../{ => Dns}/DnsQueryLogControl.xaml.cs | 1 + PrivateWin10/Controls/FirewallLogList.xaml.cs | 1 + .../Controls/FirewallRuleList.xaml.cs | 1 + .../Controls/NetworkSocketList.xaml.cs | 22 +- .../{ => Notify}/ConnectionNotify.xaml | 9 +- .../{ => Notify}/ConnectionNotify.xaml.cs | 3 + .../Controls/{ => Notify}/RuleNotify.xaml | 0 .../Controls/{ => Notify}/RuleNotify.xaml.cs | 1 + .../Controls/{ => Notify}/TweakNotify.xaml | 0 .../Controls/{ => Notify}/TweakNotify.xaml.cs | 1 + .../Controls/Presets/PresetControl.xaml | 62 ++ .../Controls/Presets/PresetControl.xaml.cs | 156 +++++ .../Controls/Presets/PresetCustom.xaml | 12 + .../Controls/Presets/PresetCustom.xaml.cs | 33 + .../Controls/Presets/PresetFirewall.xaml | 58 ++ .../Controls/Presets/PresetFirewall.xaml.cs | 103 +++ .../Controls/Presets/PresetItemControl.xaml | 90 +++ .../Presets/PresetItemControl.xaml.cs | 155 +++++ .../Controls/Presets/PresetTweaks.xaml | 22 + .../Controls/Presets/PresetTweaks.xaml.cs | 55 ++ .../Controls/Presets/RuleItemControl.xaml | 45 ++ .../Controls/Presets/RuleItemControl.xaml.cs | 192 +++++ .../Controls/Presets/TweakItemControl.xaml | 35 + .../Controls/Presets/TweakItemControl.xaml.cs | 147 ++++ PrivateWin10/Controls/ProgramControl.xaml.cs | 38 +- .../Controls/ProgramListControl.xaml.cs | 224 +----- PrivateWin10/Controls/ProgramTreeControl.xaml | 6 + .../Controls/ProgramTreeControl.xaml.cs | 5 + .../ProgramTreeControl/AbstractTreeItem.cs | 1 + .../ProgramTreeControl/ManualTreeSorter.cs | 2 +- .../ProgramTreeControl/ProgSetTreeItem.cs | 3 + .../ProgramTreeControl/ProgramTreeItem.cs | 3 +- .../Controls/{ => Tweaks}/TweakControl.xaml | 2 +- .../{ => Tweaks}/TweakControl.xaml.cs | 15 +- .../Controls/{ => Tweaks}/TweakGroup.xaml | 2 +- .../Controls/{ => Tweaks}/TweakGroup.xaml.cs | 43 +- PrivateWin10/Core/Presets/CustomPreset.cs | 58 ++ PrivateWin10/Core/Presets/FirewallPreset.cs | 166 +++++ PrivateWin10/Core/Presets/PresetGroup.cs | 140 ++++ PrivateWin10/Core/Presets/PresetItem.cs | 117 ++++ PrivateWin10/Core/Presets/PresetManager.cs | 165 +++++ PrivateWin10/Core/Presets/TweakPreset.cs | 128 ++++ PrivateWin10/Core/Priv10Service.cs | 75 +- PrivateWin10/Core/TweakManager.cs | 174 +++++ PrivateWin10/IPC/FirewallRule.cs | 609 ++++++++++++++++ PrivateWin10/IPC/FirewallRuleEx.cs | 115 +++ PrivateWin10/IPC/MiscObjects.cs | 384 ++++++++++ PrivateWin10/IPC/NetworkSocket.cs | 97 +++ PrivateWin10/IPC/Priv10Client.cs | 470 +++++++++++-- PrivateWin10/IPC/Priv10Host.cs | 263 ------- PrivateWin10/IPC/Program.cs | 153 ++++ PrivateWin10/{Core => IPC}/ProgramID.cs | 31 +- PrivateWin10/IPC/ProgramSet.cs | 142 ++++ PrivateWin10/MainWindow.xaml.cs | 8 +- PrivateWin10/Models/CategoryModel.cs | 1 - PrivateWin10/Models/GroupModel.cs | 4 +- PrivateWin10/Models/ServiceModel.cs | 3 +- PrivateWin10/Pages/AboutPage.xaml | 2 +- PrivateWin10/Pages/ControlPage.xaml | 184 +++++ PrivateWin10/Pages/ControlPage.xaml.cs | 283 ++++++++ PrivateWin10/Pages/FirewallPage.xaml | 6 +- PrivateWin10/Pages/FirewallPage.xaml.cs | 132 +++- PrivateWin10/Pages/OverviewPage.xaml.cs | 10 +- PrivateWin10/Pages/PrivacyPage.xaml.cs | 135 ++-- PrivateWin10/Pages/SettingsPage.xaml.cs | 25 +- PrivateWin10/PrivateWin10.csproj | 196 +++--- PrivateWin10/PrivateWin10.csproj.user | 4 +- PrivateWin10/Properties/AssemblyInfo.cs | 4 +- .../Resources/ResourceDictionary.xaml | 8 +- PrivateWin10/Resources/icons8-in-16.png | Bin 0 -> 541 bytes PrivateWin10/Resources/icons8-out-16.png | Bin 0 -> 541 bytes PrivateWin10/Translate.cs | 22 + PrivateWin10/Translation.ini | 398 ----------- PrivateWin10/TrayIcon.cs | 128 ++-- PrivateWin10/Windows/InputWnd.xaml.cs | 3 +- PrivateWin10/Windows/NotificationWnd.xaml.cs | 11 +- PrivateWin10/Windows/ProgramWnd.xaml.cs | 1 + PrivateWin10/Windows/RuleWindow.xaml.cs | 22 +- PrivateWin10/Windows/SetupWnd.xaml.cs | 9 +- TweakEngine/Properties/AssemblyInfo.cs | 36 + TweakEngine/TweakEngine.csproj | 74 ++ .../TweakList.cs | 308 +++----- .../TweakPresets.cs | 35 +- .../TweakTools.cs | 10 +- 189 files changed, 14544 insertions(+), 8119 deletions(-) create mode 100644 FirewallCore/FirewallCore.vcxproj create mode 100644 FirewallCore/FirewallCore.vcxproj.filters create mode 100644 MiscHelpers/API/AppManager.cs create mode 100644 MiscHelpers/API/AuditPolicy.cs rename {PrivateWin10/API/Network => MiscHelpers/API}/DnsApi.cs (93%) rename {PrivateWin10/Core/DnsProxy => MiscHelpers/API}/DnsConfigurator.cs (95%) create mode 100644 MiscHelpers/API/FileOps.cs create mode 100644 MiscHelpers/API/IPHelper.cs rename {PrivateWin10 => MiscHelpers}/API/MiscStats.cs (95%) create mode 100644 MiscHelpers/API/NetFunc.cs create mode 100644 MiscHelpers/API/ProcFunc.cs create mode 100644 MiscHelpers/API/ProcessUtilities.cs create mode 100644 MiscHelpers/API/RegistryMonitor.cs create mode 100644 MiscHelpers/API/ServiceHelper.cs create mode 100644 MiscHelpers/API/TokenManipulator.cs create mode 100644 MiscHelpers/API/UwpFunc.cs rename {PrivateWin10 => MiscHelpers}/API/WinVer.cs (95%) rename {PrivateWin10 => MiscHelpers}/API/WindowsFirewall.cs (76%) create mode 100644 MiscHelpers/Common/AppLog.cs create mode 100644 MiscHelpers/Common/ClonableDictionary.cs create mode 100644 MiscHelpers/Common/ClonableList.cs rename {PrivateWin10 => MiscHelpers}/Common/DataGridExt.cs (96%) create mode 100644 MiscHelpers/Common/HttpTask.cs create mode 100644 MiscHelpers/Common/IconExtractor.cs create mode 100644 MiscHelpers/Common/ImgFunc.cs create mode 100644 MiscHelpers/Common/MiscFunc.cs create mode 100644 MiscHelpers/Common/MultiValueDictionary.cs create mode 100644 MiscHelpers/Common/TextHelpers.cs create mode 100644 MiscHelpers/Common/WinConsole.cs create mode 100644 MiscHelpers/Common/WpfFunc.cs create mode 100644 MiscHelpers/MiscHelpers.csproj create mode 100644 MiscHelpers/Properties/AssemblyInfo.cs rename {PrivateWin10/Common/PipeIPC => PrivateAPI/IPC}/PipeClient.cs (59%) rename {PrivateWin10/Common/PipeIPC => PrivateAPI/IPC}/PipeHost.cs (76%) rename {PrivateWin10/Common/PipeIPC => PrivateAPI/IPC}/PipeIPC.cs (58%) create mode 100644 PrivateAPI/IPC/Priv10Conv.cs create mode 100644 PrivateAPI/IPC/Priv10Logger.cs create mode 100644 PrivateAPI/PrivateAPI.csproj create mode 100644 PrivateAPI/Properties/AssemblyInfo.cs rename {PrivateWin10 => PrivateService}/API/EtwLogger.cs (100%) create mode 100644 PrivateService/App.config create mode 100644 PrivateService/Common/AdminFunc.cs rename {PrivateWin10 => PrivateService}/Core/DnsInspector.cs (93%) rename {PrivateWin10 => PrivateService}/Core/DnsInspector/DnsCacheMonitor.cs (95%) rename {PrivateWin10 => PrivateService}/Core/DnsInspector/DnsQueryWatcher.cs (95%) rename {PrivateWin10 => PrivateService}/Core/DnsInspector/HostNameResolver.cs (96%) rename {PrivateWin10 => PrivateService}/Core/DnsProxy/DnsBlockList.cs (92%) rename {PrivateWin10 => PrivateService}/Core/DnsProxy/DnsProxyServer.cs (94%) rename {PrivateWin10 => PrivateService}/Core/NetworkMonitor.cs (97%) rename {PrivateWin10 => PrivateService}/Core/NetworkSocket.cs (71%) rename {PrivateWin10 => PrivateService}/Core/Priv10Engine.cs (92%) create mode 100644 PrivateService/Core/Priv10Service.cs rename {PrivateWin10 => PrivateService}/Core/ProcessMonitor.cs (95%) rename {PrivateWin10 => PrivateService}/Core/Program.cs (85%) create mode 100644 PrivateService/Core/ProgramID.cs rename {PrivateWin10 => PrivateService}/Core/ProgramList.cs (93%) rename {PrivateWin10 => PrivateService}/Core/ProgramSet.cs (73%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/FirewallGuard.cs (97%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/FirewallManager.cs (92%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/FirewallMonitor.cs (97%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/FirewallRule.cs (96%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/FirewallRuleEx.cs (100%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/WinNetFwAPI.cs (96%) rename {PrivateWin10 => PrivateService}/Core/WindowsFirewall/WinNetFwCom.cs (95%) create mode 100644 PrivateService/IPC/Priv10Host.cs create mode 100644 PrivateService/PrivateService.csproj create mode 100644 PrivateService/Properties/AssemblyInfo.cs create mode 100644 PrivateService/Service.cs delete mode 100644 PrivateWin10/API/AppManager.cs delete mode 100644 PrivateWin10/API/AuditPolicy.cs delete mode 100644 PrivateWin10/API/FileOps.cs delete mode 100644 PrivateWin10/API/Network/IPHelper.cs delete mode 100644 PrivateWin10/API/Network/NetFunc.cs delete mode 100644 PrivateWin10/API/ProcFunc.cs delete mode 100644 PrivateWin10/API/ProcessUtilities.cs delete mode 100644 PrivateWin10/API/RegistryMonitor.cs delete mode 100644 PrivateWin10/API/ServiceHelper.cs delete mode 100644 PrivateWin10/API/TokenManipulator.cs delete mode 100644 PrivateWin10/API/UwpFunc.cs delete mode 100644 PrivateWin10/Common/AppLog.cs delete mode 100644 PrivateWin10/Common/ClonableDictionary.cs delete mode 100644 PrivateWin10/Common/ClonableList.cs delete mode 100644 PrivateWin10/Common/HttpTask.cs delete mode 100644 PrivateWin10/Common/IconExtractor.cs delete mode 100644 PrivateWin10/Common/ImgFunc.cs delete mode 100644 PrivateWin10/Common/MiscFunc.cs delete mode 100644 PrivateWin10/Common/MultiValueDictionary.cs delete mode 100644 PrivateWin10/Common/TextHelpers.cs delete mode 100644 PrivateWin10/Common/WinConsole.cs delete mode 100644 PrivateWin10/Common/WpfFunc.cs create mode 100644 PrivateWin10/Controls/ControlList.cs rename PrivateWin10/Controls/{ => Dns}/DnsBlockListsControl.xaml (100%) rename PrivateWin10/Controls/{ => Dns}/DnsBlockListsControl.xaml.cs (96%) rename PrivateWin10/Controls/{ => Dns}/DnsFilterListControl.xaml (100%) rename PrivateWin10/Controls/{ => Dns}/DnsFilterListControl.xaml.cs (97%) rename PrivateWin10/Controls/{ => Dns}/DnsLogList.xaml (100%) rename PrivateWin10/Controls/{ => Dns}/DnsLogList.xaml.cs (97%) rename PrivateWin10/Controls/{ => Dns}/DnsQueryLogControl.xaml (100%) rename PrivateWin10/Controls/{ => Dns}/DnsQueryLogControl.xaml.cs (96%) rename PrivateWin10/Controls/{ => Notify}/ConnectionNotify.xaml (91%) rename PrivateWin10/Controls/{ => Notify}/ConnectionNotify.xaml.cs (95%) rename PrivateWin10/Controls/{ => Notify}/RuleNotify.xaml (100%) rename PrivateWin10/Controls/{ => Notify}/RuleNotify.xaml.cs (96%) rename PrivateWin10/Controls/{ => Notify}/TweakNotify.xaml (100%) rename PrivateWin10/Controls/{ => Notify}/TweakNotify.xaml.cs (93%) create mode 100644 PrivateWin10/Controls/Presets/PresetControl.xaml create mode 100644 PrivateWin10/Controls/Presets/PresetControl.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/PresetCustom.xaml create mode 100644 PrivateWin10/Controls/Presets/PresetCustom.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/PresetFirewall.xaml create mode 100644 PrivateWin10/Controls/Presets/PresetFirewall.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/PresetItemControl.xaml create mode 100644 PrivateWin10/Controls/Presets/PresetItemControl.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/PresetTweaks.xaml create mode 100644 PrivateWin10/Controls/Presets/PresetTweaks.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/RuleItemControl.xaml create mode 100644 PrivateWin10/Controls/Presets/RuleItemControl.xaml.cs create mode 100644 PrivateWin10/Controls/Presets/TweakItemControl.xaml create mode 100644 PrivateWin10/Controls/Presets/TweakItemControl.xaml.cs rename PrivateWin10/Controls/{ => Tweaks}/TweakControl.xaml (95%) rename PrivateWin10/Controls/{ => Tweaks}/TweakControl.xaml.cs (88%) rename PrivateWin10/Controls/{ => Tweaks}/TweakGroup.xaml (95%) rename PrivateWin10/Controls/{ => Tweaks}/TweakGroup.xaml.cs (75%) create mode 100644 PrivateWin10/Core/Presets/CustomPreset.cs create mode 100644 PrivateWin10/Core/Presets/FirewallPreset.cs create mode 100644 PrivateWin10/Core/Presets/PresetGroup.cs create mode 100644 PrivateWin10/Core/Presets/PresetItem.cs create mode 100644 PrivateWin10/Core/Presets/PresetManager.cs create mode 100644 PrivateWin10/Core/Presets/TweakPreset.cs create mode 100644 PrivateWin10/Core/TweakManager.cs create mode 100644 PrivateWin10/IPC/FirewallRule.cs create mode 100644 PrivateWin10/IPC/FirewallRuleEx.cs create mode 100644 PrivateWin10/IPC/MiscObjects.cs create mode 100644 PrivateWin10/IPC/NetworkSocket.cs delete mode 100644 PrivateWin10/IPC/Priv10Host.cs create mode 100644 PrivateWin10/IPC/Program.cs rename PrivateWin10/{Core => IPC}/ProgramID.cs (85%) create mode 100644 PrivateWin10/IPC/ProgramSet.cs create mode 100644 PrivateWin10/Pages/ControlPage.xaml create mode 100644 PrivateWin10/Pages/ControlPage.xaml.cs create mode 100644 PrivateWin10/Resources/icons8-in-16.png create mode 100644 PrivateWin10/Resources/icons8-out-16.png delete mode 100644 PrivateWin10/Translation.ini create mode 100644 TweakEngine/Properties/AssemblyInfo.cs create mode 100644 TweakEngine/TweakEngine.csproj rename PrivateWin10/Core/Tweaks/TweakManager.cs => TweakEngine/TweakList.cs (50%) rename PrivateWin10/Core/Tweaks/TweakStore.cs => TweakEngine/TweakPresets.cs (96%) rename PrivateWin10/Core/Tweaks/TweakEngine.cs => TweakEngine/TweakTools.cs (96%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ca6128..5d9521f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,37 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). + + +## [0.80] - 2020-10-07 + +### Added +- added a few more tweaks + Disable Device Census + Disable Kernel Crash Dump creation + +### Changed +- All microsoft binaries under System32 are now classifyed as system components (not only services and kernel) +- moved shared code to a helper library +- splited the project in separated UI and service components +- reingeniered the IPC mechanism for better performance +- notification window now displays all changed rules also those that changed when the client wasn't running +- reworked client start + +### Fixed +- list sometimes opening in the wron mode +- fixed issue with program and program set description resolvationo +- fixed some crash issues + + + + + ## [0.75b] - 2020-03-20 + ### Fixed -- crash issue with notification window +- This release fixes a crash issue with the notification window. + ## [0.75] - 2020-02-18 @@ -165,6 +193,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [0.60.1] - 2019-12-01 ### Added +- added process monitor, using ETW events to aid resolving PID to program, when the prgram in question already exited. - added upstream dns diplay to the dns proxy page ### Changed diff --git a/ETW/O365.Security.Native.ETW/O365.Security.Native.ETW.vcxproj b/ETW/O365.Security.Native.ETW/O365.Security.Native.ETW.vcxproj index 897f3c3..d49bc52 100644 --- a/ETW/O365.Security.Native.ETW/O365.Security.Native.ETW.vcxproj +++ b/ETW/O365.Security.Native.ETW/O365.Security.Native.ETW.vcxproj @@ -1,18 +1,34 @@  + + DebugSigning + Win32 + DebugSigning x64 + + Debug + Win32 + Debug x64 + + ReleaseSigning + Win32 + ReleaseSigning x64 + + Release + Win32 + Release x64 @@ -36,6 +52,13 @@ Unicode false + + v141 + true + DynamicLibrary + Unicode + false + v141 true @@ -43,18 +66,37 @@ Unicode false + + v141 + true + DynamicLibrary + Unicode + false + v141 DynamicLibrary true false + + v141 + DynamicLibrary + true + false + v141 DynamicLibrary true false + + v141 + DynamicLibrary + true + false + @@ -68,6 +110,12 @@ + + false + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs + true + + false $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs @@ -75,16 +123,32 @@ $(SolutionDir)..\35MSSharedLib1024.snk true + + false + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs + true + $(SolutionDir)..\35MSSharedLib1024.snk + true + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs + + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs + + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs $(SolutionDir)..\35MSSharedLib1024.snk true + + $(SolutionDir);$(VC_IncludePath);$(WindowsSDK_IncludePath);;$(ProjectDir)..\krabs + $(SolutionDir)..\35MSSharedLib1024.snk + true + TYPEASSERT;_DEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) @@ -109,6 +173,30 @@ sign + + + TYPEASSERT;_DEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) + Level4 + true + MultiThreadedDebugDLL + $(ProjectDir)..\inc;%(AdditionalIncludeDirectories) + true + stdcpp17 + + + true + true + + + if not exist $(SolutionDir)..\35MSSharedLib1024.snk sn.exe -k $(SolutionDir)..\35MSSharedLib1024.snk + + + Verify snk file is generated + + + sign + + TYPEASSERT;_DEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) @@ -133,16 +221,50 @@ sign + + + TYPEASSERT;_DEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) + Level4 + true + MultiThreadedDebugDLL + $(ProjectDir)..\inc;%(AdditionalIncludeDirectories) + true + stdcpp17 + + + true + true + + + if not exist $(SolutionDir)..\35MSSharedLib1024.snk sn.exe -k $(SolutionDir)..\35MSSharedLib1024.snk + + + Verify snk file is generated + + + sign + + Level4 + + + Level4 + + Level4 + + + Level4 + + true @@ -164,6 +286,27 @@ sign + + + true + NDEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) + $(ProjectDir)..\inc;%(AdditionalIncludeDirectories) + true + stdcpp17 + + + true + + + if not exist $(SolutionDir)..\35MSSharedLib1024.snk sn.exe -k $(SolutionDir)..\35MSSharedLib1024.snk + + + Verify snk file is generated + + + sign + + true @@ -185,6 +328,27 @@ sign + + + true + NDEBUG;UNICODE;_WINDLL;%(PreprocessorDefinitions) + $(ProjectDir)..\inc;%(AdditionalIncludeDirectories) + true + stdcpp17 + + + true + + + if not exist $(SolutionDir)..\35MSSharedLib1024.snk sn.exe -k $(SolutionDir)..\35MSSharedLib1024.snk + + + Verify snk file is generated + + + sign + + diff --git a/FirewallCore/FirewallCore.vcxproj b/FirewallCore/FirewallCore.vcxproj new file mode 100644 index 0000000..1542f96 --- /dev/null +++ b/FirewallCore/FirewallCore.vcxproj @@ -0,0 +1,163 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {AD34AA9B-ADBD-4ECF-A04C-DF43217AEF74} + v4.5 + ManagedCProj + FirewallCore + 10.0.17763.0 + + + + DynamicLibrary + true + v141 + true + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + DynamicLibrary + true + v141 + true + Unicode + false + + + DynamicLibrary + false + v141 + true + Unicode + false + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Use + Level3 + Disabled + WIN32;_DEBUG;%(PreprocessorDefinitions) + + + + + + + + Use + Level3 + Disabled + _DEBUG;%(PreprocessorDefinitions) + + + + + + + + Use + Level3 + WIN32;NDEBUG;%(PreprocessorDefinitions) + + + + + + + + Use + Level3 + NDEBUG;%(PreprocessorDefinitions) + + + + + + + + + + + + + + + Create + Create + Create + Create + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/FirewallCore/FirewallCore.vcxproj.filters b/FirewallCore/FirewallCore.vcxproj.filters new file mode 100644 index 0000000..9cbca76 --- /dev/null +++ b/FirewallCore/FirewallCore.vcxproj.filters @@ -0,0 +1,49 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Header Files + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + Source Files + + + + + Resource Files + + + + + Resource Files + + + \ No newline at end of file diff --git a/MiscHelpers/API/AppManager.cs b/MiscHelpers/API/AppManager.cs new file mode 100644 index 0000000..3927d4d --- /dev/null +++ b/MiscHelpers/API/AppManager.cs @@ -0,0 +1,318 @@ +using Microsoft.Win32; +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Security.Principal; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Xml.Linq; +//using Windows.ApplicationModel; +//using Windows.Management.Deployment; + +namespace MiscHelpers +{ + + public class AppManager + { + + public AppManager() + { + + } + + [DllImport("kernelbase", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern int AppContainerLookupMoniker(IntPtr Sid, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string packageFamilyName); + + [DllImport("kernelbase", CharSet = CharSet.Unicode, SetLastError = true)] + public static extern int AppContainerDeriveSidFromMoniker([In, MarshalAs(UnmanagedType.LPWStr)] string packageFamilyName, ref IntPtr pSID); + + [DllImport("advapi32", CharSet = CharSet.Unicode)] + public static extern bool ConvertStringSidToSid([In, MarshalAs(UnmanagedType.LPWStr)] string pStringSid, ref IntPtr pSID); + + [DllImport("advapi32", CharSet = CharSet.Unicode)] + public static extern bool ConvertSidToStringSid(IntPtr pSID, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string pStringSid); + + static public string SidToAppPackage(string sid) + { + string packageID = ""; + + IntPtr pSid = new IntPtr(); + ConvertStringSidToSid(sid, ref pSid); + + int ret = AppContainerLookupMoniker(pSid, ref packageID); + + Marshal.FreeHGlobal(pSid); + + if (ret != ERROR_SUCCESS) + { + return sid; + + /*var subKey = Registry.ClassesRoot.OpenSubKey(@"Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Mappings\" + sid, false); + if (subKey != null) + packageID = subKey.GetValue("Moniker").ToString();*/ + } + + return packageID; + } + + static public string AppPackageToSid(string packageID) + { + string strSID = ""; + + IntPtr pSid = new IntPtr(); + + int ret = AppContainerDeriveSidFromMoniker(packageID, ref pSid); + + ConvertSidToStringSid(pSid, ref strSID); + + Marshal.FreeHGlobal(pSid); + + return strSID; + } + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern Int32 GetApplicationUserModelId(IntPtr hProcess, ref UInt32 AppModelIDLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder sbAppUserModelID); + + const int ERROR_INSUFFICIENT_BUFFER = 0x7a; + const int ERROR_SUCCESS = 0x0; + + public string GetAppPackageByPID_(int PID) + { + // WARNING: this is not consistent with the windows firewall, we need to go by the proces token + + //var process = System.Diagnostics.Process.GetProcessById(PID); // throws error if pid is not found + var processHandle = ProcFunc.OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, PID); + if (processHandle == IntPtr.Zero) + return null; + + uint cchLen = 256; + StringBuilder sbName = new StringBuilder((int)cchLen); + Int32 lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName); + if (ERROR_INSUFFICIENT_BUFFER == lResult) + { + sbName = new StringBuilder((int)cchLen); + lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName); + } + ProcFunc.CloseHandle(processHandle); + + if (lResult != ERROR_SUCCESS) + return null; + + string sResult = sbName.ToString(); + int pos = sResult.LastIndexOf("!"); // bla!App + if (pos != -1) + sResult = sResult.Substring(0, pos); + + return sResult; + } + + private Dictionary AppInfosBySid = new Dictionary(); + private ReaderWriterLockSlim AppInfosBySidLock = new ReaderWriterLockSlim(); + + private Windows.Management.Deployment.PackageManager packageManager = new Windows.Management.Deployment.PackageManager(); + + public UwpFunc.AppInfo GetAppInfoBySid(string sid) + { + UwpFunc.AppInfo info = null; + AppInfosBySidLock.EnterReadLock(); + AppInfosBySid.TryGetValue(sid, out info); + AppInfosBySidLock.ExitReadLock(); + if (info != null) + return info; + + string appPackageID = SidToAppPackage(sid); + if (appPackageID == null || appPackageID.Length == 0) + return null; + + IEnumerable packages; + + try + { + packages = packageManager.FindPackages(appPackageID); + } + catch + { + return null; + } + + foreach (var package in packages) + { + info = GetInfo(package, sid); + if (info != null) + { + AppInfosBySidLock.EnterWriteLock(); + if (!AppInfosBySid.ContainsKey(sid)) + AppInfosBySid.Add(sid, info); + AppInfosBySidLock.ExitWriteLock(); + break; + } + } + + return info; + } + + + private UwpFunc.AppInfo GetInfo(Windows.ApplicationModel.Package package, string sid) + { + string path; + try + { + path = package.InstalledLocation.Path; + } + catch + { + return null; + } + + string manifest = Path.Combine(path, "AppxManifest.xml"); + + if (!File.Exists(manifest)) + return null; + + XElement xelement; + try + { + string manifestXML = File.ReadAllText(manifest); + + int startIndex = manifestXML.IndexOf("", StringComparison.Ordinal); + int num = manifestXML.IndexOf("", StringComparison.Ordinal); + xelement = XElement.Parse(manifestXML.Substring(startIndex, num - startIndex + 13).Replace("uap:", string.Empty)); + } + catch (Exception err) + { + AppLog.Exception(err); + return null; + } + + string displayName = null; + string logoPath = null; + try + { + displayName = xelement.Element((XName)"DisplayName")?.Value; + logoPath = xelement.Element((XName)"Logo")?.Value; + } + catch (Exception err) + { + AppLog.Exception(err); + } + + if (displayName == null) + return null; + + try + { + Uri result; + if (Uri.TryCreate(displayName, UriKind.Absolute, out result)) + { + string pathToPri = Path.Combine(package.InstalledLocation.Path, "resources.pri"); + string resourceKey1 = "ms-resource://" + package.Id.Name + "/resources/" + ((IEnumerable)result.Segments).Last(); + string stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey1); + if (!string.IsNullOrEmpty(stringFromPriFile.Trim())) + displayName = stringFromPriFile; + else + { + string str = string.Concat(((IEnumerable)result.Segments).Skip(1)); + string resourceKey2 = "ms-resource://" + package.Id.Name + "/" + str; + stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey2); + if (!string.IsNullOrEmpty(stringFromPriFile.Trim())) + displayName = stringFromPriFile; + } + } + + if (logoPath != null) + { + string path1 = Path.Combine(package.InstalledLocation.Path, logoPath); + if (File.Exists(path1)) + logoPath = path1; + else + { + string path2 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path1, "scale-100.png")); + if (File.Exists(path2)) + logoPath = path2; + else + { + string path3 = Path.Combine(Path.Combine(package.InstalledLocation.Path, "en-us"), logoPath); + string path4 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path3, "scale-100.png")); + if (File.Exists(path4)) + logoPath = path4; + else + logoPath = null; + } + } + } + } + catch (Exception err) + { + AppLog.Exception(err); + } + + return new UwpFunc.AppInfo() { Name = displayName, Logo = logoPath, ID = package.Id.FamilyName, SID = sid }; + } + + bool FullListFetched = false; + + public void UpdateAppCache() + { + Dictionary AppInfos = new Dictionary(); + + IEnumerable packages = (IEnumerable)packageManager.FindPackages(); + foreach (var package in packages) + { + string appSID = AppPackageToSid(package.Id.FamilyName).ToLower(); + + UwpFunc.AppInfo info = GetInfo(package, appSID); + if (info != null) + { + if (!AppInfos.ContainsKey(appSID)) + AppInfos.Add(appSID, info); + /* + UwpFunc.AppInfo old_info; + if (AppInfos.TryGetValue(appSID, out old_info)) + AppLog.Debug("Warning an app with the SID: {0} is already listed", appSID); + */ + } + } + + AppInfosBySidLock.EnterWriteLock(); + AppInfosBySid = AppInfos; + AppInfosBySidLock.ExitWriteLock(); + FullListFetched = true; + } + + public List GetAllApps(bool bReload = false) + { + if (!FullListFetched || bReload) + UpdateAppCache(); + + List Apps = new List(); + AppInfosBySidLock.EnterReadLock(); + foreach (UwpFunc.AppInfo info in AppInfosBySid.Values) + Apps.Add(info); + AppInfosBySidLock.ExitReadLock(); + return Apps; + } + + ////////////////////////////////////////////////////////////////////////////////////////////// + // App resource handling + + + public string GetAppResourceStr(string resourcePath) + { + // Note: PackageManager requirers admin privilegs + + var AppResource = TextHelpers.Split2(resourcePath.Substring(2, resourcePath.Length - 3), "?"); + var package = packageManager.FindPackage(AppResource.Item1); + if (package != null) + { + string pathToPri = Path.Combine(package.InstalledLocation.Path, "resources.pri"); + return MiscFunc.GetResourceStr(pathToPri, AppResource.Item2); + } + + return resourcePath; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/AuditPolicy.cs b/MiscHelpers/API/AuditPolicy.cs new file mode 100644 index 0000000..0dcfc15 --- /dev/null +++ b/MiscHelpers/API/AuditPolicy.cs @@ -0,0 +1,290 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + static public class AuditPolicy + { + /// + /// The AuditEnumerateCategories function enumerates the available audit-policy categories. + /// + /// A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. + /// A pointer to the number of elements in the ppAuditCategoriesArray array. + /// A System.Boolean value that indicates whether the function completed successfully. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375636(v=vs.85).aspx + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditEnumerateCategories(out IntPtr ppAuditCategoriesArray, out uint pCountReturned); + + + /// + /// The AuditLookupCategoryName function retrieves the display name of the specified audit-policy category. + /// + /// A pointer to a GUID structure that specifies an audit-policy category. + /// The address of a pointer to a null-terminated string that contains the display name of the audit-policy category specified by the pAuditCategoryGuid function. + /// A System.Boolean value that indicates whether the function completed successfully. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375687(v=vs.85).aspx + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditLookupCategoryName(ref Guid pAuditCategoryGuid, out StringBuilder ppszCategoryName); + + + /// + /// The AuditEnumerateSubCategories function enumerates the available audit-policy subcategories. + /// + /// The GUID of an audit-policy category for which subcategories are enumerated. If the value of the bRetrieveAllSubCategories parameter is TRUE, this parameter is ignored. + /// TRUE to enumerate all audit-policy subcategories; FALSE to enumerate only the subcategories of the audit-policy category specified by the pAuditCategoryGuid parameter. + /// A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. The GUID structures specify the audit-policy subcategories available on the computer. + /// A pointer to the number of audit-policy subcategories returned in the ppAuditSubCategoriesArray array. + /// A System.Boolean value that indicates whether the function completed successfully. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375648(v=vs.85).aspx + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditEnumerateSubCategories(ref Guid pAuditCategoryGuid, bool bRetrieveAllSubCategories, out IntPtr ppAuditSubCategoriesArray, out uint pCountReturned); + + + /// + /// The AuditLookupSubCategoryName function retrieves the display name of the specified audit-policy subcategory. + /// + /// A pointer to a GUID structure that specifies an audit-policy subcategory. + /// The address of a pointer to a null-terminated string that contains the display name of the audit-policy subcategory specified by the pAuditSubCategoryGuid parameter. + /// A System.Boolean value that indicates whether the function completed successfully. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375693(v=vs.85).aspx + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditLookupSubCategoryName(ref Guid pAuditSubCategoryGuid, out StringBuilder ppszSubCategoryName); + + + /// + /// The AuditFree function frees the memory allocated by audit functions for the specified buffer. + /// + /// A pointer to the buffer to free. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375654(v=vs.85).aspx + [DllImport("advapi32.dll")] + private static extern void AuditFree(IntPtr buffer); + + + /// + /// The AuditQuerySystemPolicy function retrieves system audit policy for one or more audit-policy subcategories. + /// + /// A pointer to an array of GUID values that specify the subcategories for which to query audit policy. + /// The number of elements in each of the pSubCategoryGuids and ppAuditPolicy arrays. + /// A pointer to a single buffer that contains both an array of pointers to AUDIT_POLICY_INFORMATION structures and the structures themselves. + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375702(v=vs.85).aspx + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditQuerySystemPolicy(ref Guid pSubCategoryGuids, uint PolicyCount, out IntPtr ppAuditPolicy); + + /// + /// The AuditSetSystemPolicy function sets system audit policy for one or more audit-policy subcategories. + /// + /// A pointer to an array of AUDIT_POLICY_INFORMATION structures. Each structure specifies system audit policy for one audit-policy subcategory. + /// The number of elements in the pAuditPolicy array. + /// https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-auditsetsystempolicy + [DllImport("advapi32.dll", SetLastError = true)] + private static extern bool AuditSetSystemPolicy(IntPtr pAuditPolicy, uint PolicyCount); + + /// + /// Gets the GUIDs of the audit categories. + /// + /// The GUIDs of the audit categories on the local machine. + static public List GetCategoryIdentifiers() + { + List identifiers = new List(); + IntPtr buffer; + uint categoryCount; + bool success = AuditEnumerateCategories(out buffer, out categoryCount); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + for (int i = 0, elemOffs = (int)buffer; i < categoryCount; i++) + { + Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid)); + identifiers.Add(Convert.ToString(guid)); + elemOffs += Marshal.SizeOf(typeof(Guid)); + } + AuditFree(buffer); + return identifiers; + } + + + /// + /// Returns the display name of the audit category with the specified GUID. + /// + /// The GUID of the category for which the display name should be returned. + /// The display name of the category - for example "Account Management". + static public String GetCategoryDisplayName(String guid) + { + return GetCategoryDisplayName(new Guid(guid)); + } + + + /// + /// Returns the display name of the audit category with the specified GUID. + /// + /// The GUID of the category for which the display name should be returned. + /// The display name of the category - for example "Account Management". + static public String GetCategoryDisplayName(Guid guid) + { + StringBuilder buffer = new StringBuilder(); + bool success = AuditLookupCategoryName(ref guid, out buffer); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + if (buffer == null) { throw new ArgumentException(String.Format("CategoryDisplayNameNotFoundException on guid: {0}", guid)); } + String categoryDisplayName = buffer.ToString(); + buffer = null; + return categoryDisplayName; + } + + + /// + /// Gets the GUIDs of the audit subcategories of the specified category. + /// + /// The GUID of the category for which the subcategories should be returned. + /// The GUIDs of the audit subcategories for the specified category. + static public List GetSubCategoryIdentifiers(String categoryGuid) + { + return GetSubCategoryIdentifiers(new Guid(categoryGuid)); + } + + + /// + /// Gets the GUIDs of the audit subcategories of the specified category. + /// + /// The GUID of the category for which the subcategories should be returned. + /// The GUIDs of the audit subcategories for the specified category. + static public List GetSubCategoryIdentifiers(Guid categoryGuid) + { + List identifiers = new List(); + IntPtr buffer; + uint subCategoryCount; + bool success = AuditEnumerateSubCategories(ref categoryGuid, false, out buffer, out subCategoryCount); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + for (int i = 0, elemOffs = (int)buffer; i < subCategoryCount; i++) + { + Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid)); + identifiers.Add(Convert.ToString(guid)); + elemOffs += Marshal.SizeOf(typeof(Guid)); + } + AuditFree(buffer); + return identifiers; + } + + + /// + /// Returns the display name of the audit subcategory with the specified GUID. + /// + /// The GUID of the subcategory for which the display name should be returned. + /// The display name of the subcategory - for example "Audit Credential Validation". + static public String GetSubCategoryDisplayName(String guid) + { + return GetSubCategoryDisplayName(new Guid(guid)); + } + + + /// + /// Returns the display name of the audit subcategory with the specified GUID. + /// + /// The GUID of the subcategory for which the display name should be returned. + /// The display name of the subcategory - for example "Audit Credential Validation". + static public String GetSubCategoryDisplayName(Guid guid) + { + StringBuilder buffer = new StringBuilder(); + bool success = AuditLookupSubCategoryName(ref guid, out buffer); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + String subCategoryDisplayName = buffer.ToString(); + buffer = null; + return subCategoryDisplayName; + } + + + /// + /// Gets the audit policy configured for the specified subcategory GUID. + /// + /// The GUID of the subcategory for which the policy should be returned. + /// Returns an AUDIT_POLICY_INFORMATION that contains information about the policy. + static public AUDIT_POLICY_INFORMATION GetSystemPolicy(String subCategoryGuid) + { + return GetSystemPolicy(new Guid(subCategoryGuid)); + } + + + /// + /// Gets the audit policy configured for the specified subcategory GUID. + /// + /// The GUID of the subcategory for which the policy should be returned. + /// Returns an AUDIT_POLICY_INFORMATION that contains information about the policy. + static public AUDIT_POLICY_INFORMATION GetSystemPolicy(Guid subCategoryGuid) + { + List identifiers = new List(); + IntPtr buffer; + bool success = AuditQuerySystemPolicy(ref subCategoryGuid, 1, out buffer); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + AUDIT_POLICY_INFORMATION policyInformation = (AUDIT_POLICY_INFORMATION)Marshal.PtrToStructure(buffer, typeof(AUDIT_POLICY_INFORMATION)); + AuditFree(buffer); + return policyInformation; + } + + /// + /// Sets the audit policy configuration. + /// + /// AUDIT_POLICY_INFORMATION to be set + /// nothing + static public void SetSystemPolicy(AUDIT_POLICY_INFORMATION policyInformation) + { + List identifiers = new List(); + IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(policyInformation)); + Marshal.StructureToPtr(policyInformation, buffer, true); + bool success = AuditSetSystemPolicy(buffer, 1); + AuditFree(buffer); + if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } + } + + + /// + /// The AUDIT_POLICY_INFORMATION structure specifies a security event type and when to audit that type. + /// + /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa965467(v=vs.85).aspx + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct AUDIT_POLICY_INFORMATION + { + + /// + /// A GUID structure that specifies an audit subcategory. + /// + public Guid AuditSubCategoryGuid; + + /// + /// A set of bit flags that specify the conditions under which the security event type specified by the AuditSubCategoryGuid and AuditCategoryGuid members are audited. + /// + public AUDIT_POLICY_INFORMATION_TYPE AuditingInformation; + + /// + /// A GUID structure that specifies an audit-policy category. + /// + public Guid AuditCategoryGuid; + + } + + + /// + /// Represents the auditing type. + /// + [Flags] + public enum AUDIT_POLICY_INFORMATION_TYPE + { + + /// + /// Do not audit the specified event type. + /// + None = 0, + + /// + /// Audit successful occurrences of the specified event type. + /// + Success = 1, + + /// + /// Audit failed attempts to cause the specified event type. + /// + Failure = 2, + } + } +} \ No newline at end of file diff --git a/PrivateWin10/API/Network/DnsApi.cs b/MiscHelpers/API/DnsApi.cs similarity index 93% rename from PrivateWin10/API/Network/DnsApi.cs rename to MiscHelpers/API/DnsApi.cs index 06b2b9f..02bc402 100644 --- a/PrivateWin10/API/Network/DnsApi.cs +++ b/MiscHelpers/API/DnsApi.cs @@ -5,7 +5,7 @@ using System.Text; using System.Threading.Tasks; -namespace PrivateWin10 +namespace MiscHelpers { public static class DnsApi { @@ -138,13 +138,13 @@ public enum DnsFreeType : uint // kernel32.dll [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - internal static extern IntPtr LoadLibrary(string lpFileName); + public static extern IntPtr LoadLibrary(string lpFileName); [DllImport("kernel32.dll", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)] - internal static extern IntPtr GetProcAddress(IntPtr hModule, string procName); + public static extern IntPtr GetProcAddress(IntPtr hModule, string procName); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool FreeLibrary(IntPtr hModule); + public static extern bool FreeLibrary(IntPtr hModule); } } diff --git a/PrivateWin10/Core/DnsProxy/DnsConfigurator.cs b/MiscHelpers/API/DnsConfigurator.cs similarity index 95% rename from PrivateWin10/Core/DnsProxy/DnsConfigurator.cs rename to MiscHelpers/API/DnsConfigurator.cs index 05a46a2..f670e34 100644 --- a/PrivateWin10/Core/DnsProxy/DnsConfigurator.cs +++ b/MiscHelpers/API/DnsConfigurator.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; +using MiscHelpers; using System; using System.Collections.Generic; using System.Linq; @@ -6,9 +7,9 @@ using System.Text; using System.Threading.Tasks; -namespace PrivateWin10 +namespace MiscHelpers { - class DnsConfigurator + public static class DnsConfigurator { [DllImport("dhcpcsvc.dll", SetLastError = true, CharSet = CharSet.Unicode)] public static extern uint DhcpNotifyConfigChange(string ServerName, string AdapterName, bool NewIpAddress, uint IpIndex, uint IpAddress, uint SubnetMask, int DhcpAction); diff --git a/MiscHelpers/API/FileOps.cs b/MiscHelpers/API/FileOps.cs new file mode 100644 index 0000000..0103b65 --- /dev/null +++ b/MiscHelpers/API/FileOps.cs @@ -0,0 +1,236 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Security.AccessControl; +using System.Security.Principal; +using System.IO; +using MiscHelpers; + +namespace MiscHelpers +{ + public class FileOps + { + static public string FormatSize(decimal size) + { + if (size > 1024 * 1024 * 1024) + return (size / (1024 * 1024 * 1024)).ToString("F") + " GB"; + if (size > 1024 * 1024) + return (size / (1024 * 1024)).ToString("F") + " MB"; + if (size > 1024) + return (size / (1024)).ToString("F") + " KB"; + return ((Int64)size).ToString() + " B"; + } + + static public bool MoveFile(string from, string to, bool Overwrite = false) + { + try + { + if (File.Exists(to)) + { + if (!Overwrite) + return false; + File.Delete(to); + } + + File.Move(from, to); + + if (File.Exists(from)) + return false; + } + catch (Exception err) + { + AppLog.Exception(err); + return false; + } + return true; + } + + static public bool DeleteFile(string path) + { + try + { + File.Delete(path); + return true; + } + catch + { + return false; + } + } + + static public int TestFileAdminSec(String filePath) + { + //get file info + FileInfo fi = new FileInfo(filePath); + if (!fi.Exists) + return 2; + + //get security access + FileSecurity fs = fi.GetAccessControl(); + + //get any special user access + AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); // get as SID not string + + + //remove any special access + foreach (FileSystemAccessRule rule in rules) + { + if (rule.AccessControlType != AccessControlType.Allow) + continue; + if (rule.IdentityReference.Value.Equals(SID_Admins) || rule.IdentityReference.Value.Equals(SID_System)) + continue; + if ((rule.FileSystemRights & (FileSystemRights.Write | FileSystemRights.Delete)) != 0) + return 0; + } + return 1; + } + + static public void SetFileAdminSec(String filePath) + { + //get file info + FileInfo fi = new FileInfo(filePath); + if (!fi.Exists) + { + FileStream f_out = fi.OpenWrite(); + f_out.Close(); + } + + //get security access + FileSecurity fs = fi.GetAccessControl(); + + //remove any inherited access + fs.SetAccessRuleProtection(true, false); + + //get any special user access + AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); // show as names + + //remove any special access + foreach (FileSystemAccessRule rule in rules) + fs.RemoveAccessRule(rule); + + fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_Admins), FileSystemRights.FullControl, AccessControlType.Allow)); + fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_System), FileSystemRights.FullControl, AccessControlType.Allow)); + fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_Users), FileSystemRights.Read, AccessControlType.Allow)); + + //add current user with full control. + //fs.AddAccessRule(new FileSystemAccessRule(domainName + "\\" + userName, FileSystemRights.FullControl, AccessControlType.Allow)); + + //add all other users delete only permissions. + //SecurityIdentifier sid = new SecurityIdentifier("S-1-5-11"); // Authenticated Users + //fs.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Delete, AccessControlType.Allow)); + + //flush security access. + File.SetAccessControl(filePath, fs); + } + + static public void SetAnyDirSec(string filePath) + { + DirectoryInfo info = new DirectoryInfo(filePath); + DirectorySecurity security = info.GetAccessControl(); + security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_World), FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); + security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_World), FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); + info.SetAccessControl(security); + } + + static public bool TestWrite(string filePath) + { + FileInfo fi = new FileInfo(filePath); + try + { + FileStream f_out = fi.OpenWrite(); + f_out.Close(); + return true; + } + catch + { + return false; + } + } + + public static string SID_null = "S-1-0-0"; // Null SID + public static string SID_World = "S-1-1-0"; // World + public static string SID_Local = "S-1-2-0"; // Local + public static string SID_Console = "S-1-2-1"; // Console Logon + public static string SID_OwnerID = "S-1-3-0"; // Creator Owner ID + public static string SID_GroupID = "S-1-3-1"; // Creator Group ID + public static string SID_OwnerSvr = "S-1-3-2"; // Creator Owner Server + public static string SID_CreatorSvr = "S-1-3-3"; // Creator Group Server + public static string SID_OwnerRights = "S-1-3-4"; // Owner Rights + public static string SID_NonUnique = "S-1-4"; // Non-unique Authority + public static string SID_NTAuth = "S-1-5"; // NT Authority + public static string SID_AllServices = "S-1-5-80-0"; // All Services + public static string SID_DialUp = "S-1-5-1"; // Dialup + public static string SID_LocalAcc = "S-1-5-113"; // Local account + public static string SID_LocalAccAdmin = "S-1-5-114"; // Local account and member of Administrators group + public static string SID_Net = "S-1-5-2"; // Network + public static string SID_Natch = "S-1-5-3"; // Batch + public static string SID_Interactive = "S-1-5-4"; // Interactive + //public static string SID_ = "S-1-5-5- *X*- *Y* Logon Session + public static string SID_Service = "S-1-5-6"; // Service + public static string SID_AnonLogin = "S-1-5-7"; // Anonymous Logon + + public static string SID_Proxy = "S-1-5-8"; // Proxy + public static string SID_EDC = "S-1-5-9"; // Enterprise Domain Controllers + public static string SID_Self = "S-1-5-10"; // Self + public static string SID_AuthenticetedUser = "S-1-5-11"; // Authenticated Users + + public static string SID_Restricted = "S-1-5-12"; // Restricted Code + public static string SID_TermUser = "S-1-5-13"; // Terminal Server User + public static string SID_RemoteLogin = "S-1-5-14"; // Remote Interactive Logon + public static string SID_ThisORg = "S-1-5-15"; // This Organization + public static string SID_IIS = "S-1-5-17"; // IIS_USRS + public static string SID_System = "S-1-5-18"; // System(or LocalSystem) + + public static string SID_NTAuthL = "S-1-5-19"; // NT Authority(LocalService) + public static string SID_NetServices = "S-1-5-20"; // Network Service + + public static string SID_Admins = "S-1-5-32-544"; // Administrators + public static string SID_Users = "S-1-5-32-545"; // Users + public static string SID_Guests = "S-1-5-32-546"; // Guests + public static string SID_PowerUsers = "S-1-5-32-547"; // Power Users + public static string SID_AccOps = "S-1-5-32-548"; // Account Operators + public static string SID_ServerOps = "S-1-5-32-549"; // Server Operators + public static string SID_PrintOps = "S-1-5-32-550"; // Print Operators + public static string SID_BackupOps = "S-1-5-32-551"; // Backup Operators + public static string SID_Replicators = "S-1-5-32-552"; // Replicators + public static string SID_NTLM_Auth = "S-1-5-64-10"; // NTLM Authentication + public static string SID_SCh_Auth = "S-1-5-64-14"; // SChannel Authentication + public static string SID_DigestAuth = "S-1-5-64-21"; // Digest Authentication + public static string SID_NT_Service = "S-1-5-80"; // NT Service + public static string SID_All_Services = "S-1-5-80-0"; // All Services + public static string SID_VM = "S-1-5-83-0"; // NT VIRTUAL MACHINE\Virtual Machines + public static string SID_UntrustedLevel = "S-1-16-0"; // Untrusted Mandatory Level + public static string SID_LowLevel = "S-1-16-4096"; // Low Mandatory Level + public static string SID_MediumLevel = "S-1-16-8192"; // Medium Mandatory Level + public static string SID_MediumPLevel = "S-1-16-8448"; // Medium Plus Mandatory Level + public static string SID_HighLevel = "S-1-16-12288"; // High Mandatory Level + public static string SID_SysLevel = "S-1-16-16384"; // System Mandatory Level + public static string SID_PPLevel = "S-1-16-20480"; // Protected Process Mandatory Level + public static string SID_SPLevel = "S-1-16-28672"; // Secure Process Mandatory Level + + public static bool TakeOwn(string path) + { + bool ret = true; + try + { + TokenManipulator.AddPrivilege(TokenManipulator.SE_TAKE_OWNERSHIP_NAME); + + FileSecurity ac = File.GetAccessControl(path); + ac.SetOwner(new SecurityIdentifier(FileOps.SID_Admins)); + File.SetAccessControl(path, ac); + } + catch (Exception err) + { + AppLog.Exception(err); + ret = false; + } + finally + { + TokenManipulator.RemovePrivilege(TokenManipulator.SE_TAKE_OWNERSHIP_NAME); + } + return ret; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/IPHelper.cs b/MiscHelpers/API/IPHelper.cs new file mode 100644 index 0000000..54052c5 --- /dev/null +++ b/MiscHelpers/API/IPHelper.cs @@ -0,0 +1,484 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + + public class IPHelper + { + [StructLayout(LayoutKind.Sequential)] + public struct TCPIP_OWNER_MODULE_BASIC_INFO + { + public IntPtr pModuleName; + public IntPtr pModulePath; + } + + + public class ModuleInfo + { + public string ModuleName; + public string ModulePath; + + public ModuleInfo(TCPIP_OWNER_MODULE_BASIC_INFO inf) + { + ModuleName = inf.pModuleName != null ? Marshal.PtrToStringAuto(inf.pModuleName) : ""; + ModulePath = inf.pModulePath != null ? Marshal.PtrToStringAuto(inf.pModulePath) : ""; + } + } + + public interface I_SOCKET_ROW + { + int ProcessId { get; } + ModuleInfo Module { get; } + + UInt32 ProtocolType { get; } + IPAddress RemoteAddress { get; } + UInt16 RemotePort { get; } + IPAddress LocalAddress { get; } + UInt16 LocalPort { get; } + MIB_TCP_STATE State { get; } + DateTime CreationTime { get; } + } + + + + [DllImport("kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)] + public static extern void ZeroMemory(IntPtr dest, IntPtr size); + + public const UInt32 NO_ERROR = 0; + public const UInt32 ERROR_INSUFFICIENT_BUFFER = 122; + public const UInt32 ERROR_NOT_FOUND = 1168; + + public enum AF_INET + { + IP4 = 2, + IP6 = 23 + } + + public enum AF_PROT + { + TCP = 6, + UDP = 17 + } + + ////////////////////////////////////////////////////// + // TCP + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetExtendedTcpTable(IntPtr pTcpTable, ref UInt32 dwOutBufLen, [MarshalAs(UnmanagedType.Bool)] bool order, AF_INET ipVersion, TCP_TABLE_CLASS tblClass, UInt32 reserved); + + public enum TCP_TABLE_CLASS + { + TCP_TABLE_BASIC_LISTENER, + TCP_TABLE_BASIC_CONNECTIONS, + TCP_TABLE_BASIC_ALL, + TCP_TABLE_OWNER_PID_LISTENER, + TCP_TABLE_OWNER_PID_CONNECTIONS, + TCP_TABLE_OWNER_PID_ALL, + TCP_TABLE_OWNER_MODULE_LISTENER, + TCP_TABLE_OWNER_MODULE_CONNECTIONS, + TCP_TABLE_OWNER_MODULE_ALL + } + + public enum TCPIP_OWNER_MODULE_INFO_CLASS + { + TCPIP_OWNER_MODULE_INFO_BASIC + } + + public enum MIB_TCP_STATE + { + CLOSED = 1, + LISTENING, + SYN_SENT, + SYN_RCVD, + ESTABLISHED, + FIN_WAIT1, + FIN_WAIT2, + CLOSE_WAIT, + CLOSING, + LAST_ACK, + TIME_WAIT, + DELETE_TCB, + UNDEFINED = 65535 + } + + public enum TCP_ESTATS_TYPE + { + TcpConnectionEstatsSynOpts, + TcpConnectionEstatsData, + TcpConnectionEstatsSndCong, + TcpConnectionEstatsPath, + TcpConnectionEstatsSendBuff, + TcpConnectionEstatsRec, + TcpConnectionEstatsObsRec, + TcpConnectionEstatsBandwidth, + TcpConnectionEstatsFineRtt, + TcpConnectionEstatsMaximum + } + + + ////////////////////////////////////////////////////// + // TCPv4 + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetOwnerModuleFromTcpEntry(ref MIB_TCPROW_OWNER_MODULE pTcpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); + + /*[DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetPerTcpConnectionEStats(ref MIB_TCPROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, IntPtr Ros, UInt32 RosVersion, UInt32 RosSize, IntPtr Rod, UInt32 RodVersion, UInt32 RodSize); + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 SetPerTcpConnectionEStats(ref MIB_TCPROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, UInt32 Offset);*/ + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_TCPROW_OWNER_MODULE : I_SOCKET_ROW + { + internal UInt32 dwState; + internal UInt32 dwLocalAddr; + internal UInt32 dwLocalPort; + internal UInt32 dwRemoteAddr; + internal UInt32 dwRemotePort; + internal UInt32 dwOwningPid; + internal Int64 liCreateTimestamp; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal ulong[] OwningModuleInfo; + + public int ProcessId { get { return (int)dwOwningPid; } } + public ModuleInfo Module + { + get + { + ModuleInfo Info = null; + + uint buffSize = 0; + GetOwnerModuleFromTcpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); + IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); + + if (GetOwnerModuleFromTcpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR && buffSize != 0) + Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); + + if (buffer != IntPtr.Zero) + Marshal.FreeHGlobal(buffer); + + return Info; + } + } + + public UInt32 ProtocolType { get { return (UInt32)AF_PROT.TCP | (UInt32)AF_INET.IP4 << 8; } } + public IPAddress RemoteAddress { get { return new IPAddress((UInt32)dwRemoteAddr); } } + public UInt16 RemotePort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwRemotePort); } } + public IPAddress LocalAddress { get { return new IPAddress((UInt32)dwLocalAddr); } } + public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } + public MIB_TCP_STATE State { get { return (MIB_TCP_STATE)dwState; } } + public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } + } + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_TCPTABLE_OWNER_MODULE + { + public UInt32 dwNumEntries; + public MIB_TCPROW_OWNER_MODULE FirstEntry; + } + + public static IntPtr GetTcpSockets(ref List Sockets) + { + uint tcp4Size = 0; + IntPtr tcp4Table = IntPtr.Zero; + IPHelper.GetExtendedTcpTable(IntPtr.Zero, ref tcp4Size, false, IPHelper.AF_INET.IP4, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0); + tcp4Table = Marshal.AllocHGlobal((int)tcp4Size); + + if (IPHelper.GetExtendedTcpTable(tcp4Table, ref tcp4Size, false, IPHelper.AF_INET.IP4, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0) == IPHelper.NO_ERROR) + { + IPHelper.MIB_TCPTABLE_OWNER_MODULE table = ((IPHelper.MIB_TCPTABLE_OWNER_MODULE)Marshal.PtrToStructure(tcp4Table, typeof(IPHelper.MIB_TCPTABLE_OWNER_MODULE))); + IntPtr rowPtr = (IntPtr)((long)tcp4Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_TCPTABLE_OWNER_MODULE), "FirstEntry")); + + for (uint i = 0; i < table.dwNumEntries; i++) + { + IPHelper.MIB_TCPROW_OWNER_MODULE mibRow = (IPHelper.MIB_TCPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_TCPROW_OWNER_MODULE)); + rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); + + Sockets.Add(mibRow); + } + } + + return tcp4Table; + } + + ////////////////////////////////////////////////////// + // TCPv6 + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetOwnerModuleFromTcp6Entry(ref MIB_TCP6ROW_OWNER_MODULE pTcpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); + + /*[DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetPerTcp6ConnectionEStats(ref MIB_TCP6ROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, IntPtr Ros, UInt32 RosVersion, UInt32 RosSize, IntPtr Rod, UInt32 RodVersion, UInt32 RodSize); + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 SetPerTcp6ConnectionEStats(ref MIB_TCP6ROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, UInt32 Offset);*/ + + //[DllImport("ntdll.dll", SetLastError = true)] + //public static extern void RtlIpv6AddressToString(byte[] Addr, out StringBuilder res); + + + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] + public struct MIB_TCP6ROW_OWNER_MODULE : I_SOCKET_ROW + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal byte[] ucLocalAddress; + internal UInt32 dwLocalScopeId; + internal UInt32 dwLocalPort; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal byte[] ucRemoteAddress; + internal UInt32 dwRemoteScopeId; + internal UInt32 dwRemotePort; + internal UInt32 dwState; + internal UInt32 dwOwningPid; + internal Int64 liCreateTimestamp; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal ulong[] OwningModuleInfo; + + public int ProcessId { get { return (int)dwOwningPid; } } + public ModuleInfo Module + { + get + { + ModuleInfo Info = null; + + uint buffSize = 0; + GetOwnerModuleFromTcp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); + IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); + + if (GetOwnerModuleFromTcp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR && buffSize > 0) + Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); + + if (buffer != IntPtr.Zero) + Marshal.FreeHGlobal(buffer); + + return Info; + } + } + + public UInt32 ProtocolType { get { return (UInt32)AF_PROT.TCP | (UInt32)AF_INET.IP6 << 8; } } + public IPAddress RemoteAddress { get { return new IPAddress(ucLocalAddress); } } + public UInt16 RemotePort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwRemotePort); } } + public IPAddress LocalAddress { get { return new IPAddress(ucLocalAddress); } } + public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } + public MIB_TCP_STATE State { get { return (MIB_TCP_STATE)dwState; } } + public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } + } + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_TCP6TABLE_OWNER_MODULE + { + public UInt32 dwNumEntries; + public MIB_TCP6ROW_OWNER_MODULE FirstEntry; + } + + public static IntPtr GetTcp6Sockets(ref List Sockets) + { + uint tcp6Size = 0; + IntPtr tcp6Table = IntPtr.Zero; + IPHelper.GetExtendedTcpTable(IntPtr.Zero, ref tcp6Size, false, IPHelper.AF_INET.IP6, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0); + tcp6Table = Marshal.AllocHGlobal((int)tcp6Size); + + if (IPHelper.GetExtendedTcpTable(tcp6Table, ref tcp6Size, false, IPHelper.AF_INET.IP6, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0) == IPHelper.NO_ERROR) + { + IPHelper.MIB_TCP6TABLE_OWNER_MODULE table = ((IPHelper.MIB_TCP6TABLE_OWNER_MODULE)Marshal.PtrToStructure(tcp6Table, typeof(IPHelper.MIB_TCP6TABLE_OWNER_MODULE))); + IntPtr rowPtr = (IntPtr)((long)tcp6Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_TCP6TABLE_OWNER_MODULE), "FirstEntry")); + + for (uint i = 0; i < table.dwNumEntries; i++) + { + IPHelper.MIB_TCP6ROW_OWNER_MODULE mibRow = (IPHelper.MIB_TCP6ROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_TCP6ROW_OWNER_MODULE)); + rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); + + Sockets.Add(mibRow); + } + } + + return tcp6Table; + } + + ////////////////////////////////////////////////////// + // UDP + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetExtendedUdpTable(IntPtr pUdpTable, ref UInt32 dwOutBufLen, bool order, AF_INET ipVersion, UDP_TABLE_CLASS tblClass, UInt32 reserved); + + public enum UDP_TABLE_CLASS + { + UDP_TABLE_BASIC, + UDP_TABLE_OWNER_PID, + UDP_TABLE_OWNER_MODULE + } + + ////////////////////////////////////////////////////// + // UDPv4 + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetOwnerModuleFromUdpEntry(ref MIB_UDPROW_OWNER_MODULE pUdpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); + + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_UDPROW_OWNER_MODULE : I_SOCKET_ROW + { + internal UInt32 dwLocalAddr; + internal UInt32 dwLocalPort; + internal UInt32 dwOwningPid; + internal Int64 liCreateTimestamp; + internal UInt32 dwFlags; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal ulong[] OwningModuleInfo; + + public int ProcessId { get { return (int)dwOwningPid; } } + public ModuleInfo Module + { + get + { + ModuleInfo Info = null; + + uint buffSize = 0; + GetOwnerModuleFromUdpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); + IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); + + if (GetOwnerModuleFromUdpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR) + Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); + + if (buffer != IntPtr.Zero) + Marshal.FreeHGlobal(buffer); + + return Info; + } + } + + public UInt32 ProtocolType { get { return (UInt32)AF_PROT.UDP | (UInt32)AF_INET.IP4 << 8; } } + public IPAddress RemoteAddress { get { return null; } } + public UInt16 RemotePort { get { return 0; } } + public IPAddress LocalAddress { get { return new IPAddress((UInt32)dwLocalAddr); } } + public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } + public MIB_TCP_STATE State { get { return MIB_TCP_STATE.UNDEFINED; } } + public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } + } + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_UDPTABLE_OWNER_MODULE + { + public UInt32 dwNumEntries; + public MIB_UDPROW_OWNER_MODULE FirstEntry; + } + + public static IntPtr GetUdpSockets(ref List Sockets) + { + uint udp4Size = 0; + IntPtr udp4Table = IntPtr.Zero; + IPHelper.GetExtendedUdpTable(IntPtr.Zero, ref udp4Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0); + udp4Table = Marshal.AllocHGlobal((int)udp4Size); + + if (IPHelper.GetExtendedUdpTable(udp4Table, ref udp4Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0) == IPHelper.NO_ERROR) + { + IPHelper.MIB_UDPTABLE_OWNER_MODULE table = ((IPHelper.MIB_UDPTABLE_OWNER_MODULE)Marshal.PtrToStructure(udp4Table, typeof(IPHelper.MIB_UDPTABLE_OWNER_MODULE))); + IntPtr rowPtr = (IntPtr)((long)udp4Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_UDPTABLE_OWNER_MODULE), "FirstEntry")); + + for (uint i = 0; i < table.dwNumEntries; i++) + { + IPHelper.MIB_UDPROW_OWNER_MODULE mibRow = (IPHelper.MIB_UDPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_UDPROW_OWNER_MODULE)); + rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); + + Sockets.Add(mibRow); + } + } + + return udp4Table; + } + + ////////////////////////////////////////////////////// + // UDPv6 + // + + [DllImport("iphlpapi.dll", SetLastError = true)] + public static extern UInt32 GetOwnerModuleFromUdp6Entry(ref MIB_UDP6ROW_OWNER_MODULE pUdpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); + + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_UDP6ROW_OWNER_MODULE : I_SOCKET_ROW + { + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal byte[] ucLocalAddress; + internal UInt32 dwLocalScopeId; + internal UInt32 dwLocalPort; + internal UInt32 dwOwningPid; + internal Int64 liCreateTimestamp; + internal UInt32 dwFlags; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + internal ulong[] OwningModuleInfo; + + public int ProcessId { get { return (int)dwOwningPid; } } + public ModuleInfo Module + { + get + { + ModuleInfo Info = null; + + uint buffSize = 0; + GetOwnerModuleFromUdp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); + IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); + + if (GetOwnerModuleFromUdp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR) + Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); + + if (buffer != IntPtr.Zero) + Marshal.FreeHGlobal(buffer); + + return Info; + } + } + + public UInt32 ProtocolType { get { return (UInt32)AF_PROT.UDP | (UInt32)AF_INET.IP6 << 8; } } + public IPAddress RemoteAddress { get { return null; } } + public UInt16 RemotePort { get { return 0; } } + public IPAddress LocalAddress { get { return new IPAddress(ucLocalAddress); } } + public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } + public MIB_TCP_STATE State { get { return MIB_TCP_STATE.UNDEFINED; } } + public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } + } + + [StructLayout(LayoutKind.Sequential)] + public struct MIB_UDP6TABLE_OWNER_MODULE + { + public UInt32 dwNumEntries; + public MIB_UDP6ROW_OWNER_MODULE FirstEntry; + } + + public static IntPtr GetUdp6Sockets(ref List Sockets) + { + uint udp6Size = 0; + IntPtr udp6Table = IntPtr.Zero; + IPHelper.GetExtendedUdpTable(IntPtr.Zero, ref udp6Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0); + udp6Table = Marshal.AllocHGlobal((int)udp6Size); + + if (IPHelper.GetExtendedUdpTable(udp6Table, ref udp6Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0) == IPHelper.NO_ERROR) + { + IPHelper.MIB_UDP6TABLE_OWNER_MODULE table = ((IPHelper.MIB_UDP6TABLE_OWNER_MODULE)Marshal.PtrToStructure(udp6Table, typeof(IPHelper.MIB_UDP6TABLE_OWNER_MODULE))); + IntPtr rowPtr = (IntPtr)((long)udp6Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_UDP6TABLE_OWNER_MODULE), "FirstEntry")); + + for (uint i = 0; i < table.dwNumEntries; i++) + { + IPHelper.MIB_UDPROW_OWNER_MODULE mibRow = (IPHelper.MIB_UDPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_UDPROW_OWNER_MODULE)); + rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); + + Sockets.Add(mibRow); + } + } + + return udp6Table; + } + } +} \ No newline at end of file diff --git a/PrivateWin10/API/MiscStats.cs b/MiscHelpers/API/MiscStats.cs similarity index 95% rename from PrivateWin10/API/MiscStats.cs rename to MiscHelpers/API/MiscStats.cs index 6e8c852..d03acd2 100644 --- a/PrivateWin10/API/MiscStats.cs +++ b/MiscHelpers/API/MiscStats.cs @@ -4,7 +4,7 @@ using System.Text; using System.Threading.Tasks; -namespace PrivateWin10 +namespace MiscHelpers { [Serializable()] diff --git a/MiscHelpers/API/NetFunc.cs b/MiscHelpers/API/NetFunc.cs new file mode 100644 index 0000000..4a44543 --- /dev/null +++ b/MiscHelpers/API/NetFunc.cs @@ -0,0 +1,443 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; +using System.Numerics; + + +namespace MiscHelpers +{ + public class NetFunc + { + public static BigInteger IpToInt(IPAddress ip) + { + List ipFormat = ip.GetAddressBytes().ToList(); + ipFormat.Reverse(); + ipFormat.Add(0); + return new BigInteger(ipFormat.ToArray()); + } + + public static BigInteger IpStrToInt(string strIP, out int type) + { + IPAddress ip; + if (!IPAddress.TryParse(strIP, out ip)) + { + type = 0; + return BigInteger.Zero; + } + byte[] bytes = ip.GetAddressBytes(); + if (bytes.Length == 4) + type = 4; + else + type = 6; + List ipFormat = bytes.ToList(); + ipFormat.Reverse(); + ipFormat.Add(0); + return new BigInteger(ipFormat.ToArray()); + } + + public static BigInteger MaxIPofType(int type) + { + IPAddress ip = (type == 4) ? IPAddress.Parse("255.255.255.255") : IPAddress.Parse("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); + List ipFormat = ip.GetAddressBytes().ToList(); + ipFormat.Reverse(); + ipFormat.Add(0); + return new BigInteger(ipFormat.ToArray()); + } + + public enum KnownProtocols + { + Min = 0, + Max = 142, + Any = 256 + } + + /*public static string Protocol2Str(int Protocol, string unkStr = "Unknow") + { + switch (Protocol) + { + case 0: return "HOPOPT (IPv6 Hop-by-Hop Option)"; + case 1: return "ICMP (Internet Control Message Protocol)"; + case 2: return "IGMP (Internet Group Management Protocol)"; + case 3: return "GGP (Gateway-to-Gateway)"; + case 4: return "IP (IP in IP (encapsulation))"; + case 5: return "Stream"; + case 6: return "TCP (Transmission Control Protocol)"; + case 7: return "CBT (Core Based Trees)"; + case 8: return "EGP (Exterior Gateway Protocol)"; + case 9: return "IGP (any private interior gateway)"; + case 10: return "BBN-RCC-MON (BBN RCC Monitoring)"; + case 11: return "NVP-II (Network Voice Protocol)"; + case 12: return "PUP"; + case 13: return "ARGUS"; + case 14: return "EMCON"; + case 15: return "XNET (Cross Net Debugger)"; + case 16: return "CHAOS"; + case 17: return "UDP (User Datagram Protocol)"; + case 18: return "Multiplexing"; + case 19: return "DCN-MEAS (DCN Measurement Subsystems)"; + case 20: return "HMP (Host Monitoring)"; + case 21: return "PRM (Packet Radio Measurement)"; + case 22: return "XNS-IDP (XEROX NS IDP)"; + case 23: return "TRUNK-1"; + case 24: return "TRUNK-2"; + case 25: return "LEAF-1"; + case 26: return "LEAF-2"; + case 27: return "RDP (Reliable Data Protocol)"; + case 28: return "IRTP (Internet Reliable Transaction Protocol)"; + case 29: return "ISO-TP4 (ISO Transport Protocol Class 4)"; + case 30: return "NETBLT (Bulk Data Transfer Protocol)"; + case 31: return "MFE-NSP (MFE Network Services Protocol)"; + case 32: return "MERIT-INP (MERIT Internodal Protocol)"; + case 33: return "DCCP (Datagram Congestion Control Protocol)"; + case 34: return "3PC (Third Party Connect Protocol)"; + case 35: return "IDPR (Inter-Domain Policy Routing Protocol)"; + case 36: return "XTP"; + case 37: return "DDP (Datagram Delivery Protocol)"; + case 38: return "IDPR-CMTP (IDPR Control Message Transport Proto)"; + case 39: return "TP++ (TP++ Transport Protocol)"; + case 40: return "IL (IL Transport Protocol)"; + case 41: return "Verkapselung von IPv6- in IPv4-Pakete"; + case 42: return "SDRP (Source Demand Routing Protocol)"; + case 43: return "IPv6-Route (Routing Header for IPv6)"; + case 44: return "IPv6-Frag (Fragment Header for IPv6)"; + case 45: return "IDRP (Inter-Domain Routing Protocol)"; + case 46: return "RSVP (Reservation Protocol)"; + case 47: return "GRE (Generic Routing Encapsulation)"; + case 48: return "MHRP (Mobile Host Routing Protocol)"; + case 49: return "BNA"; + case 50: return "ESP (Encapsulating Security Payload)"; + case 51: return "AH (Authentication Header)"; + case 52: return "I-NLSP (Integrated Net Layer Security TUBA)"; + case 53: return "SWIPE (IP with Encryption)"; + case 54: return "NARP (NBMA Address Resolution Protocol)"; + case 55: return "MOBILE (IP Mobility)"; + case 56: return "TLSP (Transport Layer Security Protocol)"; + case 57: return "SKIP"; + case 58: return "IPv6-ICMP (ICMP for IPv6)"; + case 59: return "IPv6-NoNxt (Kein nächster Header für IPv6)"; + case 60: return "IPv6-Opts (Destination Options for IPv6)"; + case 61: return "Jedes Host-interne Protokoll"; + case 62: return "CFTP"; + case 63: return "Jedes lokale Netz"; + case 64: return "SAT-EXPAK (SATNET and Backroom EXPAK)"; + case 65: return "KRYPTOLAN"; + case 66: return "RVD (MIT Remote Virtual Disk Protocol)"; + case 67: return "IPPC (Internet Pluribus Packet Core)"; + case 68: return "Jedes verteilte Dateisystem"; + case 69: return "SAT-MON (SATNET Monitoring)"; + case 70: return "VISA"; + case 71: return "IPCV (Internet Packet Core Utility)"; + case 72: return "CPNX (Computer Protocol Network Executive)"; + case 73: return "CPHB (Computer Protocol Heart Beat)"; + case 74: return "WSN (Wang Span Network)"; + case 75: return "PVP (Packet Video Protocol)"; + case 76: return "BR-SAT-MON (Backroom SATNET Monitoring)"; + case 77: return "SUN-ND (SUN ND PROTOCOL-Temporary)"; + case 78: return "WB-MON (WIDEBAND Monitoring)"; + case 79: return "WB-EXPAK (WIDEBAND EXPAK)"; + case 80: return "ISO-IP (ISO Internet Protocol)"; + case 81: return "VMTP"; + case 82: return "SECURE-VMTP"; + case 83: return "VINES"; + case 84: return "TTP"; + case 85: return "NSFNET-IGP (NSFNET-IGP)"; + case 86: return "DGP (Dissimilar Gateway Protocol)"; + case 87: return "TCF"; + case 88: return "EIGRP"; + case 89: return "OSPF"; + case 90: return "Sprite-RPC (Sprite RPC Protocol)"; + case 91: return "LARP (Locus Address Resolution Protocol)"; + case 92: return "MTP (Multicast Transport Protocol)"; + case 93: return "AX.25 (AX.25 Frames)"; + case 94: return "IPIP (IP-within-IP Encapsulation Protocol)"; + case 95: return "MICP (Mobile Internetworking Control Pro.)"; + case 96: return "SCC-SP (Semaphore Communications Sec. Pro.)"; + case 97: return "ETHERIP (Ethernet-within-IP Encapsulation)"; + case 98: return "ENCAP (Encapsulation Header)"; + case 99: return "Jeder private Verschlüsselungsentwurf"; + case 100: return "GMTP"; + case 101: return "IFMP (Ipsilon Flow Management Protocol)"; + case 102: return "PNNI (over IP)"; + case 103: return "PIM (Protocol Independent Multicast)"; + case 104: return "ARIS"; + case 105: return "SCPS"; + case 106: return "QNX"; + case 107: return "A/N (Active Networks)"; + case 108: return "IPComp (IP Payload Compression Protocol)"; + case 109: return "SNP (Sitara Networks Protocol)"; + case 110: return "Compaq-Peer (Compaq Peer Protocol)"; + case 111: return "IPX-in-IP (IPX in IP)"; + case 112: return "VRRP (Virtual Router Redundancy Protocol)"; + case 113: return "PGM (PGM Reliable Transport Protocol)"; + case 114: return "any 0-hop protocol"; + case 115: return "L2TP (Layer Two Tunneling Protocol)"; + case 116: return "DDX (D-II Data Exchange (DDX))"; + case 117: return "IATP (Interactive Agent Transfer Protocol)"; + case 118: return "STP (Schedule Transfer Protocol)"; + case 119: return "SRP (SpectraLink Radio Protocol)"; + case 120: return "UTI"; + case 121: return "SMP (Simple Message Protocol)"; + case 122: return "SM"; + case 123: return "PTP (Performance Transparency Protocol)"; + case 124: return "ISIS over IPv4"; + case 125: return "FIRE"; + case 126: return "CRTP (Combat Radio Transport Protocol)"; + case 127: return "CRUDP (Combat Radio User Datagram)"; + case 128: return "SSCOPMCE"; + case 129: return "IPLT"; + case 130: return "SPS (Secure Packet Shield)"; + case 131: return "PIPE (Private IP Encapsulation within IP)"; + case 132: return "SCTP (Stream Control Transmission Protocol)"; + case 133: return "FC (Fibre Channel)"; + case 134: return "RSVP-E2E-IGNORE"; + case 135: return "Mobility Header"; + case 136: return "UDPLite"; + case 137: return "MPLS-in-IP"; + case 138: return "manet (MANET Protocols)"; + case 139: return "HIP (Host Identity Protocol)"; + case 140: return "Shim6 (Shim6 Protocol)"; + case 141: return "WESP (Wrapped Encapsulating Security Payload)"; + case 142: return "ROHC (Robust Header Compression)"; + case 256: return "Unspecifyed"; + default: return unkStr; + } + }*/ + + public static string Protocol2Str(UInt32 Protocol) + { + switch (Protocol) + { + case 0: return "HOPOPT"; + case 1: return "ICMP"; + case 2: return "IGMP"; + case 3: return "GGP"; + case 4: return "IP"; + //case 5: return "Stream"; + case 6: return "TCP"; + case 7: return "CBT"; + case 8: return "EGP"; + case 9: return "IGP"; + case 10: return "BBN-RCC-MON"; + case 11: return "NVP-II"; + case 12: return "PUP"; + case 13: return "ARGUS"; + case 14: return "EMCON"; + case 15: return "XNET"; + case 16: return "CHAOS"; + case 17: return "UDP"; + //case 18: return "Multiplexing"; + case 19: return "DCN-MEAS"; + case 20: return "HMP"; + case 21: return "PRM"; + case 22: return "XNS-IDP"; + case 23: return "TRUNK-1"; + case 24: return "TRUNK-2"; + case 25: return "LEAF-1"; + case 26: return "LEAF-2"; + case 27: return "RDP"; + case 28: return "IRTP"; + case 29: return "ISO-TP4"; + case 30: return "NETBLT"; + case 31: return "MFE-NSP"; + case 32: return "MERIT-INP"; + case 33: return "DCCP"; + case 34: return "3PC"; + case 35: return "IDPR"; + case 36: return "XTP"; + case 37: return "DDP"; + case 38: return "IDPR-CMTP"; + case 39: return "TP++"; + case 40: return "IL"; + ///case 41: return "Verkapselung von IPv6- in IPv4-Pakete"; + case 42: return "SDRP"; + case 43: return "IPv6-Route"; + case 44: return "IPv6-Frag"; + case 45: return "IDRP"; + case 46: return "RSVP"; + case 47: return "GRE"; + case 48: return "MHRP"; + case 49: return "BNA"; + case 50: return "ESP"; + case 51: return "AH"; + case 52: return "I-NLSP"; + case 53: return "SWIPE"; + case 54: return "NARP"; + case 55: return "MOBILE"; + case 56: return "TLSP"; + case 57: return "SKIP"; + case 58: return "IPv6-ICMP"; + case 59: return "IPv6-NoNxt"; + case 60: return "IPv6-Opts"; + //case 61: return "Jedes Host-interne Protokoll"; + case 62: return "CFTP"; + //case 63: return "Jedes lokale Netz"; + case 64: return "SAT-EXPAK"; + case 65: return "KRYPTOLAN"; + case 66: return "RVD"; + case 67: return "IPPC"; + //case 68: return "Jedes verteilte Dateisystem"; + case 69: return "SAT-MON"; + case 70: return "VISA"; + case 71: return "IPCV"; + case 72: return "CPNX"; + case 73: return "CPHB"; + case 74: return "WSN"; + case 75: return "PVP"; + case 76: return "BR-SAT-MON"; + case 77: return "SUN-ND"; + case 78: return "WB-MON"; + case 79: return "WB-EXPAK"; + case 80: return "ISO-IP"; + case 81: return "VMTP"; + case 82: return "SECURE-VMTP"; + case 83: return "VINES"; + case 84: return "TTP"; + case 85: return "NSFNET-IGP"; + case 86: return "DGP"; + case 87: return "TCF"; + case 88: return "EIGRP"; + case 89: return "OSPF"; + case 90: return "Sprite-RPC"; + case 91: return "LARP"; + case 92: return "MTP"; + case 93: return "AX.25"; + case 94: return "IPIP"; + case 95: return "MICP"; + case 96: return "SCC-SP"; + case 97: return "ETHERIP"; + case 98: return "ENCAP"; + //case 99: return "Jeder private Verschlüsselungsentwurf"; + case 100: return "GMTP"; + case 101: return "IFMP"; + case 102: return "PNNI"; + case 103: return "PIM"; + case 104: return "ARIS"; + case 105: return "SCPS"; + case 106: return "QNX"; + case 107: return "A/N"; + case 108: return "IPComp"; + case 109: return "SNP"; + case 110: return "Compaq-Peer"; + case 111: return "IPX-in-IP"; + case 112: return "VRRP"; + case 113: return "PGM"; + case 114: return "any 0-hop protocol"; + case 115: return "L2TP"; + case 116: return "DDX"; + case 117: return "IATP"; + case 118: return "STP"; + case 119: return "SRP"; + case 120: return "UTI"; + case 121: return "SMP"; + case 122: return "SM"; + case 123: return "PTP"; + case 124: return "ISIS"; + case 125: return "FIRE"; + case 126: return "CRTP"; + case 127: return "CRUDP"; + case 128: return "SSCOPMCE"; + case 129: return "IPLT"; + case 130: return "SPS"; + case 131: return "PIPE"; + case 132: return "SCTP"; + case 133: return "FC"; + case 134: return "RSVP-E2E-IGNORE"; + case 135: return "Mobility Header"; + case 136: return "UDPLite"; + case 137: return "MPLS-in-IP"; + case 138: return "manet"; + case 139: return "HIP"; + case 140: return "Shim6"; + case 141: return "WESP"; + case 142: return "ROHC"; + case 256: return "???"; + default: return "#" + Protocol.ToString(); + } + } + + public static readonly Dictionary KnownIcmp4Types = new Dictionary() { + {0,"Echo Reply"}, + {3,"Destination Unreachable"}, + {4,"Source Quench"}, + {5,"Redirect"}, + {8,"Echo Request"}, + {9,"Router Advertisement"}, + {10,"Router Solicitation"}, + {11,"Time Exceeded"}, + {12,"Parameter Problem"}, + {13,"Timestamp (erleichtert die Zeitsynchronisation)"}, + {14,"Timestamp Reply"}, + {15,"Information Request"}, + {16,"Information Reply"}, + {17,"Address Mask Request"}, + {18,"Address Mask Reply"}, + {19,"Reserved (for Security)"}, + {30,"Traceroute"}, + {31,"Datagram Conversion Error"}, + {32,"Mobile Host Redirect"}, + {33,"Ursprünglich IPv6 Where-Are-You (ersetzt durch ICMPv6)"}, + {34,"Ursprünglich IPv6 I-Am-Here (ersetzt durch ICMPv6)"}, + {35,"Mobile Registration Request"}, + {36,"Mobile Registration Reply"}, + {37,"Domain Name Request"}, + {38,"Domain Name Reply"}, + {39,"SKIP"}, + {40,"Photuris"}, + {41,"ICMP messages utilized by experimental mobility protocols such as Seamoby"}, + }; + + public static readonly Dictionary KnownIcmp6Types = new Dictionary() { + {1, "Destination Unreachable"}, + {2, "Packet Too Big"}, + {3, "Time Exceeded"}, + {4, "Parameter Problem"}, + {128, "Echo Request"}, + {129, "Echo Reply"}, + {130, "Multicast Listener Query"}, + {131, "Version 1 Multicast Listener Report"}, + {132, "Multicast Listener Done"}, + {133, "Router Solicitation"}, + {134, "Router Advertisement"}, + {135, "Neighbor Solicitation"}, + {136, "Neighbor Advertisement"}, + {137, "Redirect"}, + {138, "Router Renumbering"}, + {139, "ICMP Node Information Query"}, + {140, "ICMP Node Information Response"}, + {141, "Inverse Neighbor Discovery Solicitation Message"}, + {142, "Inverse Neighbor Discovery Advertisement Message"}, + {143, "Version 2 Multicast Listener Report"}, + {144, "Home Agent Address Discovery Request Message"}, + {145, "Home Agent Address Discovery Reply Message"}, + {146, "Mobile Prefix Solicitation"}, + {147, "Mobile Prefix Advertisement"}, + {148, "Certification Path Solicitation Message"}, + {149, "Certification Path Advertisement Message"}, + {150, "ICMP messages utilized by experimental mobility protocols such as Seamoby"}, + {151, "Multicast Router Advertisement"}, + {152, "Multicast Router Solicitation"}, + {153, "Multicast Router Termination"}, + {155, "RPL Control Message"}, + }; + + public static bool IsLocalHost(IPAddress addr) + { + return (addr.Equals(IPAddress.Loopback) || addr.Equals(IPAddress.IPv6Loopback)); + } + + public static bool IsMultiCast(IPAddress addr) + { + // ipv4 multicast: 224.0.0.0 to 239.255.255.255 + // ipv6 multicast: ff00:: to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff + if (addr.IsIPv6Multicast) + return true; + byte[] addressBytes = addr.GetAddressBytes(); + if (addressBytes.Length == 4) + return addressBytes[0] >= 224 && addressBytes[0] <= 239; + return false; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/ProcFunc.cs b/MiscHelpers/API/ProcFunc.cs new file mode 100644 index 0000000..56dcbda --- /dev/null +++ b/MiscHelpers/API/ProcFunc.cs @@ -0,0 +1,166 @@ +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + public static class ProcFunc + { + public static int CurID = System.Diagnostics.Process.GetCurrentProcess().Id; + + public const int SystemPID = 4; // on windows system is has always PID 4 + + [DllImport("kernel32.dll")] + public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId); + + [DllImport("psapi.dll")] + public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize); + + [DllImport("advapi32.dll", SetLastError = true)] + public static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle); + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool CloseHandle(IntPtr hObject); + + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static string GetProcessFileNameByPID(int pid) + { + if (pid == ProcFunc.SystemPID) + return MiscFunc.NtOsKrnlPath; + + // todo add cache, may be? + var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, pid); + if (processHandle == IntPtr.Zero) + return null; + + string result = null; + + const int lengthSb = 4096 + 1; + var sb = new StringBuilder(lengthSb); + if (GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, lengthSb) > 0) + { + result = sb.ToString(); + } + + CloseHandle(processHandle); + + return result; + } + + + [DllImport("kernel32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool GetProcessTimes(IntPtr hProcess, out long lpCreationTime, out long lpExitTime, out long lpKernelTime, out long lpUserTime); + + public static long GetProcessCreationTime(int pid) + { + var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, pid); + if (processHandle == IntPtr.Zero) + return 0; + + long RawCreationTime; + long RawExitTime; + long RawKernelTime; + long RawUserTime; + GetProcessTimes(processHandle, out RawCreationTime, out RawExitTime, out RawKernelTime, out RawUserTime); + + CloseHandle(processHandle); + + return RawCreationTime; + } + + [DllImport("advapi32", CharSet = CharSet.Unicode)] + public static extern bool ConvertStringSidToSid([In, MarshalAs(UnmanagedType.LPWStr)] string pStringSid, ref IntPtr pSID); + + [DllImport("advapi32", CharSet = CharSet.Unicode)] + public static extern bool ConvertSidToStringSid(IntPtr pSID, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string pStringSid); + + [StructLayout(LayoutKind.Sequential)] + public struct TOKEN_APPCONTAINER_INFORMATION + { + public IntPtr Sid; + } + + [DllImport("ntdll.dll")] + public static extern uint NtQueryInformationToken([In] IntPtr TokenHandle, [In] uint TokenInformationClass, [In] IntPtr TokenInformation, [In] int TokenInformationLength, [Out] [Optional] out int ReturnLength); + + static public string GetAppPackageSidByPID(int PID) + { + //var process = System.Diagnostics.Process.GetProcessById(PID); // throws error if pid is not found + var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, PID); + if (processHandle == IntPtr.Zero) + return null; + + string strSID = null; + + IntPtr tokenHandle = IntPtr.Zero; + if (OpenProcessToken(processHandle, 8, out tokenHandle)) + { + int retLen; + NtQueryInformationToken(tokenHandle, 31 /*TokenAppContainerSid*/, IntPtr.Zero, 0, out retLen); + + IntPtr buffer = Marshal.AllocHGlobal((int)retLen); + ulong status = NtQueryInformationToken(tokenHandle, 31 /*TokenAppContainerSid*/, buffer, retLen, out retLen); + if (status >= 0) + { + var appContainerInfo = (TOKEN_APPCONTAINER_INFORMATION)Marshal.PtrToStructure(buffer, typeof(TOKEN_APPCONTAINER_INFORMATION)); + + ConvertSidToStringSid(appContainerInfo.Sid, ref strSID); + } + Marshal.FreeHGlobal(buffer); + + CloseHandle(tokenHandle); + } + + CloseHandle(processHandle); + + return strSID; + } + + /* + [DllImport("Kernel32.dll")] + private static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize); + + // Note: we can not access a module of a otehr bitness as we are so we need a native solution + public static string GetMainModuleFileName(this Process process) + { + var fileNameBuilder = new StringBuilder(1024); + uint bufferLength = (uint)fileNameBuilder.Capacity + 1; + try + { + return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) ? fileNameBuilder.ToString() : null; + } + catch + { + return null; + } + } + */ + + static public string GetPathFromCmdLine(string commandLine) + { + if (commandLine[0] == '"') + { + int pos = commandLine.IndexOf('"', 1); + if (pos != -1) + return commandLine.Substring(1, pos - 1); + } + else + { + int pos = commandLine.IndexOf(' '); + if (pos != -1) + return commandLine.Substring(0, pos); + } + return commandLine; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/ProcessUtilities.cs b/MiscHelpers/API/ProcessUtilities.cs new file mode 100644 index 0000000..447eaac --- /dev/null +++ b/MiscHelpers/API/ProcessUtilities.cs @@ -0,0 +1,325 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + + // All offset values below have been tested on Windows 7 & 8 only + // but you can use WinDbg "dt ntdll!_PEB" command and search for ProcessParameters offset to find the truth, depending on the OS version + public static class ProcessUtilities + { + public static readonly bool Is64BitProcess = IntPtr.Size > 4; + public static readonly bool Is64BitOperatingSystem = Is64BitProcess || Is64BitChecker.InternalCheckIsWow64(); + + public static string GetCurrentDirectory(int processId) + { + try + { + return GetProcessParametersString(processId, PEB_OFFSET.CurrentDirectory); + } + catch + { + return null; + } + } + + public static string GetCurrentDirectory(this Process process) + { + if (process == null) + throw new ArgumentNullException("process"); + + return GetCurrentDirectory(process.Id); + } + + #region GetCommandLine + //public static string GetCommandLine(int processId) + //{ + // return null;// GetProcessParametersString(processId, Is64BitOperatingSystem ? 0x70 : 0x40); + //} + + //public static string GetCommandLine(this Process process) + //{ + // if (process == null) + // throw new ArgumentNullException("process"); + + // return GetCommandLine(process.Id); + //} + #endregion + + private static string GetProcessParametersString(int processId, PEB_OFFSET Offset) + { + IntPtr handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId); + if (handle == IntPtr.Zero) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + bool IsWow64Process = Is64BitChecker.InternalCheckIsWow64(); + bool IsTargetWow64Process = Is64BitChecker.GetProcessIsWow64(handle); + bool IsTarget64BitProcess = Is64BitOperatingSystem && !IsTargetWow64Process; + + long offset = 0; + long processParametersOffset = IsTarget64BitProcess ? 0x20 : 0x10; + switch (Offset) + { + case PEB_OFFSET.CurrentDirectory: + offset = IsTarget64BitProcess ? 0x38 : 0x24; + break; + case PEB_OFFSET.CommandLine: + default: + return null; + } + + try + { + long pebAddress = 0; + if (IsTargetWow64Process) // OS : 64Bit, Cur : 32 or 64, Tar: 32bit + { + IntPtr peb32 = new IntPtr(); + + int hr = NtQueryInformationProcess(handle, (int)PROCESSINFOCLASS.ProcessWow64Information, ref peb32, IntPtr.Size, IntPtr.Zero); + if (hr != 0) throw new Win32Exception(hr); + pebAddress = peb32.ToInt64(); + + IntPtr pp = new IntPtr(); + if (!ReadProcessMemory(handle, new IntPtr(pebAddress + processParametersOffset), ref pp, new IntPtr(Marshal.SizeOf(pp)), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + UNICODE_STRING_32 us = new UNICODE_STRING_32(); + if (!ReadProcessMemory(handle, new IntPtr(pp.ToInt64() + offset), ref us, new IntPtr(Marshal.SizeOf(us)), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + if ((us.Buffer == 0) || (us.Length == 0)) + return null; + + string s = new string('\0', us.Length / 2); + if (!ReadProcessMemory(handle, new IntPtr(us.Buffer), s, new IntPtr(us.Length), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + return s; + } + else if (IsWow64Process)//Os : 64Bit, Cur 32, Tar 64 + { + PROCESS_BASIC_INFORMATION_WOW64 pbi = new PROCESS_BASIC_INFORMATION_WOW64(); + int hr = NtWow64QueryInformationProcess64(handle, (int)PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, Marshal.SizeOf(pbi), IntPtr.Zero); + if (hr != 0) throw new Win32Exception(hr); + pebAddress = pbi.PebBaseAddress; + + long pp = 0; + hr = NtWow64ReadVirtualMemory64(handle, pebAddress + processParametersOffset, ref pp, Marshal.SizeOf(pp), IntPtr.Zero); + if (hr != 0) + throw new Win32Exception(hr); + + UNICODE_STRING_WOW64 us = new UNICODE_STRING_WOW64(); + hr = NtWow64ReadVirtualMemory64(handle, pp + offset, ref us, Marshal.SizeOf(us), IntPtr.Zero); + if (hr != 0) + throw new Win32Exception(hr); + + if ((us.Buffer == 0) || (us.Length == 0)) + return null; + + string s = new string('\0', us.Length / 2); + hr = NtWow64ReadVirtualMemory64(handle, us.Buffer, s, us.Length, IntPtr.Zero); + if (hr != 0) + throw new Win32Exception(hr); + + return s; + } + else// Os,Cur,Tar : 64 or 32 + { + PROCESS_BASIC_INFORMATION pbi = new PROCESS_BASIC_INFORMATION(); + int hr = NtQueryInformationProcess(handle, (int)PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, Marshal.SizeOf(pbi), IntPtr.Zero); + if (hr != 0) throw new Win32Exception(hr); + pebAddress = pbi.PebBaseAddress.ToInt64(); + + IntPtr pp = new IntPtr(); + if (!ReadProcessMemory(handle, new IntPtr(pebAddress + processParametersOffset), ref pp, new IntPtr(Marshal.SizeOf(pp)), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + UNICODE_STRING us = new UNICODE_STRING(); + if (!ReadProcessMemory(handle, new IntPtr((long)pp + offset), ref us, new IntPtr(Marshal.SizeOf(us)), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + if ((us.Buffer == IntPtr.Zero) || (us.Length == 0)) + return null; + + string s = new string('\0', us.Length / 2); + if (!ReadProcessMemory(handle, us.Buffer, s, new IntPtr(us.Length), IntPtr.Zero)) + throw new Win32Exception(Marshal.GetLastWin32Error()); + + return s; + } + } + finally + { + CloseHandle(handle); + } + } + + private const int PROCESS_QUERY_INFORMATION = 0x400; + private const int PROCESS_VM_READ = 0x10; + + [StructLayout(LayoutKind.Sequential)] + private struct PROCESS_BASIC_INFORMATION + { + public IntPtr Reserved1; + public IntPtr PebBaseAddress; + public IntPtr Reserved2_0; + public IntPtr Reserved2_1; + public IntPtr UniqueProcessId; + public IntPtr Reserved3; + } + + [StructLayout(LayoutKind.Sequential)] + private struct UNICODE_STRING + { + public short Length; + public short MaximumLength; + public IntPtr Buffer; + } + + // for 32-bit process in a 64-bit OS only + [StructLayout(LayoutKind.Sequential)] + private struct PROCESS_BASIC_INFORMATION_WOW64 + { + public long Reserved1; + public long PebBaseAddress; + public long Reserved2_0; + public long Reserved2_1; + public long UniqueProcessId; + public long Reserved3; + } + + // for 32-bit process + [StructLayout(LayoutKind.Sequential)] + private struct UNICODE_STRING_WOW64 + { + public short Length; + public short MaximumLength; + public long Buffer; + } + + [StructLayout(LayoutKind.Sequential)] + private struct UNICODE_STRING_32 + { + public short Length; + public short MaximumLength; + public int Buffer; + } + + [DllImport("ntdll.dll")] + private static extern int NtQueryInformationProcess(IntPtr ProcessHandle, int ProcessInformationClass, ref PROCESS_BASIC_INFORMATION ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); + + //ProcessWow64Information, // q: ULONG_PTR + [DllImport("ntdll.dll")] + private static extern int NtQueryInformationProcess(IntPtr ProcessHandle, int ProcessInformationClass, ref IntPtr ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref IntPtr lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING_32 lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); + + //[DllImport("kernel32.dll", SetLastError = true)] + //private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING_WOW64 lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("kernel32.dll", SetLastError = true)] + private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); + + [DllImport("kernel32.dll")] + private static extern bool CloseHandle(IntPtr hObject); + + // for 32-bit process in a 64-bit OS only + [DllImport("ntdll.dll")] + private static extern int NtWow64QueryInformationProcess64(IntPtr ProcessHandle, int ProcessInformationClass, ref PROCESS_BASIC_INFORMATION_WOW64 ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); + + [DllImport("ntdll.dll")] + private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, ref long lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("ntdll.dll")] + private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, ref UNICODE_STRING_WOW64 lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); + + [DllImport("ntdll.dll")] + private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); + + + + // ref: http://www.microsoft.com/whdc/system/Sysinternals/MoreThan64proc.mspx + public enum PROCESSINFOCLASS : int + { + ProcessBasicInformation = 0, // 0, q: PROCESS_BASIC_INFORMATION, PROCESS_EXTENDED_BASIC_INFORMATION + ProcessWow64Information = 26, // q: ULONG_PTR + } + + [Flags] + public enum PEB_OFFSET + { + CurrentDirectory = 0, + DllPath, + ImagePathName, + CommandLine, + WindowTitle, + DesktopInfo, + ShellInfo, + RuntimeData, + TypeMask = 0xffff, + Wow64 = 0x10000, + }; + + public class Is64BitChecker + { + [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process); + + public static bool GetProcessIsWow64(IntPtr hProcess) + { + if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6) + { + bool retVal; + if (!IsWow64Process(hProcess, out retVal)) + { + return false; + } + return retVal; + } + else + { + return false; + } + } + + public static bool InternalCheckIsWow64() + { + if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6) + { + using (Process p = Process.GetCurrentProcess()) + { + bool retVal; + if (!IsWow64Process(p.Handle, out retVal)) + { + return false; + } + return retVal; + } + } + else + { + return false; + } + } + } + + } + +} \ No newline at end of file diff --git a/MiscHelpers/API/RegistryMonitor.cs b/MiscHelpers/API/RegistryMonitor.cs new file mode 100644 index 0000000..2eac5ae --- /dev/null +++ b/MiscHelpers/API/RegistryMonitor.cs @@ -0,0 +1,396 @@ +using System; +using System.ComponentModel; +using System.IO; +using System.Threading; +using System.Runtime.InteropServices; +using Microsoft.Win32; + +namespace MiscHelpers +{ + + /// + /// RegistryMonitor allows you to monitor specific registry key. + /// + /// + /// If a monitored registry key changes, an event is fired. You can subscribe to these + /// events by adding a delegate to . + /// The Windows API provides a function + /// + /// RegNotifyChangeKeyValue, which is not covered by the + /// class. imports + /// that function and encapsulates it in a convenient manner. + /// + /// + /// + /// This sample shows how to monitor HKEY_CURRENT_USER\Environment for changes: + /// + /// public class MonitorSample + /// { + /// static void Main() + /// { + /// RegistryMonitor monitor = new RegistryMonitor(RegistryHive.CurrentUser, "Environment"); + /// monitor.RegChanged += new EventHandler(OnRegChanged); + /// monitor.Start(); + /// + /// while(true); + /// + /// monitor.Stop(); + /// } + /// + /// private void OnRegChanged(object sender, EventArgs e) + /// { + /// Console.WriteLine("registry key has changed"); + /// } + /// } + /// + /// + public class RegistryMonitor : IDisposable + { + #region P/Invoke + + [DllImport("advapi32.dll", SetLastError = true)] + private static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int samDesired, + out IntPtr phkResult); + + [DllImport("advapi32.dll", SetLastError = true)] + private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtree, + RegChangeNotifyFilters dwNotifyFilter, IntPtr hEvent, + bool fAsynchronous); + + [DllImport("advapi32.dll", SetLastError = true)] + private static extern int RegCloseKey(IntPtr hKey); + + private const int KEY_QUERY_VALUE = 0x0001; + private const int KEY_NOTIFY = 0x0010; + private const int STANDARD_RIGHTS_READ = 0x00020000; + + private static readonly IntPtr HKEY_CLASSES_ROOT = new IntPtr(unchecked((int)0x80000000)); + private static readonly IntPtr HKEY_CURRENT_USER = new IntPtr(unchecked((int)0x80000001)); + private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int)0x80000002)); + private static readonly IntPtr HKEY_USERS = new IntPtr(unchecked((int)0x80000003)); + private static readonly IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(unchecked((int)0x80000004)); + private static readonly IntPtr HKEY_CURRENT_CONFIG = new IntPtr(unchecked((int)0x80000005)); + private static readonly IntPtr HKEY_DYN_DATA = new IntPtr(unchecked((int)0x80000006)); + + /// + /// Filter for notifications reported by . + /// + [Flags] + public enum RegChangeNotifyFilters + { + /// Notify the caller if a subkey is added or deleted. + Key = 1, + /// Notify the caller of changes to the attributes of the key, + /// such as the security descriptor information. + Attribute = 2, + /// Notify the caller of changes to a value of the key. This can + /// include adding or deleting a value, or changing an existing value. + Value = 4, + /// Notify the caller of changes to the security descriptor + /// of the key. + Security = 8, + } + + #endregion + + #region Event handling + + /// + /// Occurs when the specified registry key has changed. + /// + public event EventHandler RegChanged; + + /// + /// Raises the event. + /// + /// + ///

+ /// OnRegChanged is called when the specified registry key has changed. + ///

+ /// + /// When overriding in a derived class, be sure to call + /// the base class's method. + /// + ///
+ protected virtual void OnRegChanged() + { + EventHandler handler = RegChanged; + if (handler != null) + handler(this, null); + } + + /// + /// Occurs when the access to the registry fails. + /// + public event ErrorEventHandler Error; + + /// + /// Raises the event. + /// + /// The which occured while watching the registry. + /// + ///

+ /// OnError is called when an exception occurs while watching the registry. + ///

+ /// + /// When overriding in a derived class, be sure to call + /// the base class's method. + /// + ///
+ protected virtual void OnError(Exception e) + { + ErrorEventHandler handler = Error; + if (handler != null) + handler(this, new ErrorEventArgs(e)); + } + + #endregion + + #region Private member variables + + private IntPtr _registryHive; + private string _registrySubName; + private object _threadLock = new object(); + private Thread _thread; + private bool _disposed = false; + private ManualResetEvent _eventTerminate = new ManualResetEvent(false); + + private RegChangeNotifyFilters _regFilter = RegChangeNotifyFilters.Key | RegChangeNotifyFilters.Attribute | + RegChangeNotifyFilters.Value | RegChangeNotifyFilters.Security; + + #endregion + + /// + /// Initializes a new instance of the class. + /// + /// The registry key to monitor. + public RegistryMonitor(RegistryKey registryKey) + { + InitRegistryKey(registryKey.Name); + } + + /// + /// Initializes a new instance of the class. + /// + /// The name. + public RegistryMonitor(string name) + { + if (name == null || name.Length == 0) + throw new ArgumentNullException("name"); + + InitRegistryKey(name); + } + + /// + /// Initializes a new instance of the class. + /// + /// The registry hive. + /// The sub key. + public RegistryMonitor(RegistryHive registryHive, string subKey) + { + InitRegistryKey(registryHive, subKey); + } + + /// + /// Disposes this object. + /// + public void Dispose() + { + Stop(); + _disposed = true; + GC.SuppressFinalize(this); + } + + /// + /// Gets or sets the RegChangeNotifyFilter. + /// + public RegChangeNotifyFilters RegChangeNotifyFilter + { + get { return _regFilter; } + set + { + lock (_threadLock) + { + if (IsMonitoring) + throw new InvalidOperationException("Monitoring thread is already running"); + + _regFilter = value; + } + } + } + + #region Initialization + + private void InitRegistryKey(RegistryHive hive, string name) + { + switch (hive) + { + case RegistryHive.ClassesRoot: + _registryHive = HKEY_CLASSES_ROOT; + break; + + case RegistryHive.CurrentConfig: + _registryHive = HKEY_CURRENT_CONFIG; + break; + + case RegistryHive.CurrentUser: + _registryHive = HKEY_CURRENT_USER; + break; + + case RegistryHive.DynData: + _registryHive = HKEY_DYN_DATA; + break; + + case RegistryHive.LocalMachine: + _registryHive = HKEY_LOCAL_MACHINE; + break; + + case RegistryHive.PerformanceData: + _registryHive = HKEY_PERFORMANCE_DATA; + break; + + case RegistryHive.Users: + _registryHive = HKEY_USERS; + break; + + default: + throw new InvalidEnumArgumentException("hive", (int)hive, typeof(RegistryHive)); + } + _registrySubName = name; + } + + private void InitRegistryKey(string name) + { + string[] nameParts = name.Split('\\'); + + switch (nameParts[0]) + { + case "HKEY_CLASSES_ROOT": + case "HKCR": + _registryHive = HKEY_CLASSES_ROOT; + break; + + case "HKEY_CURRENT_USER": + case "HKCU": + _registryHive = HKEY_CURRENT_USER; + break; + + case "HKEY_LOCAL_MACHINE": + case "HKLM": + _registryHive = HKEY_LOCAL_MACHINE; + break; + + case "HKEY_USERS": + _registryHive = HKEY_USERS; + break; + + case "HKEY_CURRENT_CONFIG": + _registryHive = HKEY_CURRENT_CONFIG; + break; + + default: + _registryHive = IntPtr.Zero; + throw new ArgumentException("The registry hive '" + nameParts[0] + "' is not supported", "value"); + } + + _registrySubName = String.Join("\\", nameParts, 1, nameParts.Length - 1); + } + + #endregion + + /// + /// true if this object is currently monitoring; + /// otherwise, false. + /// + public bool IsMonitoring + { + get { return _thread != null; } + } + + /// + /// Start monitoring. + /// + public void Start() + { + if (_disposed) + throw new ObjectDisposedException(null, "This instance is already disposed"); + + lock (_threadLock) + { + if (!IsMonitoring) + { + _eventTerminate.Reset(); + _thread = new Thread(new ThreadStart(MonitorThread)); + _thread.IsBackground = true; + _thread.Start(); + } + } + } + + /// + /// Stops the monitoring thread. + /// + public void Stop() + { + if (_disposed) + throw new ObjectDisposedException(null, "This instance is already disposed"); + + lock (_threadLock) + { + Thread thread = _thread; + if (thread != null) + { + _eventTerminate.Set(); + thread.Join(); + } + } + } + + private void MonitorThread() + { + try + { + ThreadLoop(); + } + catch (Exception e) + { + OnError(e); + } + _thread = null; + } + + private void ThreadLoop() + { + IntPtr registryKey; + int result = RegOpenKeyEx(_registryHive, _registrySubName, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY, + out registryKey); + if (result != 0) + throw new Win32Exception(result); + + try + { + AutoResetEvent _eventNotify = new AutoResetEvent(false); + WaitHandle[] waitHandles = new WaitHandle[] { _eventNotify, _eventTerminate }; + while (!_eventTerminate.WaitOne(0, true)) + { + result = RegNotifyChangeKeyValue(registryKey, true, _regFilter, _eventNotify.Handle, true); + if (result != 0) + throw new Win32Exception(result); + + if (WaitHandle.WaitAny(waitHandles) == 0) + { + OnRegChanged(); + } + } + } + finally + { + if (registryKey != IntPtr.Zero) + { + RegCloseKey(registryKey); + } + } + } + } + +} \ No newline at end of file diff --git a/MiscHelpers/API/ServiceHelper.cs b/MiscHelpers/API/ServiceHelper.cs new file mode 100644 index 0000000..e049fae --- /dev/null +++ b/MiscHelpers/API/ServiceHelper.cs @@ -0,0 +1,657 @@ +using Microsoft.Win32; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.ServiceProcess; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using MiscHelpers; + +namespace MiscHelpers +{ + + public static class ServiceHelper + { + private const int STANDARD_RIGHTS_REQUIRED = 0xF0000; + private const int SERVICE_WIN32_OWN_PROCESS = 0x00000010; + private const int ERROR_INSUFFICIENT_BUFFER = 0x0000007a; + private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF; + private const int SC_STATUS_PROCESS_INFO = 0; + + + + #region OpenSCManager + [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] + static extern IntPtr OpenSCManager(string machineName, string databaseName, ScmAccessRights dwDesiredAccess); + #endregion + + #region OpenService + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] + static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, ServiceAccessRights dwDesiredAccess); + #endregion + + #region CreateService + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] + private static extern IntPtr CreateService(IntPtr hSCManager, string lpServiceName, string lpDisplayName, ServiceAccessRights dwDesiredAccess, int dwServiceType, ServiceBootFlag dwStartType, ServiceError dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lp, string lpPassword); + #endregion + + #region CloseServiceHandle + [DllImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + static extern bool CloseServiceHandle(IntPtr hSCObject); + #endregion + + #region QueryServiceConfig + [StructLayout(LayoutKind.Sequential)] + public class ServiceConfigInfo + { + [MarshalAs(UnmanagedType.U4)] + public UInt32 ServiceType; + [MarshalAs(UnmanagedType.U4)] + public UInt32 StartType; + [MarshalAs(UnmanagedType.U4)] + public UInt32 ErrorControl; + [MarshalAs(UnmanagedType.LPWStr)] + public String BinaryPathName; + [MarshalAs(UnmanagedType.LPWStr)] + public String LoadOrderGroup; + [MarshalAs(UnmanagedType.U4)] + public UInt32 TagID; + [MarshalAs(UnmanagedType.LPWStr)] + public String Dependencies; + [MarshalAs(UnmanagedType.LPWStr)] + public String ServiceStartName; + [MarshalAs(UnmanagedType.LPWStr)] + public String DisplayName; + } + + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] + public static extern int QueryServiceConfig(IntPtr service, IntPtr queryServiceConfig, int bufferSize, ref int bytesNeeded); + #endregion + + + #region ChangeServiceConfig + [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] + private static extern bool ChangeServiceConfig(IntPtr hService, UInt32 dwServiceType, ServiceBootFlag dwStartType, UInt32 dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName); + #endregion + + #region QueryServiceStatus + [StructLayout(LayoutKind.Sequential)] + public class SERVICE_STATUS + { + public int dwServiceType = 0; + public ServiceState dwCurrentState = 0; + public int dwControlsAccepted = 0; + public int dwWin32ExitCode = 0; + public int dwServiceSpecificExitCode = 0; + public int dwCheckPoint = 0; + public int dwWaitHint = 0; + } + + [DllImport("advapi32.dll", SetLastError = true)] + private static extern int QueryServiceStatus(IntPtr hService, SERVICE_STATUS lpServiceStatus); + #endregion + + #region QueryServiceStatusEx + [StructLayout(LayoutKind.Sequential)] + public sealed class SERVICE_STATUS_PROCESS + { + [MarshalAs(UnmanagedType.U4)] + public uint dwServiceType; + [MarshalAs(UnmanagedType.U4)] + public uint dwCurrentState; + [MarshalAs(UnmanagedType.U4)] + public uint dwControlsAccepted; + [MarshalAs(UnmanagedType.U4)] + public uint dwWin32ExitCode; + [MarshalAs(UnmanagedType.U4)] + public uint dwServiceSpecificExitCode; + [MarshalAs(UnmanagedType.U4)] + public uint dwCheckPoint; + [MarshalAs(UnmanagedType.U4)] + public uint dwWaitHint; + [MarshalAs(UnmanagedType.U4)] + public uint dwProcessId; + [MarshalAs(UnmanagedType.U4)] + public uint dwServiceFlags; + } + + [DllImport("advapi32.dll", SetLastError = true)] + internal static extern bool QueryServiceStatusEx(IntPtr hService, int infoLevel, IntPtr lpBuffer, uint cbBufSize, out uint pcbBytesNeeded); + #endregion + + #region DeleteService + [DllImport("advapi32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + private static extern bool DeleteService(IntPtr hService); + #endregion + + #region ControlService + [DllImport("advapi32.dll")] + private static extern int ControlService(IntPtr hService, ServiceControl dwControl, SERVICE_STATUS lpServiceStatus); + #endregion + + #region StartService + [DllImport("advapi32.dll", SetLastError = true)] + private static extern int StartService(IntPtr hService, int dwNumServiceArgs, int lpServiceArgVectors); + #endregion + + public static void Uninstall(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess); + + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess); + if (service == IntPtr.Zero) + throw new ApplicationException("Service not installed."); + + try + { + StopService(service); + + if (!DeleteService(service)) + throw new ApplicationException("Could not delete service " + Marshal.GetLastWin32Error()); + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + } + + public static bool ServiceIsInstalled(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect); + + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus); + + if (service == IntPtr.Zero) + return false; + + CloseServiceHandle(service); + return true; + } + finally + { + CloseServiceHandle(scm); + } + } + + public static void Install(string serviceName, string displayName, string fileName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess); + + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess); + + if (service == IntPtr.Zero) + service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, SERVICE_WIN32_OWN_PROCESS, ServiceBootFlag.AutoStart, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null); + + if (service == IntPtr.Zero) + throw new ApplicationException("Failed to install service."); + + CloseServiceHandle(service); + } + finally + { + CloseServiceHandle(scm); + } + } + + public static void ChangeStartMode(string serviceName, ServiceBootFlag mode) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect | ScmAccessRights.EnumerateService); + + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryConfig | ServiceAccessRights.ChangeConfig); + if (service == IntPtr.Zero) + throw new ApplicationException("Could not open service."); + + try + { + if (!ChangeServiceConfig(service, SERVICE_NO_CHANGE, mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null)) + throw new ApplicationException("Could not configure service " + Marshal.GetLastWin32Error()); + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + } + + public static ServiceConfigInfo GetServiceInfo(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect | ScmAccessRights.EnumerateService); + + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryConfig | ServiceAccessRights.ChangeConfig); + if (service == IntPtr.Zero) + throw new ApplicationException("Could not open service."); + + try + { + int bytesNeeded = 0; + if (QueryServiceConfig(service, IntPtr.Zero, 0, ref bytesNeeded) == 0 && bytesNeeded == 0) + throw new ApplicationException("Could not query service configuration" + Marshal.GetLastWin32Error()); + + IntPtr qscPtr = Marshal.AllocCoTaskMem(bytesNeeded); + try + { + if (QueryServiceConfig(service, qscPtr, bytesNeeded, ref bytesNeeded) == 0) + throw new ApplicationException("Could not query service configuration" + Marshal.GetLastWin32Error()); + + return (ServiceConfigInfo)Marshal.PtrToStructure(qscPtr, typeof(ServiceConfigInfo)); + } + finally + { + Marshal.FreeCoTaskMem(qscPtr); + } + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + } + + public static ServiceConfigInfo GetServiceInfoSafe(string serviceName) + { + try + { + return GetServiceInfo(serviceName); + } + catch + { + return null; + } + } + + public static bool StartService(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect); + + bool ret = false; + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus | ServiceAccessRights.Start); + if (service == IntPtr.Zero) + throw new ApplicationException("Could not open service."); + + try + { + StartService(service); + ret = true; + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + return ret; + } + + public static bool StopService(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect); + + bool ret = false; + try + { + IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus | ServiceAccessRights.Stop); + if (service == IntPtr.Zero) + throw new ApplicationException("Could not open service."); + + try + { + StopService(service); + ret = true; + } + finally + { + CloseServiceHandle(service); + } + } + finally + { + CloseServiceHandle(scm); + } + return ret; + } + + private static void StartService(IntPtr service) + { + SERVICE_STATUS status = new SERVICE_STATUS(); + StartService(service, 0, 0); + var changedStatus = WaitForServiceStatus(service, ServiceState.StartPending, ServiceState.Running); + if (!changedStatus) + throw new ApplicationException("Unable to start service"); + } + + private static void StopService(IntPtr service) + { + SERVICE_STATUS status = new SERVICE_STATUS(); + ControlService(service, ServiceControl.Stop, status); + var changedStatus = WaitForServiceStatus(service, ServiceState.StopPending, ServiceState.Stopped); + if (!changedStatus) + throw new ApplicationException("Unable to stop service"); + } + + public static ServiceState GetServiceState(string serviceName) + { + SERVICE_STATUS_PROCESS ssp = GetServiceStatus(serviceName); + if (ssp == null) + return ServiceState.NotFound; + return (ServiceState)ssp.dwCurrentState; + } + + public static SERVICE_STATUS_PROCESS GetServiceStatus(string serviceName) + { + IntPtr scm = OpenSCManager(ScmAccessRights.Connect); + IntPtr zero = IntPtr.Zero; + IntPtr service = IntPtr.Zero; + + try + { + service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus); + + UInt32 dwBytesAlloc = 0; + UInt32 dwBytesNeeded = 36; + do + { + dwBytesAlloc = dwBytesNeeded; + // Allocate required buffer and call again. + zero = Marshal.AllocHGlobal((int)dwBytesAlloc); + if (QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, zero, dwBytesAlloc, out dwBytesNeeded)) + { + var ssp = new SERVICE_STATUS_PROCESS(); + Marshal.PtrToStructure(zero, ssp); + return ssp; + } + // retry with new size info + } while (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER && dwBytesAlloc < dwBytesNeeded); + } + finally + { + if (zero != IntPtr.Zero) + Marshal.FreeHGlobal(zero); + if (service != IntPtr.Zero) + CloseServiceHandle(service); + CloseServiceHandle(scm); + } + return null; + } + + private static bool WaitForServiceStatus(IntPtr service, ServiceState waitStatus, ServiceState desiredStatus) + { + SERVICE_STATUS status = new SERVICE_STATUS(); + + QueryServiceStatus(service, status); + if (status.dwCurrentState == desiredStatus) return true; + + int dwStartTickCount = Environment.TickCount; + int dwOldCheckPoint = status.dwCheckPoint; + + while (status.dwCurrentState == waitStatus) + { + // Do not wait longer than the wait hint. A good interval is + // one tenth the wait hint, but no less than 1 second and no + // more than 10 seconds. + + int dwWaitTime = status.dwWaitHint / 10; + + if (dwWaitTime < 1000) dwWaitTime = 1000; + else if (dwWaitTime > 10000) dwWaitTime = 10000; + + Thread.Sleep(dwWaitTime); + + // Check the status again. + + if (QueryServiceStatus(service, status) == 0) break; + + if (status.dwCheckPoint > dwOldCheckPoint) + { + // The service is making progress. + dwStartTickCount = Environment.TickCount; + dwOldCheckPoint = status.dwCheckPoint; + } + else + { + if (Environment.TickCount - dwStartTickCount > status.dwWaitHint) + { + // No progress made within the wait hint + break; + } + } + } + return (status.dwCurrentState == desiredStatus); + } + + private static IntPtr OpenSCManager(ScmAccessRights rights) + { + IntPtr scm = OpenSCManager(null, null, rights); + if (scm == IntPtr.Zero) + throw new ApplicationException("Could not connect to service control manager."); + + return scm; + } + + + public enum ServiceState + { + Unknown = -1, // The state cannot be (has not been) retrieved. + NotFound = 0, // The service is not known on the host server. + Stopped = 1, + StartPending = 2, + StopPending = 3, + Running = 4, + ContinuePending = 5, + PausePending = 6, + Paused = 7 + } + + [Flags] + public enum ScmAccessRights + { + Connect = 0x0001, + CreateService = 0x0002, + EnumerateService = 0x0004, + Lock = 0x0008, + QueryLockStatus = 0x0010, + ModifyBootConfig = 0x0020, + StandardRightsRequired = 0xF0000, + AllAccess = (StandardRightsRequired | Connect | CreateService | + EnumerateService | Lock | QueryLockStatus | ModifyBootConfig) + } + + [Flags] + public enum ServiceAccessRights + { + QueryConfig = 0x1, + ChangeConfig = 0x2, + QueryStatus = 0x4, + EnumerateDependants = 0x8, + Start = 0x10, + Stop = 0x20, + PauseContinue = 0x40, + Interrogate = 0x80, + UserDefinedControl = 0x100, + Delete = 0x00010000, + StandardRightsRequired = 0xF0000, + AllAccess = (StandardRightsRequired | QueryConfig | ChangeConfig | + QueryStatus | EnumerateDependants | Start | Stop | PauseContinue | + Interrogate | UserDefinedControl) + } + + public enum ServiceBootFlag + { + Start = 0x00000000, + SystemStart = 0x00000001, + AutoStart = 0x00000002, + DemandStart = 0x00000003, + Disabled = 0x00000004 + } + + public enum ServiceControl + { + Stop = 0x00000001, + Pause = 0x00000002, + Continue = 0x00000003, + Interrogate = 0x00000004, + Shutdown = 0x00000005, + ParamChange = 0x00000006, + NetBindAdd = 0x00000007, + NetBindRemove = 0x00000008, + NetBindEnable = 0x00000009, + NetBindDisable = 0x0000000A + } + + public enum ServiceError + { + Ignore = 0x00000000, + Normal = 0x00000001, + Severe = 0x00000002, + Critical = 0x00000003 + } + + private static string GetServiceImagePath(string serviceName) + { + RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\" + serviceName); + if (regkey.GetValue("ImagePath") == null) + return null; + return regkey.GetValue("ImagePath").ToString(); + } + + public class ServiceInfo + { + public ServiceInfo(ServiceController sc) + { + ServiceName = sc.ServiceName; + var ImagePath = GetServiceImagePath(sc.ServiceName); + ServicePath = ImagePath != null ? ProcFunc.GetPathFromCmdLine(ImagePath) : ""; + DisplayName = sc.DisplayName; + if (sc.Status == ServiceControllerStatus.Stopped) + LastKnownPID = -1; + else + { + var ssp = GetServiceStatus(sc.ServiceName); + LastKnownPID = ssp != null ? (int)ssp.dwProcessId : -1; + } + } + public string ServiceName; + public string ServicePath; + public string DisplayName; + public int LastKnownPID; + } + + private static DateTime ServiceCacheTime = DateTime.MinValue; + private static MultiValueDictionary ServiceCacheByPID = new MultiValueDictionary(); + private static Dictionary ServiceCache = new Dictionary(); + private static ReaderWriterLockSlim ServiceCacheLock = new ReaderWriterLockSlim(); + + + private static void RefreshServices() + { + ServiceCacheLock.EnterWriteLock(); + ServiceCacheTime = DateTime.Now; + ServiceCache.Clear(); + ServiceCacheByPID.Clear(); + foreach (ServiceController sc in ServiceController.GetServices()) + { + ServiceInfo info = new ServiceInfo(sc); + if (!ServiceCache.ContainsKey(sc.ServiceName)) // should not happen but in case + ServiceCache.Add(sc.ServiceName, info); + if (info.LastKnownPID != -1) + ServiceCacheByPID.Add(info.LastKnownPID, info); + } + // this takes roughly 30 ms + ServiceCacheLock.ExitWriteLock(); + } + + public static List GetServicesByPID(int pid) + { + bool doUpdate = false; + if (pid == -1) // -1 means get all and we always want a fresh list + doUpdate = true; + else + { + ServiceCacheLock.EnterReadLock(); + doUpdate = ServiceCacheTime <= DateTime.FromFileTimeUtc(ProcFunc.GetProcessCreationTime(pid)).ToLocalTime(); + ServiceCacheLock.ExitReadLock(); + } + if (doUpdate) + RefreshServices(); + + ServiceCacheLock.EnterReadLock(); + CloneableList values; + if (pid == -1) + values = ServiceCacheByPID.GetAllValues(); + else if (!ServiceCacheByPID.TryGetValue(pid, out values)) + values = null; + ServiceCacheLock.ExitReadLock(); + return (values != null && values.Count == 0) ? null : values; + } + + public static List GetAllServices() + { + ServiceCacheLock.EnterReadLock(); + bool doUpdate = ServiceCacheTime < DateTime.Now.AddSeconds(-30); + ServiceCacheLock.ExitReadLock(); + if (doUpdate) + RefreshServices(); + + ServiceCacheLock.EnterReadLock(); + List list = ServiceCache.Values.ToList(); + ServiceCacheLock.ExitReadLock(); + return list; + } + + public static string GetServiceName(string name) + { + if (name == null || name.Length == 0) + return ""; + + ServiceCacheLock.EnterReadLock(); + ServiceInfo info = null; + bool found = ServiceCache.TryGetValue(name, out info); + ServiceCacheLock.ExitReadLock(); + + if (!found) + { + ServiceController sc = new ServiceController(name); + try { info = new ServiceInfo(sc); } catch { } + sc.Close(); + + ServiceCacheLock.EnterWriteLock(); + if (info != null && !ServiceCache.ContainsKey(sc.ServiceName)) // should not happen but in case + ServiceCache.Add(sc.ServiceName, info); + ServiceCacheLock.ExitWriteLock(); + } + + return info == null ? "" : info.DisplayName; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/TokenManipulator.cs b/MiscHelpers/API/TokenManipulator.cs new file mode 100644 index 0000000..c4073d6 --- /dev/null +++ b/MiscHelpers/API/TokenManipulator.cs @@ -0,0 +1,123 @@ +using System; +using System.Runtime.InteropServices; + +namespace MiscHelpers +{ + public class TokenManipulator + { + + + [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] + internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, + ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); + + + [DllImport("kernel32.dll", ExactSpelling = true)] + internal static extern IntPtr GetCurrentProcess(); + + + [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] + internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr + phtok); + + + [DllImport("advapi32.dll", SetLastError = true)] + internal static extern bool LookupPrivilegeValue(string host, string name, + ref long pluid); + + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + internal struct TokPriv1Luid + { + public int Count; + public long Luid; + public int Attr; + } + + internal const int SE_PRIVILEGE_DISABLED = 0x00000000; + internal const int SE_PRIVILEGE_ENABLED = 0x00000002; + internal const int TOKEN_QUERY = 0x00000008; + internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; + + public const string SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"; + public const string SE_AUDIT_NAME = "SeAuditPrivilege"; + public const string SE_BACKUP_NAME = "SeBackupPrivilege"; + public const string SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"; + public const string SE_CREATE_GLOBAL_NAME = "SeCreateGlobalPrivilege"; + public const string SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"; + public const string SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"; + public const string SE_CREATE_SYMBOLIC_LINK_NAME = "SeCreateSymbolicLinkPrivilege"; + public const string SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"; + public const string SE_DEBUG_NAME = "SeDebugPrivilege"; + public const string SE_ENABLE_DELEGATION_NAME = "SeEnableDelegationPrivilege"; + public const string SE_IMPERSONATE_NAME = "SeImpersonatePrivilege"; + public const string SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"; + public const string SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"; + public const string SE_INC_WORKING_SET_NAME = "SeIncreaseWorkingSetPrivilege"; + public const string SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"; + public const string SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"; + public const string SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"; + public const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege"; + public const string SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"; + public const string SE_RELABEL_NAME = "SeRelabelPrivilege"; + public const string SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"; + public const string SE_RESTORE_NAME = "SeRestorePrivilege"; + public const string SE_SECURITY_NAME = "SeSecurityPrivilege"; + public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; + public const string SE_SYNC_AGENT_NAME = "SeSyncAgentPrivilege"; + public const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"; + public const string SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"; + public const string SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"; + public const string SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"; + public const string SE_TCB_NAME = "SeTcbPrivilege"; + public const string SE_TIME_ZONE_NAME = "SeTimeZonePrivilege"; + public const string SE_TRUSTED_CREDMAN_ACCESS_NAME = "SeTrustedCredManAccessPrivilege"; + public const string SE_UNDOCK_NAME = "SeUndockPrivilege"; + public const string SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"; + + public static bool AddPrivilege(string privilege) + { + try + { + bool retVal; + TokPriv1Luid tp; + IntPtr hproc = GetCurrentProcess(); + IntPtr htok = IntPtr.Zero; + retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); + tp.Count = 1; + tp.Luid = 0; + tp.Attr = SE_PRIVILEGE_ENABLED; + retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid); + retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); + return retVal; + } + catch (Exception ex) + { + throw ex; + } + + } + public static bool RemovePrivilege(string privilege) + { + try + { + bool retVal; + TokPriv1Luid tp; + IntPtr hproc = GetCurrentProcess(); + IntPtr htok = IntPtr.Zero; + retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); + tp.Count = 1; + tp.Luid = 0; + tp.Attr = SE_PRIVILEGE_DISABLED; + retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid); + retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); + return retVal; + } + catch (Exception ex) + { + throw ex; + } + + } + } +} \ No newline at end of file diff --git a/MiscHelpers/API/UwpFunc.cs b/MiscHelpers/API/UwpFunc.cs new file mode 100644 index 0000000..ab2b06a --- /dev/null +++ b/MiscHelpers/API/UwpFunc.cs @@ -0,0 +1,105 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace MiscHelpers +{ + public class UwpFunc + { + + const long APPMODEL_ERROR_NO_PACKAGE = 15700L; + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); + + static public bool IsRunningAsUwp() + { + if (IsWindows7OrLower) + { + return false; + } + else + { + int length = 0; + StringBuilder sb = new StringBuilder(0); + int result = GetCurrentPackageFullName(ref length, sb); + + sb = new StringBuilder(length); + result = GetCurrentPackageFullName(ref length, sb); + + return result != APPMODEL_ERROR_NO_PACKAGE; + } + } + + /* + +------------------------------------------------------------------------------+ + | | PlatformID | Major version | Minor version | + +------------------------------------------------------------------------------+ + | Windows 95 | Win32Windows | 4 | 0 | + | Windows 98 | Win32Windows | 4 | 10 | + | Windows Me | Win32Windows | 4 | 90 | + | Windows NT 4.0 | Win32NT | 4 | 0 | + | Windows 2000 | Win32NT | 5 | 0 | + | Windows XP | Win32NT | 5 | 1 | + | Windows 2003 | Win32NT | 5 | 2 | + | Windows Vista | Win32NT | 6 | 0 | + | Windows 2008 | Win32NT | 6 | 0 | + | Windows 7 | Win32NT | 6 | 1 | + | Windows 2008 R2 | Win32NT | 6 | 1 | + | Windows 8 | Win32NT | 6 | 2 | + | Windows 8.1 | Win32NT | 6 | 3 | + +------------------------------------------------------------------------------+ + | Windows 10 | Win32NT | 10 | 0 | + +------------------------------------------------------------------------------+ + */ + + static public bool IsWindows7OrLower + { + get + { + int versionMajor = Environment.OSVersion.Version.Major; + int versionMinor = Environment.OSVersion.Version.Minor; + double version = versionMajor + (double)versionMinor / 10; + return version <= 6.1; + } + } + + static public bool IsWindows8 + { + get + { + int versionMajor = Environment.OSVersion.Version.Major; + int versionMinor = Environment.OSVersion.Version.Minor; + double version = versionMajor + (double)versionMinor / 10; + return version == 6.2 || version == 6.3; + } + } + + [Serializable()] + [DataContract(Name = "AppInfo", Namespace = "http://schemas.datacontract.org/")] + public class AppInfo + { + [DataMember()] + public string Name; + [DataMember()] + public string Logo; + [DataMember()] + public string ID; + [DataMember()] + public string SID; + } + + public static void AddBinding(System.Windows.Controls.Control ctrl, KeyGesture keyGesture, ExecutedRoutedEventHandler executed) + { + RoutedCommand cmd = new RoutedCommand(); + cmd.InputGestures.Add(keyGesture); + ctrl.CommandBindings.Add(new CommandBinding(cmd, executed)); + } + } + +} \ No newline at end of file diff --git a/PrivateWin10/API/WinVer.cs b/MiscHelpers/API/WinVer.cs similarity index 95% rename from PrivateWin10/API/WinVer.cs rename to MiscHelpers/API/WinVer.cs index 0f292a9..b7a9ff8 100644 --- a/PrivateWin10/API/WinVer.cs +++ b/MiscHelpers/API/WinVer.cs @@ -1,11 +1,12 @@ using Microsoft.Win32; +using MiscHelpers; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -namespace PrivateWin10 +namespace MiscHelpers { [Serializable()] public class WinVer @@ -28,7 +29,7 @@ public class WinVer protected const int ten1809 = 17763; // Redstone 5 protected const int ten19H1 = 18362; // 19H1 protected const int ten19H2 = 18363; // 19H2 - //protected const int ten20H1 + protected const int ten20H1 = 19041; //protected const int ten20H2 //protected const int ten21H1 //... @@ -56,6 +57,7 @@ public class WinVer static public WinVer Win1809 { get { return new WinVer() { minVer = ver10, build10 = ten1809 }; } } static public WinVer Win19H1 { get { return new WinVer() { minVer = ver10, build10 = ten19H1 }; } } static public WinVer Win19H2 { get { return new WinVer() { minVer = ver10, build10 = ten19H2 }; } } + static public WinVer Win20H1 { get { return new WinVer() { minVer = ver10, build10 = ten20H1 }; } } public enum Edition10 diff --git a/PrivateWin10/API/WindowsFirewall.cs b/MiscHelpers/API/WindowsFirewall.cs similarity index 76% rename from PrivateWin10/API/WindowsFirewall.cs rename to MiscHelpers/API/WindowsFirewall.cs index 388f661..074a8db 100644 --- a/PrivateWin10/API/WindowsFirewall.cs +++ b/MiscHelpers/API/WindowsFirewall.cs @@ -8,27 +8,28 @@ #pragma warning disable 169 // disable warning CS0169: The field 'Test.unused' is never used -namespace PrivateWin10 + +namespace MiscHelpers { public static class WindowsFirewall { // based on informations form https://github.com/processhacker/plugins-extra/blob/master/FirewallMonitorPlugin/wf.h - //http://msdn.microsoft.com/en-us/library/cc231461.aspx - public enum FW_BINARY_VERSION: ushort + //http://msdn.microsoft.com/en-us/library/cc231461.aspx + public enum FW_BINARY_VERSION : ushort { //FW_BINARY_VERSION_VISTA = 0x0200, - //FW_BINARY_VERSION_SERVER2K8 = 0x0201, - FW_BINARY_VERSION_SEVEN = 0x020A, - FW_BINARY_VERSION_WIN8 = 0x0214, - - FW_BINARY_VERSION_WIN10 = 0x0216, - FW_BINARY_VERSION_THRESHOLD = 0x0218, // 1507 - FW_BINARY_VERSION_THRESHOLD2= 0x0219, // 1511 - FW_BINARY_VERSION_REDSTONE1 = 0x021A, // 1607 - FW_BINARY_VERSION_170x = 0x021B, // Redstone 2 & Redstone 3 - FW_BINARY_VERSION_180x = 0x021C, // Redstone 4 & Redstone 5 - FW_BINARY_VERSION_19Hx = 0x021E + //FW_BINARY_VERSION_SERVER2K8 = 0x0201, + FW_BINARY_VERSION_SEVEN = 0x020A, + FW_BINARY_VERSION_WIN8 = 0x0214, + + FW_BINARY_VERSION_WIN10 = 0x0216, + FW_BINARY_VERSION_THRESHOLD = 0x0218, // 1507 + FW_BINARY_VERSION_THRESHOLD2 = 0x0219, // 1511 + FW_BINARY_VERSION_REDSTONE1 = 0x021A, // 1607 + FW_BINARY_VERSION_170x = 0x021B, // Redstone 2 & Redstone 3 + FW_BINARY_VERSION_180x = 0x021C, // Redstone 4 & Redstone 5 + FW_BINARY_VERSION_19Hx = 0x021E } public const int ERROR_SUCCESS = 0x0; @@ -120,28 +121,28 @@ public enum FW_TRANSACTIONAL_STATE [Flags] public enum FW_ENUM_RULES_FLAGS : ushort { - FW_ENUM_RULES_FLAG_NONE = 0x0000, - FW_ENUM_RULES_FLAG_RESOLVE_NAME = 0x0001, - FW_ENUM_RULES_FLAG_RESOLVE_DESCRIPTION = 0x0002, - FW_ENUM_RULES_FLAG_RESOLVE_APPLICATION = 0x0004, - FW_ENUM_RULES_FLAG_RESOLVE_KEYWORD = 0x0008, - FW_ENUM_RULES_FLAG_RESOLVE_GPO_NAME = 0x0010, - FW_ENUM_RULES_FLAG_EFFECTIVE = 0x0020, - FW_ENUM_RULES_FLAG_INCLUDE_METADATA = 0x0040, + FW_ENUM_RULES_FLAG_NONE = 0x0000, + FW_ENUM_RULES_FLAG_RESOLVE_NAME = 0x0001, + FW_ENUM_RULES_FLAG_RESOLVE_DESCRIPTION = 0x0002, + FW_ENUM_RULES_FLAG_RESOLVE_APPLICATION = 0x0004, + FW_ENUM_RULES_FLAG_RESOLVE_KEYWORD = 0x0008, + FW_ENUM_RULES_FLAG_RESOLVE_GPO_NAME = 0x0010, + FW_ENUM_RULES_FLAG_EFFECTIVE = 0x0020, + FW_ENUM_RULES_FLAG_INCLUDE_METADATA = 0x0040, - FW_ENUM_RULES_FLAG_MAX = 0x0080 + FW_ENUM_RULES_FLAG_MAX = 0x0080 } public enum FW_RULE_STATUS_CLASS : uint { - FW_RULE_STATUS_CLASS_OK = 0x00010000, - FW_RULE_STATUS_CLASS_PARTIALLY_IGNORED = 0x00020000, - FW_RULE_STATUS_CLASS_IGNORED = 0x00040000, - FW_RULE_STATUS_CLASS_PARSING_ERROR = 0x00080000, - FW_RULE_STATUS_CLASS_SEMANTIC_ERROR = 0x00100000, - FW_RULE_STATUS_CLASS_RUNTIME_ERROR = 0x00200000, - FW_RULE_STATUS_CLASS_ERROR = 0x00380000, - FW_RULE_STATUS_CLASS_ALL = 0xFFFF0000 + FW_RULE_STATUS_CLASS_OK = 0x00010000, + FW_RULE_STATUS_CLASS_PARTIALLY_IGNORED = 0x00020000, + FW_RULE_STATUS_CLASS_IGNORED = 0x00040000, + FW_RULE_STATUS_CLASS_PARSING_ERROR = 0x00080000, + FW_RULE_STATUS_CLASS_SEMANTIC_ERROR = 0x00100000, + FW_RULE_STATUS_CLASS_RUNTIME_ERROR = 0x00200000, + FW_RULE_STATUS_CLASS_ERROR = 0x00380000, + FW_RULE_STATUS_CLASS_ALL = 0xFFFF0000 } public struct FW_QUERY @@ -227,14 +228,14 @@ public struct FW_QUERY_CONDITION public enum FW_PROFILE_TYPE : uint { - FW_PROFILE_TYPE_INVALID = 0, - FW_PROFILE_TYPE_DOMAIN = 0x001, - FW_PROFILE_TYPE_STANDARD = 0x002, - FW_PROFILE_TYPE_PRIVATE = FW_PROFILE_TYPE_STANDARD, - FW_PROFILE_TYPE_PUBLIC = 0x004, - FW_PROFILE_TYPE_ALL = 0x7FFFFFFF, - FW_PROFILE_TYPE_CURRENT = 0x80000000, - FW_PROFILE_TYPE_NONE = FW_PROFILE_TYPE_CURRENT | FW_PROFILE_TYPE_DOMAIN + FW_PROFILE_TYPE_INVALID = 0, + FW_PROFILE_TYPE_DOMAIN = 0x001, + FW_PROFILE_TYPE_STANDARD = 0x002, + FW_PROFILE_TYPE_PRIVATE = FW_PROFILE_TYPE_STANDARD, + FW_PROFILE_TYPE_PUBLIC = 0x004, + FW_PROFILE_TYPE_ALL = 0x7FFFFFFF, + FW_PROFILE_TYPE_CURRENT = 0x80000000, + FW_PROFILE_TYPE_NONE = FW_PROFILE_TYPE_CURRENT | FW_PROFILE_TYPE_DOMAIN } public enum FW_DIRECTION : uint @@ -247,57 +248,57 @@ public enum FW_DIRECTION : uint public enum IP_PROTOCOL : ushort { - HOPOPT = 0, - ICMPv4 = 1, - IGMP = 2, - TCP = 6, - UDP = 17, - IPv6 = 41, - IPv6Route = 43, - IPv6Frag = 44, - GRE = 47, - ICMPv6 = 58, - IPv6NoNxt = 59, - IPv6Opts = 60, - VRRP = 112, - PGM = 113, - L2TP = 115, - - Any = 256, - Custom = 257 + HOPOPT = 0, + ICMPv4 = 1, + IGMP = 2, + TCP = 6, + UDP = 17, + IPv6 = 41, + IPv6Route = 43, + IPv6Frag = 44, + GRE = 47, + ICMPv6 = 58, + IPv6NoNxt = 59, + IPv6Opts = 60, + VRRP = 112, + PGM = 113, + L2TP = 115, + + Any = 256, + Custom = 257 } [Flags] public enum FW_PORT_KEYWORD : ushort { - FW_PORT_KEYWORD_NONE = 0x00, - FW_PORT_KEYWORD_DYNAMIC_RPC_PORTS = 0x01, - FW_PORT_KEYWORD_RPC_EP = 0x02, - FW_PORT_KEYWORD_TEREDO_PORT = 0x04, + FW_PORT_KEYWORD_NONE = 0x00, + FW_PORT_KEYWORD_DYNAMIC_RPC_PORTS = 0x01, + FW_PORT_KEYWORD_RPC_EP = 0x02, + FW_PORT_KEYWORD_TEREDO_PORT = 0x04, // sinve win7 - FW_PORT_KEYWORD_IP_TLS_IN = 0x08, - FW_PORT_KEYWORD_IP_TLS_OUT = 0x10, + FW_PORT_KEYWORD_IP_TLS_IN = 0x08, + FW_PORT_KEYWORD_IP_TLS_OUT = 0x10, // since win8 - FW_PORT_KEYWORD_DHCP = 0x20, - FW_PORT_KEYWORD_PLAYTO_DISCOVERY = 0x40, + FW_PORT_KEYWORD_DHCP = 0x20, + FW_PORT_KEYWORD_PLAYTO_DISCOVERY = 0x40, // since win10 - FW_PORT_KEYWORD_MDNS = 0x80, + FW_PORT_KEYWORD_MDNS = 0x80, // since 1607 - FW_PORT_KEYWORD_CORTANA_OUT = 0x100, + FW_PORT_KEYWORD_CORTANA_OUT = 0x100, // since 1903 - FW_PORT_KEYWORD_PROXIMAL_TCP_CDP = 0x200, + FW_PORT_KEYWORD_PROXIMAL_TCP_CDP = 0x200, FW_PORT_KEYWORD_MAX_V2_1 = FW_PORT_KEYWORD_IP_TLS_IN, FW_PORT_KEYWORD_MAX_V2_10 = FW_PORT_KEYWORD_DHCP, FW_PORT_KEYWORD_MAX_V2_20 = FW_PORT_KEYWORD_MDNS, FW_PORT_KEYWORD_MAX_V2_24 = FW_PORT_KEYWORD_CORTANA_OUT, FW_PORT_KEYWORD_MAX_V2_25 = FW_PORT_KEYWORD_PROXIMAL_TCP_CDP, - FW_PORT_KEYWORD_MAX = 0x400 + FW_PORT_KEYWORD_MAX = 0x400 } public struct FW_PORT_RANGE @@ -381,16 +382,16 @@ public struct FW_PROTOCOL_UNION [Flags] public enum FW_ADDRESS_KEYWORD : uint { - FW_ADDRESS_KEYWORD_NONE = 0x0000, + FW_ADDRESS_KEYWORD_NONE = 0x0000, FW_ADDRESS_KEYWORD_LOCAL_SUBNET = 0x0001, - FW_ADDRESS_KEYWORD_DNS = 0x0002, - FW_ADDRESS_KEYWORD_DHCP = 0x0004, - FW_ADDRESS_KEYWORD_WINS = 0x0008, + FW_ADDRESS_KEYWORD_DNS = 0x0002, + FW_ADDRESS_KEYWORD_DHCP = 0x0004, + FW_ADDRESS_KEYWORD_WINS = 0x0008, FW_ADDRESS_KEYWORD_DEFAULT_GATEWAY = 0x0010, // since win8 - FW_ADDRESS_KEYWORD_INTRANET = 0x0020, - FW_ADDRESS_KEYWORD_INTERNET = 0x0040, + FW_ADDRESS_KEYWORD_INTRANET = 0x0020, + FW_ADDRESS_KEYWORD_INTERNET = 0x0040, FW_ADDRESS_KEYWORD_PLAYTO_RENDERERS = 0x0080, FW_ADDRESS_KEYWORD_REMOTE_INTRANET = 0x0100, @@ -399,7 +400,7 @@ public enum FW_ADDRESS_KEYWORD : uint FW_ADDRESS_KEYWORD_MAX_V2_10 = FW_ADDRESS_KEYWORD_INTRANET, FW_ADDRESS_KEYWORD_MAX_V2_29 = FW_ADDRESS_KEYWORD_CAPTIVE_PORTAL, - FW_ADDRESS_KEYWORD_MAX = 0x0400 + FW_ADDRESS_KEYWORD_MAX = 0x0400 } public struct FW_IPV4_SUBNET_LIST @@ -445,9 +446,9 @@ public struct FW_INTERFACE_LUIDS [Flags] public enum FW_INTERFACE_TYPE : uint { - FW_INTERFACE_TYPE_ALL = 0x0000, - FW_INTERFACE_TYPE_LAN = 0x0001, - FW_INTERFACE_TYPE_WIRELESS = 0x0002, + FW_INTERFACE_TYPE_ALL = 0x0000, + FW_INTERFACE_TYPE_LAN = 0x0001, + FW_INTERFACE_TYPE_WIRELESS = 0x0002, FW_INTERFACE_TYPE_REMOTE_ACCESS = 0x0004, FW_INTERFACE_TYPE_MAX = 0x0008 @@ -467,29 +468,29 @@ public enum FW_RULE_ACTION : uint [Flags] public enum FW_RULE_FLAGS : ushort { - FW_RULE_FLAGS_NONE = 0x0000, - FW_RULE_FLAGS_ACTIVE = 0x0001, - FW_RULE_FLAGS_AUTHENTICATE = 0x0002, - FW_RULE_FLAGS_AUTHENTICATE_WITH_ENCRYPTION = 0x0004, - FW_RULE_FLAGS_ROUTEABLE_ADDRS_TRAVERSE = 0x0008, - FW_RULE_FLAGS_LOOSE_SOURCE_MAPPED = 0x0010, - + FW_RULE_FLAGS_NONE = 0x0000, + FW_RULE_FLAGS_ACTIVE = 0x0001, + FW_RULE_FLAGS_AUTHENTICATE = 0x0002, + FW_RULE_FLAGS_AUTHENTICATE_WITH_ENCRYPTION = 0x0004, + FW_RULE_FLAGS_ROUTEABLE_ADDRS_TRAVERSE = 0x0008, + FW_RULE_FLAGS_LOOSE_SOURCE_MAPPED = 0x0010, + // since win 7 - FW_RULE_FLAGS_AUTH_WITH_NO_ENCAPSULATION = 0x0020, - - FW_RULE_FLAGS_AUTH_WITH_ENC_NEGOTIATE = 0x0040, + FW_RULE_FLAGS_AUTH_WITH_NO_ENCAPSULATION = 0x0020, + + FW_RULE_FLAGS_AUTH_WITH_ENC_NEGOTIATE = 0x0040, FW_RULE_FLAGS_ROUTEABLE_ADDRS_TRAVERSE_DEFER_APP = 0x0080, FW_RULE_FLAGS_ROUTEABLE_ADDRS_TRAVERSE_DEFER_USER = 0x0100, - FW_RULE_FLAGS_AUTHENTICATE_BYPASS_OUTBOUND = 0x0200, - + FW_RULE_FLAGS_AUTHENTICATE_BYPASS_OUTBOUND = 0x0200, + // since win8 - FW_RULE_FLAGS_ALLOW_PROFILE_CROSSING = 0x0400, - FW_RULE_FLAGS_LOCAL_ONLY_MAPPED = 0x0800, + FW_RULE_FLAGS_ALLOW_PROFILE_CROSSING = 0x0400, + FW_RULE_FLAGS_LOCAL_ONLY_MAPPED = 0x0800, // since 1507 - FW_RULE_FLAGS_LUA_CONDITIONAL_ACE = 0x1000, + FW_RULE_FLAGS_LUA_CONDITIONAL_ACE = 0x1000, - FW_RULE_FLAGS_BIND_TO_INTERFACE = 0x2000, // not used + FW_RULE_FLAGS_BIND_TO_INTERFACE = 0x2000, // not used FW_RULE_FLAGS_MAX_V2_1 = FW_RULE_FLAGS_AUTH_WITH_NO_ENCAPSULATION, FW_RULE_FLAGS_MAX_V2_9 = FW_RULE_FLAGS_AUTH_WITH_ENC_NEGOTIATE, @@ -528,26 +529,26 @@ public enum FW_RULE_ORIGIN_TYPE [Flags] public enum FW_TRUST_TUPLE_KEYWORD : uint { - FW_TRUST_TUPLE_KEYWORD_NONE = 0x0000, - FW_TRUST_TUPLE_KEYWORD_PROXIMITY = 0x0001, + FW_TRUST_TUPLE_KEYWORD_NONE = 0x0000, + FW_TRUST_TUPLE_KEYWORD_PROXIMITY = 0x0001, FW_TRUST_TUPLE_KEYWORD_PROXIMITY_SHARING = 0x0002, // since win10 - FW_TRUST_TUPLE_KEYWORD_WFD_PRINT = 0x0004, - FW_TRUST_TUPLE_KEYWORD_WFD_DISPLAY = 0x0008, - FW_TRUST_TUPLE_KEYWORD_WFD_DEVICES = 0x0010, + FW_TRUST_TUPLE_KEYWORD_WFD_PRINT = 0x0004, + FW_TRUST_TUPLE_KEYWORD_WFD_DISPLAY = 0x0008, + FW_TRUST_TUPLE_KEYWORD_WFD_DEVICES = 0x0010, // since 1703 - FW_TRUST_TUPLE_KEYWORD_WFD_KM_DRIVER = 0x0020, - FW_TRUST_TUPLE_KEYWORD_UPNP = 0x0040, + FW_TRUST_TUPLE_KEYWORD_WFD_KM_DRIVER = 0x0020, + FW_TRUST_TUPLE_KEYWORD_UPNP = 0x0040, // since 1803 - FW_TRUST_TUPLE_KEYWORD_WFD_CDP = 0x0080, - + FW_TRUST_TUPLE_KEYWORD_WFD_CDP = 0x0080, + FW_TRUST_TUPLE_KEYWORD_MAX_V2_20 = FW_TRUST_TUPLE_KEYWORD_WFD_PRINT, FW_TRUST_TUPLE_KEYWORD_MAX_V2_26 = FW_TRUST_TUPLE_KEYWORD_WFD_KM_DRIVER, FW_TRUST_TUPLE_KEYWORD_MAX_V2_27 = FW_TRUST_TUPLE_KEYWORD_WFD_CDP, - FW_TRUST_TUPLE_KEYWORD_MAX = 0x0100 + FW_TRUST_TUPLE_KEYWORD_MAX = 0x0100 } public struct FW_NETWORK_NAMES @@ -559,11 +560,11 @@ public struct FW_NETWORK_NAMES [Flags] public enum FW_RULE_FLAGS2 : ushort { - FW_RULE_FLAGS2_NONE = 0x0000, - FW_RULE_FLAGS2_SYSTEMOS_ONLY = 0x0001, - FW_RULE_FLAGS2_GAMEOS_ONLY = 0x0002, - FW_RULE_FLAGS2_DEVMODE = 0x0004, - + FW_RULE_FLAGS2_NONE = 0x0000, + FW_RULE_FLAGS2_SYSTEMOS_ONLY = 0x0001, + FW_RULE_FLAGS2_GAMEOS_ONLY = 0x0002, + FW_RULE_FLAGS2_DEVMODE = 0x0004, + FW_RULE_FLAGS2_NOT_USED_VALUE_8 = 0x0008, FW_RULE_FLAGS2_NOT_USED_VALUE_16 = 0x0010, FW_RULE_FLAGS2_NOT_USED_VALUE_32 = 0x0020, @@ -580,8 +581,8 @@ public enum FW_RULE_FLAGS2 : ushort [Flags] public enum FW_OBJECT_CTRL_FLAG : uint { - FW_OBJECT_CTRL_FLAG_NONE = 0x0000, - FW_OBJECT_CTRL_FLAG_INCLUDE_METADATA = 0x0001, + FW_OBJECT_CTRL_FLAG_NONE = 0x0000, + FW_OBJECT_CTRL_FLAG_INCLUDE_METADATA = 0x0001, } [Flags] @@ -613,8 +614,8 @@ public enum FW_ENFORCEMENT_STATE : uint FW_ENFORCEMENT_STATE_TUPLE_RESOLUTION_EMPTY = 23, FW_ENFORCEMENT_STATE_MAX = 24 - }; - + }; + public struct FW_OBJECT_METADATA { ulong qwFilterContextID; // UInt64 @@ -651,7 +652,7 @@ public struct FW_RULE public FW_RULE_ORIGIN_TYPE Origin; public string wszGPOName; public uint Reserved; // FW_OBJECT_CTRL_FLAG - // up to here its windows 7 comatible + // up to here its windows 7 comatible // since Win8 public IntPtr pMetaData; //(Reserved & FW_OBJECT_CTRL_FLAG_INCLUDE_METADATA) ? 1 : 0) @@ -676,7 +677,7 @@ public struct FW_RULE // up to at least 1909 } - + [DllImport("FirewallAPI.dll", CharSet = CharSet.Unicode)] public static extern uint FWEnumFirewallRules(IntPtr hPolicy, FW_RULE_STATUS_CLASS dwFilteredByStatus, FW_PROFILE_TYPE dwProfileFilter, FW_ENUM_RULES_FLAGS wFlags, out uint dwNumRules, out IntPtr fwRules); @@ -705,157 +706,157 @@ public struct FW_RULE public enum FW_RULE_STATUS : uint { - FW_RULE_STATUS_OK = 0x00010000, - FW_RULE_STATUS_PARTIALLY_IGNORED = 0x00020000, - FW_RULE_STATUS_IGNORED = 0x00040000, - FW_RULE_STATUS_PARSING_ERROR_NAME = 0x00080001, - FW_RULE_STATUS_PARSING_ERROR_DESC = 0x00080002, - FW_RULE_STATUS_PARSING_ERROR_APP = 0x00080003, - FW_RULE_STATUS_PARSING_ERROR_SVC = 0x00080004, - FW_RULE_STATUS_PARSING_ERROR_RMA = 0x00080005, - FW_RULE_STATUS_PARSING_ERROR_RUA = 0x00080006, - FW_RULE_STATUS_PARSING_ERROR_EMBD = 0x00080007, - FW_RULE_STATUS_PARSING_ERROR_RULE_ID = 0x00080008, - FW_RULE_STATUS_PARSING_ERROR_PHASE1_AUTH = 0x00080009, - FW_RULE_STATUS_PARSING_ERROR_PHASE2_CRYPTO = 0x0008000A, - FW_RULE_STATUS_PARSING_ERROR_REMOTE_ENDPOINTS = 0x0008000F, - FW_RULE_STATUS_PARSING_ERROR_REMOTE_ENDPOINT_FQDN = 0x00080010, - FW_RULE_STATUS_PARSING_ERROR_KEY_MODULE = 0x00080011, - FW_RULE_STATUS_PARSING_ERROR_PHASE2_AUTH = 0x0008000B, - FW_RULE_STATUS_PARSING_ERROR_RESOLVE_APP = 0x0008000C, - FW_RULE_STATUS_PARSING_ERROR_MAINMODE_ID = 0x0008000D, - FW_RULE_STATUS_PARSING_ERROR_PHASE1_CRYPTO = 0x0008000E, - FW_RULE_STATUS_PARSING_ERROR = 0x00080000, - FW_RULE_STATUS_SEMANTIC_ERROR_RULE_ID = 0x00100010, - FW_RULE_STATUS_SEMANTIC_ERROR_PORTS = 0x00100020, - FW_RULE_STATUS_SEMANTIC_ERROR_PORT_KEYW = 0x00100021, - FW_RULE_STATUS_SEMANTIC_ERROR_PORT_RANGE = 0x00100022, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4_SUBNETS = 0x00100040, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6_SUBNETS = 0x00100041, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4_RANGES = 0x00100042, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6_RANGES = 0x00100043, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_RANGE = 0x00100044, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_MASK = 0x00100045, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_PREFIX = 0x00100046, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_KEYW = 0x00100047, - FW_RULE_STATUS_SEMANTIC_ERROR_LADDR_PROP = 0x00100048, - FW_RULE_STATUS_SEMANTIC_ERROR_RADDR_PROP = 0x00100049, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6 = 0x0010004A, - FW_RULE_STATUS_SEMANTIC_ERROR_LADDR_INTF = 0x0010004B, - FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4 = 0x0010004C, - FW_RULE_STATUS_SEMANTIC_ERROR_TUNNEL_ENDPOINT_ADDR = 0x0010004D, - FW_RULE_STATUS_SEMANTIC_ERROR_DTE_VER = 0x0010004E, - FW_RULE_STATUS_SEMANTIC_ERROR_DTE_MISMATCH_ADDR = 0x0010004F, - FW_RULE_STATUS_SEMANTIC_ERROR_PROFILE = 0x00100050, - FW_RULE_STATUS_SEMANTIC_ERROR_ICMP = 0x00100060, - FW_RULE_STATUS_SEMANTIC_ERROR_ICMP_CODE = 0x00100061, - FW_RULE_STATUS_SEMANTIC_ERROR_IF_ID = 0x00100070, - FW_RULE_STATUS_SEMANTIC_ERROR_IF_TYPE = 0x00100071, - FW_RULE_STATUS_SEMANTIC_ERROR_ACTION = 0x00100080, - FW_RULE_STATUS_SEMANTIC_ERROR_ALLOW_BYPASS = 0x00100081, - FW_RULE_STATUS_SEMANTIC_ERROR_DO_NOT_SECURE = 0x00100082, - FW_RULE_STATUS_SEMANTIC_ERROR_ACTION_BLOCK_IS_ENCRYPTED_SECURE = 0x00100083, - FW_RULE_STATUS_SEMANTIC_ERROR_DIR = 0x00100090, - FW_RULE_STATUS_SEMANTIC_ERROR_PROT = 0x001000A0, - FW_RULE_STATUS_SEMANTIC_ERROR_PROT_PROP = 0x001000A1, - FW_RULE_STATUS_SEMANTIC_ERROR_DEFER_EDGE_PROP = 0x001000A2, - FW_RULE_STATUS_SEMANTIC_ERROR_ALLOW_BYPASS_OUTBOUND = 0x001000A3, - FW_RULE_STATUS_SEMANTIC_ERROR_DEFER_USER_INVALID_RULE = 0x001000A4, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS = 0x001000B0, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_AUTH = 0x001000B1, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_BLOCK = 0x001000B2, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_DYN_RPC = 0x001000B3, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTHENTICATE_ENCRYPT = 0x001000B4, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE_VER = 0x001000B5, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE = 0x001000B6, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ESP_NO_ENCAP_VER = 0x001000B7, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ESP_NO_ENCAP = 0x001000B8, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_TUNNEL_AUTH_MODES_VER = 0x001000B9, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_TUNNEL_AUTH_MODES = 0x001000BA, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_IP_TLS_VER = 0x001000BB, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_PORTRANGE_VER = 0x001000BC, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ADDRS_TRAVERSE_DEFER_VER = 0x001000BD, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE_OUTBOUND = 0x001000BE, - FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTHENTICATE_WITH_OUTBOUND_BYPASS_VER = 0x001000BF, - FW_RULE_STATUS_SEMANTIC_ERROR_REMOTE_AUTH_LIST = 0x001000C0, - FW_RULE_STATUS_SEMANTIC_ERROR_REMOTE_USER_LIST = 0x001000C1, - FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM = 0x001000E0, - FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM_OP_VER = 0x001000E1, - FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM_OP = 0x001000E2, - FW_RULE_STATUS_SEMANTIC_ERROR_DTE_NOANY_ADDR = 0x001000F0, - FW_RULE_STATUS_SEMANTIC_TUNNEL_EXEMPT_WITH_GATEWAY = 0x001000F1, - FW_RULE_STATUS_SEMANTIC_TUNNEL_EXEMPT_VER = 0x001000F2, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_AUTH_SET_ID = 0x00100500, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_SET_ID = 0x00100510, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_SET_ID = 0x00100511, - FW_RULE_STATUS_SEMANTIC_ERROR_SET_ID = 0x00101000, - FW_RULE_STATUS_SEMANTIC_ERROR_IPSEC_PHASE = 0x00101010, - FW_RULE_STATUS_SEMANTIC_ERROR_EMPTY_SUITES = 0x00101020, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_AUTH_METHOD = 0x00101030, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_AUTH_METHOD = 0x00101031, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_ANONYMOUS = 0x00101032, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_DUPLICATE = 0x00101033, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_VER = 0x00101034, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_SUITE_FLAGS = 0x00101040, - FW_RULE_STATUS_SEMANTIC_ERROR_HEALTH_CERT = 0x00101041, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_SIGNCERT_VER = 0x00101042, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_INTERMEDIATE_CA_VER = 0x00101043, - FW_RULE_STATUS_SEMANTIC_ERROR_MACHINE_SHKEY = 0x00101050, - FW_RULE_STATUS_SEMANTIC_ERROR_CA_NAME = 0x00101060, - FW_RULE_STATUS_SEMANTIC_ERROR_MIXED_CERTS = 0x00101061, - FW_RULE_STATUS_SEMANTIC_ERROR_NON_CONTIGUOUS_CERTS = 0x00101062, - FW_RULE_STATUS_SEMANTIC_ERROR_MIXED_CA_TYPE_IN_BLOCK = 0x00101063, - FW_RULE_STATUS_SEMANTIC_ERROR_MACHINE_USER_AUTH = 0x00101070, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_NON_DEFAULT_ID = 0x00105000, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_FLAGS = 0x00105001, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_TIMEOUT_MINUTES = 0x00105002, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_TIMEOUT_SESSIONS = 0x00105003, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_KEY_EXCHANGE = 0x00105004, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_ENCRYPTION = 0x00105005, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_HASH = 0x00105006, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_ENCRYPTION_VER = 0x00105007, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_HASH_VER = 0x00105008, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_PFS = 0x00105020, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_PROTOCOL = 0x00105021, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_ENCRYPTION = 0x00105022, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_HASH = 0x00105023, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_TIMEOUT_MINUTES = 0x00105024, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_TIMEOUT_KBYTES = 0x00105025, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_ENCRYPTION_VER = 0x00105026, - FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_HASH_VER = 0x00105027, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_OR_AND_CONDITIONS = 0x00106000, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_AND_CONDITIONS = 0x00106001, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_KEY = 0x00106002, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_MATCH_TYPE = 0x00106003, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_DATA_TYPE = 0x00106004, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_KEY_AND_DATA_TYPE = 0x00106005, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEYS_PROTOCOL_PORT = 0x00106006, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_PROFILE = 0x00106007, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_STATUS = 0x00106008, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_FILTERID = 0x00106009, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_APP_PATH = 0x00106010, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_PROTOCOL = 0x00106011, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_LOCAL_PORT = 0x00106012, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_REMOTE_PORT = 0x00106013, - FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_SVC_NAME = 0x00106015, - FW_RULE_STATUS_SEMANTIC_ERROR_REQUIRE_IN_CLEAR_OUT_ON_TRANSPORT = 0x00107000, - FW_RULE_STATUS_SEMANTIC_ERROR_TUNNEL_BYPASS_TUNNEL_IF_SECURE_ON_TRANSPORT = 0x00107001, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_NOENCAP_ON_TUNNEL = 0x00107002, - FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_NOENCAP_ON_PSK = 0x00107003, - FW_RULE_STATUS_SEMANTIC_ERROR_CRYPTO_ENCR_HASH = 0x00105040, - FW_RULE_STATUS_SEMANTIC_ERROR_CRYPTO_ENCR_HASH_COMPAT = 0x00105041, - FW_RULE_STATUS_SEMANTIC_ERROR_SCHEMA_VERSION = 0x00105050, - FW_RULE_STATUS_SEMANTIC_ERROR = 0x00100000, - FW_RULE_STATUS_RUNTIME_ERROR_PHASE1_AUTH_NOT_FOUND = 0x00200001, - FW_RULE_STATUS_RUNTIME_ERROR_PHASE2_AUTH_NOT_FOUND = 0x00200002, - FW_RULE_STATUS_RUNTIME_ERROR_PHASE2_CRYPTO_NOT_FOUND = 0x00200003, - FW_RULE_STATUS_RUNTIME_ERROR_AUTH_MCHN_SHKEY_MISMATCH = 0x00200004, - FW_RULE_STATUS_RUNTIME_ERROR_PHASE1_CRYPTO_NOT_FOUND = 0x00200005, - FW_RULE_STATUS_RUNTIME_ERROR_AUTH_NOENCAP_ON_TUNNEL = 0x00200006, - FW_RULE_STATUS_RUNTIME_ERROR_AUTH_NOENCAP_ON_PSK = 0x00200007, - FW_RULE_STATUS_RUNTIME_ERROR = 0x00200000, - FW_RULE_STATUS_ERROR = FW_RULE_STATUS_PARSING_ERROR | FW_RULE_STATUS_SEMANTIC_ERROR | FW_RULE_STATUS_RUNTIME_ERROR, - FW_RULE_STATUS_ALL = 0xFFFF0000 + FW_RULE_STATUS_OK = 0x00010000, + FW_RULE_STATUS_PARTIALLY_IGNORED = 0x00020000, + FW_RULE_STATUS_IGNORED = 0x00040000, + FW_RULE_STATUS_PARSING_ERROR_NAME = 0x00080001, + FW_RULE_STATUS_PARSING_ERROR_DESC = 0x00080002, + FW_RULE_STATUS_PARSING_ERROR_APP = 0x00080003, + FW_RULE_STATUS_PARSING_ERROR_SVC = 0x00080004, + FW_RULE_STATUS_PARSING_ERROR_RMA = 0x00080005, + FW_RULE_STATUS_PARSING_ERROR_RUA = 0x00080006, + FW_RULE_STATUS_PARSING_ERROR_EMBD = 0x00080007, + FW_RULE_STATUS_PARSING_ERROR_RULE_ID = 0x00080008, + FW_RULE_STATUS_PARSING_ERROR_PHASE1_AUTH = 0x00080009, + FW_RULE_STATUS_PARSING_ERROR_PHASE2_CRYPTO = 0x0008000A, + FW_RULE_STATUS_PARSING_ERROR_REMOTE_ENDPOINTS = 0x0008000F, + FW_RULE_STATUS_PARSING_ERROR_REMOTE_ENDPOINT_FQDN = 0x00080010, + FW_RULE_STATUS_PARSING_ERROR_KEY_MODULE = 0x00080011, + FW_RULE_STATUS_PARSING_ERROR_PHASE2_AUTH = 0x0008000B, + FW_RULE_STATUS_PARSING_ERROR_RESOLVE_APP = 0x0008000C, + FW_RULE_STATUS_PARSING_ERROR_MAINMODE_ID = 0x0008000D, + FW_RULE_STATUS_PARSING_ERROR_PHASE1_CRYPTO = 0x0008000E, + FW_RULE_STATUS_PARSING_ERROR = 0x00080000, + FW_RULE_STATUS_SEMANTIC_ERROR_RULE_ID = 0x00100010, + FW_RULE_STATUS_SEMANTIC_ERROR_PORTS = 0x00100020, + FW_RULE_STATUS_SEMANTIC_ERROR_PORT_KEYW = 0x00100021, + FW_RULE_STATUS_SEMANTIC_ERROR_PORT_RANGE = 0x00100022, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4_SUBNETS = 0x00100040, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6_SUBNETS = 0x00100041, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4_RANGES = 0x00100042, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6_RANGES = 0x00100043, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_RANGE = 0x00100044, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_MASK = 0x00100045, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_PREFIX = 0x00100046, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_KEYW = 0x00100047, + FW_RULE_STATUS_SEMANTIC_ERROR_LADDR_PROP = 0x00100048, + FW_RULE_STATUS_SEMANTIC_ERROR_RADDR_PROP = 0x00100049, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V6 = 0x0010004A, + FW_RULE_STATUS_SEMANTIC_ERROR_LADDR_INTF = 0x0010004B, + FW_RULE_STATUS_SEMANTIC_ERROR_ADDR_V4 = 0x0010004C, + FW_RULE_STATUS_SEMANTIC_ERROR_TUNNEL_ENDPOINT_ADDR = 0x0010004D, + FW_RULE_STATUS_SEMANTIC_ERROR_DTE_VER = 0x0010004E, + FW_RULE_STATUS_SEMANTIC_ERROR_DTE_MISMATCH_ADDR = 0x0010004F, + FW_RULE_STATUS_SEMANTIC_ERROR_PROFILE = 0x00100050, + FW_RULE_STATUS_SEMANTIC_ERROR_ICMP = 0x00100060, + FW_RULE_STATUS_SEMANTIC_ERROR_ICMP_CODE = 0x00100061, + FW_RULE_STATUS_SEMANTIC_ERROR_IF_ID = 0x00100070, + FW_RULE_STATUS_SEMANTIC_ERROR_IF_TYPE = 0x00100071, + FW_RULE_STATUS_SEMANTIC_ERROR_ACTION = 0x00100080, + FW_RULE_STATUS_SEMANTIC_ERROR_ALLOW_BYPASS = 0x00100081, + FW_RULE_STATUS_SEMANTIC_ERROR_DO_NOT_SECURE = 0x00100082, + FW_RULE_STATUS_SEMANTIC_ERROR_ACTION_BLOCK_IS_ENCRYPTED_SECURE = 0x00100083, + FW_RULE_STATUS_SEMANTIC_ERROR_DIR = 0x00100090, + FW_RULE_STATUS_SEMANTIC_ERROR_PROT = 0x001000A0, + FW_RULE_STATUS_SEMANTIC_ERROR_PROT_PROP = 0x001000A1, + FW_RULE_STATUS_SEMANTIC_ERROR_DEFER_EDGE_PROP = 0x001000A2, + FW_RULE_STATUS_SEMANTIC_ERROR_ALLOW_BYPASS_OUTBOUND = 0x001000A3, + FW_RULE_STATUS_SEMANTIC_ERROR_DEFER_USER_INVALID_RULE = 0x001000A4, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS = 0x001000B0, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_AUTH = 0x001000B1, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_BLOCK = 0x001000B2, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTO_DYN_RPC = 0x001000B3, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTHENTICATE_ENCRYPT = 0x001000B4, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE_VER = 0x001000B5, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE = 0x001000B6, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ESP_NO_ENCAP_VER = 0x001000B7, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ESP_NO_ENCAP = 0x001000B8, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_TUNNEL_AUTH_MODES_VER = 0x001000B9, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_TUNNEL_AUTH_MODES = 0x001000BA, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_IP_TLS_VER = 0x001000BB, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_PORTRANGE_VER = 0x001000BC, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_ADDRS_TRAVERSE_DEFER_VER = 0x001000BD, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTH_WITH_ENC_NEGOTIATE_OUTBOUND = 0x001000BE, + FW_RULE_STATUS_SEMANTIC_ERROR_FLAGS_AUTHENTICATE_WITH_OUTBOUND_BYPASS_VER = 0x001000BF, + FW_RULE_STATUS_SEMANTIC_ERROR_REMOTE_AUTH_LIST = 0x001000C0, + FW_RULE_STATUS_SEMANTIC_ERROR_REMOTE_USER_LIST = 0x001000C1, + FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM = 0x001000E0, + FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM_OP_VER = 0x001000E1, + FW_RULE_STATUS_SEMANTIC_ERROR_PLATFORM_OP = 0x001000E2, + FW_RULE_STATUS_SEMANTIC_ERROR_DTE_NOANY_ADDR = 0x001000F0, + FW_RULE_STATUS_SEMANTIC_TUNNEL_EXEMPT_WITH_GATEWAY = 0x001000F1, + FW_RULE_STATUS_SEMANTIC_TUNNEL_EXEMPT_VER = 0x001000F2, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_AUTH_SET_ID = 0x00100500, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_SET_ID = 0x00100510, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_SET_ID = 0x00100511, + FW_RULE_STATUS_SEMANTIC_ERROR_SET_ID = 0x00101000, + FW_RULE_STATUS_SEMANTIC_ERROR_IPSEC_PHASE = 0x00101010, + FW_RULE_STATUS_SEMANTIC_ERROR_EMPTY_SUITES = 0x00101020, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_AUTH_METHOD = 0x00101030, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_AUTH_METHOD = 0x00101031, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_ANONYMOUS = 0x00101032, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_DUPLICATE = 0x00101033, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_METHOD_VER = 0x00101034, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_SUITE_FLAGS = 0x00101040, + FW_RULE_STATUS_SEMANTIC_ERROR_HEALTH_CERT = 0x00101041, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_SIGNCERT_VER = 0x00101042, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_INTERMEDIATE_CA_VER = 0x00101043, + FW_RULE_STATUS_SEMANTIC_ERROR_MACHINE_SHKEY = 0x00101050, + FW_RULE_STATUS_SEMANTIC_ERROR_CA_NAME = 0x00101060, + FW_RULE_STATUS_SEMANTIC_ERROR_MIXED_CERTS = 0x00101061, + FW_RULE_STATUS_SEMANTIC_ERROR_NON_CONTIGUOUS_CERTS = 0x00101062, + FW_RULE_STATUS_SEMANTIC_ERROR_MIXED_CA_TYPE_IN_BLOCK = 0x00101063, + FW_RULE_STATUS_SEMANTIC_ERROR_MACHINE_USER_AUTH = 0x00101070, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_NON_DEFAULT_ID = 0x00105000, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_FLAGS = 0x00105001, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_TIMEOUT_MINUTES = 0x00105002, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_TIMEOUT_SESSIONS = 0x00105003, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_KEY_EXCHANGE = 0x00105004, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_ENCRYPTION = 0x00105005, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_HASH = 0x00105006, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_ENCRYPTION_VER = 0x00105007, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE1_CRYPTO_HASH_VER = 0x00105008, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_PFS = 0x00105020, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_PROTOCOL = 0x00105021, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_ENCRYPTION = 0x00105022, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_HASH = 0x00105023, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_TIMEOUT_MINUTES = 0x00105024, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_TIMEOUT_KBYTES = 0x00105025, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_ENCRYPTION_VER = 0x00105026, + FW_RULE_STATUS_SEMANTIC_ERROR_PHASE2_CRYPTO_HASH_VER = 0x00105027, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_OR_AND_CONDITIONS = 0x00106000, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_AND_CONDITIONS = 0x00106001, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_KEY = 0x00106002, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_MATCH_TYPE = 0x00106003, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_DATA_TYPE = 0x00106004, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_CONDITION_KEY_AND_DATA_TYPE = 0x00106005, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEYS_PROTOCOL_PORT = 0x00106006, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_PROFILE = 0x00106007, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_STATUS = 0x00106008, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_FILTERID = 0x00106009, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_APP_PATH = 0x00106010, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_PROTOCOL = 0x00106011, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_LOCAL_PORT = 0x00106012, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_REMOTE_PORT = 0x00106013, + FW_RULE_STATUS_SEMANTIC_ERROR_QUERY_KEY_SVC_NAME = 0x00106015, + FW_RULE_STATUS_SEMANTIC_ERROR_REQUIRE_IN_CLEAR_OUT_ON_TRANSPORT = 0x00107000, + FW_RULE_STATUS_SEMANTIC_ERROR_TUNNEL_BYPASS_TUNNEL_IF_SECURE_ON_TRANSPORT = 0x00107001, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_NOENCAP_ON_TUNNEL = 0x00107002, + FW_RULE_STATUS_SEMANTIC_ERROR_AUTH_NOENCAP_ON_PSK = 0x00107003, + FW_RULE_STATUS_SEMANTIC_ERROR_CRYPTO_ENCR_HASH = 0x00105040, + FW_RULE_STATUS_SEMANTIC_ERROR_CRYPTO_ENCR_HASH_COMPAT = 0x00105041, + FW_RULE_STATUS_SEMANTIC_ERROR_SCHEMA_VERSION = 0x00105050, + FW_RULE_STATUS_SEMANTIC_ERROR = 0x00100000, + FW_RULE_STATUS_RUNTIME_ERROR_PHASE1_AUTH_NOT_FOUND = 0x00200001, + FW_RULE_STATUS_RUNTIME_ERROR_PHASE2_AUTH_NOT_FOUND = 0x00200002, + FW_RULE_STATUS_RUNTIME_ERROR_PHASE2_CRYPTO_NOT_FOUND = 0x00200003, + FW_RULE_STATUS_RUNTIME_ERROR_AUTH_MCHN_SHKEY_MISMATCH = 0x00200004, + FW_RULE_STATUS_RUNTIME_ERROR_PHASE1_CRYPTO_NOT_FOUND = 0x00200005, + FW_RULE_STATUS_RUNTIME_ERROR_AUTH_NOENCAP_ON_TUNNEL = 0x00200006, + FW_RULE_STATUS_RUNTIME_ERROR_AUTH_NOENCAP_ON_PSK = 0x00200007, + FW_RULE_STATUS_RUNTIME_ERROR = 0x00200000, + FW_RULE_STATUS_ERROR = FW_RULE_STATUS_PARSING_ERROR | FW_RULE_STATUS_SEMANTIC_ERROR | FW_RULE_STATUS_RUNTIME_ERROR, + FW_RULE_STATUS_ALL = 0xFFFF0000 } //////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1120,13 +1121,13 @@ public enum FW_CS_RULE_ACTION [Flags] public enum FW_CS_RULE_FLAGS : ushort { - FW_CS_RULE_FLAGS_NONE = 0x00, - FW_CS_RULE_FLAGS_ACTIVE = 0x01, - FW_CS_RULE_FLAGS_DTM = 0x02, + FW_CS_RULE_FLAGS_NONE = 0x00, + FW_CS_RULE_FLAGS_ACTIVE = 0x01, + FW_CS_RULE_FLAGS_DTM = 0x02, FW_CS_RULE_FLAGS_TUNNEL_BYPASS_IF_ENCRYPTED = 0x08, FW_CS_RULE_FLAGS_OUTBOUND_CLEAR = 0x10, - FW_CS_RULE_FLAGS_APPLY_AUTHZ = 0x20, - + FW_CS_RULE_FLAGS_APPLY_AUTHZ = 0x20, + FW_CS_RULE_FLAGS_MAX_V2_1 = FW_CS_RULE_FLAGS_DTM, FW_CS_RULE_FLAGS_MAX_V2_8 = 0x04, FW_CS_RULE_FLAGS_MAX = 0x40 @@ -1288,10 +1289,10 @@ public struct FW_CRYPTO_SET_PHASE2 public static extern uint FWDeleteAllCryptoSets(IntPtr hPolicy, FW_IPSEC_PHASE IpSecPhase); [DllImport("FirewallAPI.dll", CharSet = CharSet.Unicode)] - public static extern uint FWVerifyCryptoSet(ushort wBinaryVersion, ref FW_CRYPTO_SET_PHASE1 pCryptoSet); + public static extern uint FWVerifyCryptoSet(ushort wBinaryVersion, ref FW_CRYPTO_SET_PHASE1 pCryptoSet); [DllImport("FirewallAPI.dll", CharSet = CharSet.Unicode)] - public static extern uint FWVerifyCryptoSet(ushort wBinaryVersion, ref FW_CRYPTO_SET_PHASE2 pCryptoSet); + public static extern uint FWVerifyCryptoSet(ushort wBinaryVersion, ref FW_CRYPTO_SET_PHASE2 pCryptoSet); [DllImport("FirewallAPI.dll", CharSet = CharSet.Unicode)] public static extern uint FWFreeCryptoSets(IntPtr pCryptoSets); @@ -1352,7 +1353,7 @@ public enum FW_CRYPTO_ENCRYPTION_TYPE FW_CRYPTO_ENCRYPTION_AES_GCM128 = 6, FW_CRYPTO_ENCRYPTION_AES_GCM192 = 7, FW_CRYPTO_ENCRYPTION_AES_GCM256 = 8, - + FW_CRYPTO_ENCRYPTION_MAX_V2_0 = FW_CRYPTO_ENCRYPTION_AES_GCM128, FW_CRYPTO_ENCRYPTION_MAX = 9 } @@ -1367,7 +1368,7 @@ public enum FW_CRYPTO_HASH_TYPE FW_CRYPTO_HASH_AES_GMAC128 = 5, FW_CRYPTO_HASH_AES_GMAC192 = 6, FW_CRYPTO_HASH_AES_GMAC256 = 7, - + FW_CRYPTO_HASH_MAX_V2_0 = FW_CRYPTO_HASH_SHA256, FW_CRYPTO_HASH_MAX = 8 } @@ -1418,7 +1419,7 @@ public enum FW_CRYPTO_PROTOCOL_TYPE FW_CRYPTO_PROTOCOL_ESP = 2, FW_CRYPTO_PROTOCOL_BOTH = 3, FW_CRYPTO_PROTOCOL_AUTH_NO_ENCAP = 4, - + FW_CRYPTO_PROTOCOL_MAX_2_1 = (FW_CRYPTO_PROTOCOL_BOTH + 1), FW_CRYPTO_PROTOCOL_MAX = 5 } @@ -1462,38 +1463,38 @@ public struct FW_PHASE2_SA_DETAILS // /* todo: - - typedef struct _tag_FW_MM_RULE - { - struct _tag_FW_MM_RULE *pNext; - USHORT wSchemaVersion; - //[string, range(1,512), ref] - PWCHAR wszRuleId; - //[string, range(1,10001)] - PWCHAR wszName; - //[string, range(1,10001)] - PWCHAR wszDescription; - FW_PROFILE_TYPE dwProfiles; - FW_ADDRESSES Endpoint1; - FW_ADDRESSES Endpoint2; - //[string, range(1,255)] - PWCHAR wszPhase1AuthSet; - //[string, range(1,255)] - PWCHAR wszPhase1CryptoSet; - USHORT wFlags; - //[string, range(1,10001)] - PWCHAR wszEmbeddedContext; - FW_OS_PLATFORM_LIST PlatformValidityList; - //[range(FW_RULE_ORIGIN_INVALID, FW_RULE_ORIGIN_MAX-1)] - FW_RULE_ORIGIN_TYPE Origin; - //[string, range(1,10001)] - PWCHAR wszGPOName; - FW_RULE_STATUS Status; - ULONG Reserved; - //[size_is((Reserved & FW_OBJECT_CTRL_FLAG_INCLUDE_METADATA) ? 1 : 0)] - PFW_OBJECT_METADATA pMetaData; + + typedef struct _tag_FW_MM_RULE + { + struct _tag_FW_MM_RULE *pNext; + USHORT wSchemaVersion; + //[string, range(1,512), ref] + PWCHAR wszRuleId; + //[string, range(1,10001)] + PWCHAR wszName; + //[string, range(1,10001)] + PWCHAR wszDescription; + FW_PROFILE_TYPE dwProfiles; + FW_ADDRESSES Endpoint1; + FW_ADDRESSES Endpoint2; + //[string, range(1,255)] + PWCHAR wszPhase1AuthSet; + //[string, range(1,255)] + PWCHAR wszPhase1CryptoSet; + USHORT wFlags; + //[string, range(1,10001)] + PWCHAR wszEmbeddedContext; + FW_OS_PLATFORM_LIST PlatformValidityList; + //[range(FW_RULE_ORIGIN_INVALID, FW_RULE_ORIGIN_MAX-1)] + FW_RULE_ORIGIN_TYPE Origin; + //[string, range(1,10001)] + PWCHAR wszGPOName; + FW_RULE_STATUS Status; + ULONG Reserved; + //[size_is((Reserved & FW_OBJECT_CTRL_FLAG_INCLUDE_METADATA) ? 1 : 0)] + PFW_OBJECT_METADATA pMetaData; } FW_MM_RULE, *PFW_MM_RULE; - + typedef ULONG(WINAPI* _FWEnumMainModeRules)(_In_ FW_POLICY_STORE_HANDLE hPolicy, _In_ FW_RULE_STATUS_CLASS dwFilteredByStatus, _In_ FW_PROFILE_TYPE dwProfileFilter, _In_ FW_ENUM_RULES_FLAGS wFlags, __out PULONG pdwNumRules, __out_ecount(pdwNumRules) PFW_MM_RULE* ppMMRules); @@ -1514,4 +1515,4 @@ typedef ULONG(WINAPI* _FWQueryMainModeRules)(_In_ FW_POLICY_STORE_HANDLE hPolicy */ } -} +} \ No newline at end of file diff --git a/MiscHelpers/Common/AppLog.cs b/MiscHelpers/Common/AppLog.cs new file mode 100644 index 0000000..0e484ff --- /dev/null +++ b/MiscHelpers/Common/AppLog.cs @@ -0,0 +1,274 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.Eventing.Reader; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Threading; + +namespace MiscHelpers +{ + public class AppLog : IDisposable + { + static private AppLog mInstance = null; + public static AppLog GetInstance() { return mInstance; } + + private int mLogLimit = 200; + private string mLogName = null; + private static ReaderWriterLockSlim mLocker = new ReaderWriterLockSlim(); + + EventLogWatcher mEventWatcher = null; + + public struct LogEntry + { + public EventLogEntryType entryType; + public short categoryID; + public long eventID; + public string strMessage; + public Dictionary Params; + //public byte[] binData; + public DateTime timeGenerated; + + public void SetData(string[] dataStr) + { + strMessage = dataStr[0]; + if (dataStr.Length < 2) + return; + + var keys = dataStr[1].Split('|'); + if (keys.Length != dataStr.Length - 2) + return; + + Params = new Dictionary(); + for (int i = 2; i < dataStr.Length; i++) + Params.Add(keys[i - 2], dataStr[i]); + } + + public object[] GetData() + { + if (Params == null || Params.Count == 0) + return new string[] { strMessage }; + + string[] dataStr; + dataStr = new string[Params.Count + 2]; + dataStr[0] = strMessage; // first entry is the message text + dataStr[1] = string.Join("|", Params.Keys); // second entry is the list of parameter names that follow + + int i = 2; + foreach (var str in Params.Values) + dataStr[i++] = str; + + return dataStr; + } + } + + private List mLogList = null; + + public class LogEventArgs : EventArgs + { + public LogEntry entry; + + public LogEventArgs(LogEntry entry) + { + this.entry = entry; + } + }; + + public event EventHandler LogEvent; + + public AppLog(string LogName = null) + { + mInstance = this; + if (LogName != null && EventLog.Exists(LogName)) + mLogName = LogName; + } + + public void Dispose() + { + if (mEventWatcher != null) + { + mEventWatcher.EventRecordWritten -= new EventHandler(OnLogEntry); + mEventWatcher.Dispose(); + mEventWatcher = null; + } + } + + public void EnableLogging() + { + mLogList = new List(); + + if (mLogName != null) + { + mEventWatcher = new EventLogWatcher(new EventLogQuery(mLogName, PathType.LogName)); + mEventWatcher.EventRecordWritten += new EventHandler(OnLogEntry); + mEventWatcher.Enabled = true; + } + } + + public bool SetupEventLog(string LogName) + { + if (mLogName != null) + return true; + + try + { + if (!EventLog.SourceExists(LogName)) + EventLog.CreateEventSource(LogName, LogName); + mLogName = LogName; + return true; + } + catch { } + return false; + } + + public void RemoveEventLog(string LogName) + { + try { EventLog.Delete(LogName); } catch { } + try { EventLog.DeleteEventSource(LogName); } catch { } + } + + public bool UsingEventLog() + { + return mLogName != null; + } + + public static void Add(EventLogEntryType entryType, long eventID, short categoryID, string strMessage, Dictionary Params = null/*, byte[] binData = null*/) + { + Console.WriteLine("Log: " + strMessage); + + if (mInstance != null) + mInstance.AddLogEntry(entryType, eventID, categoryID, strMessage, Params/*, binData*/); + } + + private void AddLogEntry(EventLogEntryType entryType, long eventID, short categoryID, string strMessage, Dictionary Params = null/*, byte[] binData = null*/) + { + LogEntry Entry = new LogEntry(); + Entry.entryType = entryType; + Entry.categoryID = categoryID; + Entry.eventID = eventID; + Entry.strMessage = strMessage; + Entry.Params = Params; + //Entry.binData = binData; + Entry.timeGenerated = DateTime.Now; + + if (mLogName == null) + AddToLog(Entry); + else + EventLog.WriteEvent(mLogName, new EventInstance(Entry.eventID, Entry.categoryID, Entry.entryType), /*Entry.binData,*/ Entry.GetData()); + } + + private void AddToLog(LogEntry Entry) + { + LogEvent?.Invoke(this, new LogEventArgs(Entry)); + + if (mLogList == null) + return; + + mLocker.EnterWriteLock(); + mLogList.Add(Entry); + while (mLogList.Count > mLogLimit) + mLogList.RemoveAt(0); + mLocker.ExitWriteLock(); + } + + private void OnLogEntry(object obj, EventRecordWrittenEventArgs arg) + { + if (arg.EventRecord == null || arg.EventRecord.Properties.Count == 0) + return; + + try + { + LogEntry Entry = new LogEntry(); + Entry.eventID = arg.EventRecord.Id; + Entry.categoryID = (short)arg.EventRecord.Task; + switch (arg.EventRecord.Level.Value) + { + case 2: Entry.entryType = EventLogEntryType.Error; break; + case 3: Entry.entryType = EventLogEntryType.Warning; break; + case 4: + default: Entry.entryType = EventLogEntryType.Information; break; + } + Entry.timeGenerated = arg.EventRecord.TimeCreated.Value; + string[] dataStr = new string[arg.EventRecord.Properties.Count]; + for (int i = 0; i < arg.EventRecord.Properties.Count; i++) + dataStr[i] = arg.EventRecord.Properties[i].Value.ToString(); + Entry.SetData(dataStr); + //Entry.binData = + + AddToLog(Entry); + } + catch { } + } + + public void LoadLog() + { + if (mLogName == null) + return; + + mLocker.EnterWriteLock(); + try + { + mLogList = new List(); + + EventLog eventLog = new EventLog(mLogName); + for (int idx = (eventLog.Entries.Count > mLogLimit ? eventLog.Entries.Count - mLogLimit : 0); idx < eventLog.Entries.Count; idx++) + { + EventLogEntry logEntry = eventLog.Entries[idx]; + + if (logEntry.ReplacementStrings.Length == 0) + continue; + + LogEntry Entry = new LogEntry(); + Entry.eventID = logEntry.InstanceId; + Entry.categoryID = logEntry.CategoryNumber; + Entry.entryType = logEntry.EntryType; + Entry.timeGenerated = logEntry.TimeGenerated; + Entry.SetData(logEntry.ReplacementStrings); + //Entry.binData = logEntry.Data; + + mLogList.Add(Entry); + } + } + catch { } + mLocker.ExitWriteLock(); + } + + public List GetFullLog() + { + List log = null; + if (mLogList != null) + { + mLocker.EnterReadLock(); + log = new List(mLogList); + mLocker.ExitReadLock(); + } + return log; + } + + static public long ExceptionLogID = 0; + static public long ExceptionCategory = 0; + + static public void Exception(Exception ex) + { +#if DEBUG + Debugger.Break(); +#endif + + var st = new StackTrace(); + var sf = st.GetFrame(1); + var name = sf.GetMethod().Name; + + String message = "Exception in " + name + ": " + ex.Message; + Dictionary values = new Dictionary(); + values.Add("Exception", ex.ToString()); + Add(EventLogEntryType.Error, ExceptionLogID, (short)ExceptionCategory, message, values); + } + + static public void Debug(string message, params object[] args) + { + Console.WriteLine(args.Length == 0 ? message : string.Format(message, args)); + } + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/ClonableDictionary.cs b/MiscHelpers/Common/ClonableDictionary.cs new file mode 100644 index 0000000..5fba19b --- /dev/null +++ b/MiscHelpers/Common/ClonableDictionary.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + public class CloneableDictionary : Dictionary + { + public CloneableDictionary Clone() // shallow copy! + { + CloneableDictionary clone = new CloneableDictionary(); + foreach (KeyValuePair kvp in this) + { + clone.Add(kvp.Key, kvp.Value); + } + return clone; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/ClonableList.cs b/MiscHelpers/Common/ClonableList.cs new file mode 100644 index 0000000..bdadb16 --- /dev/null +++ b/MiscHelpers/Common/ClonableList.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + public class CloneableList : List + { + public CloneableList Clone() // shallow copy! + { + CloneableList clone = new CloneableList(); + foreach (TValue v in this) + { + clone.Add(v); + } + return clone; + } + } +} \ No newline at end of file diff --git a/PrivateWin10/Common/DataGridExt.cs b/MiscHelpers/Common/DataGridExt.cs similarity index 96% rename from PrivateWin10/Common/DataGridExt.cs rename to MiscHelpers/Common/DataGridExt.cs index 9bd9103..47e0956 100644 --- a/PrivateWin10/Common/DataGridExt.cs +++ b/MiscHelpers/Common/DataGridExt.cs @@ -8,7 +8,7 @@ using System.Windows.Controls.Primitives; using System.Windows.Input; -namespace PrivateWin10 +namespace MiscHelpers { public class DataGridExt { diff --git a/MiscHelpers/Common/HttpTask.cs b/MiscHelpers/Common/HttpTask.cs new file mode 100644 index 0000000..68cd9ae --- /dev/null +++ b/MiscHelpers/Common/HttpTask.cs @@ -0,0 +1,365 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Threading; + +namespace MiscHelpers +{ + + public class HttpTask + { + //const int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout + const int BUFFER_SIZE = 1024; + private byte[] BufferRead; + private HttpWebRequest request; + private HttpWebResponse response; + private Stream streamResponse; + private Stream streamWriter; + private Dispatcher mDispatcher; + private string mUrl; + private string mDlPath; + private string mDlName; + private int mLength = -1; + private int mOffset = -1; + private bool Canceled = false; + private DateTime lastTime; + + public string DlUrl { get { return mUrl; } } + public string DlPath { get { return mDlPath; } } + public string DlName { get { return mDlName; } } + + public HttpTask(string Url, string DlPath, string DlName = null) + { + mUrl = Url; + mDlPath = DlPath; + mDlName = DlName; + + BufferRead = null; + request = null; + response = null; + streamResponse = null; + streamWriter = null; + mDispatcher = Dispatcher.CurrentDispatcher; + } + + // Abort the request if the timer fires. + /*private static void TimeoutCallback(object state, bool timedOut) + { + if (timedOut) + { + HttpWebRequest request = state as HttpWebRequest; + if (request != null) + request.Abort(); + } + }*/ + + public bool Start() + { + Canceled = false; + try + { + // Create a HttpWebrequest object to the desired URL. + request = (HttpWebRequest)WebRequest.Create(mUrl); + //myHttpWebRequest.AllowAutoRedirect = false; + + /** + * If you are behind a firewall and you do not have your browser proxy setup + * you need to use the following proxy creation code. + + // Create a proxy object. + WebProxy myProxy = new WebProxy(); + + // Associate a new Uri object to the _wProxy object, using the proxy address + // selected by the user. + myProxy.Address = new Uri("http://myproxy"); + + + // Finally, initialize the Web request object proxy property with the _wProxy + // object. + myHttpWebRequest.Proxy=myProxy; + ***/ + + BufferRead = new byte[BUFFER_SIZE]; + mOffset = 0; + + // Start the asynchronous request. + IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), this); + + // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted + //ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true); + return true; + } + catch (Exception e) + { + Console.WriteLine("\nMain Exception raised!"); + Console.WriteLine("\nMessage:{0}", e.Message); + } + return false; + } + + public void Cancel() + { + Canceled = true; + if (request != null) + request.Abort(); + } + + private void Finish(int Success, int ErrCode, Exception Error = null) + { + // Release the HttpWebResponse resource. + if (response != null) + { + response.Close(); + if (streamResponse != null) + streamResponse.Close(); + if (streamWriter != null) + streamWriter.Close(); + + } + response = null; + request = null; + streamResponse = null; + BufferRead = null; + + if (Success == 1) + { + try + { + if (File.Exists(mDlPath + @"\" + mDlName)) + File.Delete(mDlPath + @"\" + mDlName); + File.Move(mDlPath + @"\" + mDlName + ".tmp", mDlPath + @"\" + mDlName); + } + catch + { + AppLog.Debug("Failed to rename download {0}", mDlPath + @"\" + mDlName + ".tmp"); + mDlName += ".tmp"; + } + + try { File.SetLastWriteTime(mDlPath + @"\" + mDlName, lastTime); } catch { } // set last mod time + } + else if (Success == 2) + { + AppLog.Debug("File already downloaded {0}", mDlPath + @"\" + mDlName); + } + else + { + try { File.Delete(mDlPath + @"\" + mDlName + ".tmp"); } catch { } // delete partial file + AppLog.Debug("Failed to download file {0}", mDlPath + @"\" + mDlName); + } + + Finished?.Invoke(this, new FinishedEventArgs(Success > 0 ? 0 : Canceled ? -1 : ErrCode, Error)); + } + + static public string GetNextTempFile(string path, string baseName) + { + for (int i = 0; i < 10000; i++) + { + if (!File.Exists(path + @"\" + baseName + "_" + i + ".tmp")) + return baseName + "_" + i; + } + return baseName; + } + + private static void RespCallback(IAsyncResult asynchronousResult) + { + int Success = 0; + int ErrCode = 0; + Exception Error = null; + HttpTask task = (HttpTask)asynchronousResult.AsyncState; + try + { + // State of request is asynchronous. + task.response = (HttpWebResponse)task.request.EndGetResponse(asynchronousResult); + + ErrCode = (int)task.response.StatusCode; + + Console.WriteLine("The server at {0} returned {1}", task.response.ResponseUri, task.response.StatusCode); + + string fileName = Path.GetFileName(task.response.ResponseUri.ToString()); + task.lastTime = DateTime.Now; + + Console.WriteLine("With headers:"); + foreach (string key in task.response.Headers.AllKeys) + { + Console.WriteLine("\t{0}:{1}", key, task.response.Headers[key]); + + if (key.Equals("Content-Length", StringComparison.CurrentCultureIgnoreCase)) + { + task.mLength = int.Parse(task.response.Headers[key]); + } + else if (key.Equals("Content-Disposition", StringComparison.CurrentCultureIgnoreCase)) + { + string cd = task.response.Headers[key]; + fileName = cd.Substring(cd.IndexOf("filename=") + 9).Replace("\"", ""); + } + else if (key.Equals("Last-Modified", StringComparison.CurrentCultureIgnoreCase)) + { + task.lastTime = DateTime.Parse(task.response.Headers[key]); + } + } + + //Console.WriteLine(task.lastTime); + + if (task.mDlName == null) + task.mDlName = fileName; + + FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName); + if (testInfo.Exists && testInfo.LastWriteTime == task.lastTime && testInfo.Length == task.mLength) + { + task.request.Abort(); + Success = 2; + } + else + { + // prepare download filename + if (!Directory.Exists(task.mDlPath)) + Directory.CreateDirectory(task.mDlPath); + if (task.mDlName.Length == 0 || task.mDlName[0] == '?') + task.mDlName = GetNextTempFile(task.mDlPath, "Download"); + + FileInfo info = new FileInfo(task.mDlPath + @"\" + task.mDlName + ".tmp"); + if (info.Exists) + info.Delete(); + + // Read the response into a Stream object. + task.streamResponse = task.response.GetResponseStream(); + + task.streamWriter = info.OpenWrite(); + + // Begin the Reading of the contents of the HTML page and print it to the console. + task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task); + return; + } + } + catch (WebException e) + { + if (e.Response != null) + { + string fileName = Path.GetFileName(e.Response.ResponseUri.AbsolutePath.ToString()); + + if (task.mDlName == null) + task.mDlName = fileName; + + FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName); + if (testInfo.Exists) + Success = 2; + } + + if (Success == 0) + { + ErrCode = -2; + Error = e; + Console.WriteLine("\nRespCallback Exception raised!"); + Console.WriteLine("\nMessage:{0}", e.Message); + Console.WriteLine("\nStatus:{0}", e.Status); + } + } + catch (Exception e) + { + ErrCode = -2; + Error = e; + Console.WriteLine("\nRespCallback Exception raised!"); + Console.WriteLine("\nMessage:{0}", e.Message); + } + task.mDispatcher.Invoke(new Action(() => + { + task.Finish(Success, ErrCode, Error); + })); + } + + private int mOldPercent = -1; + + private static void ReadCallBack(IAsyncResult asyncResult) + { + int Success = 0; + int ErrCode = 0; + Exception Error = null; + HttpTask task = (HttpTask)asyncResult.AsyncState; + try + { + int read = task.streamResponse.EndRead(asyncResult); + // Read the HTML page and then print it to the console. + if (read > 0) + { + task.streamWriter.Write(task.BufferRead, 0, read); + task.mOffset += read; + + int Percent = task.mLength > 0 ? (int)((Int64)100 * task.mOffset / task.mLength) : -1; + if (Percent != task.mOldPercent) + { + task.mOldPercent = Percent; + task.mDispatcher.Invoke(new Action(() => + { + task.Progress?.Invoke(task, new ProgressEventArgs(Percent)); + })); + } + + // setup next read + task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task); + return; + } + else + { + // this is done on finisch + //task.streamWriter.Close(); + //task.streamResponse.Close(); + Success = 1; + } + + } + catch (Exception e) + { + ErrCode = -3; + Error = e; + Console.WriteLine("\nReadCallBack Exception raised!"); + Console.WriteLine("\nMessage:{0}", e.Message); + } + task.mDispatcher.Invoke(new Action(() => + { + task.Finish(Success, ErrCode, Error); + })); + } + + public class FinishedEventArgs : EventArgs + { + public FinishedEventArgs(int ErrCode = 0, Exception Error = null) + { + this.ErrCode = ErrCode; + this.Error = Error; + } + public string GetError() + { + if (Error != null) + return Error.ToString(); + switch (ErrCode) + { + case 0: return "Ok"; + case -1: return "Canceled"; + default: return ErrCode.ToString(); + } + } + public bool Success { get { return ErrCode == 0; } } + public bool Cancelled { get { return ErrCode == -1; } } + + public int ErrCode = 0; + public Exception Error = null; + } + public event EventHandler Finished; + + public class ProgressEventArgs : EventArgs + { + public ProgressEventArgs(int Percent) + { + this.Percent = Percent; + } + public int Percent = 0; + } + public event EventHandler Progress; + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/IconExtractor.cs b/MiscHelpers/Common/IconExtractor.cs new file mode 100644 index 0000000..d7428c3 --- /dev/null +++ b/MiscHelpers/Common/IconExtractor.cs @@ -0,0 +1,383 @@ +/* + * IconExtractor/IconUtil for .NET + * Copyright (C) 2014 Tsuda Kageyu. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; +using System.Runtime.InteropServices; +using System.Security; +using System.Text; +using System.Windows.Forms; + +namespace MiscHelpers +{ + + public class IconExtractor + { + //////////////////////////////////////////////////////////////////////// + // Constants + + // Flags for LoadLibraryEx(). + + private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002; + + // Resource types for EnumResourceNames(). + + private readonly static IntPtr RT_ICON = (IntPtr)3; + private readonly static IntPtr RT_GROUP_ICON = (IntPtr)14; + + private const int MAX_PATH = 260; + + //////////////////////////////////////////////////////////////////////// + // Fields + + private byte[][] iconData = null; // Binary data of each icon. + + //////////////////////////////////////////////////////////////////////// + // Public properties + + /// + /// Gets the full path of the associated file. + /// + public string FileName + { + get; + private set; + } + + /// + /// Gets the count of the icons in the associated file. + /// + public int Count + { + get { return iconData == null ? 0 : iconData.Length; } + } + + /// + /// Initializes a new instance of the IconExtractor class from the specified file name. + /// + /// The file to extract icons from. + public IconExtractor(string fileName) + { + Initialize(fileName); + } + + /// + /// Extracts an icon from the file. + /// + /// Zero based index of the icon to be extracted. + /// A System.Drawing.Icon object. + /// Always returns new copy of the Icon. It should be disposed by the user. + public Icon GetIcon(int index) + { + if (index < 0 || Count <= index) + throw new ArgumentOutOfRangeException("index"); + + // Create an Icon based on a .ico file in memory. + + using (var ms = new MemoryStream(iconData[index])) + { + return new Icon(ms); + } + } + + /// + /// Extracts an icon from the file. + /// + /// Zero based index of the icon to be extracted. + /// Size of the icon to extract + /// A System.Drawing.Icon object. + /// Always returns new copy of the Icon. It should be disposed by the user. + public Icon GetIcon(int index, Size size) + { + if (index < 0 || Count <= index) + throw new ArgumentOutOfRangeException("index"); + + // Create an Icon based on a .ico file in memory. + + using (var ms = new MemoryStream(iconData[index])) + { + return new Icon(ms, size); + } + } + + /// + /// Extracts all the icons from the file. + /// + /// An array of System.Drawing.Icon objects. + /// Always returns new copies of the Icons. They should be disposed by the user. + public Icon[] GetAllIcons() + { + var icons = new List(); + for (int i = 0; i < Count; ++i) + icons.Add(GetIcon(i)); + + return icons.ToArray(); + } + + private void Initialize(string fileName) + { + if (fileName == null) + return; + + IntPtr hModule = IntPtr.Zero; + try + { + hModule = LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE); + if (hModule == IntPtr.Zero) + return; + + FileName = GetFileName(hModule); + + // Enumerate the icon resource and build .ico files in memory. + + var tmpData = new List(); + + ENUMRESNAMEPROC callback = (h, t, name, l) => + { + // Refer the following URL for the data structures used here: + // http://msdn.microsoft.com/en-us/library/ms997538.aspx + + // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's. + + var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name); + + // Calculate the size of an entire .icon file. + + int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount + int len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count + for (int i = 0; i < count; ++i) + len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes + + using (var dst = new BinaryWriter(new MemoryStream(len))) + { + // Copy GRPICONDIR to ICONDIR. + + dst.Write(dir, 0, 6); + + int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count + + for (int i = 0; i < count; ++i) + { + // Load the picture. + + ushort id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID + var pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id); + + // Copy GRPICONDIRENTRY to ICONDIRENTRY. + + dst.Seek(6 + 16 * i, SeekOrigin.Begin); + + dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical. + dst.Write(pic.Length); // ICONDIRENTRY.dwBytesInRes + dst.Write(picOffset); // ICONDIRENTRY.dwImageOffset + + // Copy a picture. + + dst.Seek(picOffset, SeekOrigin.Begin); + dst.Write(pic, 0, pic.Length); + + picOffset += pic.Length; + } + + tmpData.Add(((MemoryStream)dst.BaseStream).ToArray()); + } + + return true; + }; + EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero); + + iconData = tmpData.ToArray(); + } + finally + { + if (hModule != IntPtr.Zero) + FreeLibrary(hModule); + } + } + + private byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name) + { + // Load the binary data from the specified resource. + + IntPtr hResInfo = FindResource(hModule, name, type); + if (hResInfo == IntPtr.Zero) + throw new Win32Exception(); + + IntPtr hResData = LoadResource(hModule, hResInfo); + if (hResData == IntPtr.Zero) + throw new Win32Exception(); + + IntPtr pResData = LockResource(hResData); + if (pResData == IntPtr.Zero) + throw new Win32Exception(); + + uint size = SizeofResource(hModule, hResInfo); + if (size == 0) + throw new Win32Exception(); + + byte[] buf = new byte[size]; + Marshal.Copy(pResData, buf, 0, buf.Length); + + return buf; + } + + private string GetFileName(IntPtr hModule) + { + // Alternative to GetModuleFileName() for the module loaded with + // LOAD_LIBRARY_AS_DATAFILE option. + + // Get the file name in the format like: + // "\\Device\\HarddiskVolume2\\Windows\\System32\\shell32.dll" + + string fileName; + { + var buf = new StringBuilder(MAX_PATH); + int len = GetMappedFileName( + GetCurrentProcess(), hModule, buf, buf.Capacity); + if (len == 0) + throw new Win32Exception(); + + fileName = buf.ToString(); + } + + // Convert the device name to drive name like: + // "C:\\Windows\\System32\\shell32.dll" + + for (char c = 'A'; c <= 'Z'; ++c) + { + var drive = c + ":"; + var buf = new StringBuilder(MAX_PATH); + int len = QueryDosDevice(drive, buf, buf.Capacity); + if (len == 0) + continue; + + var devPath = buf.ToString(); + if (fileName.StartsWith(devPath)) + return (drive + fileName.Substring(devPath.Length)); + } + + return fileName; + } + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags); + + [DllImport("kernel32.dll", SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern bool FreeLibrary(IntPtr hModule); + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public static extern bool EnumResourceNames(IntPtr hModule, IntPtr lpszType, ENUMRESNAMEPROC lpEnumFunc, IntPtr lParam); + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public static extern IntPtr FindResource(IntPtr hModule, IntPtr lpName, IntPtr lpType); + + [DllImport("kernel32.dll", SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo); + + [DllImport("kernel32.dll", SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern IntPtr LockResource(IntPtr hResData); + + [DllImport("kernel32.dll", SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo); + + [DllImport("kernel32.dll", SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern IntPtr GetCurrentProcess(); + + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public static extern int QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax); + + [DllImport("psapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public static extern int GetMappedFileName(IntPtr hProcess, IntPtr lpv, StringBuilder lpFilename, int nSize); + + + [UnmanagedFunctionPointer(CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Unicode)] + [SuppressUnmanagedCodeSecurity] + public delegate bool ENUMRESNAMEPROC(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam); + + + + + public class PickerDialog : CommonDialog + { + + [DllImport("shell32.dll", EntryPoint = "#62", CharSet = CharSet.Unicode, SetLastError = true)] + [SuppressUnmanagedCodeSecurity] + public static extern bool SHPickIconDialog(IntPtr hWnd, StringBuilder pszFilename, int cchFilenameMax, out int pnIconIndex); + + private const int MAX_PATH = 260; + + [DefaultValue(default(string))] + public string FileName + { + get; + set; + } + + [DefaultValue(0)] + public int IconIndex + { + get; + set; + } + + protected override bool RunDialog(IntPtr hwndOwner) + { + var buf = new StringBuilder(FileName, MAX_PATH); + int index; + + bool ok = SHPickIconDialog(hwndOwner, buf, MAX_PATH, out index); + if (ok) + { + FileName = Environment.ExpandEnvironmentVariables(buf.ToString()); + IconIndex = index; + } + + return ok; + } + + public override void Reset() + { + FileName = null; + IconIndex = 0; + } + } + } + +} \ No newline at end of file diff --git a/MiscHelpers/Common/ImgFunc.cs b/MiscHelpers/Common/ImgFunc.cs new file mode 100644 index 0000000..fa14d17 --- /dev/null +++ b/MiscHelpers/Common/ImgFunc.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Interop; +using System.Windows.Media; +using System.Windows.Media.Imaging; + +namespace MiscHelpers +{ + public static class ImgFunc + { + private static Dictionary IconCache = new Dictionary(); + private static ReaderWriterLockSlim IconCacheLock = new ReaderWriterLockSlim(); + + public static ImageSource ExeIcon16 = GetIcon(MiscFunc.NtOsKrnlPath, 16); + + public static ImageSource GetIcon(string path, double size) + { + string key = path + "@" + size.ToString(); + + ImageSource image = null; + IconCacheLock.EnterReadLock(); + bool bFound = IconCache.TryGetValue(key, out image); + IconCacheLock.ExitReadLock(); + if (bFound) + return image; + + try + { + var pathIndex = TextHelpers.Split2(path, "|"); + + IconExtractor extractor = new IconExtractor(pathIndex.Item1); + int index = MiscFunc.parseInt(pathIndex.Item2); + if (index < extractor.Count) + image = ToImageSource(extractor.GetIcon(index, new System.Drawing.Size((int)size, (int)size))); + + if (image == null) + { + if (File.Exists(MiscFunc.NtOsKrnlPath)) // if running in WOW64 this does not exist + image = ToImageSource(Icon.ExtractAssociatedIcon(MiscFunc.NtOsKrnlPath)); + else // fall back to an other icon + image = ToImageSource(Icon.ExtractAssociatedIcon(MiscFunc.Shell32Path)); + } + + image.Freeze(); + } + catch (Exception err) + { + //AppLog.Exception(err); + } + + IconCacheLock.EnterWriteLock(); + if (!IconCache.ContainsKey(key)) + IconCache.Add(key, image); + IconCacheLock.ExitWriteLock(); + return image; + } + + public delegate ImageSource IconExtract(string path, double size); + + public static IAsyncResult GetIconAsync(string path, double size, Func cb) + { + IconExtract iconExtract = new IconExtract(ImgFunc.GetIcon); + return iconExtract.BeginInvoke(path, size, new AsyncCallback((IAsyncResult asyncResult) => + { + ImageSource icon = (asyncResult.AsyncState as IconExtract).EndInvoke(asyncResult); + cb(icon); + }), iconExtract); + } + + + [DllImport("gdi32.dll", SetLastError = true)] + private static extern bool DeleteObject(IntPtr hObject); + + public static ImageSource ToImageSource(this Icon icon) + { + Bitmap bitmap = icon.ToBitmap(); + IntPtr hBitmap = bitmap.GetHbitmap(); + ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); + if (!DeleteObject(hBitmap)) + throw new Win32Exception(); + return wpfBitmap; + } + } + +} \ No newline at end of file diff --git a/MiscHelpers/Common/MiscFunc.cs b/MiscHelpers/Common/MiscFunc.cs new file mode 100644 index 0000000..1a470c4 --- /dev/null +++ b/MiscHelpers/Common/MiscFunc.cs @@ -0,0 +1,420 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Drawing; +using System.IO; +using System.Linq; +using System.Management; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Security.Principal; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Forms; +using System.Windows.Interop; +using System.Windows.Media; + +namespace MiscHelpers +{ + internal struct LASTINPUTINFO + { + public uint cbSize; + public uint dwTime; + } + + public static class MiscFunc + { + public static UInt64 GetUTCTime() + { + return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; + } + + public static UInt64 GetUTCTimeMs() + { + return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds; + } + + + public static UInt64 DateTime2Ms(DateTime dateTime) + { + return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds; + } + + static public List EnumAllFiles(string sourcePath) + { + List files = new List(); + + foreach (string fileName in Directory.GetFiles(sourcePath)) + files.Add(fileName); + + foreach (string dirName in Directory.GetDirectories(sourcePath)) + { + if ((new DirectoryInfo(dirName).Attributes & FileAttributes.ReparsePoint) != 0) + continue; // skip junctions + + files.AddRange(EnumAllFiles(dirName)); + } + + return files; + } + + public static TValue GetOrCreate(this IDictionary dict, TKey key) + where TValue : new() + { + TValue val; + + if (!dict.TryGetValue(key, out val)) + { + val = new TValue(); + dict.Add(key, val); + } + + return val; + } + + public static bool Exec(string cmd, string args, bool hidden = true) + { + try + { + Process process = new Process(); + process.StartInfo.UseShellExecute = false; + if (hidden) + { + process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; + process.StartInfo.CreateNoWindow = true; + } + process.StartInfo.FileName = cmd; + process.StartInfo.Arguments = args; + process.StartInfo.Verb = "runas"; // run as admin + process.Start(); + process.WaitForExit(); + return true; + } + catch (Exception err) + { + AppLog.Exception(err); + } + return false; + } + + public static UInt64 GetCurTick() + { + return (UInt64)DateTime.Now.Ticks / 10000; // ticks in ms + } + + /*internal static void ActiveSleep(int ms) + { + DateTime until = DateTime.Now.Add(new TimeSpan(0, 0, 0, 0, ms)); + while (until >= DateTime.Now) + System.Windows.Forms.Application.DoEvents(); + }*/ + + [DllImport("User32.dll")] + private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + [DllImport("Kernel32.dll")] + private static extern uint GetLastError(); + + public static uint GetIdleTime() // in seconds + { + LASTINPUTINFO lastInPut = new LASTINPUTINFO(); + lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut); + if (!GetLastInputInfo(ref lastInPut)) + { + throw new Exception(GetLastError().ToString()); + } + return ((uint)Environment.TickCount - lastInPut.dwTime) / 1000; + } + + public static int parseInt(string str, int def = 0) + { + int ret; + if (int.TryParse(str, out ret)) + return ret; + return def; + } + + public static double parseDouble(string str, double def = 0) + { + double ret; + if (double.TryParse(str, out ret)) + return ret; + return def; + } + + public static bool? parseBool(string str, bool? def = false) + { + if (str.Length == 0) + return def; + + bool ret; + if (bool.TryParse(str, out ret)) + return ret; + return def; + } + + public static string NtOsKrnlPath = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\ntoskrnl.exe"); + + public static string Shell32Path = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\shell32.dll"); + + public static string parsePath(string path) + { + try + { + if (path.Contains(@"\device\mup\")) + return @"\" + path.Substring(11, path.Length - 11); + string[] strArray = path.Split(new char[1] { '\\' }, StringSplitOptions.RemoveEmptyEntries); + string vol = @"\" + strArray[0] + @"\" + strArray[1]; + path = path.Replace(vol, GetDriveLetter(vol)); + if (path.Contains('~')) + path = Path.GetFullPath(path); + return path; + } + catch (Exception err) + { + AppLog.Exception(err); + } + return ""; + } + + [DllImport("kernel32.dll")] + public static extern UInt64 GetTickCount64(); + + [DllImport("kernel32.dll")] + public static extern uint QueryDosDevice(string lpDeviceName, [In, Out] char[] lpTargetPath, int ucchMax); + + private static Dictionary> DriveLetterCache = new Dictionary>(); + private static ReaderWriterLockSlim DriveLetterCacheLock = new ReaderWriterLockSlim(); + + private static string GetDriveLetter(string longPath) + { + Tuple temp; + DriveLetterCacheLock.EnterReadLock(); + if (DriveLetterCache.TryGetValue(longPath.ToLower(), out temp)) + { + if (temp.Item2 > GetTickCount64()) + { + DriveLetterCacheLock.ExitReadLock(); + return temp.Item1; + } + DriveLetterCache.Remove(longPath.ToLower()); + } + DriveLetterCacheLock.ExitReadLock(); + + // ToDo: build a cache on WM_DEVICECHANGE + + string ret = null; + char[] lpTargetPath = new char[260 + 1]; + for (char ltr = 'A'; ltr <= 'Z'; ltr++) + { + uint size = QueryDosDevice(ltr + ":", lpTargetPath, 260); + if (size > 0 && longPath.Equals(new String(lpTargetPath, 0, (int)size - 2), StringComparison.OrdinalIgnoreCase)) + { + ret = ltr + ":"; + break; + } + } + + if (ret == null) + return "?:"; + + DriveLetterCacheLock.EnterWriteLock(); + if (DriveLetterCache.ContainsKey(longPath.ToLower()) == false) + DriveLetterCache.Add(longPath.ToLower(), new Tuple(ret, GetTickCount64() + 1 * 60 * 1000)); // cahce values for 1 minutes + DriveLetterCacheLock.ExitWriteLock(); + return ret; + } + + public static string Sys32Path = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\"); + public static string SysWOWPath = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\SysWOW64\"); + + public static bool IsWindowsBinary(string path) + { + if (path == null || path.Length == 0) + return false; + + if (path.IndexOf(Sys32Path, StringComparison.OrdinalIgnoreCase) == 0 || path.IndexOf(SysWOWPath, StringComparison.OrdinalIgnoreCase) == 0) + { + try + { + FileVersionInfo info = FileVersionInfo.GetVersionInfo(path); + + bool isMS = info.CompanyName == "Microsoft Corporation"; + + return isMS; + } + catch { } + } + + return false; + } + + public static string GetExeDescription(string appPath) + { + string descr = null; + if (File.Exists(appPath)) + { + try + { + FileVersionInfo info = FileVersionInfo.GetVersionInfo(appPath); + descr = info?.FileDescription; + } + catch { } + } + return (descr != null && descr.Length > 0) ? descr : ""; + } + + + /*public static string GetServiceNameByPID(int pid) + { + ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Name FROM Win32_Service WHERE ProcessId = " + pid); + foreach (ManagementObject queryObj in searcher.Get()) + { + string ret = queryObj["Name"].ToString(); + return ret; + } + return null; + }*/ + + public static bool StrCmp(string value, string test) + { + if (value == null) + return test == null; + if (test == null) + return false; + return value.Equals(test, StringComparison.OrdinalIgnoreCase); + } + + /*public static string GetServiceName(string name) + { + ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DisplayName FROM Win32_Service WHERE Name = \"" + name + "\""); + foreach (ManagementObject queryObj in searcher.Get()) + { + return queryObj["DisplayName"].ToString(); + } + return ""; + }*/ + + [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] + public static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved); + + public static string GetResourceStr(string resourcePath) + { + StringBuilder buffer = new StringBuilder(4096); + int result = SHLoadIndirectString(resourcePath, buffer, buffer.Capacity, IntPtr.Zero); + if (result == 0) + return buffer.ToString(); + return resourcePath; + } + + public static string GetResourceStr(string path, string resID) + { + return GetResourceStr("@{" + path + "? " + resID + "}"); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static public string GetCurrentMethod() + { + try + { + var st = new StackTrace(); + var sf = st.GetFrame(1); + return sf.GetMethod().Name; + } + catch + { + return "Unknown"; + } + } + + static public bool IsOnScreen(Rectangle formRectangle) + { + Screen[] screens = Screen.AllScreens; + foreach (Screen screen in screens) + { + if (screen.WorkingArea.Contains(formRectangle)) + { + return true; + } + } + return false; + } + + public static bool IsValidRegex(string pattern) + { + if (string.IsNullOrEmpty(pattern)) return false; + + try + { + Regex.Match("", pattern); + } + catch (ArgumentException) + { + return false; + } + + return true; + } + + public static bool IsEqual(T L, T R) + { + if (L == null) + return (R == null); + return L.Equals(R); + } + + public static T Max(T x, T y) + { + return (Comparer.Default.Compare(x, y) > 0) ? x : y; + } + + public static T Min(T x, T y) + { + return (Comparer.Default.Compare(x, y) < 0) ? x : y; + } + + public static class ClipboardNative + { + [DllImport("user32.dll")] + private static extern bool OpenClipboard(IntPtr hWndNewOwner); + + [DllImport("user32.dll")] + private static extern bool CloseClipboard(); + + [DllImport("user32.dll")] + private static extern bool SetClipboardData(uint uFormat, IntPtr data); + + private const uint CF_UNICODETEXT = 13; + + public static bool CopyTextToClipboard(string text) + { + if (!OpenClipboard(IntPtr.Zero)) + { + return false; + } + + var global = Marshal.StringToHGlobalUni(text); + + SetClipboardData(CF_UNICODETEXT, global); + CloseClipboard(); + + //------------------------------------------- + // Not sure, but it looks like we do not need + // to free HGLOBAL because Clipboard is now + // responsible for the copied data. (?) + // + // Otherwise the second call will crash + // the app with a Win32 exception + // inside OpenClipboard() function + //------------------------------------------- + // Marshal.FreeHGlobal(global); + + return true; + } + } + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/MultiValueDictionary.cs b/MiscHelpers/Common/MultiValueDictionary.cs new file mode 100644 index 0000000..5f848f5 --- /dev/null +++ b/MiscHelpers/Common/MultiValueDictionary.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace MiscHelpers +{ + public class MultiValueDictionary : Dictionary> + { + public MultiValueDictionary() : base() + { + } + + public MultiValueDictionary Clone() // shallow copy! + { + MultiValueDictionary clone = new MultiValueDictionary(); + foreach (KeyValuePair> kvp in this) + { + clone.Add(kvp.Key, kvp.Value.Clone()); + } + return clone; + } + + public void Add(TKey key, TValue value) + { + CloneableList container = null; + if (!this.TryGetValue(key, out container)) + { + container = new CloneableList(); + base.Add(key, container); + } + container.Add(value); + } + + public bool ContainsValue(TKey key, TValue value) + { + bool toReturn = false; + CloneableList values = null; + if (this.TryGetValue(key, out values)) + { + toReturn = values.Contains(value); + } + return toReturn; + } + + public void Remove(TKey key, TValue value) + { + CloneableList container = null; + if (this.TryGetValue(key, out container)) + { + container.Remove(value); + if (container.Count <= 0) + { + this.Remove(key); + } + } + } + + public CloneableList GetValues(TKey key, bool returnEmptySet = true) + { + CloneableList toReturn = null; + if (!base.TryGetValue(key, out toReturn) && returnEmptySet) + { + toReturn = new CloneableList(); + } + return toReturn; + } + + public CloneableList GetOrAdd(TKey key) + { + CloneableList toReturn = null; + if (!base.TryGetValue(key, out toReturn)) + { + toReturn = new CloneableList(); + base.Add(key, toReturn); + } + return toReturn; + } + + public int GetCount() + { + int Count = 0; + foreach (KeyValuePair> pair in this) + Count += pair.Value.Count; + return Count; + } + + public TValue GetAt(int index) + { + int Count = 0; + foreach (KeyValuePair> pair in this) + { + if (Count + pair.Value.Count > index) + return pair.Value[index - Count]; + Count += pair.Value.Count; + } + throw new IndexOutOfRangeException(); + } + + public TKey GetKey(int index) + { + int Count = 0; + foreach (KeyValuePair> pair in this) + { + if (Count + pair.Value.Count > index) + return pair.Key; + Count += pair.Value.Count; + } + throw new IndexOutOfRangeException(); + } + + public CloneableList GetAllValues() + { + CloneableList toReturn = new CloneableList(); + foreach (List values in this.Values) + { + foreach (TValue value in values) + toReturn.Add(value); + } + return toReturn; + } + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/TextHelpers.cs b/MiscHelpers/Common/TextHelpers.cs new file mode 100644 index 0000000..b10c578 --- /dev/null +++ b/MiscHelpers/Common/TextHelpers.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace MiscHelpers +{ + public class TextHelpers + { + public static String GetLeft(ref String Line, String sep = " ") + { + int pos = Line.IndexOf(sep); + String ret; + if (pos == -1) + { + ret = Line; + Line = ""; + } + else + { + ret = Line.Substring(0, pos); + Line = Line.Remove(0, pos + 1); + } + return ret; + } + + public static String get2nd(String Line, String sep = ":") + { + int pos = Line.IndexOf(sep); + if (pos == -1) + return ""; + return Line.Substring(pos + 1).Trim(); + } + + public static String get1st(String Line, String sep = ":") + { + int pos = Line.IndexOf(sep); + if (pos == -1) + return Line; + return Line.Substring(0, pos).Trim(); + } + + public static Tuple Split2(string str, String sep = ":", bool rev = false) + { + int pos = rev ? str.LastIndexOf(sep) : str.IndexOf(sep); + if (pos == -1) + return new Tuple(str.Trim(), ""); + return new Tuple(str.Substring(0, pos).Trim(), str.Substring(pos + 1).Trim()); + } + + public static bool CompareWildcard(string str, string find) + { + if (str == null) + return false; + //var like = "^" + Regex.Escape(find).Replace("_", ".").Replace("%", ".*") + "$"; + var like = Regex.Escape(find).Replace("_", ".").Replace("\\*", ".*"); + return Regex.IsMatch(str, like, RegexOptions.IgnoreCase); + } + + public static List TokenizeStr(string input) + { + List output = new List(); + foreach (string str in Regex.Matches(input, @"[\""].+?[\""]|[^ ]+").Cast().Select(m => m.Value).ToList()) + { + if (str.Length > 2 && str.ElementAt(0) == '"') + output.Add(str.Substring(1, str.Length - 2)); + else + output.Add(str); + } + return output; + } + + public static List SplitStr(string str, string sep, bool bKeepEmpty = false) + { + List strList = new List(); + String[] spearator = { sep }; + foreach (var curStr in str.Split(spearator, StringSplitOptions.None)) + { + var tmpStr = curStr.Trim(); + if (tmpStr.Length > 0 || bKeepEmpty) + strList.Add(tmpStr); + } + return strList; + } + } +} diff --git a/MiscHelpers/Common/WinConsole.cs b/MiscHelpers/Common/WinConsole.cs new file mode 100644 index 0000000..fb63fab --- /dev/null +++ b/MiscHelpers/Common/WinConsole.cs @@ -0,0 +1,102 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using System.IO; +using Microsoft.Win32.SafeHandles; + +namespace MiscHelpers +{ + public static class WinConsole + { + static public bool Initialize(bool alwaysCreateNewConsole = true) + { + if (AttachConsole(ATTACH_PARRENT) != 0) + return true; + if (!alwaysCreateNewConsole) + return false; + if (AllocConsole() != 0) + { + InitializeOutStream(); + InitializeInStream(); + return true; + } + return false; + } + + private static void InitializeOutStream() + { + var fs = CreateFileStream("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, FileAccess.Write); + if (fs != null) + { + var writer = new StreamWriter(fs) { AutoFlush = true }; + Console.SetOut(writer); + Console.SetError(writer); + } + } + + private static void InitializeInStream() + { + var fs = CreateFileStream("CONIN$", GENERIC_READ, FILE_SHARE_READ, FileAccess.Read); + if (fs != null) + { + Console.SetIn(new StreamReader(fs)); + } + } + + private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode, FileAccess dotNetFileAccess) + { + var file = new SafeFileHandle(CreateFileW(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero), true); + if (!file.IsInvalid) + { + var fs = new FileStream(file, dotNetFileAccess); + return fs; + } + return null; + } + + #region Win API Functions and Constants + [DllImport("kernel32.dll", + EntryPoint = "AllocConsole", + SetLastError = true, + CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall)] + private static extern int AllocConsole(); + + [DllImport("kernel32.dll", + EntryPoint = "AttachConsole", + SetLastError = true, + CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall)] + private static extern UInt32 AttachConsole(UInt32 dwProcessId); + + [DllImport("kernel32.dll", + EntryPoint = "CreateFileW", + SetLastError = true, + CharSet = CharSet.Auto, + CallingConvention = CallingConvention.StdCall)] + private static extern IntPtr CreateFileW( + string lpFileName, + UInt32 dwDesiredAccess, + UInt32 dwShareMode, + IntPtr lpSecurityAttributes, + UInt32 dwCreationDisposition, + UInt32 dwFlagsAndAttributes, + IntPtr hTemplateFile + ); + + private const UInt32 GENERIC_WRITE = 0x40000000; + private const UInt32 GENERIC_READ = 0x80000000; + private const UInt32 FILE_SHARE_READ = 0x00000001; + private const UInt32 FILE_SHARE_WRITE = 0x00000002; + private const UInt32 OPEN_EXISTING = 0x00000003; + private const UInt32 FILE_ATTRIBUTE_NORMAL = 0x80; + private const UInt32 ERROR_ACCESS_DENIED = 5; + + private const UInt32 ATTACH_PARRENT = 0xFFFFFFFF; + + #endregion + } +} \ No newline at end of file diff --git a/MiscHelpers/Common/WpfFunc.cs b/MiscHelpers/Common/WpfFunc.cs new file mode 100644 index 0000000..67ede7a --- /dev/null +++ b/MiscHelpers/Common/WpfFunc.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Drawing; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Ribbon; +using System.Windows.Media; + +namespace MiscHelpers +{ + public class WpfFunc + { + public class ViewModelHelper : INotifyPropertyChanged//, IDisposable + { + /*public void Dispose() + { + }*/ + + protected void SetProperty(string Name, T newValue, ref T curValue) + { + if (Equals(newValue, curValue)) + return; + curValue = newValue; + RaisePropertyChanged(Name); + } + + protected void SetPropertyCmb(string Name, ContentControl newValue, ref ContentControl curValue, ref string curText) + { + SetProperty(Name, newValue, ref curValue); + if (curValue != null) + curText = curValue.Content.ToString(); + } + + + #region INotifyPropertyChanged Members + + public event PropertyChangedEventHandler PropertyChanged; + + /// + /// Raises the PropertyChanged event if needed. + /// + /// The name of the property that changed. + protected virtual void RaisePropertyChanged(string propertyName) + { + if (PropertyChanged != null) + { + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } + } + + #endregion + } + + static public ContentControl CmbPick(ComboBox box, string tag) + { + if (tag == null) + return null; + for (int i = 0; i < box.Items.Count; i++) + { + ContentControl item = (box.Items[i] as ContentControl); + if (item.Tag != null && item.Tag.ToString().Equals(tag)) + return item; + } + return null; + } + + static public bool CmbSelect(ComboBox box, string tag) + { + ContentControl item = CmbPick(box, tag); + if (item != null) + { + box.SelectedItem = item; + return true; + } + return false; + + } + + static public ComboBoxItem CmbAdd(ComboBox box, string text, object tag) + { + var item = new ComboBoxItem() { Content = text, Tag = tag }; + box.Items.Add(item); + return item; + } + + + static public RibbonGalleryItem CmbPick(RibbonGalleryCategory cat, string tag) + { + if (tag == null) + return null; + for (int i = 0; i < cat.Items.Count; i++) + { + RibbonGalleryItem item = (cat.Items[i] as RibbonGalleryItem); + if (item.Tag != null && item.Tag.ToString().Equals(tag)) + return item; + } + return null; + } + + static public bool CmbSelect(RibbonGallery gal, string tag) + { + ContentControl item = CmbPick(gal.Items[0] as RibbonGalleryCategory, tag); + if (item != null) + { + gal.SelectedItem = item; + return true; + } + return false; + } + + static public void CmbAdd(RibbonGallery gal, string text, object tag) + { + (gal.Items[0] as RibbonGalleryCategory).Items.Add(new RibbonGalleryItem { Content = text, Tag = tag }); + } + + public static List SplitAndValidate(string Values, ref bool? duplicate) + { + if (Values == null) + return null; + List ValueList = new List(); + foreach (string Value in Values.Split(',')) + { + string temp = Value.Trim(); + if (duplicate != null && ValueList.Contains(temp)) + { + duplicate = true; + return null; + } + ValueList.Add(temp); + } + if (ValueList.Count == 0) + return null; + return ValueList; + } + + public static T FindChild(DependencyObject parentObj) where T : DependencyObject + { + if (parentObj == null) + return null; + + try + { + if (parentObj is T) + return parentObj as T; + + for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(parentObj); i++) + { + T childObj = FindChild(System.Windows.Media.VisualTreeHelper.GetChild(parentObj, i)); + if (childObj != null) + return childObj; + } + } + catch (Exception err) + { + AppLog.Exception(err); + } + return null; + } + + public static MenuItem AddMenu(ItemsControl menu, string label, RoutedEventHandler handler, object icon = null, object Tag = null) + { + var item = new MenuItem() { Header = label, Tag = Tag }; + if (handler != null) + item.Click += handler; + if (icon != null) + item.Icon = new System.Windows.Controls.Image() { Source = icon as ImageSource }; + menu.Items.Add(item); + return item; + } + + private void DumpVisualTree(DependencyObject parent, int level) + { + string typeName = parent.GetType().Name; + string name = (string)(parent.GetValue(FrameworkElement.NameProperty) ?? ""); + + Console.WriteLine(string.Format("{0}: {1}", typeName, name)); + + if (parent == null) return; + + for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) + { + DependencyObject child = VisualTreeHelper.GetChild(parent, i); + DumpVisualTree(child, level + 1); + } + } + + private void DumpLogicalTree(object parent, int level) + { + string typeName = parent.GetType().Name; + string name = null; + DependencyObject doParent = parent as DependencyObject; + // Not everything in the logical tree is a dependency object + if (doParent != null) + { + name = (string)(doParent.GetValue(FrameworkElement.NameProperty) ?? ""); + } + else + { + name = parent.ToString(); + } + + Console.WriteLine(string.Format("{0}: {1}", typeName, name)); + + if (doParent == null) return; + + foreach (object child in LogicalTreeHelper.GetChildren(doParent)) + { + DumpLogicalTree(child, level + 1); + } + } + } + + public static class DependencyObjectExtensions + { + private static readonly PropertyInfo InheritanceContextProp = typeof(DependencyObject).GetProperty("InheritanceContext", BindingFlags.NonPublic | BindingFlags.Instance); + + public static IEnumerable GetParents(this DependencyObject child) + { + while (child != null) + { + var parent = LogicalTreeHelper.GetParent(child); + if (parent == null) + { + if (child is FrameworkElement) + { + parent = VisualTreeHelper.GetParent(child); + } + if (parent == null && child is ContentElement) + { + parent = ContentOperations.GetParent((ContentElement)child); + } + if (parent == null) + { + parent = InheritanceContextProp.GetValue(child, null) as DependencyObject; + } + } + child = parent; + yield return parent; + } + } + } +} \ No newline at end of file diff --git a/MiscHelpers/MiscHelpers.csproj b/MiscHelpers/MiscHelpers.csproj new file mode 100644 index 0000000..b562be0 --- /dev/null +++ b/MiscHelpers/MiscHelpers.csproj @@ -0,0 +1,98 @@ + + + + + Debug + AnyCPU + {694C85C2-BB43-4525-A953-709050002631} + Library + Properties + MiscHelpers + MiscHelpers + v4.5 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {E34CB9F1-C7F7-424C-BE29-027DCC09363A} + 1 + 0 + 0 + tlbimp + False + True + + + + \ No newline at end of file diff --git a/MiscHelpers/Properties/AssemblyInfo.cs b/MiscHelpers/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..11c44bb --- /dev/null +++ b/MiscHelpers/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("MiscHelpers")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("MiscHelpers")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("694c85c2-bb43-4525-a953-709050002631")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PrivateWin10/Common/PipeIPC/PipeClient.cs b/PrivateAPI/IPC/PipeClient.cs similarity index 59% rename from PrivateWin10/Common/PipeIPC/PipeClient.cs rename to PrivateAPI/IPC/PipeClient.cs index 3c21e51..29a5ec6 100644 --- a/PrivateWin10/Common/PipeIPC/PipeClient.cs +++ b/PrivateAPI/IPC/PipeClient.cs @@ -1,7 +1,9 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.IO.Pipes; using System.Linq; using System.Text; @@ -9,8 +11,9 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; +using System.Xml; -namespace PipeIPC +namespace PrivateAPI { public class PipeClient { @@ -19,9 +22,9 @@ public class PipeClient protected class PipeConnector : IPCStream { ManualResetEvent done = new ManualResetEvent(false); - RemoteCall retObj = null; + Tuple> retObj = null; int seqIDctr = 0; - public event EventHandler PushNotification; + public event EventHandler>> PushNotification; public PipeConnector(string serverName, string pipeName) { @@ -41,19 +44,22 @@ public bool Connect(int TimeOut = 10000) DataReceived += (sndr, data) => { - RemoteCall call = ByteArrayToObject(data); + EMessageTypes type = EMessageTypes.eCall; + int seqID = 0; + string func = null; + List args = ParsePacket(data, ref type, ref seqID, ref func); - if (call.type == "push") + if (type == EMessageTypes.ePush) { - PushNotification?.Invoke(this, call); + PushNotification?.Invoke(this, new Tuple>(func, args)); } - else if (call.type == "call") + else if (type == EMessageTypes.eCall) { - if (call.seqID != seqIDctr) - AppLog.Debug("call.seqID != seqIDctr"); + if (seqID != seqIDctr) + Debug.Write("seqID != seqIDctr"); else { - retObj = call; + retObj = new Tuple>(func, args); done.Set(); } } @@ -68,17 +74,11 @@ public bool Connect(int TimeOut = 10000) return true; } - public object RemoteExec(string fx, object args) + public List RemoteExec(string func, List args) { - RemoteCall call = new RemoteCall(); - call.seqID = ++seqIDctr; - call.type = "call"; - call.func = fx; - call.args = args; - retObj = null; done.Reset(); - if(Send(ObjectToByteArray(call))) + if(SendPacket(EMessageTypes.eCall, ++seqIDctr, func, args)) #if DEBUG done.WaitOne(); // give us time to debug #else @@ -88,10 +88,10 @@ public object RemoteExec(string fx, object args) if (retObj == null) throw new Exception("Pipe Broke!"); - if (retObj.args is Exception) - throw (Exception)retObj.args; + //if (retObj.args is Exception) + // throw (Exception)retObj.args; - return retObj.args; + return retObj.Item2; } } @@ -123,7 +123,7 @@ public bool DoConnect(int TimeOut = 10000) clientPipe.PushNotification += (sndr, call) => { - HandlePushNotification(call.func, call.args); + HandlePushNotification(call.Item1, call.Item2); }; return true; @@ -132,33 +132,50 @@ public bool DoConnect(int TimeOut = 10000) public int Connect(int TimeOut = 10000, bool mNoDouble = false) { // Note: when we close a IPC host without terminating its process we are left with some still active listeners, so we test communication and reconnect if needed - for (long endTime = (long)MiscFunc.GetTickCount64() + (long)TimeOut; TimeOut > 0; TimeOut = (int)(endTime - (long)MiscFunc.GetTickCount64())) + for (DateTime endTime = DateTime.Now.AddMilliseconds(TimeOut); DateTime.Now < endTime; ) { if (!DoConnect(TimeOut)) continue; - IPCSession session = RemoteExec("InitSession", Process.GetCurrentProcess().SessionId, null); - if (session != null) - return (mNoDouble || session.duplicate == false) ? 1 : -1; + + List ret = null; + using (MemoryStream xmlStream = new MemoryStream()) + { + List args = new List(); + + args.Add(BitConverter.GetBytes(Process.GetCurrentProcess().SessionId)); + + ret = RemoteExec("InitSession", args); + } + + if (ret != null) + { + bool Duplicate = BitConverter.ToBoolean(ret[0], 0); + + return (mNoDouble || Duplicate == false) ? 1 : -1; + } } return 0; } - public T RemoteExec(string fx, object args, T defRet) + public List RemoteExec(string func, List args) { - T ret; + List ret = null; +#if !DEBUG try +#endif { - if (clientPipe == null && Connect(3000, true) == 0) - throw new Exception("Connection Failed"); + if (clientPipe == null) // && Connect(3000, true) == 0) + throw new Exception("Not Connected"); - ret = (T)(clientPipe.RemoteExec(fx, args)); + ret = clientPipe.RemoteExec(func, args); } +#if !DEBUG catch (Exception err) { AppLog.Exception(err); - ret = defRet; } +#endif return ret; } @@ -176,7 +193,7 @@ public void Close() clientPipe = null; } - public virtual void HandlePushNotification(string func, object args) + public virtual void HandlePushNotification(string func, List param) { throw new NotImplementedException(); } diff --git a/PrivateWin10/Common/PipeIPC/PipeHost.cs b/PrivateAPI/IPC/PipeHost.cs similarity index 76% rename from PrivateWin10/Common/PipeIPC/PipeHost.cs rename to PrivateAPI/IPC/PipeHost.cs index c62b530..7628d2b 100644 --- a/PrivateWin10/Common/PipeIPC/PipeHost.cs +++ b/PrivateAPI/IPC/PipeHost.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.IO.Pipes; using System.Linq; using System.Security.AccessControl; @@ -8,8 +9,9 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Threading; +using System.Xml; -namespace PipeIPC +namespace PrivateAPI { public partial class PipeHost { @@ -89,25 +91,32 @@ public void AddListener() serverPipe.DataReceived += (sndr, data) => { //mDispatcher.BeginInvoke(new Action(() => { - RemoteCall call = PipeListener.ByteArrayToObject(data); - if (call.func == "InitSession") - { - int SessionId = (int)call.args; + EMessageTypes type = EMessageTypes.eCall; + int seqID = 0; + string func = null; + List args = PipeListener.ParsePacket(data, ref type, ref seqID, ref func); - IPCSession session = new IPCSession(); - //session.version = App.mVersion; - session.duplicate = mDispatcher.Invoke(new Func(() => { + List ret = null; + if (func == "InitSession") + { + int SessionId = BitConverter.ToInt32(args[0], 0); + + bool Duplicate = mDispatcher.Invoke(new Func(() => { return CountSessions(SessionId) > 0; })); - call.args = session; + + ret = new List(); + + ret.Add(BitConverter.GetBytes(Duplicate)); serverPipe.SessionID = SessionId; } - else - call = Process(call); + else if(args != null) + ret = Process(func, args); - serverPipe.Send(PipeListener.ObjectToByteArray(call)); + if(ret != null) + serverPipe.SendPacket(type, seqID, func, ret); //})); }; @@ -134,24 +143,18 @@ public void Close() serverPipes.Clear(); } - public void SendPushNotification(string func, object args) + public void SendPushNotification(string func, List args) { foreach (PipeListener serverPipes in serverPipes) { if (!serverPipes.IsConnected()) continue; - RemoteCall call = new RemoteCall(); - call.seqID = 0; - call.type = "push"; - call.func = func; - call.args = args; - - serverPipes.Send(PipeListener.ObjectToByteArray(call)); + serverPipes.SendPacket(EMessageTypes.ePush, 0, func, args); } } - protected virtual RemoteCall Process(RemoteCall call) + protected virtual List Process(string func, List param) { throw new NotImplementedException(); } diff --git a/PrivateWin10/Common/PipeIPC/PipeIPC.cs b/PrivateAPI/IPC/PipeIPC.cs similarity index 58% rename from PrivateWin10/Common/PipeIPC/PipeIPC.cs rename to PrivateAPI/IPC/PipeIPC.cs index d609e3c..8995a6d 100644 --- a/PrivateWin10/Common/PipeIPC/PipeIPC.cs +++ b/PrivateAPI/IPC/PipeIPC.cs @@ -8,27 +8,12 @@ using System.IO; using System.Threading; -namespace PipeIPC +namespace PrivateAPI { - [Serializable()] - public class IPCSession + public enum EMessageTypes { - //public string version; - public bool duplicate; - } - - [Serializable()] - public class RemoteCall - { - public int seqID; - public string type; - public string func; - public object args; - - public static T GetArg(object args, int index) - { - return (T)(((object[])args)[index]); - } + eCall = 0, + ePush = 1 } public class IPCStream where T : PipeStream @@ -75,6 +60,63 @@ public bool Send(byte[] bytes) return true; } + public bool SendPacket(EMessageTypes type, int seqID, string func, List args) + { + using (MemoryStream dataStream = new MemoryStream()) + { + var dataWriter = new BinaryWriter(dataStream); + + dataWriter.Write((byte)type); + + dataWriter.Write(seqID); + + dataWriter.Write(func); + + dataWriter.Write(args.Count); + foreach (byte[] arg in args) + { + dataWriter.Write(arg.Length); + dataWriter.Write(arg); + } + + dataWriter.Dispose(); + + return Send(dataStream.ToArray()); + } + } + + public static List ParsePacket(byte[] packet, ref EMessageTypes type, ref int seqID, ref string func) + { + try + { + using (MemoryStream dataStream = new MemoryStream(packet)) + { + var dataReader = new BinaryReader(dataStream); + + type = (EMessageTypes)dataReader.ReadByte(); + + seqID = dataReader.ReadInt32(); + + func = dataReader.ReadString(); + + List args = new List(); + + int count = dataReader.ReadInt32(); + for (int i = 0; i < count; i++) + { + int length = dataReader.ReadInt32(); + args.Add(dataReader.ReadBytes(length)); + } + + return args; + } + } + catch + { + return null; + } + } + protected void startRecv() { revcRunning = true; @@ -119,22 +161,5 @@ void recvProc() DataReceived?.Invoke(this, buff); } } - public static byte[] ObjectToByteArray(RemoteCall obj) - { - BinaryFormatter bf = new BinaryFormatter(); - var ms = new MemoryStream(); - bf.Serialize(ms, obj); - return ms.ToArray(); - } - - public static RemoteCall ByteArrayToObject(byte[] arrBytes) - { - var memStream = new MemoryStream(); - var binForm = new BinaryFormatter(); - memStream.Write(arrBytes, 0, arrBytes.Length); - memStream.Seek(0, SeekOrigin.Begin); - var obj = binForm.Deserialize(memStream); - return (RemoteCall)obj; - } } } \ No newline at end of file diff --git a/PrivateAPI/IPC/Priv10Conv.cs b/PrivateAPI/IPC/Priv10Conv.cs new file mode 100644 index 0000000..744f5f3 --- /dev/null +++ b/PrivateAPI/IPC/Priv10Conv.cs @@ -0,0 +1,258 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace PrivateAPI +{ + public static class Priv10Conv + { + public static byte[] PutStr(object value) + { + if (value == null) + return new byte[0]; + return Encoding.UTF8.GetBytes(value.ToString()); + } + + public static string GetStr(byte[] value) + { + if (value.Length == 0) + return null; + return Encoding.UTF8.GetString(value); + } + + public static byte[] PutStrList(List list) + { + return PutList(list, PutStr); + } + + public static List GetStrList(byte[] value) + { + return GetList(value, GetStr); + } + + public static byte[] PutBool(bool value) + { + return BitConverter.GetBytes(value); + } + + public static bool GetBool(byte[] value) + { + return BitConverter.ToBoolean(value, 0); + } + + public static byte[] PutBoolx(bool? value) + { + if(value == null) + return new byte[0]; + return BitConverter.GetBytes(value.Value); + } + + public static bool? GetBoolx(byte[] value) + { + if(value.Length == 0) + return null; + return BitConverter.ToBoolean(value, 0); + } + + public static byte[] PutInt(int value) + { + return BitConverter.GetBytes(value); + } + + public static int GetInt(byte[] value) + { + return BitConverter.ToInt32(value, 0); + } + + public static byte[] PutUInt64(UInt64 value) + { + return BitConverter.GetBytes(value); + } + + public static UInt64 GetUInt64(byte[] value) + { + return BitConverter.ToUInt64(value, 0); + } + + public static T GetEnum(byte[] value) + { + return (T)Enum.Parse(typeof(T), Encoding.ASCII.GetString(value)); + } + + public static Guid GetGuid(byte[] data) + { + return new Guid(data); + } + + public static byte[] PutGuid(Guid guid) + { + return guid.ToByteArray(); + } + + public static byte[] PutGuids(List list) + { + return PutList(list, PutGuid); + } + + public static List GetGuids(byte[] data) + { + return GetList(data, GetGuid); + } + + ///////////////////////////////////////// + // + + public static byte[] PutObjXml(T obj, Action store) + { + if(obj == null) + return new byte[0]; + using (MemoryStream xmlStream = new MemoryStream()) + { + using (XmlWriter writer = XmlWriter.Create(xmlStream)) + { + writer.WriteStartDocument(); + store(obj, writer); + writer.WriteEndDocument(); + } + return xmlStream.ToArray(); + } + } + + public static T GetXmlObj(byte[] data, Action load) where T: new() + { + if (data.Length == 0) + return default(T); + using (var memStream = new MemoryStream(data)) + { + XmlDocument xDoc = new XmlDocument(); + xDoc.Load(memStream); + + T value = new T(); + load(value, xDoc.DocumentElement); + return value; + } + } + + public static byte[] PutList(List list, Func store) + { + if (list == null) + return new byte[0]; + using (MemoryStream dataStream = new MemoryStream()) + { + using (var dataWriter = new BinaryWriter(dataStream)) + { + dataWriter.Write(list.Count); + + foreach (T item in list) + { + byte[] data = store(item); + dataWriter.Write(data.Length); + dataWriter.Write(data); + } + } + return dataStream.ToArray(); + } + } + + public static List GetList(byte[] data, Func load) + { + if (data.Length == 0) + return null; + List list = new List(); + using (MemoryStream dataStream = new MemoryStream(data)) + { + var dataReader = new BinaryReader(dataStream); + + int count = dataReader.ReadInt32(); + for (int i = 0; i < count; i++) + { + int length = dataReader.ReadInt32(); + list.Add(load(dataReader.ReadBytes(length))); + } + } + return list; + } + + public static byte[] PutGuidMMap(Dictionary> MMap, Func store) + { + if (MMap == null) + return new byte[0]; + using (MemoryStream dataStream = new MemoryStream()) + { + using (var dataWriter = new BinaryWriter(dataStream)) + { + long countPos = dataWriter.BaseStream.Position; + int counter = 0; + dataWriter.Write(counter); + + foreach (var list in MMap) + { + foreach (T item in list.Value) + { + dataWriter.Write(list.Key.ToByteArray()); + byte[] data = store(item); + dataWriter.Write(data.Length); + dataWriter.Write(data); + counter++; + } + } + + dataWriter.Seek((int)countPos, SeekOrigin.Begin); + dataWriter.Write(counter); + } + return dataStream.ToArray(); + } + } + + public static Dictionary> GetGuidMMap(byte[] data, Func load) + { + if (data.Length == 0) + return null; + Dictionary> rules = new Dictionary>(); + using (MemoryStream dataStream = new MemoryStream(data)) + { + var dataReader = new BinaryReader(dataStream); + + int count = dataReader.ReadInt32(); + for (int i = 0; i < count; i++) + { + Guid id = new Guid(dataReader.ReadBytes(16)); + int length = dataReader.ReadInt32(); + List list; + if (!rules.TryGetValue(id, out list)) + { + list = new List(); + rules.Add(id, list); + } + list.Add(load(dataReader.ReadBytes(length))); + } + } + return rules; + } + + public static byte[] PutXmlObj(T obj) + { + DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(T)); + using (MemoryStream xmlStream = new MemoryStream()) + { + xmlSerializer.WriteObject(xmlStream, obj); + return xmlStream.ToArray(); + } + } + + public static T GetXmlObj(byte[] value) + { + DataContractSerializer xmlSerializer = new DataContractSerializer(typeof(T)); + using (MemoryStream stream = new MemoryStream(value)) + { + T obj = (T)xmlSerializer.ReadObject(stream); + return obj; + } + } + } +} diff --git a/PrivateAPI/IPC/Priv10Logger.cs b/PrivateAPI/IPC/Priv10Logger.cs new file mode 100644 index 0000000..e6244ca --- /dev/null +++ b/PrivateAPI/IPC/Priv10Logger.cs @@ -0,0 +1,86 @@ +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrivateAPI +{ + public static class Priv10Logger + { + public enum EventIDs : long + { + Undefined = 0x0000, + + // generic + Exception, + AppError, + AppWarning, + AppInfo, + + TweakBegin = 0x0100, + TweakChanged, + TweakFixed, + TweakError, + TweakEnd = 0x01FF, + + FirewallBegin = 0x0200, + RuleChanged, + RuleDeleted, + RuleAdded, + //FirewallNewProg + FirewallEnd = 0x02FF, + } + + public enum EventFlags : short + { + DebugEvents = 0x0100, + AppLogEntries = 0x0200, + Notifications = 0x0400, // Show a Notification + PopUpMessages = 0x0800, // Show a PopUp Message + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + // Event Logging + + static public void LogCriticalError(string message, params object[] args) + { +#if DEBUG + Debugger.Break(); +#endif + LogError("Critical Error: " + message, args); + } + + static public void LogError(string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Error, (long)EventIDs.AppError, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); + } + + static public void LogError(EventIDs eventID, Dictionary Params, EventFlags flags, string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Error, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); + } + + static public void LogWarning(string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Warning, (long)EventIDs.AppWarning, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); + } + + static public void LogWarning(EventIDs eventID, Dictionary Params, EventFlags flags, string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Warning, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); + } + + static public void LogInfo(string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Information, (long)EventIDs.AppInfo, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); + } + + static public void LogInfo(EventIDs eventID, Dictionary Params, EventFlags flags, string message, params object[] args) + { + AppLog.Add(EventLogEntryType.Information, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); + } + } +} diff --git a/PrivateAPI/PrivateAPI.csproj b/PrivateAPI/PrivateAPI.csproj new file mode 100644 index 0000000..6a19dce --- /dev/null +++ b/PrivateAPI/PrivateAPI.csproj @@ -0,0 +1,60 @@ + + + + + Debug + AnyCPU + {2370EE89-9321-408D-9806-90720B6DDEA4} + Library + Properties + PrivateAPI + PrivateAPI + v4.5 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + {694c85c2-bb43-4525-a953-709050002631} + MiscHelpers + + + + \ No newline at end of file diff --git a/PrivateAPI/Properties/AssemblyInfo.cs b/PrivateAPI/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3fec604 --- /dev/null +++ b/PrivateAPI/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PrivateIPC")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PrivateIPC")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("2370ee89-9321-408d-9806-90720b6ddea4")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PrivateWin10/API/EtwLogger.cs b/PrivateService/API/EtwLogger.cs similarity index 100% rename from PrivateWin10/API/EtwLogger.cs rename to PrivateService/API/EtwLogger.cs diff --git a/PrivateService/App.config b/PrivateService/App.config new file mode 100644 index 0000000..fad249e --- /dev/null +++ b/PrivateService/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/PrivateService/Common/AdminFunc.cs b/PrivateService/Common/AdminFunc.cs new file mode 100644 index 0000000..5ed9289 --- /dev/null +++ b/PrivateService/Common/AdminFunc.cs @@ -0,0 +1,34 @@ + +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Security.AccessControl; +using System.Security.Principal; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +public class AdminFunc +{ + public static bool IsAdministrator() + { + WindowsIdentity identity = WindowsIdentity.GetCurrent(); + WindowsPrincipal principal = new WindowsPrincipal(identity); + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } + + [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] + static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerPresent); + + static public bool IsDebugging() + { + bool isDebuggerPresent = false; + CheckRemoteDebuggerPresent(Process.GetCurrentProcess().Handle, ref isDebuggerPresent); + return isDebuggerPresent; + } +} + diff --git a/PrivateWin10/Core/DnsInspector.cs b/PrivateService/Core/DnsInspector.cs similarity index 93% rename from PrivateWin10/Core/DnsInspector.cs rename to PrivateService/Core/DnsInspector.cs index cce05cd..333ccd1 100644 --- a/PrivateWin10/Core/DnsInspector.cs +++ b/PrivateService/Core/DnsInspector.cs @@ -2,10 +2,13 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Runtime.Serialization; using System.Text; using System.Threading; using System.Threading.Tasks; - +using MiscHelpers; +using PrivateAPI; +using PrivateService; namespace PrivateWin10 { @@ -81,7 +84,9 @@ private void OnDnsQueryWatched(object sender, DnsQueryWatcher.DnsEvent Event) List Services = ServiceHelper.GetServicesByPID(Event.ProcessId); ProgramID ProgID = App.engine.GetProgIDbyPID(Event.ProcessId, (Services == null || Services.Count > 1) ? null : Services[0].ServiceName); if (ProgID == null) - App.LogWarning("Watched a DNS query for a terminated process with id {0}", Event.ProcessId); + { + Priv10Logger.LogWarning("Watched a DNS query '{0}' for a terminated process with id {1}", Event.HostName, Event.ProcessId); + } else { Program prog = App.engine.ProgramList.FindProgram(ProgID, true, ProgramList.FuzzyModes.Any); @@ -234,10 +239,14 @@ public void AddDnsCacheEntry(DnsCacheMonitor.DnsCacheEntry curEntry) [Serializable()] + [DataContract(Name = "WithHost", Namespace = "http://schemas.datacontract.org/")] public class WithHost { + [DataMember()] public NameSources RemoteHostNameSource = NameSources.None; + [DataMember()] public string RemoteHostName = ""; + [DataMember()] public string RemoteHostNameAlias = ""; public class ChangeEventArgs : EventArgs diff --git a/PrivateWin10/Core/DnsInspector/DnsCacheMonitor.cs b/PrivateService/Core/DnsInspector/DnsCacheMonitor.cs similarity index 95% rename from PrivateWin10/Core/DnsInspector/DnsCacheMonitor.cs rename to PrivateService/Core/DnsInspector/DnsCacheMonitor.cs index 60e9a00..cdb95dc 100644 --- a/PrivateWin10/Core/DnsInspector/DnsCacheMonitor.cs +++ b/PrivateService/Core/DnsInspector/DnsCacheMonitor.cs @@ -5,17 +5,25 @@ using System.Threading.Tasks; using System.Runtime.InteropServices; using System.Net; +using MiscHelpers; +using PrivateService; +using System.Runtime.Serialization; namespace PrivateWin10 { public class DnsCacheMonitor: IDisposable { [Serializable()] + [DataContract(Name = "DnsCacheEntry", Namespace = "http://schemas.datacontract.org/")] public class DnsCacheEntry { + [DataMember()] public string HostName; + [DataMember()] public DnsApi.DnsRecordType RecordType = DnsApi.DnsRecordType.ANY; + [DataMember()] public IPAddress Address; + [DataMember()] public String ResolvedString; public enum States { @@ -24,8 +32,11 @@ public enum States Blocked, NotFound } + [DataMember()] public States State = States.Unknown; + [DataMember()] public DateTime TimeStamp = DateTime.Now; + [DataMember()] public DateTime ExpirationTime; public DateTime GetAge() { diff --git a/PrivateWin10/Core/DnsInspector/DnsQueryWatcher.cs b/PrivateService/Core/DnsInspector/DnsQueryWatcher.cs similarity index 95% rename from PrivateWin10/Core/DnsInspector/DnsQueryWatcher.cs rename to PrivateService/Core/DnsInspector/DnsQueryWatcher.cs index f79795c..67c6c72 100644 --- a/PrivateWin10/Core/DnsInspector/DnsQueryWatcher.cs +++ b/PrivateService/Core/DnsInspector/DnsQueryWatcher.cs @@ -1,10 +1,13 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading; using System.Threading.Tasks; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -57,7 +60,7 @@ public DnsQueryWatcher() } catch { - App.LogError("Failed to initialized DnsInspectorETW"); + Priv10Logger.LogError("Failed to initialized DnsInspectorETW"); } } diff --git a/PrivateWin10/Core/DnsInspector/HostNameResolver.cs b/PrivateService/Core/DnsInspector/HostNameResolver.cs similarity index 96% rename from PrivateWin10/Core/DnsInspector/HostNameResolver.cs rename to PrivateService/Core/DnsInspector/HostNameResolver.cs index 2049a23..5e13c93 100644 --- a/PrivateWin10/Core/DnsInspector/HostNameResolver.cs +++ b/PrivateService/Core/DnsInspector/HostNameResolver.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; @@ -7,6 +8,7 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using PrivateService; namespace PrivateWin10 { diff --git a/PrivateWin10/Core/DnsProxy/DnsBlockList.cs b/PrivateService/Core/DnsProxy/DnsBlockList.cs similarity index 92% rename from PrivateWin10/Core/DnsProxy/DnsBlockList.cs rename to PrivateService/Core/DnsProxy/DnsBlockList.cs index 996481b..e98a3ac 100644 --- a/PrivateWin10/Core/DnsProxy/DnsBlockList.cs +++ b/PrivateService/Core/DnsProxy/DnsBlockList.cs @@ -12,12 +12,18 @@ using System.Collections; using System.Threading; using System.Xml; +using MiscHelpers; +using PrivateService; +using System.Runtime.Serialization; +using PrivateAPI; namespace PrivateWin10 { [Serializable()] + [DataContract(Name = "DomainFilter", Namespace = "http://schemas.datacontract.org/")] public class DomainFilter { + [DataMember()] public bool Enabled = true; public enum Formats { @@ -25,20 +31,31 @@ public enum Formats RegExp, WildCard } + [DataMember()] public Formats Format = Formats.Plain; + [DataMember()] public string Domain = ""; + [DataMember()] public int HitCount = 0; + [DataMember()] public DateTime? LastHit = null; } [Serializable()] + [DataContract(Name = "DomainBlocklist", Namespace = "http://schemas.datacontract.org/")] public class DomainBlocklist { + [DataMember()] public bool Enabled = true; + [DataMember()] public string Url = ""; + [DataMember()] public DateTime? LastUpdate = null; + [DataMember()] public int EntryCount = 0; + [DataMember()] public string Status = ""; + [DataMember()] public string FileName = ""; } @@ -394,7 +411,7 @@ public bool Load() double.TryParse(xDoc.DocumentElement.GetAttribute("Version"), out fileVersion); if (fileVersion != xmlVersion) { - App.LogError("Failed to load DNS Blocklist, unknown file version {0}, expected {1}", fileVersion, xmlVersion); + Priv10Logger.LogError("Failed to load DNS Blocklist, unknown file version {0}, expected {1}", fileVersion, xmlVersion); return false; } @@ -602,7 +619,7 @@ public Tuple LoadBlockLists() } /*catch (DirectoryNotFoundException) { - App.LogError("Could not load blocklists from {0}", App.dataPath + @"\DnsBlockLists"); + Priv10Logger.LogError("Could not load blocklists from {0}", App.dataPath + @"\DnsBlockLists"); }*/ catch (Exception err) { @@ -651,11 +668,11 @@ public int LoadBlockList(string BlockListPath) } if (success == 0) - App.LogError("Failed to load blocklist: {0}", Path.GetFileName(BlockListPath)); + Priv10Logger.LogError("Failed to load blocklist: {0}", Path.GetFileName(BlockListPath)); else if (success < count) - App.LogWarning("Loaded {1} DNS blocklist entries from {0}, {2} entries were invalid", Path.GetFileName(BlockListPath), success, count - success); + Priv10Logger.LogWarning("Loaded {1} DNS blocklist entries from {0}, {2} entries were invalid", Path.GetFileName(BlockListPath), success, count - success); else - App.LogInfo("Loaded {1} DNS blocklist entries from {0}", Path.GetFileName(BlockListPath), success); + Priv10Logger.LogInfo("Loaded {1} DNS blocklist entries from {0}", Path.GetFileName(BlockListPath), success); return success; } @@ -765,7 +782,7 @@ public bool RefreshDomainBlocklist(string Url = "") // empty means all private void DownloadsFinished() { LoadBlockLists(); - App.LogInfo("Filished Updating blocklists"); + Priv10Logger.LogInfo("Filished Updating blocklists"); } private List PendingDownloads = new List(); @@ -798,12 +815,12 @@ private void DownloadNextFile() mCurTask.Finished += OnFinished; if (!mCurTask.Start()) { - App.LogError("Failed to start download of blocklist: {0}", Url); + Priv10Logger.LogError("Failed to start download of blocklist: {0}", Url); PendingDownloads.Remove(Url); DownloadNextFile(); } else - App.LogInfo("Started downloading blocklist: {0}", Url); + Priv10Logger.LogInfo("Started downloading blocklist: {0}", Url); } void OnFinished(object sender, HttpTask.FinishedEventArgs args) @@ -823,9 +840,9 @@ void OnFinished(object sender, HttpTask.FinishedEventArgs args) { Blocklist.Status = "Download Error"; // todo localize - App.LogError("Blocklist download failed: {0}; Reason: {1}", mCurTask.DlUrl, args.GetError()); + Priv10Logger.LogError("Blocklist download failed: {0}; Reason: {1}", mCurTask.DlUrl, args.GetError()); if (mCurTask.DlName != null && File.Exists(mCurTask.DlPath + @"\" + mCurTask.DlName)) - App.LogWarning("An older version of the Blocklist is present and will be used."); + Priv10Logger.LogWarning("An older version of the Blocklist is present and will be used."); } else Blocklist.Status = "Downloaded"; // todo localize diff --git a/PrivateWin10/Core/DnsProxy/DnsProxyServer.cs b/PrivateService/Core/DnsProxy/DnsProxyServer.cs similarity index 94% rename from PrivateWin10/Core/DnsProxy/DnsProxyServer.cs rename to PrivateService/Core/DnsProxy/DnsProxyServer.cs index 0e73dca..1ca8b28 100644 --- a/PrivateWin10/Core/DnsProxy/DnsProxyServer.cs +++ b/PrivateService/Core/DnsProxy/DnsProxyServer.cs @@ -3,6 +3,7 @@ using DNS.Protocol; using DNS.Protocol.ResourceRecords; using Microsoft.Win32; +using MiscHelpers; using System; using System.Collections.Generic; using System.Linq; @@ -12,6 +13,8 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -55,13 +58,13 @@ public bool Init() } catch { - App.LogError("Failed to start DNS server"); + Priv10Logger.LogError("Failed to start DNS server"); return false; } if (!blockList.Load()) { - App.LogError("Restoring default blocklist list, as loading from file failed!"); + Priv10Logger.LogError("Restoring default blocklist list, as loading from file failed!"); blockList.AddDefaultLists(); } blockList.LoadBlockLists(); diff --git a/PrivateWin10/Core/NetworkMonitor.cs b/PrivateService/Core/NetworkMonitor.cs similarity index 97% rename from PrivateWin10/Core/NetworkMonitor.cs rename to PrivateService/Core/NetworkMonitor.cs index 94d7753..7807ffe 100644 --- a/PrivateWin10/Core/NetworkMonitor.cs +++ b/PrivateService/Core/NetworkMonitor.cs @@ -11,6 +11,9 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using MiscHelpers; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -64,7 +67,7 @@ public NetworkMonitor() } catch { - App.LogError("Failed to initialized NetworkMonitorETW"); + Priv10Logger.LogError("Failed to initialized NetworkMonitorETW"); } regWatchers = new RegistryMonitor[] { diff --git a/PrivateWin10/Core/NetworkSocket.cs b/PrivateService/Core/NetworkSocket.cs similarity index 71% rename from PrivateWin10/Core/NetworkSocket.cs rename to PrivateService/Core/NetworkSocket.cs index 12b8262..ade82f5 100644 --- a/PrivateWin10/Core/NetworkSocket.cs +++ b/PrivateService/Core/NetworkSocket.cs @@ -2,34 +2,57 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; +using MiscHelpers; +using PrivateService; namespace PrivateWin10 { [Serializable()] + [DataContract(Name = "NetworkSocket", Namespace = "http://schemas.datacontract.org/")] public class NetworkSocket : WithHost { + [DataMember()] public Guid guid; + [DataMember()] public ProgramID ProgID; public Program Program = null; + [DataMember()] public UInt64 RemovedTimeStamp = 0; public UInt64 HashID; + [DataMember()] public int ProcessId; + [DataMember()] public UInt32 ProtocolType; + [DataMember()] public IPAddress LocalAddress; + [DataMember()] public UInt16 LocalPort; + [DataMember()] public IPAddress RemoteAddress; + [DataMember()] public UInt16 RemotePort; + [DataMember()] public DateTime CreationTime = DateTime.Now; + [DataMember()] public int State; + + [DataMember()] public Tuple Access = Tuple.Create(0, 0); // outbound, inbound public NetworkStats Stats; + [DataMember()] + public UInt64 UploadRate { get { return Stats.UploadRate.ByteRate; } set { } } + [DataMember()] + public UInt64 DownloadRate { get { return Stats.DownloadRate.ByteRate; } set { } } + + [DataMember()] public DateTime LastActivity; public NetworkSocket() @@ -151,37 +174,5 @@ public void CountDownload(uint transferSize) Stats.ReceivedBytes += transferSize; LastActivity = DateTime.Now; } - - public string GetStateString() - { - if(RemovedTimeStamp != 0) - return Translate.fmt("str_closed"); - - if ((ProtocolType & (UInt32)IPHelper.AF_PROT.UDP) == (UInt32)IPHelper.AF_PROT.UDP) - { - if (State == (int)IPHelper.MIB_TCP_STATE.CLOSED) - return Translate.fmt("str_closed"); - return Translate.fmt("str_open"); - } - - // all these are TCP states - switch (State) - { - case (int)IPHelper.MIB_TCP_STATE.CLOSED: return Translate.fmt("str_closed"); - case (int)IPHelper.MIB_TCP_STATE.LISTENING: return Translate.fmt("str_listen"); - case (int)IPHelper.MIB_TCP_STATE.SYN_SENT: return Translate.fmt("str_syn_sent"); - case (int)IPHelper.MIB_TCP_STATE.SYN_RCVD: return Translate.fmt("str_syn_received"); - case (int)IPHelper.MIB_TCP_STATE.ESTABLISHED: return Translate.fmt("str_established"); - case (int)IPHelper.MIB_TCP_STATE.FIN_WAIT1: return Translate.fmt("str_fin_wait_1"); - case (int)IPHelper.MIB_TCP_STATE.FIN_WAIT2: return Translate.fmt("str_fin_wait_2"); - case (int)IPHelper.MIB_TCP_STATE.CLOSE_WAIT: return Translate.fmt("str_close_wait"); - case (int)IPHelper.MIB_TCP_STATE.CLOSING: return Translate.fmt("str_closing"); - case (int)IPHelper.MIB_TCP_STATE.LAST_ACK: return Translate.fmt("str_last_ack"); - case (int)IPHelper.MIB_TCP_STATE.TIME_WAIT: return Translate.fmt("str_time_wait"); - case (int)IPHelper.MIB_TCP_STATE.DELETE_TCB: return Translate.fmt("str_delete_tcb"); - case -1: return Translate.fmt("str_fw_blocked"); - default: return Translate.fmt("str_undefined"); - } - } } } diff --git a/PrivateWin10/Core/Priv10Engine.cs b/PrivateService/Core/Priv10Engine.cs similarity index 92% rename from PrivateWin10/Core/Priv10Engine.cs rename to PrivateService/Core/Priv10Engine.cs index 04a738b..29a589d 100644 --- a/PrivateWin10/Core/Priv10Engine.cs +++ b/PrivateService/Core/Priv10Engine.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Eventing.Reader; @@ -9,6 +10,9 @@ using System.Threading; using System.Threading.Tasks; using System.Windows.Threading; +using TweakEngine; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -29,44 +33,19 @@ public class Priv10Engine //: IDisposable ManualResetEvent mStarted = new ManualResetEvent(false); //ManualResetEvent mFinished = new ManualResetEvent(false); DispatcherTimer mTimer; - //volatile bool mDoQuit = false; + volatile bool mDoQuit = false; DateTime LastSaveTime = DateTime.Now; #if DEBUG - List EtwLoggers = new List(); + //List EtwLoggers = new List(); #endif public delegate void FX(); - [Serializable()] - public class FwEventArgs : EventArgs + public enum UpdateTypes { - public Guid guid; - public Program.LogEntry entry; - public ProgramID progID; - public List services = null; - public bool update; - } - - [Serializable()] - public class UpdateArgs : EventArgs - { - public Guid guid; - public enum Types - { - ProgSet = 0, - Rules - } - public Types type; - } - - [Serializable()] - public class ChangeArgs : EventArgs - { - public Program prog; - public FirewallRuleEx rule; - public Priv10Engine.RuleEventType type; - public Priv10Engine.RuleFixAction action; + ProgSet = 0, + Rules } public void RunInEngineThread(FX fx) @@ -111,6 +90,14 @@ public void Run() mDispatcher = Dispatcher.CurrentDispatcher; // Init + FirewallManager = new FirewallManager(); + + if (!UwpFunc.IsWindows7OrLower) + { + Console.WriteLine("Initializing app manager..."); + PkgMgr = new AppManager(); + } + AppLog.Debug("Loading program list..."); ProgramList = new ProgramList(); ProgramList.Load(); @@ -158,16 +145,9 @@ public void Run() }); }; - if (!UwpFunc.IsWindows7OrLower) - { - Console.WriteLine("Initializing app manager..."); - PkgMgr = new AppManager(); - } - if (App.GetConfigInt("Startup", "LoadLog", 0) != 0) LoadLogAsync(); - FirewallManager = new FirewallManager(); LoadFwRules(); CleanupFwRules(); @@ -293,8 +273,8 @@ protected void OnTimerTick(object sender, EventArgs e) DnsProxy.Store(); } - //if (mDoQuit) - // mDispatcher.InvokeShutdown(); + if (mDoQuit) + mDispatcher.InvokeShutdown(); } public void Stop() @@ -558,13 +538,13 @@ public void LoadLogAsync() public void NotifyProgramUpdate(Guid guid) { if (App.host != null) - App.host.NotifyUpdate(guid, UpdateArgs.Types.ProgSet); + App.host.NotifyUpdate(guid, UpdateTypes.ProgSet); } public void NotifyRulesUpdte(Guid guid) { if (App.host != null) - App.host.NotifyUpdate(guid, UpdateArgs.Types.Rules); + App.host.NotifyUpdate(guid, UpdateTypes.Rules); } public void OnRulesUpdated(ProgramSet progSet) @@ -646,7 +626,7 @@ protected void ProcessRuleChanges() prog.Rules.TryGetValue(changeEvent.RuleId, out knownRule); if(knownRule == null) - App.LogCriticalError("rule lists are inconsistent"); + Priv10Logger.LogCriticalError("rule lists are inconsistent"); } FirewallRule rule = null; @@ -695,7 +675,8 @@ public bool LoadFwRules() bool bApproveAll = OldRules.Count == 0; if (bApproveAll) - App.LogInfo(Translate.fmt("msg_rules_approved")); + Priv10Logger.LogInfo("All valid Firewall Rules have been approved."); + //Priv10Logger.LogInfo(Translate.fmt("msg_rules_approved")); foreach (FirewallRule rule in rules) { @@ -761,7 +742,7 @@ protected void OnRuleAdded(FirewallRule rule, bool bApproved = false) Program prog = ProgramList.FindProgram(rule.ProgID, true); if (prog.Rules.ContainsKey(rule.guid)) { - App.LogCriticalError("rule lists are inconsistent 2"); + Priv10Logger.LogCriticalError("rule lists are inconsistent 2"); return; } @@ -970,39 +951,44 @@ private void LogRuleEvent(Program prog, FirewallRuleEx rule, RuleEventType type, Params.Add("ProgID", prog.ID.AsString()); Params.Add("SetGuid", prog.ProgSet.guid.ToString()); - App.EventIDs EventID; + Priv10Logger.EventIDs EventID; string strEvent; switch (type) { - case RuleEventType.Added: strEvent = Translate.fmt("msg_rule_added"); - EventID = App.EventIDs.RuleAdded; break; - case RuleEventType.Removed: strEvent = Translate.fmt("msg_rule_removed"); - EventID = App.EventIDs.RuleDeleted; break; - default: strEvent = Translate.fmt("msg_rule_changed"); - EventID = App.EventIDs.RuleChanged; break; + case RuleEventType.Added: strEvent = "Added"; //Translate.fmt("msg_rule_added"); + EventID = Priv10Logger.EventIDs.RuleAdded; break; + case RuleEventType.Removed: strEvent = "Removed"; //Translate.fmt("msg_rule_removed"); + EventID = Priv10Logger.EventIDs.RuleDeleted; break; + default: strEvent = "Changed"; //Translate.fmt("msg_rule_changed"); + EventID = Priv10Logger.EventIDs.RuleChanged; break; } string RuleName = App.GetResourceStr(rule.Name); string Message; // "Firewall rule \"{0}\" for \"{1}\" was {2}." - switch (action) + /*switch (action) { case RuleFixAction.Disabled: Message = Translate.fmt("msg_rule_event", RuleName, prog.Description, strEvent + Translate.fmt("msg_rule_disabled")); break; case RuleFixAction.Restored: Message = Translate.fmt("msg_rule_event", RuleName, prog.Description, strEvent + Translate.fmt("msg_rule_restored")); break; default: Message = Translate.fmt("msg_rule_event", RuleName, prog.Description, strEvent); break; + }*/ + switch (action) + { + case RuleFixAction.Disabled: Message = string.Format("Firewall rule \"{0}\" for \"{1}\" was {2}, the rule has been disabled.", RuleName, prog.Description, strEvent); break; + case RuleFixAction.Restored: Message = string.Format("Firewall rule \"{0}\" for \"{1}\" was {2}, the original rule was restored.", RuleName, prog.Description, strEvent); break; + default: Message = string.Format("Firewall rule \"{0}\" for \"{1}\" was {2}.", RuleName, prog.Description, strEvent); break; } if (type == RuleEventType.UnChanged || action == RuleFixAction.Restored) - App.LogInfo(EventID, Params, App.EventFlags.AppLogEntries, Message); + Priv10Logger.LogInfo(EventID, Params, Priv10Logger.EventFlags.AppLogEntries, Message); else - App.LogWarning(EventID, Params, App.EventFlags.AppLogEntries, Message); - - + Priv10Logger.LogWarning(EventID, Params, Priv10Logger.EventFlags.AppLogEntries, Message); } public void ApproveRules() { - App.LogInfo(Translate.fmt("msg_rules_approved")); + //Priv10Logger.LogInfo(Translate.fmt("msg_rules_approved")); + Priv10Logger.LogInfo("All valid Firewall Rules have been approved."); foreach (Program prog in ProgramList.Programs.Values) { @@ -1228,16 +1214,16 @@ private void OnRulesUpdatedEx(Program prog) NotifyProgramUpdate(prog.ProgSet.guid); } - public bool RemoveRule(FirewallRule rule) + public bool RemoveRule(string guid, ProgramID ProgID) { return mDispatcher.Invoke(new Func(() => { - if (!FirewallManager.RemoveRule(rule.guid)) + if (!FirewallManager.RemoveRule(guid)) return false; - var prog = ProgramList.FindProgram(rule.ProgID); + var prog = ProgramList.FindProgram(ProgID); if (prog != null) { - prog.Rules.Remove(rule.guid); + prog.Rules.Remove(guid); OnRulesUpdated(prog); } @@ -1300,17 +1286,17 @@ public enum ApprovalMode ApproveChanges } - public int SetRuleApproval(ApprovalMode Mode, FirewallRule rule) + public int SetRuleApproval(ApprovalMode Mode, string guid, ProgramID ProgID) { return mDispatcher.Invoke(new Func(() => { int count = 0; - if (rule != null) + if (guid != null) { - var prog = ProgramList.FindProgram(rule.ProgID); + var prog = ProgramList.FindProgram(ProgID); if (prog != null) { FirewallRuleEx ruleEx; - if (prog.Rules.TryGetValue(rule.guid, out ruleEx)) + if (prog.Rules.TryGetValue(guid, out ruleEx)) { if (Mode != ApprovalMode.RestoreRules ? ApproveRule(Mode == ApprovalMode.ApproveChanges, prog, ruleEx) : RestoreRule(prog, ruleEx)) count++; @@ -1584,34 +1570,34 @@ public bool RefreshDomainBlocklist(string Url = "") // empty means all ///////////////////////////////////////// // Privacy tweaks - public bool ApplyTweak(TweakManager.Tweak tweak) + public bool ApplyTweak(TweakList.Tweak tweak) { return mDispatcher.Invoke(new Func(() => { - return TweakEngine.ApplyTweak(tweak); + return TweakEngine.TweakTools.ApplyTweak(tweak); })); } - public bool TestTweak(TweakManager.Tweak tweak) + public bool TestTweak(TweakList.Tweak tweak) { return mDispatcher.Invoke(new Func(() => { - return TweakEngine.TestTweak(tweak); + return TweakEngine.TweakTools.TestTweak(tweak); })); } - public bool UndoTweak(TweakManager.Tweak tweak) + public bool UndoTweak(TweakList.Tweak tweak) { return mDispatcher.Invoke(new Func(() => { - return TweakEngine.UndoTweak(tweak); + return TweakEngine.TweakTools.UndoTweak(tweak); })); } ///////////////////////////////////////// // Misc - /*public bool Quit() + public bool Quit() { mDoQuit = true; return true; - }*/ + } } } diff --git a/PrivateService/Core/Priv10Service.cs b/PrivateService/Core/Priv10Service.cs new file mode 100644 index 0000000..a57f48a --- /dev/null +++ b/PrivateService/Core/Priv10Service.cs @@ -0,0 +1,81 @@ +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using PrivateService; +using PrivateAPI; + +namespace PrivateWin10 +{ + public class Priv10Service : ServiceBase + { + public Priv10Service() + { + CanHandlePowerEvent = true; + CanHandleSessionChangeEvent = true; + CanPauseAndContinue = true; + CanShutdown = true; + ServiceName = App.SvcName; + + CanHandleSessionChangeEvent = true; + CanPauseAndContinue = false; + } + + protected override void OnSessionChange(SessionChangeDescription sesionChangeDescription) + { + //com channel_.SessionChanged(sesionChangeDescription.SessionId); + base.OnSessionChange(sesionChangeDescription); + } + + protected override void OnStart(string[] args) + { + Priv10Logger.LogInfo("priv10 Service starting"); + + Thread thread = new Thread(new ThreadStart(Run)); + thread.IsBackground = true; + thread.SetApartmentState(ApartmentState.STA); // needed for tweaks + thread.Start(); + + //Priv10Logger.LogInfo("priv10 Service started"); + } + + private void Run() + { + try + { + App.engine.Run(); + + this.Stop(); + } + catch + { + ExitCode = -1; + Environment.Exit(-1); + } + } + + protected override void OnStop() + { + try + { + Priv10Logger.LogInfo("priv10 Service stopping..."); + + App.engine.Stop(); + + Priv10Logger.LogInfo("priv10 Service stopped"); + } + catch { } + base.OnStop(); + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + } + + } +} diff --git a/PrivateWin10/Core/ProcessMonitor.cs b/PrivateService/Core/ProcessMonitor.cs similarity index 95% rename from PrivateWin10/Core/ProcessMonitor.cs rename to PrivateService/Core/ProcessMonitor.cs index 38e6361..13a9cc3 100644 --- a/PrivateWin10/Core/ProcessMonitor.cs +++ b/PrivateService/Core/ProcessMonitor.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -7,6 +8,8 @@ using System.Text; using System.Threading; using System.Threading.Tasks; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -56,7 +59,7 @@ public ProcessMonitor() } catch { - App.LogError("Failed to initialized ProcessMonitorEtw"); + Priv10Logger.LogError("Failed to initialized ProcessMonitorEtw"); } } diff --git a/PrivateWin10/Core/Program.cs b/PrivateService/Core/Program.cs similarity index 85% rename from PrivateWin10/Core/Program.cs rename to PrivateService/Core/Program.cs index 7e07696..be43455 100644 --- a/PrivateWin10/Core/Program.cs +++ b/PrivateService/Core/Program.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -7,15 +8,20 @@ using System.Text; using System.Threading.Tasks; using System.Xml; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { [Serializable()] + [DataContract(Name = "Program", Namespace = "http://schemas.datacontract.org/")] public class Program { //public Guid guid; + [DataMember()] public ProgramID ID; + [DataMember()] public string Description; //[NonSerialized()] // Note: BinaryFormatter can handle circular references @@ -33,28 +39,46 @@ public class Program [NonSerialized()] public Dictionary DnsLog = new Dictionary(); + [DataMember()] public int RuleCount = 0; + [DataMember()] public int EnabledRules = 0; + [DataMember()] public int DisabledRules = 0; + [DataMember()] public int ChgedRules = 0; + [DataMember()] public DateTime LastAllowed = DateTime.MinValue; + [DataMember()] public int AllowedCount = 0; + [DataMember()] public DateTime LastBlocked = DateTime.MinValue; + [DataMember()] public int BlockedCount = 0; public DateTime LastActivity { get { return MiscFunc.Max(LastAllowed, LastBlocked); } } - private bool ActivityChanged = false; + //private bool ActivityChanged = false; + [DataMember()] public int SocketCount = 0; + [DataMember()] public int SocketsWeb = 0; + [DataMember()] public int SocketsTcp = 0; + [DataMember()] public int SocketsSrv = 0; + [DataMember()] public int SocketsUdp = 0; + [DataMember()] public UInt64 UploadRate = 0; + [DataMember()] public UInt64 DownloadRate = 0; + [DataMember()] public UInt64 TotalUpload = 0; + [DataMember()] public UInt64 TotalDownload = 0; + // The old values keep the last total of all closed sockets internal UInt64 OldUpload = 0; internal UInt64 OldDownload = 0; @@ -70,45 +94,6 @@ public void AssignSet(ProgramSet progSet) ProgSet.Programs.Add(ID, this); } - [OnSerializing] - private void OnSerializing(StreamingContext c) - { - RuleCount = 0; - EnabledRules = 0; - DisabledRules = 0; - ChgedRules = 0; - foreach (FirewallRuleEx rule in Rules.Values) - { - RuleCount++; - if (rule.Enabled) - EnabledRules++; - else - DisabledRules++; - if (rule.State != FirewallRuleEx.States.Approved) - ChgedRules++; - } - - SocketsWeb = 0; - SocketsTcp = 0; - SocketsUdp = 0; - SocketsSrv = 0; - foreach (NetworkSocket entry in Sockets.Values) - { - if ((entry.ProtocolType & (UInt32)IPHelper.AF_PROT.UDP) == (UInt32)IPHelper.AF_PROT.UDP) - { - SocketsUdp++; - } - else if ((entry.ProtocolType & (UInt32)IPHelper.AF_PROT.TCP) == (UInt32)IPHelper.AF_PROT.TCP) - { - SocketsTcp++; - if (entry.RemotePort == 80 || entry.RemotePort == 443) - SocketsWeb++; - if (entry.State == (int)IPHelper.MIB_TCP_STATE.LISTENING) - SocketsSrv++; - } - } - } - public Program() { //guid = Guid.NewGuid(); @@ -120,16 +105,21 @@ public Program(ProgramID progID) ID = progID.Duplicate(); + Description = GetDescription(); + } + + public string GetDescription() + { string Name = ""; string Info = null; switch (ID.Type) { case ProgramID.Types.System: - Name = Translate.fmt("name_system"); + Name = "Windows NT-Kernel/System"; // Translate.fmt("name_system"); break; case ProgramID.Types.Global: - Name = Translate.fmt("name_global"); + Name = "All Processes"; // Translate.fmt("name_global"); break; case ProgramID.Types.Program: Name = System.IO.Path.GetFileName(ID.Path); @@ -142,14 +132,13 @@ public Program(ProgramID progID) case ProgramID.Types.App: Name = ID.GetPackageName(); var AppPkg = App.engine.FirewallManager.GetAppPkgBySid(ID.GetPackageSID()); - Info = AppPkg?.Name; + Info = App.GetResourceStr(AppPkg?.Name); break; } if (Info != null && Info.Length > 0) - Description = Info + " (" + Name + ")"; - else - Description = Name; + return Info + " (" + Name + ")"; + return Name; } public bool Update() @@ -167,6 +156,10 @@ public bool Update() Entry.TotalDownload = Entry.OldDownload; } + SocketsWeb = 0; + SocketsTcp = 0; + SocketsUdp = 0; + SocketsSrv = 0; foreach (NetworkSocket Socket in Sockets.Values) { uploadRate += Socket.Stats.UploadRate.ByteRate; @@ -182,11 +175,40 @@ public bool Update() Entry.TotalUpload += Socket.Stats.SentBytes; Entry.TotalDownload += Socket.Stats.ReceivedBytes; } + + if ((Socket.ProtocolType & (UInt32)IPHelper.AF_PROT.UDP) == (UInt32)IPHelper.AF_PROT.UDP) + { + SocketsUdp++; + } + else if ((Socket.ProtocolType & (UInt32)IPHelper.AF_PROT.TCP) == (UInt32)IPHelper.AF_PROT.TCP) + { + SocketsTcp++; + if (Socket.RemotePort == 80 || Socket.RemotePort == 443) + SocketsWeb++; + if (Socket.State == (int)IPHelper.MIB_TCP_STATE.LISTENING) + SocketsSrv++; + } + } + + RuleCount = 0; + EnabledRules = 0; + DisabledRules = 0; + ChgedRules = 0; + foreach (FirewallRuleEx rule in Rules.Values) + { + RuleCount++; + if (rule.Enabled) + EnabledRules++; + else + DisabledRules++; + if (rule.State != FirewallRuleEx.States.Approved) + ChgedRules++; } if (UploadRate != uploadRate || DownloadRate != downloadRate || TotalUpload != totalUpload || TotalDownload != totalDownload - || SocketCount != Sockets.Count || ActivityChanged) + || SocketCount != Sockets.Count //|| ActivityChanged + ) { SocketCount = Sockets.Count; @@ -196,7 +218,7 @@ public bool Update() TotalUpload = totalUpload; TotalDownload = totalDownload; - ActivityChanged = false; + //ActivityChanged = false; return true; } @@ -357,11 +379,15 @@ public Tuple LookupRuleAccess(NetworkSocket Socket) } [Serializable()] + [DataContract(Name = "LogEntry", Namespace = "http://schemas.datacontract.org/")] public class LogEntry : WithHost { + [DataMember()] public Guid guid; + [DataMember()] public ProgramID ProgID; + [DataMember()] public FirewallEvent FwEvent; public enum States @@ -373,6 +399,7 @@ public enum States RuleBlocked, RuleError, // a rule was found but it appears it was not obeyed (!) } + [DataMember()] public States State = States.Undefined; public void CheckAction(FirewallRule.Actions action) @@ -397,6 +424,10 @@ public void CheckAction(FirewallRule.Actions action) } } + public LogEntry() + { + } + public LogEntry(FirewallEvent Event, ProgramID progID) { guid = Guid.NewGuid(); @@ -455,34 +486,50 @@ public void RemoveSocket(NetworkSocket socket) [Serializable()] + [DataContract(Name = "DnsEntry", Namespace = "http://schemas.datacontract.org/")] public class DnsEntry { + [DataMember()] public Guid guid; + [DataMember()] public ProgramID ProgID; + [DataMember()] public string HostName; public bool Unresolved = false; //public IPAddress LastSeenIP; + [DataMember()] public DateTime LastSeen; + [DataMember()] public int SeenCounter = 0; + [DataMember()] public int ConCounter = 0; + [DataMember()] public UInt64 TotalUpload = 0; + [DataMember()] public UInt64 TotalDownload = 0; // The old values keep the last total of all closed sockets public int OldConCounter = 0; internal UInt64 OldUpload = 0; internal UInt64 OldDownload = 0; + public DnsEntry() + { + } + public DnsEntry(ProgramID progID) { guid = Guid.NewGuid(); ProgID = progID; } - public void Store(XmlWriter writer) + public void Store(XmlWriter writer, bool WithId = false) { writer.WriteStartElement("Entry"); + if(WithId) + ProgID.Store(writer); + writer.WriteElementString("HostName", HostName); writer.WriteElementString("LastSeen", LastSeen.ToString()); writer.WriteElementString("SeenCounter", SeenCounter.ToString()); @@ -498,7 +545,12 @@ public bool Load(XmlNode entryNode) { foreach (XmlNode node in entryNode.ChildNodes) { - if (node.Name == "HostName") + if (node.Name == "ID") + { + ProgID = new ProgramID(); + ProgID.Load(node); + } + else if (node.Name == "HostName") HostName = node.InnerText; else if (node.Name == "LastSeen") DateTime.TryParse(node.InnerText, out LastSeen); @@ -610,13 +662,13 @@ public bool Load(XmlNode entryNode) if (rule.Load(childNode) && !Rules.ContainsKey(rule.guid)) { // COMPAT: update entry, old version did not save these data separatly - if (ID.Type != ProgramID.Types.Global && (rule.BinaryPath == null && rule.ServiceTag == null && rule.AppSID == null)) - rule.SetProgID(ID); + //if (ID.Type != ProgramID.Types.Global && (rule.BinaryPath == null && rule.ServiceTag == null && rule.AppSID == null)) + // rule.SetProgID(ID); Rules.Add(rule.guid, rule); } else - App.LogError("Failed to load Firewall RuleEx {0} in {1}", rule.Name != null ? rule.Name : "[un named]", this.Description); + Priv10Logger.LogError("Failed to load Firewall RuleEx {0} in {1}", rule.Name != null ? rule.Name : "[un named]", this.Description); } } else if (node.Name == "DnsLog") @@ -627,12 +679,16 @@ public bool Load(XmlNode entryNode) if (Entry.Load(childNode) && !DnsLog.ContainsKey(Entry.HostName)) DnsLog.Add(Entry.HostName, Entry); else - App.LogError("Failed to load DnsLog Entry in {0}", this.Description); + Priv10Logger.LogError("Failed to load DnsLog Entry in {0}", this.Description); } } else AppLog.Debug("Unknown Program Value, '{0}':{1}", node.Name, node.InnerText); } + + if(Description == null || Description.Substring(0,2) == "@{") + Description = GetDescription(); + return ID != null; } } diff --git a/PrivateService/Core/ProgramID.cs b/PrivateService/Core/ProgramID.cs new file mode 100644 index 0000000..7570c21 --- /dev/null +++ b/PrivateService/Core/ProgramID.cs @@ -0,0 +1,251 @@ +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; +using System.Xml; + +namespace PrivateWin10 +{ + [Serializable()] + public class ProgramID : IComparable + { + public enum Types + { + Global = 0, + System, + Service, + Program, + App + } + + public Types Type { get; protected set; } = Types.Program; + public string Path { get; protected set; } = ""; // process path + public string Aux { get; protected set; } = ""; // service name or app package sid + + //[NonSerialized()] + //public string Description = ""; + + public static ProgramID NewID(Types Type) + { + return new ProgramID(Type, "", ""); + } + + public static ProgramID NewProgID(string Path) + { + return new ProgramID(Types.Program, Path, ""); + } + + public static ProgramID NewSvcID(string Svc, string Path = "") + { + return new ProgramID(Types.Service, Path == null ? "" : Path, Svc); + } + + public static ProgramID NewAppID(string SID, string Path = "") + { + return new ProgramID(Types.App, Path == null ? "" : Path, SID); + } + + public static ProgramID New(Types type, string path, string aux) + { + return new ProgramID(type, path, aux); + } + + public ProgramID() + { + } + + private ProgramID(Types type, string path, string aux) + { + Type = type; + Path = path; + Aux = aux; + } + + public ProgramID Duplicate() + { + return new ProgramID(Type, Path, Aux); + } + + public virtual int CompareTo(object obj) + { + if ((int)Type > (int)(obj as ProgramID).Type) + return 1; + else if ((int)Type < (int)(obj as ProgramID).Type) + return -1; + + if (Aux != null) + { + int ret = string.Compare(Aux, (obj as ProgramID).Aux, true); + if (ret != 0) + return ret; + } + + return Path == null ? 0 : string.Compare(Path, (obj as ProgramID).Path, true); + } + + public string GetPath() + { + if (Type == Types.Global) + return null; + if (Type == Types.System) + return MiscFunc.NtOsKrnlPath; + return Path; + } + + public string GetPackageSID() + { + if (Type != Types.App) + return null; + return Aux; + } + + public string GetPackageName() + { + if (Type != Types.App) + return null; + // ToDo: xxx pull that through the core + //return App.engine.FirewallManager.GetAppPkgBySid(Aux)?.ID; // this only works from core but we need to work from the UI to + return AppManager.SidToAppPackage(Aux); + } + + public string GetServiceId() + { + if (Type != Types.Service) + return null; + return Aux; + } + + public string GetServiceName() + { + if (Type != Types.Service) + return null; + return ServiceHelper.GetServiceName(Aux); + } + + public void Store(XmlWriter writer, string nodeName = "ID") + { + writer.WriteStartElement(nodeName); + + writer.WriteElementString("Type", Type.ToString()); + writer.WriteElementString("Path", Path); + writer.WriteElementString("Aux", Aux); + + writer.WriteEndElement(); + } + + public bool Load(XmlNode idNode) + { + try + { + Type = (Types)Enum.Parse(typeof(Types), idNode.SelectSingleNode("Type").InnerText); + Path = idNode.SelectSingleNode("Path").InnerText; + Aux = idNode.SelectSingleNode("Aux").InnerText; + } + catch + { + return false; + } + return true; + } + + public string FormatString() + { + switch (Type) + { + case Types.System: return MiscFunc.NtOsKrnlPath; + case Types.Service: return string.Format("{0} (service: {1})", Path, Aux); // Translate.fmt("name_service", Path, Aux); + case Types.Program: return Path; + case Types.App: return string.Format("{0} (app: {1})", Path, GetPackageName()); // Translate.fmt("name_app", Path, GetPackageName()); + default: + case Types.Global: return "All Processes"; // Translate.fmt("name_global"); + } + } + + public string AsString() + { + List tokens = new List(); + tokens.Add("Type=" + Type.ToString()); + if (Path != null && Path.Length > 0) + tokens.Add("Path=" + Path); + if (Aux != null && Aux.Length > 0) + tokens.Add("Aux=" + Aux); + return string.Join("|", tokens); + } + + public static ProgramID Parse(string Str) + { + try + { + ProgramID progID = new ProgramID(); + foreach (string token in TextHelpers.SplitStr(Str, "|")) + { + var IdVal = TextHelpers.Split2(token, "="); + if (IdVal.Item1 == "Type") + progID.Type = (Types)Enum.Parse(typeof(Types), IdVal.Item2); + else if (IdVal.Item1 == "Path") + progID.Path = IdVal.Item2; + else if (IdVal.Item1 == "Aux") + progID.Aux = IdVal.Item2; + } + return progID; + } + catch + { + return null; + } + } + + private bool? WindowsBinary = null; + + public bool IsProgram() + { + return !IsSystem() && Type == Types.Program; + } + + public bool IsSystem() + { + if (Type == ProgramID.Types.System || Type == ProgramID.Types.Global || Type == ProgramID.Types.Service) + return true; + + if (WindowsBinary == null) + WindowsBinary = MiscFunc.IsWindowsBinary(Path); + return WindowsBinary.Value; + } + + public bool IsApp() + { + return Type == Types.App; + } + } + + /*public class ProgIDSearch : ProgramID + { + public enum SearchMode + { + Strict = 0, + ForRule + } + + SearchMode Mode = SearchMode.Strict; + + public ProgIDSearch(ProgramID ID, SearchMode mode = SearchMode.Strict) + { + Type = ID.Type; + Path = ID.Path; + Aux = ID.Aux; + Mode = mode; + } + + public override int CompareTo(object obj) + { + if (Mode == SearchMode.ForRule) + { + } + else + return base.CompareTo(obj); + } + }*/ +} diff --git a/PrivateWin10/Core/ProgramList.cs b/PrivateService/Core/ProgramList.cs similarity index 93% rename from PrivateWin10/Core/ProgramList.cs rename to PrivateService/Core/ProgramList.cs index 46c57ab..55ae1bb 100644 --- a/PrivateWin10/Core/ProgramList.cs +++ b/PrivateService/Core/ProgramList.cs @@ -1,10 +1,13 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -283,7 +286,7 @@ public bool Load() double.TryParse(xDoc.DocumentElement.GetAttribute("Version"), out fileVersion); if (fileVersion != xmlVersion) { - App.LogError("Failed to load programlist, unknown file version {0}, expected {1}", fileVersion, xmlVersion); + Priv10Logger.LogError("Failed to load programlist, unknown file version {0}, expected {1}", fileVersion, xmlVersion); return false; } @@ -320,8 +323,8 @@ public bool Load() } if (ErrorCount != 0) - App.LogError("Failed to load {0} program entry out of {1}", ErrorCount, TotalCount); - App.LogInfo("ProgramList loaded {0} entries", TotalCount - ErrorCount); + Priv10Logger.LogError("Failed to load {0} program entry out of {1}", ErrorCount, TotalCount); + Priv10Logger.LogInfo("ProgramList loaded {0} entries", TotalCount - ErrorCount); } catch (Exception err) { diff --git a/PrivateWin10/Core/ProgramSet.cs b/PrivateService/Core/ProgramSet.cs similarity index 73% rename from PrivateWin10/Core/ProgramSet.cs rename to PrivateService/Core/ProgramSet.cs index b3c27e5..8a9db10 100644 --- a/PrivateWin10/Core/ProgramSet.cs +++ b/PrivateService/Core/ProgramSet.cs @@ -1,31 +1,43 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Xml; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { [Serializable()] + [DataContract(Name = "ProgramSet", Namespace = "http://schemas.datacontract.org/")] public class ProgramSet { + [DataMember()] public Guid guid; + [DataMember()] public SortedDictionary Programs = new SortedDictionary(); [Serializable()] + [DataContract(Name = "ProgramConfig", Namespace = "http://schemas.datacontract.org/")] public class Config { - public string Name; - public string Category; - public string Icon; + [DataMember()] + public string Name = ""; + [DataMember()] + public string Category = ""; + [DataMember()] + public string Icon = ""; public enum AccessLevels { Unconfigured = 0, FullAccess, + //OutBoundAccess, + //InBoundAccess, CustomConfig, LocalOnly, BlockAccess, @@ -34,12 +46,16 @@ public enum AccessLevels WarningState } + [DataMember()] public bool? Notify = null; public bool? GetNotify() { return IsSilenced() ? (bool?)false : Notify; } public void SetNotify(bool? set) { SilenceUntill = 0; Notify = set; } + [DataMember()] public UInt64 SilenceUntill = 0; public bool IsSilenced() { return SilenceUntill != 0 && SilenceUntill > MiscFunc.GetUTCTime(); } + [DataMember()] public AccessLevels NetAccess = AccessLevels.Unconfigured; + [DataMember()] public AccessLevels CurAccess = AccessLevels.Unconfigured; public AccessLevels GetAccess() { @@ -49,26 +65,11 @@ public AccessLevels GetAccess() return NetAccess; } - public Config Clone() - { - var config = new Config(); - - config.Name = this.Name; - config.Category = this.Category; - config.Icon = this.Icon; - - config.Notify = this.Notify; - config.SilenceUntill = this.SilenceUntill; - config.NetAccess = this.NetAccess; - config.CurAccess = this.CurAccess; - - return config; - } - // Custom option // todo } + [DataMember()] public Config config = new Config(); public ProgramSet() @@ -84,37 +85,6 @@ public ProgramSet(Program prog) config.Name = prog.Description; } - [OnSerializing()] - private void OnSerializing(StreamingContext context) - { - - } - - [OnSerialized()] - private void OnSerialized(StreamingContext context) - { - - } - - [OnDeserializing()] - private void OnDeserializing(StreamingContext context) - { - - } - - [OnDeserialized()] - private void OnDeserialized(StreamingContext context) - { - - } - - public string GetIcon() - { - if (config.Icon != null && config.Icon.Length > 0) - return config.Icon; - return Programs.First().Key.Path; - } - public bool IsSpecial() { foreach (Program prog in Programs.Values) @@ -153,7 +123,7 @@ public int CleanUp(bool ExtendedCleanup = false) Programs.Remove(prog.ID); - App.LogInfo("CleanUp Removed program: {0}", prog.ID.FormatString()); + Priv10Logger.LogInfo("CleanUp Removed program: {0}", prog.ID.FormatString()); Count++; } } @@ -162,36 +132,7 @@ public int CleanUp(bool ExtendedCleanup = false) ///////////////////////////////////////////////////////////// // merged data - - public DateTime GetLastActivity(bool Allowed = true, bool Blocked = true) - { - DateTime lastActivity = DateTime.MinValue; - foreach (Program prog in Programs.Values) - { - if (Allowed && prog.LastAllowed > lastActivity) - lastActivity = prog.LastAllowed; - if (Blocked && prog.LastBlocked > lastActivity) - lastActivity = prog.LastBlocked; - } - return lastActivity; - } - - public UInt64 GetDataRate() - { - UInt64 DataRate = 0; - foreach (Program prog in Programs.Values) - DataRate += prog.UploadRate + prog.DownloadRate; - return DataRate; - } - - public int GetSocketCount() - { - int SocketCount = 0; - foreach (Program prog in Programs.Values) - SocketCount += prog.SocketCount; - return SocketCount; - } - + public List GetRules() { List list = new List(); @@ -243,6 +184,8 @@ public void StoreSet(XmlWriter writer) { writer.WriteStartElement("ProgramSet"); + writer.WriteElementString("Guid", guid.ToString()); + writer.WriteElementString("Name", config.Name); if (config.Category != null && config.Category.Length > 0) writer.WriteElementString("Category", config.Category); @@ -283,6 +226,8 @@ public bool LoadSet(XmlNode entryNode) prog.AssignSet(this); } } + else if (node.Name == "Guid") + guid = Guid.Parse(node.InnerText); else if (node.Name == "Name") config.Name = node.InnerText; else if (node.Name == "Category") @@ -297,6 +242,9 @@ public bool LoadSet(XmlNode entryNode) AppLog.Debug("Unknown Program Value, '{0}':{1}", node.Name, node.InnerText); } + if(Programs.Count > 0 && config.Name == null || config.Name.Substring(0,2) == "@{") + config.Name = Programs.First().Value.Description; + return Programs.Count > 0 && config.Name != null; } } diff --git a/PrivateWin10/Core/WindowsFirewall/FirewallGuard.cs b/PrivateService/Core/WindowsFirewall/FirewallGuard.cs similarity index 97% rename from PrivateWin10/Core/WindowsFirewall/FirewallGuard.cs rename to PrivateService/Core/WindowsFirewall/FirewallGuard.cs index 48642b3..a80e41a 100644 --- a/PrivateWin10/Core/WindowsFirewall/FirewallGuard.cs +++ b/PrivateService/Core/WindowsFirewall/FirewallGuard.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; +using MiscHelpers; using System; using System.Collections.Generic; using System.Diagnostics.Eventing.Reader; diff --git a/PrivateWin10/Core/WindowsFirewall/FirewallManager.cs b/PrivateService/Core/WindowsFirewall/FirewallManager.cs similarity index 92% rename from PrivateWin10/Core/WindowsFirewall/FirewallManager.cs rename to PrivateService/Core/WindowsFirewall/FirewallManager.cs index 2664f6d..99866f7 100644 --- a/PrivateWin10/Core/WindowsFirewall/FirewallManager.cs +++ b/PrivateService/Core/WindowsFirewall/FirewallManager.cs @@ -1,6 +1,7 @@ //#define FW_COM_ITF using Microsoft.Win32; +using MiscHelpers; using System; using System.Collections.Generic; using System.IO; @@ -9,6 +10,7 @@ using System.Numerics; using System.Text; using System.Threading.Tasks; +using PrivateService; namespace PrivateWin10 { @@ -49,7 +51,7 @@ public void EvaluateRules(ProgramSet progSet, bool StrictTest = false) foreach (Program prog in progSet.Programs.Values) { RuleStat Stat = new RuleStat(); - + foreach (FirewallRule rule in prog.Rules.Values) { if (!rule.Enabled) @@ -116,6 +118,10 @@ public void EvaluateRules(ProgramSet progSet, bool StrictTest = false) progSet.config.CurAccess = ProgramSet.Config.AccessLevels.BlockAccess; else if ((MergedStat.AllowAll & (int)FirewallRule.Directions.Outboun) != 0 && (!StrictTest || (MergedStat.AllowAll & (int)FirewallRule.Directions.Inbound) != 0)) progSet.config.CurAccess = ProgramSet.Config.AccessLevels.FullAccess; + /*else if ((MergedStat.AllowAll & (int)FirewallRule.Directions.Outboun) != 0) + progSet.config.CurAccess = ProgramSet.Config.AccessLevels.OutBoundAccess; + else if ((MergedStat.AllowAll & (int)FirewallRule.Directions.Inbound) != 0) + progSet.config.CurAccess = ProgramSet.Config.AccessLevels.InBoundAccess;*/ else if ((MergedStat.AllowLan & (int)FirewallRule.Directions.Outboun) != 0 && (!StrictTest || ((MergedStat.AllowLan & (int)FirewallRule.Directions.Inbound) != 0 && (MergedStat.AllowLan & (int)FirewallRule.Directions.Inbound) != 0))) progSet.config.CurAccess = ProgramSet.Config.AccessLevels.LocalOnly; else if (enabledCound > 0) @@ -140,7 +146,7 @@ public void ApplyRules(ProgramSet progSet, UInt64 expiration = 0) { EvaluateRules(progSet, true); - if (progSet.config.NetAccess == ProgramSet.Config.AccessLevels.Unconfigured || progSet.config.NetAccess == ProgramSet.Config.AccessLevels.CustomConfig) + if (progSet.config.NetAccess == ProgramSet.Config.AccessLevels.Unconfigured) return; if (progSet.config.NetAccess == progSet.config.CurAccess) @@ -150,10 +156,17 @@ public void ApplyRules(ProgramSet progSet, UInt64 expiration = 0) { ClearRules(prog, progSet.config.NetAccess != ProgramSet.Config.AccessLevels.CustomConfig); + if (progSet.config.NetAccess == ProgramSet.Config.AccessLevels.CustomConfig) + continue; // dont create any rules + for (int i = 1; i <= 2; i++) { FirewallRule.Directions direction = (FirewallRule.Directions)i; + /*if((progSet.config.NetAccess == ProgramSet.Config.AccessLevels.InBoundAccess && direction != FirewallRule.Directions.Inbound) + && (progSet.config.NetAccess == ProgramSet.Config.AccessLevels.OutBoundAccess && direction != FirewallRule.Directions.Outboun)) + continue;*/ + switch (progSet.config.NetAccess) { case ProgramSet.Config.AccessLevels.FullAccess: diff --git a/PrivateWin10/Core/WindowsFirewall/FirewallMonitor.cs b/PrivateService/Core/WindowsFirewall/FirewallMonitor.cs similarity index 97% rename from PrivateWin10/Core/WindowsFirewall/FirewallMonitor.cs rename to PrivateService/Core/WindowsFirewall/FirewallMonitor.cs index 8ab2b84..5b85eec 100644 --- a/PrivateWin10/Core/WindowsFirewall/FirewallMonitor.cs +++ b/PrivateService/Core/WindowsFirewall/FirewallMonitor.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Eventing.Reader; diff --git a/PrivateWin10/Core/WindowsFirewall/FirewallRule.cs b/PrivateService/Core/WindowsFirewall/FirewallRule.cs similarity index 96% rename from PrivateWin10/Core/WindowsFirewall/FirewallRule.cs rename to PrivateService/Core/WindowsFirewall/FirewallRule.cs index 01363cf..abd135c 100644 --- a/PrivateWin10/Core/WindowsFirewall/FirewallRule.cs +++ b/PrivateService/Core/WindowsFirewall/FirewallRule.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -330,13 +331,13 @@ public virtual void Store(XmlWriter writer, bool bRaw = false) { if(!bRaw) writer.WriteStartElement("FwRule"); - writer.WriteElementString("Guid", guid); + if (guid != null) writer.WriteElementString("Guid", guid); if (BinaryPath != null) writer.WriteElementString("BinaryPath", BinaryPath); if (ServiceTag != null) writer.WriteElementString("ServiceTag", ServiceTag); if (AppSID != null) writer.WriteElementString("AppSID", AppSID); - if (!bRaw) ProgID.Store(writer, "ProgID"); + ProgID.Store(writer, "ProgID"); if (Name != null) writer.WriteElementString("Name", Name); if (Grouping != null) writer.WriteElementString("Grouping", Grouping); diff --git a/PrivateWin10/Core/WindowsFirewall/FirewallRuleEx.cs b/PrivateService/Core/WindowsFirewall/FirewallRuleEx.cs similarity index 100% rename from PrivateWin10/Core/WindowsFirewall/FirewallRuleEx.cs rename to PrivateService/Core/WindowsFirewall/FirewallRuleEx.cs diff --git a/PrivateWin10/Core/WindowsFirewall/WinNetFwAPI.cs b/PrivateService/Core/WindowsFirewall/WinNetFwAPI.cs similarity index 96% rename from PrivateWin10/Core/WindowsFirewall/WinNetFwAPI.cs rename to PrivateService/Core/WindowsFirewall/WinNetFwAPI.cs index cc53225..1629f0f 100644 --- a/PrivateWin10/Core/WindowsFirewall/WinNetFwAPI.cs +++ b/PrivateService/Core/WindowsFirewall/WinNetFwAPI.cs @@ -1,4 +1,5 @@ -using NetFwTypeLib; +using MiscHelpers; +using NetFwTypeLib; using System; using System.Collections.Generic; using System.ComponentModel; @@ -9,7 +10,9 @@ using System.Security.Principal; using System.Text; using System.Threading.Tasks; -using static PrivateWin10.WindowsFirewall; +using PrivateService; +using static MiscHelpers.WindowsFirewall; +using PrivateAPI; namespace PrivateWin10 { @@ -350,19 +353,19 @@ public bool UpdateRule(FirewallRule rule) catch { } if(message != null) - App.LogError("Failed to Write rule, status-code: {0} ({1})", entry.Status.ToString(), message); + Priv10Logger.LogError("Failed to Write rule, status-code: {0} ({1})", entry.Status.ToString(), message); else - App.LogError("Failed to Write rule, error-code: {0} ({1})", uRet.ToString(), new Win32Exception((int)uRet).Message); + Priv10Logger.LogError("Failed to Write rule, error-code: {0} ({1})", uRet.ToString(), new Win32Exception((int)uRet).Message); } else if (bAdd) { rule.Index = RuleCounter++; Rules.Add(rule.guid, FwRule); - App.LogInfo("Added rule: {0}", FwRule.Rule.Name); + Priv10Logger.LogInfo("Added rule: {0}", FwRule.Rule.Name); } else - App.LogInfo("Updated rule: {0}", FwRule.Rule.Name); + Priv10Logger.LogInfo("Updated rule: {0}", FwRule.Rule.Name); return uRet == ERROR_SUCCESS; } @@ -375,11 +378,11 @@ public bool RemoveRule(string guid) uint uRet = FWDeleteFirewallRule(policyHandle, FwRule.Rule.guid); // FwRule.Entry.wszRuleId if (uRet != ERROR_SUCCESS) - App.LogError("Failed to Remove rule, error-code: " + uRet.ToString()); + Priv10Logger.LogError("Failed to Remove rule, error-code: " + uRet.ToString()); else { Rules.Remove(guid); - App.LogInfo("Removed rule: {0}", FwRule.Rule.Name); + Priv10Logger.LogInfo("Removed rule: {0}", FwRule.Rule.Name); } return uRet == ERROR_SUCCESS; diff --git a/PrivateWin10/Core/WindowsFirewall/WinNetFwCom.cs b/PrivateService/Core/WindowsFirewall/WinNetFwCom.cs similarity index 95% rename from PrivateWin10/Core/WindowsFirewall/WinNetFwCom.cs rename to PrivateService/Core/WindowsFirewall/WinNetFwCom.cs index 8b5735b..8bbebca 100644 --- a/PrivateWin10/Core/WindowsFirewall/WinNetFwCom.cs +++ b/PrivateService/Core/WindowsFirewall/WinNetFwCom.cs @@ -12,6 +12,8 @@ using System.Windows; using System.Net; using System.Numerics; +using PrivateService; +using PrivateAPI; namespace PrivateWin10 { @@ -93,7 +95,7 @@ public bool UpdateRule(FirewallRule rule) FwRule = new NetFwRule() { Entry = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule")), Rule = rule }; else if (!Rules.TryGetValue(rule.guid, out FwRule)) { - App.LogError("Failed Updating rule: ruls is not longer present"); + Priv10Logger.LogError("Failed Updating rule: ruls is not longer present"); return false; } else @@ -113,7 +115,7 @@ public bool UpdateRule(FirewallRule rule) } catch (Exception err) { - App.LogError("Failed to Write rule: " + err.Message); + Priv10Logger.LogError("Failed to Write rule: " + err.Message); return false; } return true; @@ -139,7 +141,7 @@ public bool RemoveRule(FirewallRule rule) } catch (Exception err) { - App.LogError("Failed to Remove rule: " + err.Message); + Priv10Logger.LogError("Failed to Remove rule: " + err.Message); return false; } return true; @@ -264,7 +266,7 @@ An IPv6 address range in the format of "start address - end address" with no spa } catch (Exception err) { - App.LogError("Reading Firewall Rule failed {0}", err.ToString()); + Priv10Logger.LogError("Reading Firewall Rule failed {0}", err.ToString()); return false; } return true; @@ -390,7 +392,7 @@ public static bool SaveRule(FirewallRule rule, INetFwRule2 entry) } catch (Exception err) { - App.LogError("Firewall Rule Commit failed {0}", err.ToString()); + Priv10Logger.LogError("Firewall Rule Commit failed {0}", err.ToString()); return false; } return true; diff --git a/PrivateService/IPC/Priv10Host.cs b/PrivateService/IPC/Priv10Host.cs new file mode 100644 index 0000000..acc4068 --- /dev/null +++ b/PrivateService/IPC/Priv10Host.cs @@ -0,0 +1,441 @@ +using MiscHelpers; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TweakEngine; +using PrivateService; +using System.IO; +using System.Xml; +using System.Diagnostics; +using PrivateAPI; +using static PrivateAPI.Priv10Conv; +using System.Runtime.Serialization; + +namespace PrivateWin10 +{ + public class Priv10Host : PipeHost + { + public Priv10Host() + { + Name = App.SvcName; + } + + + + ///////////////////////////////////////// + // + + protected byte[] PutProgID(ProgramID value) + { + if (value == null) + return new byte[0]; + return PutStr(value.AsString()); + } + + protected ProgramID GetProgID(byte[] value) + { + if (value.Length == 0) + return null; + return ProgramID.Parse(GetStr(value)); + } + + protected byte[] PutProgSets(List list) + { + return PutList(list, PutProgSet); + } + + protected byte[] PutProgSet(ProgramSet Progs) + { + using (MemoryStream dataStream = new MemoryStream()) + { + using (var dataWriter = new BinaryWriter(dataStream)) + { + dataWriter.Write(Progs.guid.ToByteArray()); + + byte[] data = PutConfig(Progs.config); + dataWriter.Write(data.Length); + dataWriter.Write(data); + + dataWriter.Write(Progs.Programs.Count); + foreach (var item in Progs.Programs) + { + data = PutProg(item.Value); + dataWriter.Write(data.Length); + dataWriter.Write(data); + } + } + return dataStream.ToArray(); + } + } + + protected byte[] PutProg(Program Prog) + { + return PutXmlObj(Prog); + } + + protected byte[] PutConfig(ProgramSet.Config config) + { + return PutXmlObj(config); + } + + protected ProgramSet.Config GetConfig(byte[] value) + { + return GetXmlObj(value); + } + + protected byte[] PutRule(FirewallRuleEx rule) + { + return PutObjXml(rule, (FirewallRuleEx obj, XmlWriter writer) => { obj.Store(writer); }); + } + + protected FirewallRule GetRule(byte[] value) + { + return GetXmlObj(value, (FirewallRule obj, XmlElement node) => { obj.Load(node); }); + } + + protected byte[] PutRules(Dictionary> rules) + { + return PutGuidMMap(rules, PutRule); + } + + protected byte[] PutLogEntry(Program.LogEntry entry) + { + return PutXmlObj(entry); + } + + protected byte[] PutLogList(Dictionary> log) + { + return PutGuidMMap(log, PutLogEntry); + } + + protected byte[] PutSock(NetworkSocket socket) + { + return PutXmlObj(socket); + } + + protected byte[] PutSocks(Dictionary> sockets) + { + return PutGuidMMap(sockets, PutSock); + } + + protected byte[] PutDomain(Program.DnsEntry entry) + { + return PutXmlObj(entry); + } + + protected byte[] PutDomains(Dictionary> entries) + { + return PutGuidMMap(entries, PutDomain); + } + + protected byte[] PutAppInfo(UwpFunc.AppInfo info) + { + return PutXmlObj(info); + } + + protected byte[] PutAppInfos(List infos) + { + return PutList(infos, PutAppInfo); + } + + ///////////////////////////////////////// + // Dns Proxy + + protected byte[] PutQuery(DnsCacheMonitor.DnsCacheEntry entry) + { + return PutXmlObj(entry); + } + + protected byte[] PutQueries(List list) + { + return PutList(list, PutQuery); + } + + protected byte[] PutDomainFilter(DomainFilter filter) + { + return PutXmlObj(filter); + } + + protected byte[] PutFilterList(List list) + { + return PutList(list, PutDomainFilter); + } + + protected DomainFilter GetDomainFilter(byte[] value) + { + return GetXmlObj(value); + } + + protected byte[] PutBlockList(List list) + { + return PutList(list, PutBlockEntry); + } + + protected byte[] PutBlockEntry(DomainBlocklist entry) + { + return PutXmlObj(entry); + } + + protected DomainBlocklist GetBlockEntry(byte[] value) + { + return GetXmlObj(value); + } + + ///////////////////////////////////////// + // Privacy tweaks + + protected byte[] PutTweak(TweakList.Tweak tweak) + { + return PutObjXml(tweak, (TweakList.Tweak obj, XmlWriter writer) => { obj.Store(writer); }); + } + + protected TweakList.Tweak GetTweak(byte[] value) + { + return GetXmlObj(value, (TweakList.Tweak obj, XmlElement node) => { obj.Load(node); }); + } + + + protected override List Process(string func, List args) + { + List ret = new List(); + //try + { + ///////////////////////////////////////// + // Windows Firewall + + if (func == "GetFilteringMode") + { + ret.Add(PutStr(App.engine.GetFilteringMode())); + } + else if (func == "SetFilteringMode") + { + ret.Add(PutBool(App.engine.SetFilteringMode(GetEnum(args[0])))); + } + else if (func == "IsFirewallGuard") + { + ret.Add(PutBool(App.engine.IsFirewallGuard())); + } + else if (func == "SetFirewallGuard") + { + ret.Add(PutBool(App.engine.SetFirewallGuard(BitConverter.ToBoolean(args[0], 0), GetEnum(args[0])))); + } + else if (func == "GetAuditPolicy") + { + ret.Add(PutStr(App.engine.GetAuditPolicy())); + } + else if (func == "SetAuditPolicy") + { + ret.Add(PutBool(App.engine.SetAuditPolicy(GetEnum(args[0])))); + } + else if (func == "GetPrograms") + { + ret.Add(PutProgSets(App.engine.GetPrograms(GetGuids(args[0])))); + } + else if (func == "GetProgram") + { + ret.Add(PutProgSet(App.engine.GetProgram(GetProgID(args[0]), GetBool(args[0])))); + } + else if (func == "AddProgram") + { + ret.Add(PutBool(App.engine.AddProgram(GetProgID(args[0]), GetGuid(args[1])))); + } + else if (func == "UpdateProgram") + { + ret.Add(PutBool(App.engine.UpdateProgram(GetGuid(args[0]), GetConfig(args[1]), GetUInt64(args[2])))); + } + else if (func == "MergePrograms") + { + ret.Add(PutBool(App.engine.MergePrograms(GetGuid(args[0]), GetGuid(args[1])))); + } + else if (func == "SplitPrograms") + { + ret.Add(PutBool(App.engine.SplitPrograms(GetGuid(args[0]), GetProgID(args[0])))); + } + else if (func == "RemoveProgram") + { + ret.Add(PutBool(App.engine.RemoveProgram(GetGuid(args[0]), GetProgID(args[0])))); + } + else if (func == "LoadRules") + { + ret.Add(PutBool(App.engine.LoadRules())); + } + else if (func == "GetRules") + { + ret.Add(PutRules(App.engine.GetRules(GetGuids(args[0])))); + } + else if (func == "UpdateRule") + { + ret.Add(PutBool(App.engine.UpdateRule(GetRule(args[0]), GetUInt64(args[1])))); + } + else if (func == "RemoveRule") + { + ret.Add(PutBool(App.engine.RemoveRule(GetStr(args[0]), GetProgID(args[1])))); + } + else if (func == "SetRuleApproval") + { + ret.Add(PutInt(App.engine.SetRuleApproval(GetEnum(args[0]), GetStr(args[1]), GetProgID(args[2])))); + } + else if (func == "BlockInternet") + { + ret.Add(PutBool(App.engine.BlockInternet(GetBool(args[0])))); + } + else if (func == "ClearLog") + { + ret.Add(PutBool(App.engine.ClearLog(GetBool(args[0])))); + } + else if (func == "ClearDnsLog") + { + ret.Add(PutBool(App.engine.ClearDnsLog())); + } + else if (func == "CleanUpPrograms") + { + ret.Add(PutInt(App.engine.CleanUpPrograms(GetBool(args[0])))); + } + else if (func == "CleanUpRules") + { + ret.Add(PutInt(App.engine.CleanUpRules())); + } + else if (func == "GetConnections") + { + ret.Add(PutLogList(App.engine.GetConnections(GetGuids(args[0])))); + } + else if (func == "GetSockets") + { + ret.Add(PutSocks(App.engine.GetSockets(GetGuids(args[0])))); + } + else if (func == "SetupDnsInspector") + { + ret.Add(PutBool(App.engine.SetupDnsInspector(GetBool(args[0])))); + } + else if (func == "GetDomains") + { + ret.Add(PutDomains(App.engine.GetDomains(GetGuids(args[0])))); + } + else if (func == "GetAllAppPkgs") + { + ret.Add(PutAppInfos(App.engine.GetAllAppPkgs(GetBool(args[0])))); + } + else if (func == "GetAppPkgRes") + { + ret.Add(PutStr(App.engine.GetAppPkgRes(GetStr(args[0])))); + } + + ///////////////////////////////////////// + // Dns Proxy + + else if (func == "ConfigureDNSProxy") + { + ret.Add(PutBool(App.engine.ConfigureDNSProxy(GetBool(args[0]), GetBoolx(args[1]), GetStr(args[2])))); + } + + // Querylog + else if (func == "GetLoggedDnsQueries") + { + ret.Add(PutQueries(App.engine.GetLoggedDnsQueries())); + } + else if (func == "ClearLoggedDnsQueries") + { + ret.Add(PutBool(App.engine.ClearLoggedDnsQueries())); + } + + // Whitelist/Blacklist + else if (func == "GetDomainFilter") + { + ret.Add(PutFilterList(App.engine.GetDomainFilter(GetEnum(args[0])))); + } + else if (func == "UpdateDomainFilter") + { + ret.Add(PutBool(App.engine.UpdateDomainFilter(GetEnum(args[0]), GetDomainFilter(args[1])))); + } + else if (func == "RemoveDomainFilter") + { + ret.Add(PutBool(App.engine.RemoveDomainFilter(GetEnum(args[0]), GetStr(args[1])))); + } + + // Blocklist + else if (func == "GetDomainBlocklists") + { + ret.Add(PutBlockList(App.engine.GetDomainBlocklists())); + } + else if (func == "UpdateDomainBlocklist") + { + ret.Add(PutBool(App.engine.UpdateDomainBlocklist(GetBlockEntry(args[0])))); + } + else if (func == "RemoveDomainBlocklist") + { + ret.Add(PutBool(App.engine.RemoveDomainBlocklist(GetStr(args[0])))); + } + else if (func == "RefreshDomainBlocklist") + { + ret.Add(PutBool(App.engine.RefreshDomainBlocklist(GetStr(args[0])))); + } + + ///////////////////////////////////////// + // Privacy tweaks + + else if (func == "ApplyTweak") + { + ret.Add(PutBool(App.engine.ApplyTweak(GetTweak(args[0])))); + } + else if (func == "TestTweak") + { + ret.Add(PutBool(App.engine.TestTweak(GetTweak(args[0])))); + } + else if (func == "UndoTweak") + { + ret.Add(PutBool(App.engine.UndoTweak(GetTweak(args[0])))); + } + + ///////////////////////////////////////// + // Misc + + else if (func == "Quit") + { + ret.Add(PutBool(App.engine.Quit())); + } + + else + { + //call.args = new Exception("Unknown FunctionCall"); + } + } + /*catch (Exception err) + { + AppLog.Exception(err); + call.args = err; + }*/ + return ret; + } + + public void NotifyActivity(Guid guid, Program.LogEntry entry, ProgramID progID, List services = null, bool update = false) + { + List args = new List(); + args.Add(PutGuid(guid)); + args.Add(PutLogEntry(entry)); + args.Add(PutProgID(progID)); + args.Add(PutStrList(services)); + args.Add(PutBool(update)); + SendPushNotification("ActivityNotification", args); + } + + public void NotifyChange(Program prog, FirewallRuleEx rule, Priv10Engine.RuleEventType type, Priv10Engine.RuleFixAction action) + { + List args = new List(); + args.Add(PutProg(prog)); + args.Add(PutRule(rule)); + args.Add(PutStr(type)); + args.Add(PutStr(action)); + SendPushNotification("ChangeNotification", args); + } + + public void NotifyUpdate(Guid guid, Priv10Engine.UpdateTypes type) + { + List args = new List(); + args.Add(PutGuid(guid)); + args.Add(PutStr(type)); + SendPushNotification("UpdateNotification", args); + } + } +} diff --git a/PrivateService/PrivateService.csproj b/PrivateService/PrivateService.csproj new file mode 100644 index 0000000..8562713 --- /dev/null +++ b/PrivateService/PrivateService.csproj @@ -0,0 +1,133 @@ + + + + + Debug + AnyCPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA} + WinExe + PrivateService + PrivateService + v4.5 + 512 + true + + + AnyCPU + true + full + false + ..\bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + AnyCPU + pdbonly + true + ..\bin\Release\ + TRACE + prompt + 4 + false + + + + + + + + + + + + + + + + + C:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd + + + + + + + + + + + + + + + + + Component + + + + + + + + + + + + + + + + + + + + + + + + {ebcb8f53-c56a-4245-893f-ce67d69d0087} + DNS + + + {ed4e6027-541f-440a-a5ee-15dbb7b89423} + Microsoft.O365.Security.Native.ETW + + + {694c85c2-bb43-4525-a953-709050002631} + MiscHelpers + + + {2370ee89-9321-408d-9806-90720b6ddea4} + PrivateAPI + + + {35fb284f-77e2-4336-b60d-2cd39522b014} + TweakEngine + + + + + {58FBCF7C-E7A9-467C-80B3-FC65E8FCCA08} + 1 + 0 + 0 + tlbimp + False + True + + + {DCB00D01-570F-4A9B-8D69-199FDBA5723B} + 1 + 0 + 0 + tlbimp + False + False + + + + \ No newline at end of file diff --git a/PrivateService/Properties/AssemblyInfo.cs b/PrivateService/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3c443d5 --- /dev/null +++ b/PrivateService/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PrivateService")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PrivateService")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8e993255-f2ef-4fd9-a0f9-e3143d16cbba")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PrivateService/Service.cs b/PrivateService/Service.cs new file mode 100644 index 0000000..860792e --- /dev/null +++ b/PrivateService/Service.cs @@ -0,0 +1,308 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.ServiceProcess; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Win32; +using System.Configuration; +using System.Data; +using System.Diagnostics; +using System.IO; +using System.IO.Compression; +using System.Net; +using System.Reflection; +using System.Runtime.ExceptionServices; +using System.Runtime.InteropServices; +using System.Threading; +using System.Windows; +using MiscHelpers; +using System.Drawing; +using TweakEngine; +using System.Windows.Forms; +using PrivateWin10; +using PrivateAPI; + +namespace PrivateService +{ + static class App + { + public static bool HasConsole = false; + public static string[] args = null; + public static string exePath = ""; + //public static string Version = "0.0"; + public static string Key = "PrivateWin10"; +#if DEBUG + public static string SvcName = "priv10dbg"; +#else + public static string SvcName = "priv10"; +#endif + public static string appPath = ""; + public static string dataPath = ""; + public static bool isPortable = false; + + public static AppLog Log = null; + + public static Priv10Engine engine = null; + + public static Priv10Host host = null; + + enum StartModes + { + Normal = 0, + Service, + Engine + } + + [STAThread] + public static void Main(string[] args) + { + App.args = args; + + HasConsole = WinConsole.Initialize(TestArg("-console")); + + if (TestArg("-dbg_wait")) + MessageBox.Show("Waiting for debugger. (press ok when attached)"); + + if (TestArg("-dbg_log")) + AppDomain.CurrentDomain.FirstChanceException += FirstChanceExceptionHandler; + + StartModes startMode = StartModes.Normal; // Normal GUI Mode + if (TestArg("-svc")) + startMode = StartModes.Service; + else if (TestArg("-engine")) + startMode = StartModes.Engine; + + Log = new AppLog(Key); + AppLog.ExceptionLogID = (long)Priv10Logger.EventIDs.Exception; + AppLog.ExceptionCategory = (short)Priv10Logger.EventFlags.DebugEvents; + + if (startMode == StartModes.Normal) + { + Log.EnableLogging(); + Log.LoadLog(); + } + // When running as worker we need the windows event log + else if (!Log.UsingEventLog()) + Log.SetupEventLog(Key); + + // load current version + exePath = Process.GetCurrentProcess().MainModule.FileName; //System.Reflection.Assembly.GetExecutingAssembly().Location; + //*FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(exePath); + //Version = fvi.FileMajorPart + "." + fvi.FileMinorPart; + //if (fvi.FileBuildPart != 0) + // Version += "." + fvi.FileBuildPart; + //if (fvi.FilePrivatePart != 0) + // Version += (char)('a' + (fvi.FilePrivatePart - 1)); + appPath = Path.GetDirectoryName(exePath); + + dataPath = appPath + @"\Data"; + if (File.Exists(GetINIPath())) // if an ini exists in the app path, its considdered to be a portable run + { + isPortable = true; + + AppLog.Debug("Portable Mode"); + } + else + { + string progData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); + if (progData == null) + progData = @"C:\ProgramData"; + + dataPath = progData + "\\" + Key; + } + + AppLog.Debug("Config Directory: {0}", dataPath); + + if (!Directory.Exists(dataPath)) + Directory.CreateDirectory(dataPath); + if(AdminFunc.IsAdministrator()) + FileOps.SetAnyDirSec(dataPath); + + Priv10Logger.LogInfo("PrivateWin10 Service Process Started, Mode {0}.", startMode.ToString()); + + // setup custom assembly resolution for x86/x64 synamic compatybility + AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler; + + // is the process starting as a service/worker? + if (startMode != StartModes.Normal) + { + engine = new Priv10Engine(); + if (startMode == StartModes.Service) + { + using (Priv10Service svc = new Priv10Service()) + ServiceBase.Run(svc); + } + else + engine.Run(); + return; + } + } + + static private Assembly AssemblyResolveHandler(object sender, ResolveEventArgs args) + { + //This handler is called only when the common language runtime tries to bind to the assembly and fails. + + string strTempAssmbPath = ""; + + //Retrieve the list of referenced assemblies in an array of AssemblyName. + Assembly objExecutingAssemblies = Assembly.GetExecutingAssembly(); + AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies(); + + //Loop through the array of referenced assembly names. + foreach (AssemblyName strAssmbName in arrReferencedAssmbNames) + { + //Check for the assembly names that have raised the "AssemblyResolve" event. + if (strAssmbName.FullName.Substring(0, strAssmbName.FullName.IndexOf(",")) == args.Name.Substring(0, args.Name.IndexOf(","))) + { + //Build the path of the assembly from where it has to be loaded. + //The following line is probably the only line of code in this method you may need to modify: + if(System.Environment.Is64BitProcess) + strTempAssmbPath = appPath + @"\x64\"; + else + strTempAssmbPath = appPath + @"\x86\"; + strTempAssmbPath += args.Name.Substring(0, args.Name.IndexOf(",")) + ".dll"; + break; + } + } + + //Load the assembly from the specified path. + Assembly MyAssembly = Assembly.LoadFrom(strTempAssmbPath); + + //Return the loaded assembly. + return MyAssembly; + } + + static private void FirstChanceExceptionHandler(object source, FirstChanceExceptionEventArgs e) + { + AppLog.Debug("FirstChanceException event raised in {0}: {1}\r\n{2}", AppDomain.CurrentDomain.FriendlyName, e.Exception.Message, e.Exception.StackTrace); + } + + static private Dictionary AppResourceStrCache = new Dictionary(); + static private ReaderWriterLockSlim AppResourceStrLock = new ReaderWriterLockSlim(); + + static public string GetResourceStr(string resourcePath) + { + if (resourcePath == null) + return ""; + if (resourcePath.Length == 0 || resourcePath[0] != '@') + return resourcePath; + + string resourceStr = null; + AppResourceStrLock.EnterReadLock(); + AppResourceStrCache.TryGetValue(resourcePath, out resourceStr); + AppResourceStrLock.ExitReadLock(); + if (resourceStr != null) + return resourceStr; + + if (resourcePath.Length > 2 && resourcePath.Substring(0, 2) == "@{") + { + //if (App.engine != null) + resourceStr = App.engine?.PkgMgr?.GetAppResourceStr(resourcePath) ?? resourcePath; + //else // xxxxxx + // resourceStr = App.client.GetAppPkgRes(resourcePath); + } + else + resourceStr = MiscFunc.GetResourceStr(resourcePath); + + AppResourceStrLock.EnterWriteLock(); + if (!AppResourceStrCache.ContainsKey(resourcePath)) + AppResourceStrCache.Add(resourcePath, resourceStr); + AppResourceStrLock.ExitWriteLock(); + + return resourceStr; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + // Misc Helpers + + static public bool TestArg(string name) + { + for (int i = 0; i < App.args.Length; i++) + { + if (App.args[i].Equals(name, StringComparison.OrdinalIgnoreCase)) + return true; + } + return false; + } + + static public string GetArg(string name) + { + for (int i = 0; i < App.args.Length; i++) + { + if (App.args[i].Equals(name, StringComparison.OrdinalIgnoreCase)) + { + string temp = App.args[i + 1]; + if (temp.Length > 0 && temp[0] != '-') + return temp; + return ""; + } + } + return null; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////// + // Config + + static public string GetConfig(string Section, string Key, string Default = "") + { + return IniReadValue(Section, Key, Default); + } + + static public int GetConfigInt(string Section, string Key, int Default = 0) + { + return MiscFunc.parseInt(IniReadValue(Section, Key, Default.ToString())); + } + + static public void SetConfig(string Section, string Key, bool Value) + { + SetConfig(Section, Key, Value ? 1 : 0); + } + + static public void SetConfig(string Section, string Key, int Value) + { + IniWriteValue(Section, Key, Value.ToString()); + } + + static public void SetConfig(string Section, string Key, string Value) + { + IniWriteValue(Section, Key, Value); + } + + private static string GetINIPath() { return dataPath + "\\" + Key + ".ini"; } + + [DllImport("kernel32")] + private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); + public static void IniWriteValue(string Section, string Key, string Value, string INIPath = null) + { + WritePrivateProfileString(Section, Key, Value, INIPath != null ? INIPath : GetINIPath()); + } + + public static void IniDeleteSection(string Section, string INIPath = null) + { + WritePrivateProfileString(Section, null, null, INIPath != null ? INIPath : GetINIPath()); + } + + [DllImport("kernel32")] + private static extern int GetPrivateProfileString(string section, string key, string def, [In, Out] char[] retVal, int size, string filePath); + public static string IniReadValue(string Section, string Key, string Default = "", string INIPath = null) + { + char[] chars = new char[8193]; + int size = GetPrivateProfileString(Section, Key, Default, chars, 8193, INIPath != null ? INIPath : GetINIPath()); + /*int size = GetPrivateProfileString(Section, Key, "\xff", chars, 8193, INIPath != null ? INIPath : GetINIPath()); + if (size == 1 && chars[0] == '\xff') + { + WritePrivateProfileString(Section, Key, Default, INIPath != null ? INIPath : GetINIPath()); + return Default; + }*/ + return new String(chars, 0, size); + } + + public static List IniEnumSections(string INIPath = null) + { + char[] chars = new char[8193]; + int size = GetPrivateProfileString(null, null, null, chars, 8193, INIPath != null ? INIPath : GetINIPath()); + return TextHelpers.SplitStr(new String(chars, 0, size), "\0"); + } + } +} diff --git a/PrivateWin10.sln b/PrivateWin10.sln index ea45f66..656b2a2 100644 --- a/PrivateWin10.sln +++ b/PrivateWin10.sln @@ -4,6 +4,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 VisualStudioVersion = 15.0.27130.2010 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateWin10", "PrivateWin10\PrivateWin10.csproj", "{7FEBF4F6-28D9-4A8F-B876-2B5C9603480F}" + ProjectSection(ProjectDependencies) = postProject + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA} = {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA} + EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{63DEFD3C-663C-45E6-B325-7630B398D543}" ProjectSection(SolutionItems) = preProject @@ -33,6 +36,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ICSharpCode.TreeView", "ICS EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateSetup", "PrivateSetup\PrivateSetup.csproj", "{290634B1-6531-40D1-83F5-A5622FBFCA73}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiscHelpers", "MiscHelpers\MiscHelpers.csproj", "{694C85C2-BB43-4525-A953-709050002631}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TweakEngine", "TweakEngine\TweakEngine.csproj", "{35FB284F-77E2-4336-B60D-2CD39522B014}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateService", "PrivateService\PrivateService.csproj", "{8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrivateAPI", "PrivateAPI\PrivateAPI.csproj", "{2370EE89-9321-408D-9806-90720B6DDEA4}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -253,6 +264,102 @@ Global {290634B1-6531-40D1-83F5-A5622FBFCA73}.ReleaseSigning|x64.Build.0 = Release|Any CPU {290634B1-6531-40D1-83F5-A5622FBFCA73}.ReleaseSigning|x86.ActiveCfg = Release|Any CPU {290634B1-6531-40D1-83F5-A5622FBFCA73}.ReleaseSigning|x86.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|Any CPU.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|x64.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|x64.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|x86.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Debug|x86.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|Any CPU.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|Any CPU.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|x64.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|x64.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|x86.ActiveCfg = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.DebugSigning|x86.Build.0 = Debug|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|Any CPU.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|Any CPU.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|x64.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|x64.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|x86.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.Release|x86.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|Any CPU.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|Any CPU.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|x64.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|x64.Build.0 = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|x86.ActiveCfg = Release|Any CPU + {694C85C2-BB43-4525-A953-709050002631}.ReleaseSigning|x86.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|x64.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|x64.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|x86.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Debug|x86.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|Any CPU.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|Any CPU.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|x64.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|x64.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|x86.ActiveCfg = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.DebugSigning|x86.Build.0 = Debug|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|Any CPU.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|x64.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|x64.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|x86.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.Release|x86.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|Any CPU.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|Any CPU.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|x64.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|x64.Build.0 = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|x86.ActiveCfg = Release|Any CPU + {35FB284F-77E2-4336-B60D-2CD39522B014}.ReleaseSigning|x86.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|x64.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|x64.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|x86.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Debug|x86.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|Any CPU.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|Any CPU.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|x64.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|x64.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|x86.ActiveCfg = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.DebugSigning|x86.Build.0 = Debug|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|Any CPU.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|x64.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|x64.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|x86.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.Release|x86.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|Any CPU.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|Any CPU.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|x64.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|x64.Build.0 = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|x86.ActiveCfg = Release|Any CPU + {8E993255-F2EF-4FD9-A0F9-E3143D16CBBA}.ReleaseSigning|x86.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|x64.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|x64.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|x86.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Debug|x86.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|Any CPU.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|Any CPU.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|x64.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|x64.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|x86.ActiveCfg = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.DebugSigning|x86.Build.0 = Debug|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|Any CPU.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|x64.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|x64.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|x86.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.Release|x86.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|Any CPU.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|Any CPU.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|x64.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|x64.Build.0 = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|x86.ActiveCfg = Release|Any CPU + {2370EE89-9321-408D-9806-90720B6DDEA4}.ReleaseSigning|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/PrivateWin10/API/AppManager.cs b/PrivateWin10/API/AppManager.cs deleted file mode 100644 index f2f6c07..0000000 --- a/PrivateWin10/API/AppManager.cs +++ /dev/null @@ -1,317 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; -using System.Security.Principal; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Xml.Linq; -using Windows.ApplicationModel; -//using Windows.Management.Deployment; - - -public class AppManager -{ - - // WARNING Dont use in a Service !!! - // Note: AppContainerLookupMoniker do not work properly when running under the system user !!! - - public AppManager() - { - - } - - [DllImport("kernelbase", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern int AppContainerLookupMoniker(IntPtr Sid, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string packageFamilyName); - - [DllImport("kernelbase", CharSet = CharSet.Unicode, SetLastError = true)] - public static extern int AppContainerDeriveSidFromMoniker([In, MarshalAs(UnmanagedType.LPWStr)] string packageFamilyName, ref IntPtr pSID); - - [DllImport("advapi32", CharSet = CharSet.Unicode)] - public static extern bool ConvertStringSidToSid([In, MarshalAs(UnmanagedType.LPWStr)] string pStringSid, ref IntPtr pSID); - - [DllImport("advapi32", CharSet = CharSet.Unicode)] - public static extern bool ConvertSidToStringSid(IntPtr pSID, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string pStringSid); - - static public string SidToAppPackage(string sid) - { - string packageID = ""; - - IntPtr pSid = new IntPtr(); - ConvertStringSidToSid(sid, ref pSid); - - int ret = AppContainerLookupMoniker(pSid, ref packageID); - - Marshal.FreeHGlobal(pSid); - - if (ret != ERROR_SUCCESS) - { - return sid; - - /*var subKey = Registry.ClassesRoot.OpenSubKey(@"Local Settings\Software\Microsoft\Windows\CurrentVersion\AppContainer\Mappings\" + sid, false); - if (subKey != null) - packageID = subKey.GetValue("Moniker").ToString();*/ - } - - return packageID; - } - - static public string AppPackageToSid(string packageID) - { - string strSID = ""; - - IntPtr pSid = new IntPtr(); - - int ret = AppContainerDeriveSidFromMoniker(packageID, ref pSid); - - ConvertSidToStringSid(pSid, ref strSID); - - Marshal.FreeHGlobal(pSid); - - return strSID; - } - - [DllImport("kernel32.dll", SetLastError = true)] - public static extern Int32 GetApplicationUserModelId(IntPtr hProcess, ref UInt32 AppModelIDLength, [MarshalAs(UnmanagedType.LPWStr)] StringBuilder sbAppUserModelID); - - const int ERROR_INSUFFICIENT_BUFFER = 0x7a; - const int ERROR_SUCCESS = 0x0; - - public string GetAppPackageByPID_(int PID) - { - // WARNING: this is not consistent with the windows firewall, we need to go by the proces token - - //var process = System.Diagnostics.Process.GetProcessById(PID); // throws error if pid is not found - var processHandle = ProcFunc.OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, PID); - if (processHandle == IntPtr.Zero) - return null; - - uint cchLen = 256; - StringBuilder sbName = new StringBuilder((int)cchLen); - Int32 lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName); - if (ERROR_INSUFFICIENT_BUFFER == lResult) - { - sbName = new StringBuilder((int)cchLen); - lResult = GetApplicationUserModelId(processHandle, ref cchLen, sbName); - } - ProcFunc.CloseHandle(processHandle); - - if (lResult != ERROR_SUCCESS) - return null; - - string sResult = sbName.ToString(); - int pos = sResult.LastIndexOf("!"); // bla!App - if (pos != -1) - sResult = sResult.Substring(0, pos); - - return sResult; - } - - private Dictionary AppInfosBySid = new Dictionary(); - private ReaderWriterLockSlim AppInfosBySidLock = new ReaderWriterLockSlim(); - - private Windows.Management.Deployment.PackageManager packageManager = new Windows.Management.Deployment.PackageManager(); - - public UwpFunc.AppInfo GetAppInfoBySid(string sid) - { - UwpFunc.AppInfo info = null; - AppInfosBySidLock.EnterReadLock(); - AppInfosBySid.TryGetValue(sid, out info); - AppInfosBySidLock.ExitReadLock(); - if (info != null) - return info; - - string appPackageID = SidToAppPackage(sid); - if (appPackageID == null || appPackageID.Length == 0) - return null; - - IEnumerable packages; - - try - { - packages = packageManager.FindPackages(appPackageID); - } - catch - { - return null; - } - - foreach (var package in packages) - { - info = GetInfo(package, sid); - if (info != null) - { - AppInfosBySidLock.EnterWriteLock(); - if (!AppInfosBySid.ContainsKey(sid)) - AppInfosBySid.Add(sid, info); - AppInfosBySidLock.ExitWriteLock(); - break; - } - } - - return info; - } - - - private UwpFunc.AppInfo GetInfo(Windows.ApplicationModel.Package package, string sid) - { - string path; - try - { - path = package.InstalledLocation.Path; - } - catch - { - return null; - } - - string manifest = Path.Combine(path, "AppxManifest.xml"); - - if (!File.Exists(manifest)) - return null; - - XElement xelement; - try - { - string manifestXML = File.ReadAllText(manifest); - - int startIndex = manifestXML.IndexOf("", StringComparison.Ordinal); - int num = manifestXML.IndexOf("", StringComparison.Ordinal); - xelement = XElement.Parse(manifestXML.Substring(startIndex, num - startIndex + 13).Replace("uap:", string.Empty)); - } - catch (Exception err) - { - AppLog.Exception(err); - return null; - } - - string displayName = null; - string logoPath = null; - try - { - displayName = xelement.Element((XName)"DisplayName")?.Value; - logoPath = xelement.Element((XName)"Logo")?.Value; - } - catch (Exception err) - { - AppLog.Exception(err); - } - - if (displayName == null) - return null; - - try - { - Uri result; - if (Uri.TryCreate(displayName, UriKind.Absolute, out result)) - { - string pathToPri = Path.Combine(package.InstalledLocation.Path, "resources.pri"); - string resourceKey1 = "ms-resource://" + package.Id.Name + "/resources/" + ((IEnumerable)result.Segments).Last(); - string stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey1); - if (!string.IsNullOrEmpty(stringFromPriFile.Trim())) - displayName = stringFromPriFile; - else - { - string str = string.Concat(((IEnumerable)result.Segments).Skip(1)); - string resourceKey2 = "ms-resource://" + package.Id.Name + "/" + str; - stringFromPriFile = MiscFunc.GetResourceStr(pathToPri, resourceKey2); - if (!string.IsNullOrEmpty(stringFromPriFile.Trim())) - displayName = stringFromPriFile; - } - } - - if (logoPath != null) - { - string path1 = Path.Combine(package.InstalledLocation.Path, logoPath); - if (File.Exists(path1)) - logoPath = path1; - else - { - string path2 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path1, "scale-100.png")); - if (File.Exists(path2)) - logoPath = path2; - else - { - string path3 = Path.Combine(Path.Combine(package.InstalledLocation.Path, "en-us"), logoPath); - string path4 = Path.Combine(package.InstalledLocation.Path, Path.ChangeExtension(path3, "scale-100.png")); - if (File.Exists(path4)) - logoPath = path4; - else - logoPath = null; - } - } - } - } - catch (Exception err) - { - AppLog.Exception(err); - } - - return new UwpFunc.AppInfo() { Name = displayName, Logo = logoPath, ID = package.Id.FamilyName, SID = sid }; - } - - bool FullListFetched = false; - - public void UpdateAppCache() - { - Dictionary AppInfos = new Dictionary(); - - IEnumerable packages = (IEnumerable)packageManager.FindPackages(); - foreach (var package in packages) - { - string appSID = AppPackageToSid(package.Id.FamilyName).ToLower(); - - UwpFunc.AppInfo info = GetInfo(package, appSID); - if (info != null) - { - if (!AppInfos.ContainsKey(appSID)) - AppInfos.Add(appSID, info); - /* - UwpFunc.AppInfo old_info; - if (AppInfos.TryGetValue(appSID, out old_info)) - AppLog.Debug("Warning an app with the SID: {0} is already listed", appSID); - */ - } - } - - AppInfosBySidLock.EnterWriteLock(); - AppInfosBySid = AppInfos; - AppInfosBySidLock.ExitWriteLock(); - FullListFetched = true; - } - - public List GetAllApps(bool bReload = false) - { - if (!FullListFetched || bReload) - UpdateAppCache(); - - List Apps = new List(); - AppInfosBySidLock.EnterReadLock(); - foreach (UwpFunc.AppInfo info in AppInfosBySid.Values) - Apps.Add(info); - AppInfosBySidLock.ExitReadLock(); - return Apps; - } - - ////////////////////////////////////////////////////////////////////////////////////////////// - // App resource handling - - - public string GetAppResourceStr(string resourcePath) - { - // Note: PackageManager requirers admin privilegs - - var AppResource = TextHelpers.Split2(resourcePath.Substring(2, resourcePath.Length - 3), "?"); - var package = packageManager.FindPackage(AppResource.Item1); - if (package != null) - { - string pathToPri = Path.Combine(package.InstalledLocation.Path, "resources.pri"); - return MiscFunc.GetResourceStr(pathToPri, AppResource.Item2); - } - - return resourcePath; - } -} diff --git a/PrivateWin10/API/AuditPolicy.cs b/PrivateWin10/API/AuditPolicy.cs deleted file mode 100644 index d0981f1..0000000 --- a/PrivateWin10/API/AuditPolicy.cs +++ /dev/null @@ -1,288 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - - -static public class AuditPolicy -{ - /// - /// The AuditEnumerateCategories function enumerates the available audit-policy categories. - /// - /// A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. - /// A pointer to the number of elements in the ppAuditCategoriesArray array. - /// A System.Boolean value that indicates whether the function completed successfully. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375636(v=vs.85).aspx - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditEnumerateCategories(out IntPtr ppAuditCategoriesArray, out uint pCountReturned); - - - /// - /// The AuditLookupCategoryName function retrieves the display name of the specified audit-policy category. - /// - /// A pointer to a GUID structure that specifies an audit-policy category. - /// The address of a pointer to a null-terminated string that contains the display name of the audit-policy category specified by the pAuditCategoryGuid function. - /// A System.Boolean value that indicates whether the function completed successfully. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375687(v=vs.85).aspx - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditLookupCategoryName(ref Guid pAuditCategoryGuid, out StringBuilder ppszCategoryName); - - - /// - /// The AuditEnumerateSubCategories function enumerates the available audit-policy subcategories. - /// - /// The GUID of an audit-policy category for which subcategories are enumerated. If the value of the bRetrieveAllSubCategories parameter is TRUE, this parameter is ignored. - /// TRUE to enumerate all audit-policy subcategories; FALSE to enumerate only the subcategories of the audit-policy category specified by the pAuditCategoryGuid parameter. - /// A pointer to a single buffer that contains both an array of pointers to GUID structures and the structures themselves. The GUID structures specify the audit-policy subcategories available on the computer. - /// A pointer to the number of audit-policy subcategories returned in the ppAuditSubCategoriesArray array. - /// A System.Boolean value that indicates whether the function completed successfully. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375648(v=vs.85).aspx - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditEnumerateSubCategories(ref Guid pAuditCategoryGuid, bool bRetrieveAllSubCategories, out IntPtr ppAuditSubCategoriesArray, out uint pCountReturned); - - - /// - /// The AuditLookupSubCategoryName function retrieves the display name of the specified audit-policy subcategory. - /// - /// A pointer to a GUID structure that specifies an audit-policy subcategory. - /// The address of a pointer to a null-terminated string that contains the display name of the audit-policy subcategory specified by the pAuditSubCategoryGuid parameter. - /// A System.Boolean value that indicates whether the function completed successfully. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375693(v=vs.85).aspx - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditLookupSubCategoryName(ref Guid pAuditSubCategoryGuid, out StringBuilder ppszSubCategoryName); - - - /// - /// The AuditFree function frees the memory allocated by audit functions for the specified buffer. - /// - /// A pointer to the buffer to free. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375654(v=vs.85).aspx - [DllImport("advapi32.dll")] - private static extern void AuditFree(IntPtr buffer); - - - /// - /// The AuditQuerySystemPolicy function retrieves system audit policy for one or more audit-policy subcategories. - /// - /// A pointer to an array of GUID values that specify the subcategories for which to query audit policy. - /// The number of elements in each of the pSubCategoryGuids and ppAuditPolicy arrays. - /// A pointer to a single buffer that contains both an array of pointers to AUDIT_POLICY_INFORMATION structures and the structures themselves. - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa375702(v=vs.85).aspx - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditQuerySystemPolicy(ref Guid pSubCategoryGuids, uint PolicyCount, out IntPtr ppAuditPolicy); - - /// - /// The AuditSetSystemPolicy function sets system audit policy for one or more audit-policy subcategories. - /// - /// A pointer to an array of AUDIT_POLICY_INFORMATION structures. Each structure specifies system audit policy for one audit-policy subcategory. - /// The number of elements in the pAuditPolicy array. - /// https://docs.microsoft.com/en-us/windows/desktop/api/ntsecapi/nf-ntsecapi-auditsetsystempolicy - [DllImport("advapi32.dll", SetLastError = true)] - private static extern bool AuditSetSystemPolicy(IntPtr pAuditPolicy, uint PolicyCount); - - /// - /// Gets the GUIDs of the audit categories. - /// - /// The GUIDs of the audit categories on the local machine. - static public List GetCategoryIdentifiers() - { - List identifiers = new List(); - IntPtr buffer; - uint categoryCount; - bool success = AuditEnumerateCategories(out buffer, out categoryCount); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - for (int i = 0, elemOffs = (int)buffer; i < categoryCount; i++) - { - Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid)); - identifiers.Add(Convert.ToString(guid)); - elemOffs += Marshal.SizeOf(typeof(Guid)); - } - AuditFree(buffer); - return identifiers; - } - - - /// - /// Returns the display name of the audit category with the specified GUID. - /// - /// The GUID of the category for which the display name should be returned. - /// The display name of the category - for example "Account Management". - static public String GetCategoryDisplayName(String guid) - { - return GetCategoryDisplayName(new Guid(guid)); - } - - - /// - /// Returns the display name of the audit category with the specified GUID. - /// - /// The GUID of the category for which the display name should be returned. - /// The display name of the category - for example "Account Management". - static public String GetCategoryDisplayName(Guid guid) - { - StringBuilder buffer = new StringBuilder(); - bool success = AuditLookupCategoryName(ref guid, out buffer); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - if (buffer == null) { throw new ArgumentException(String.Format("CategoryDisplayNameNotFoundException on guid: {0}", guid)); } - String categoryDisplayName = buffer.ToString(); - buffer = null; - return categoryDisplayName; - } - - - /// - /// Gets the GUIDs of the audit subcategories of the specified category. - /// - /// The GUID of the category for which the subcategories should be returned. - /// The GUIDs of the audit subcategories for the specified category. - static public List GetSubCategoryIdentifiers(String categoryGuid) - { - return GetSubCategoryIdentifiers(new Guid(categoryGuid)); - } - - - /// - /// Gets the GUIDs of the audit subcategories of the specified category. - /// - /// The GUID of the category for which the subcategories should be returned. - /// The GUIDs of the audit subcategories for the specified category. - static public List GetSubCategoryIdentifiers(Guid categoryGuid) - { - List identifiers = new List(); - IntPtr buffer; - uint subCategoryCount; - bool success = AuditEnumerateSubCategories(ref categoryGuid, false, out buffer, out subCategoryCount); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - for (int i = 0, elemOffs = (int)buffer; i < subCategoryCount; i++) - { - Guid guid = (Guid)Marshal.PtrToStructure((IntPtr)elemOffs, typeof(Guid)); - identifiers.Add(Convert.ToString(guid)); - elemOffs += Marshal.SizeOf(typeof(Guid)); - } - AuditFree(buffer); - return identifiers; - } - - - /// - /// Returns the display name of the audit subcategory with the specified GUID. - /// - /// The GUID of the subcategory for which the display name should be returned. - /// The display name of the subcategory - for example "Audit Credential Validation". - static public String GetSubCategoryDisplayName(String guid) - { - return GetSubCategoryDisplayName(new Guid(guid)); - } - - - /// - /// Returns the display name of the audit subcategory with the specified GUID. - /// - /// The GUID of the subcategory for which the display name should be returned. - /// The display name of the subcategory - for example "Audit Credential Validation". - static public String GetSubCategoryDisplayName(Guid guid) - { - StringBuilder buffer = new StringBuilder(); - bool success = AuditLookupSubCategoryName(ref guid, out buffer); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - String subCategoryDisplayName = buffer.ToString(); - buffer = null; - return subCategoryDisplayName; - } - - - /// - /// Gets the audit policy configured for the specified subcategory GUID. - /// - /// The GUID of the subcategory for which the policy should be returned. - /// Returns an AUDIT_POLICY_INFORMATION that contains information about the policy. - static public AUDIT_POLICY_INFORMATION GetSystemPolicy(String subCategoryGuid) - { - return GetSystemPolicy(new Guid(subCategoryGuid)); - } - - - /// - /// Gets the audit policy configured for the specified subcategory GUID. - /// - /// The GUID of the subcategory for which the policy should be returned. - /// Returns an AUDIT_POLICY_INFORMATION that contains information about the policy. - static public AUDIT_POLICY_INFORMATION GetSystemPolicy(Guid subCategoryGuid) - { - List identifiers = new List(); - IntPtr buffer; - bool success = AuditQuerySystemPolicy(ref subCategoryGuid, 1, out buffer); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - AUDIT_POLICY_INFORMATION policyInformation = (AUDIT_POLICY_INFORMATION)Marshal.PtrToStructure(buffer, typeof(AUDIT_POLICY_INFORMATION)); - AuditFree(buffer); - return policyInformation; - } - - /// - /// Sets the audit policy configuration. - /// - /// AUDIT_POLICY_INFORMATION to be set - /// nothing - static public void SetSystemPolicy(AUDIT_POLICY_INFORMATION policyInformation) - { - List identifiers = new List(); - IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(policyInformation)); - Marshal.StructureToPtr(policyInformation, buffer, true); - bool success = AuditSetSystemPolicy(buffer, 1); - AuditFree(buffer); - if (!success) { throw new Win32Exception(Marshal.GetLastWin32Error()); } - } - - - /// - /// The AUDIT_POLICY_INFORMATION structure specifies a security event type and when to audit that type. - /// - /// https://msdn.microsoft.com/en-us/library/windows/desktop/aa965467(v=vs.85).aspx - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public struct AUDIT_POLICY_INFORMATION - { - - /// - /// A GUID structure that specifies an audit subcategory. - /// - public Guid AuditSubCategoryGuid; - - /// - /// A set of bit flags that specify the conditions under which the security event type specified by the AuditSubCategoryGuid and AuditCategoryGuid members are audited. - /// - public AUDIT_POLICY_INFORMATION_TYPE AuditingInformation; - - /// - /// A GUID structure that specifies an audit-policy category. - /// - public Guid AuditCategoryGuid; - - } - - - /// - /// Represents the auditing type. - /// - [Flags] - public enum AUDIT_POLICY_INFORMATION_TYPE - { - - /// - /// Do not audit the specified event type. - /// - None = 0, - - /// - /// Audit successful occurrences of the specified event type. - /// - Success = 1, - - /// - /// Audit failed attempts to cause the specified event type. - /// - Failure = 2, - } -} diff --git a/PrivateWin10/API/FileOps.cs b/PrivateWin10/API/FileOps.cs deleted file mode 100644 index e165727..0000000 --- a/PrivateWin10/API/FileOps.cs +++ /dev/null @@ -1,232 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Security.AccessControl; -using System.Security.Principal; -using System.IO; - - -class FileOps -{ - static public string FormatSize(decimal size) - { - if (size > 1024 * 1024 * 1024) - return (size / (1024 * 1024 * 1024)).ToString("F") + " GB"; - if (size > 1024 * 1024) - return (size / (1024 * 1024)).ToString("F") + " MB"; - if (size > 1024) - return (size / (1024)).ToString("F") + " KB"; - return ((Int64)size).ToString() + " B"; - } - - static public bool MoveFile(string from, string to, bool Overwrite = false) - { - try - { - if (File.Exists(to)) - { - if (!Overwrite) - return false; - File.Delete(to); - } - - File.Move(from, to); - - if (File.Exists(from)) - return false; - } - catch (Exception err) - { - AppLog.Exception(err); - return false; - } - return true; - } - - static public bool DeleteFile(string path) - { - try - { - File.Delete(path); - return true; - } - catch - { - return false; - } - } - - static public int TestFileAdminSec(String filePath) - { - //get file info - FileInfo fi = new FileInfo(filePath); - if (!fi.Exists) - return 2; - - //get security access - FileSecurity fs = fi.GetAccessControl(); - - //get any special user access - AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); // get as SID not string - - - //remove any special access - foreach (FileSystemAccessRule rule in rules) - { - if (rule.AccessControlType != AccessControlType.Allow) - continue; - if (rule.IdentityReference.Value.Equals(SID_Admins) || rule.IdentityReference.Value.Equals(SID_System)) - continue; - if ((rule.FileSystemRights & (FileSystemRights.Write | FileSystemRights.Delete)) != 0) - return 0; - } - return 1; - } - - static public void SetFileAdminSec(String filePath) - { - //get file info - FileInfo fi = new FileInfo(filePath); - if(!fi.Exists){ - FileStream f_out = fi.OpenWrite(); - f_out.Close(); - } - - //get security access - FileSecurity fs = fi.GetAccessControl(); - - //remove any inherited access - fs.SetAccessRuleProtection(true, false); - - //get any special user access - AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); // show as names - - //remove any special access - foreach (FileSystemAccessRule rule in rules) - fs.RemoveAccessRule(rule); - - fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_Admins), FileSystemRights.FullControl, AccessControlType.Allow)); - fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_System), FileSystemRights.FullControl, AccessControlType.Allow)); - fs.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_Users), FileSystemRights.Read, AccessControlType.Allow)); - - //add current user with full control. - //fs.AddAccessRule(new FileSystemAccessRule(domainName + "\\" + userName, FileSystemRights.FullControl, AccessControlType.Allow)); - - //add all other users delete only permissions. - //SecurityIdentifier sid = new SecurityIdentifier("S-1-5-11"); // Authenticated Users - //fs.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.Delete, AccessControlType.Allow)); - - //flush security access. - File.SetAccessControl(filePath, fs); - } - - static public void SetAnyDirSec(string filePath) - { - DirectoryInfo info = new DirectoryInfo(filePath); - DirectorySecurity security = info.GetAccessControl(); - security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_World), FileSystemRights.Modify, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); - security.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(SID_World), FileSystemRights.Modify, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); - info.SetAccessControl(security); - } - - static public bool TestWrite(string filePath) - { - FileInfo fi = new FileInfo(filePath); - try - { - FileStream f_out = fi.OpenWrite(); - f_out.Close(); - return true; - } - catch - { - return false; - } - } - - public static string SID_null = "S-1-0-0"; // Null SID - public static string SID_World = "S-1-1-0"; // World - public static string SID_Local = "S-1-2-0"; // Local - public static string SID_Console = "S-1-2-1"; // Console Logon - public static string SID_OwnerID = "S-1-3-0"; // Creator Owner ID - public static string SID_GroupID = "S-1-3-1"; // Creator Group ID - public static string SID_OwnerSvr = "S-1-3-2"; // Creator Owner Server - public static string SID_CreatorSvr = "S-1-3-3"; // Creator Group Server - public static string SID_OwnerRights = "S-1-3-4"; // Owner Rights - public static string SID_NonUnique = "S-1-4"; // Non-unique Authority - public static string SID_NTAuth = "S-1-5"; // NT Authority - public static string SID_AllServices = "S-1-5-80-0"; // All Services - public static string SID_DialUp = "S-1-5-1"; // Dialup - public static string SID_LocalAcc = "S-1-5-113"; // Local account - public static string SID_LocalAccAdmin = "S-1-5-114"; // Local account and member of Administrators group - public static string SID_Net = "S-1-5-2"; // Network - public static string SID_Natch = "S-1-5-3"; // Batch - public static string SID_Interactive = "S-1-5-4"; // Interactive - //public static string SID_ = "S-1-5-5- *X*- *Y* Logon Session - public static string SID_Service = "S-1-5-6"; // Service - public static string SID_AnonLogin = "S-1-5-7"; // Anonymous Logon - - public static string SID_Proxy = "S-1-5-8"; // Proxy - public static string SID_EDC = "S-1-5-9"; // Enterprise Domain Controllers - public static string SID_Self = "S-1-5-10"; // Self - public static string SID_AuthenticetedUser = "S-1-5-11"; // Authenticated Users - - public static string SID_Restricted = "S-1-5-12"; // Restricted Code - public static string SID_TermUser = "S-1-5-13"; // Terminal Server User - public static string SID_RemoteLogin = "S-1-5-14"; // Remote Interactive Logon - public static string SID_ThisORg = "S-1-5-15"; // This Organization - public static string SID_IIS = "S-1-5-17"; // IIS_USRS - public static string SID_System = "S-1-5-18"; // System(or LocalSystem) - - public static string SID_NTAuthL = "S-1-5-19"; // NT Authority(LocalService) - public static string SID_NetServices = "S-1-5-20"; // Network Service - - public static string SID_Admins = "S-1-5-32-544"; // Administrators - public static string SID_Users = "S-1-5-32-545"; // Users - public static string SID_Guests = "S-1-5-32-546"; // Guests - public static string SID_PowerUsers = "S-1-5-32-547"; // Power Users - public static string SID_AccOps = "S-1-5-32-548"; // Account Operators - public static string SID_ServerOps = "S-1-5-32-549"; // Server Operators - public static string SID_PrintOps = "S-1-5-32-550"; // Print Operators - public static string SID_BackupOps = "S-1-5-32-551"; // Backup Operators - public static string SID_Replicators = "S-1-5-32-552"; // Replicators - public static string SID_NTLM_Auth = "S-1-5-64-10"; // NTLM Authentication - public static string SID_SCh_Auth = "S-1-5-64-14"; // SChannel Authentication - public static string SID_DigestAuth = "S-1-5-64-21"; // Digest Authentication - public static string SID_NT_Service = "S-1-5-80"; // NT Service - public static string SID_All_Services = "S-1-5-80-0"; // All Services - public static string SID_VM = "S-1-5-83-0"; // NT VIRTUAL MACHINE\Virtual Machines - public static string SID_UntrustedLevel = "S-1-16-0"; // Untrusted Mandatory Level - public static string SID_LowLevel = "S-1-16-4096"; // Low Mandatory Level - public static string SID_MediumLevel = "S-1-16-8192"; // Medium Mandatory Level - public static string SID_MediumPLevel = "S-1-16-8448"; // Medium Plus Mandatory Level - public static string SID_HighLevel = "S-1-16-12288"; // High Mandatory Level - public static string SID_SysLevel = "S-1-16-16384"; // System Mandatory Level - public static string SID_PPLevel = "S-1-16-20480"; // Protected Process Mandatory Level - public static string SID_SPLevel = "S-1-16-28672"; // Secure Process Mandatory Level - - internal static bool TakeOwn(string path) - { - bool ret = true; - try - { - TokenManipulator.AddPrivilege(TokenManipulator.SE_TAKE_OWNERSHIP_NAME); - - FileSecurity ac = File.GetAccessControl(path); - ac.SetOwner(new SecurityIdentifier(FileOps.SID_Admins)); - File.SetAccessControl(path, ac); - } - catch (Exception err) - { - AppLog.Exception(err); - ret = false; - } - finally - { - TokenManipulator.RemovePrivilege(TokenManipulator.SE_TAKE_OWNERSHIP_NAME); - } - return ret; - } -} diff --git a/PrivateWin10/API/Network/IPHelper.cs b/PrivateWin10/API/Network/IPHelper.cs deleted file mode 100644 index 3889839..0000000 --- a/PrivateWin10/API/Network/IPHelper.cs +++ /dev/null @@ -1,481 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - - -public class IPHelper -{ - [StructLayout(LayoutKind.Sequential)] - public struct TCPIP_OWNER_MODULE_BASIC_INFO - { - public IntPtr pModuleName; - public IntPtr pModulePath; - } - - - public class ModuleInfo - { - public string ModuleName; - public string ModulePath; - - public ModuleInfo(TCPIP_OWNER_MODULE_BASIC_INFO inf) - { - ModuleName = inf.pModuleName != null ? Marshal.PtrToStringAuto(inf.pModuleName) : ""; - ModulePath = inf.pModulePath != null ? Marshal.PtrToStringAuto(inf.pModulePath) : ""; - } - } - - public interface I_SOCKET_ROW - { - int ProcessId { get; } - ModuleInfo Module { get; } - - UInt32 ProtocolType { get; } - IPAddress RemoteAddress { get; } - UInt16 RemotePort { get; } - IPAddress LocalAddress { get; } - UInt16 LocalPort { get; } - MIB_TCP_STATE State { get; } - DateTime CreationTime { get; } - } - - - - [DllImport("kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)] - public static extern void ZeroMemory(IntPtr dest, IntPtr size); - - public const UInt32 NO_ERROR = 0; - public const UInt32 ERROR_INSUFFICIENT_BUFFER = 122; - public const UInt32 ERROR_NOT_FOUND = 1168; - - public enum AF_INET - { - IP4 = 2, - IP6 = 23 - } - - public enum AF_PROT - { - TCP = 6, - UDP = 17 - } - - ////////////////////////////////////////////////////// - // TCP - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetExtendedTcpTable(IntPtr pTcpTable, ref UInt32 dwOutBufLen, [MarshalAs(UnmanagedType.Bool)] bool order, AF_INET ipVersion, TCP_TABLE_CLASS tblClass, UInt32 reserved); - - public enum TCP_TABLE_CLASS - { - TCP_TABLE_BASIC_LISTENER, - TCP_TABLE_BASIC_CONNECTIONS, - TCP_TABLE_BASIC_ALL, - TCP_TABLE_OWNER_PID_LISTENER, - TCP_TABLE_OWNER_PID_CONNECTIONS, - TCP_TABLE_OWNER_PID_ALL, - TCP_TABLE_OWNER_MODULE_LISTENER, - TCP_TABLE_OWNER_MODULE_CONNECTIONS, - TCP_TABLE_OWNER_MODULE_ALL - } - - public enum TCPIP_OWNER_MODULE_INFO_CLASS - { - TCPIP_OWNER_MODULE_INFO_BASIC - } - - public enum MIB_TCP_STATE - { - CLOSED = 1, - LISTENING, - SYN_SENT, - SYN_RCVD, - ESTABLISHED, - FIN_WAIT1, - FIN_WAIT2, - CLOSE_WAIT, - CLOSING, - LAST_ACK, - TIME_WAIT, - DELETE_TCB, - UNDEFINED = 65535 - } - - public enum TCP_ESTATS_TYPE - { - TcpConnectionEstatsSynOpts, - TcpConnectionEstatsData, - TcpConnectionEstatsSndCong, - TcpConnectionEstatsPath, - TcpConnectionEstatsSendBuff, - TcpConnectionEstatsRec, - TcpConnectionEstatsObsRec, - TcpConnectionEstatsBandwidth, - TcpConnectionEstatsFineRtt, - TcpConnectionEstatsMaximum - } - - - ////////////////////////////////////////////////////// - // TCPv4 - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetOwnerModuleFromTcpEntry(ref MIB_TCPROW_OWNER_MODULE pTcpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); - - /*[DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetPerTcpConnectionEStats(ref MIB_TCPROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, IntPtr Ros, UInt32 RosVersion, UInt32 RosSize, IntPtr Rod, UInt32 RodVersion, UInt32 RodSize); - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 SetPerTcpConnectionEStats(ref MIB_TCPROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, UInt32 Offset);*/ - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_TCPROW_OWNER_MODULE : I_SOCKET_ROW - { - internal UInt32 dwState; - internal UInt32 dwLocalAddr; - internal UInt32 dwLocalPort; - internal UInt32 dwRemoteAddr; - internal UInt32 dwRemotePort; - internal UInt32 dwOwningPid; - internal Int64 liCreateTimestamp; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal ulong[] OwningModuleInfo; - - public int ProcessId { get { return (int)dwOwningPid; } } - public ModuleInfo Module - { - get - { - ModuleInfo Info = null; - - uint buffSize = 0; - GetOwnerModuleFromTcpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); - IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); - - if (GetOwnerModuleFromTcpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR && buffSize != 0) - Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); - - if (buffer != IntPtr.Zero) - Marshal.FreeHGlobal(buffer); - - return Info; - } - } - - public UInt32 ProtocolType { get { return (UInt32)AF_PROT.TCP | (UInt32)AF_INET.IP4 << 8; } } - public IPAddress RemoteAddress { get { return new IPAddress((UInt32)dwRemoteAddr); } } - public UInt16 RemotePort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwRemotePort); } } - public IPAddress LocalAddress { get { return new IPAddress((UInt32)dwLocalAddr); } } - public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } - public MIB_TCP_STATE State { get { return (MIB_TCP_STATE)dwState; } } - public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } - } - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_TCPTABLE_OWNER_MODULE - { - public UInt32 dwNumEntries; - public MIB_TCPROW_OWNER_MODULE FirstEntry; - } - - public static IntPtr GetTcpSockets(ref List Sockets) - { - uint tcp4Size = 0; - IntPtr tcp4Table = IntPtr.Zero; - IPHelper.GetExtendedTcpTable(IntPtr.Zero, ref tcp4Size, false, IPHelper.AF_INET.IP4, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0); - tcp4Table = Marshal.AllocHGlobal((int)tcp4Size); - - if (IPHelper.GetExtendedTcpTable(tcp4Table, ref tcp4Size, false, IPHelper.AF_INET.IP4, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0) == IPHelper.NO_ERROR) - { - IPHelper.MIB_TCPTABLE_OWNER_MODULE table = ((IPHelper.MIB_TCPTABLE_OWNER_MODULE)Marshal.PtrToStructure(tcp4Table, typeof(IPHelper.MIB_TCPTABLE_OWNER_MODULE))); - IntPtr rowPtr = (IntPtr)((long)tcp4Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_TCPTABLE_OWNER_MODULE), "FirstEntry")); - - for (uint i = 0; i < table.dwNumEntries; i++) - { - IPHelper.MIB_TCPROW_OWNER_MODULE mibRow = (IPHelper.MIB_TCPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_TCPROW_OWNER_MODULE)); - rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); - - Sockets.Add(mibRow); - } - } - - return tcp4Table; - } - - ////////////////////////////////////////////////////// - // TCPv6 - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetOwnerModuleFromTcp6Entry(ref MIB_TCP6ROW_OWNER_MODULE pTcpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); - - /*[DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetPerTcp6ConnectionEStats(ref MIB_TCP6ROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, IntPtr Ros, UInt32 RosVersion, UInt32 RosSize, IntPtr Rod, UInt32 RodVersion, UInt32 RodSize); - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 SetPerTcp6ConnectionEStats(ref MIB_TCP6ROW_OWNER_MODULE Row, TCP_ESTATS_TYPE EstatsType, IntPtr Rw, UInt32 RwVersion, UInt32 RwSize, UInt32 Offset);*/ - - //[DllImport("ntdll.dll", SetLastError = true)] - //public static extern void RtlIpv6AddressToString(byte[] Addr, out StringBuilder res); - - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct MIB_TCP6ROW_OWNER_MODULE : I_SOCKET_ROW - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal byte[] ucLocalAddress; - internal UInt32 dwLocalScopeId; - internal UInt32 dwLocalPort; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal byte[] ucRemoteAddress; - internal UInt32 dwRemoteScopeId; - internal UInt32 dwRemotePort; - internal UInt32 dwState; - internal UInt32 dwOwningPid; - internal Int64 liCreateTimestamp; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal ulong[] OwningModuleInfo; - - public int ProcessId { get { return (int)dwOwningPid; } } - public ModuleInfo Module - { - get - { - ModuleInfo Info = null; - - uint buffSize = 0; - GetOwnerModuleFromTcp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); - IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); - - if (GetOwnerModuleFromTcp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR) - Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); - - if (buffer != IntPtr.Zero) - Marshal.FreeHGlobal(buffer); - - return Info; - } - } - - public UInt32 ProtocolType { get { return (UInt32)AF_PROT.TCP | (UInt32)AF_INET.IP6 << 8; } } - public IPAddress RemoteAddress { get { return new IPAddress(ucLocalAddress); } } - public UInt16 RemotePort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwRemotePort); } } - public IPAddress LocalAddress { get { return new IPAddress(ucLocalAddress); } } - public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } - public MIB_TCP_STATE State { get { return (MIB_TCP_STATE)dwState; } } - public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } - } - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_TCP6TABLE_OWNER_MODULE - { - public UInt32 dwNumEntries; - public MIB_TCP6ROW_OWNER_MODULE FirstEntry; - } - - public static IntPtr GetTcp6Sockets(ref List Sockets) - { - uint tcp6Size = 0; - IntPtr tcp6Table = IntPtr.Zero; - IPHelper.GetExtendedTcpTable(IntPtr.Zero, ref tcp6Size, false, IPHelper.AF_INET.IP6, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0); - tcp6Table = Marshal.AllocHGlobal((int)tcp6Size); - - if (IPHelper.GetExtendedTcpTable(tcp6Table, ref tcp6Size, false, IPHelper.AF_INET.IP6, IPHelper.TCP_TABLE_CLASS.TCP_TABLE_OWNER_MODULE_ALL, 0) == IPHelper.NO_ERROR) - { - IPHelper.MIB_TCP6TABLE_OWNER_MODULE table = ((IPHelper.MIB_TCP6TABLE_OWNER_MODULE)Marshal.PtrToStructure(tcp6Table, typeof(IPHelper.MIB_TCP6TABLE_OWNER_MODULE))); - IntPtr rowPtr = (IntPtr)((long)tcp6Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_TCP6TABLE_OWNER_MODULE), "FirstEntry")); - - for (uint i = 0; i < table.dwNumEntries; i++) - { - IPHelper.MIB_TCP6ROW_OWNER_MODULE mibRow = (IPHelper.MIB_TCP6ROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_TCP6ROW_OWNER_MODULE)); - rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); - - Sockets.Add(mibRow); - } - } - - return tcp6Table; - } - - ////////////////////////////////////////////////////// - // UDP - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetExtendedUdpTable(IntPtr pUdpTable, ref UInt32 dwOutBufLen, bool order, AF_INET ipVersion, UDP_TABLE_CLASS tblClass, UInt32 reserved); - - public enum UDP_TABLE_CLASS - { - UDP_TABLE_BASIC, - UDP_TABLE_OWNER_PID, - UDP_TABLE_OWNER_MODULE - } - - ////////////////////////////////////////////////////// - // UDPv4 - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetOwnerModuleFromUdpEntry(ref MIB_UDPROW_OWNER_MODULE pUdpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); - - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_UDPROW_OWNER_MODULE : I_SOCKET_ROW - { - internal UInt32 dwLocalAddr; - internal UInt32 dwLocalPort; - internal UInt32 dwOwningPid; - internal Int64 liCreateTimestamp; - internal UInt32 dwFlags; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal ulong[] OwningModuleInfo; - - public int ProcessId { get { return (int)dwOwningPid; } } - public ModuleInfo Module - { - get - { - ModuleInfo Info = null; - - uint buffSize = 0; - GetOwnerModuleFromUdpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); - IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); - - if (GetOwnerModuleFromUdpEntry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR) - Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); - - if (buffer != IntPtr.Zero) - Marshal.FreeHGlobal(buffer); - - return Info; - } - } - - public UInt32 ProtocolType { get { return (UInt32)AF_PROT.UDP | (UInt32)AF_INET.IP4 << 8; } } - public IPAddress RemoteAddress { get { return null; } } - public UInt16 RemotePort { get { return 0; } } - public IPAddress LocalAddress { get { return new IPAddress((UInt32)dwLocalAddr); } } - public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } - public MIB_TCP_STATE State { get { return MIB_TCP_STATE.UNDEFINED; } } - public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } - } - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_UDPTABLE_OWNER_MODULE - { - public UInt32 dwNumEntries; - public MIB_UDPROW_OWNER_MODULE FirstEntry; - } - - public static IntPtr GetUdpSockets(ref List Sockets) - { - uint udp4Size = 0; - IntPtr udp4Table = IntPtr.Zero; - IPHelper.GetExtendedUdpTable(IntPtr.Zero, ref udp4Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0); - udp4Table = Marshal.AllocHGlobal((int)udp4Size); - - if (IPHelper.GetExtendedUdpTable(udp4Table, ref udp4Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0) == IPHelper.NO_ERROR) - { - IPHelper.MIB_UDPTABLE_OWNER_MODULE table = ((IPHelper.MIB_UDPTABLE_OWNER_MODULE)Marshal.PtrToStructure(udp4Table, typeof(IPHelper.MIB_UDPTABLE_OWNER_MODULE))); - IntPtr rowPtr = (IntPtr)((long)udp4Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_UDPTABLE_OWNER_MODULE), "FirstEntry")); - - for (uint i = 0; i < table.dwNumEntries; i++) - { - IPHelper.MIB_UDPROW_OWNER_MODULE mibRow = (IPHelper.MIB_UDPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_UDPROW_OWNER_MODULE)); - rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); - - Sockets.Add(mibRow); - } - } - - return udp4Table; - } - - ////////////////////////////////////////////////////// - // UDPv6 - // - - [DllImport("iphlpapi.dll", SetLastError = true)] - public static extern UInt32 GetOwnerModuleFromUdp6Entry(ref MIB_UDP6ROW_OWNER_MODULE pUdpEntry, TCPIP_OWNER_MODULE_INFO_CLASS Class, IntPtr Buffer, ref UInt32 pdwSize); - - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_UDP6ROW_OWNER_MODULE : I_SOCKET_ROW - { - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal byte[] ucLocalAddress; - internal UInt32 dwLocalScopeId; - internal UInt32 dwLocalPort; - internal UInt32 dwOwningPid; - internal Int64 liCreateTimestamp; - internal UInt32 dwFlags; - [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] - internal ulong[] OwningModuleInfo; - - public int ProcessId { get { return (int)dwOwningPid; } } - public ModuleInfo Module - { - get - { - ModuleInfo Info = null; - - uint buffSize = 0; - GetOwnerModuleFromUdp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, IntPtr.Zero, ref buffSize); - IntPtr buffer = Marshal.AllocHGlobal((int)buffSize); - - if (GetOwnerModuleFromUdp6Entry(ref this, TCPIP_OWNER_MODULE_INFO_CLASS.TCPIP_OWNER_MODULE_INFO_BASIC, buffer, ref buffSize) == NO_ERROR) - Info = new ModuleInfo((TCPIP_OWNER_MODULE_BASIC_INFO)Marshal.PtrToStructure(buffer, typeof(TCPIP_OWNER_MODULE_BASIC_INFO))); - - if (buffer != IntPtr.Zero) - Marshal.FreeHGlobal(buffer); - - return Info; - } - } - - public UInt32 ProtocolType { get { return (UInt32)AF_PROT.UDP | (UInt32)AF_INET.IP6 << 8; } } - public IPAddress RemoteAddress { get { return null; } } - public UInt16 RemotePort { get { return 0; } } - public IPAddress LocalAddress { get { return new IPAddress(ucLocalAddress); } } - public UInt16 LocalPort { get { return (UInt16)IPAddress.NetworkToHostOrder((short)dwLocalPort); } } - public MIB_TCP_STATE State { get { return MIB_TCP_STATE.UNDEFINED; } } - public DateTime CreationTime { get { return liCreateTimestamp == 0 ? DateTime.Now : DateTime.FromFileTime(liCreateTimestamp); } } - } - - [StructLayout(LayoutKind.Sequential)] - public struct MIB_UDP6TABLE_OWNER_MODULE - { - public UInt32 dwNumEntries; - public MIB_UDP6ROW_OWNER_MODULE FirstEntry; - } - - public static IntPtr GetUdp6Sockets(ref List Sockets) - { - uint udp6Size = 0; - IntPtr udp6Table = IntPtr.Zero; - IPHelper.GetExtendedUdpTable(IntPtr.Zero, ref udp6Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0); - udp6Table = Marshal.AllocHGlobal((int)udp6Size); - - if (IPHelper.GetExtendedUdpTable(udp6Table, ref udp6Size, false, IPHelper.AF_INET.IP4, IPHelper.UDP_TABLE_CLASS.UDP_TABLE_OWNER_MODULE, 0) == IPHelper.NO_ERROR) - { - IPHelper.MIB_UDP6TABLE_OWNER_MODULE table = ((IPHelper.MIB_UDP6TABLE_OWNER_MODULE)Marshal.PtrToStructure(udp6Table, typeof(IPHelper.MIB_UDP6TABLE_OWNER_MODULE))); - IntPtr rowPtr = (IntPtr)((long)udp6Table + (long)Marshal.OffsetOf(typeof(IPHelper.MIB_UDP6TABLE_OWNER_MODULE), "FirstEntry")); - - for (uint i = 0; i < table.dwNumEntries; i++) - { - IPHelper.MIB_UDPROW_OWNER_MODULE mibRow = (IPHelper.MIB_UDPROW_OWNER_MODULE)Marshal.PtrToStructure(rowPtr, typeof(IPHelper.MIB_UDPROW_OWNER_MODULE)); - rowPtr = (IntPtr)((long)rowPtr + (long)Marshal.SizeOf(mibRow)); - - Sockets.Add(mibRow); - } - } - - return udp6Table; - } -} diff --git a/PrivateWin10/API/Network/NetFunc.cs b/PrivateWin10/API/Network/NetFunc.cs deleted file mode 100644 index f99e1a6..0000000 --- a/PrivateWin10/API/Network/NetFunc.cs +++ /dev/null @@ -1,440 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Net; -using System.Numerics; - - -public class NetFunc -{ - public static BigInteger IpToInt(IPAddress ip) - { - List ipFormat = ip.GetAddressBytes().ToList(); - ipFormat.Reverse(); - ipFormat.Add(0); - return new BigInteger(ipFormat.ToArray()); - } - - public static BigInteger IpStrToInt(string strIP, out int type) - { - IPAddress ip; - if (!IPAddress.TryParse(strIP, out ip)) - { - type = 0; - return BigInteger.Zero; - } - byte[] bytes = ip.GetAddressBytes(); - if (bytes.Length == 4) - type = 4; - else - type = 6; - List ipFormat = bytes.ToList(); - ipFormat.Reverse(); - ipFormat.Add(0); - return new BigInteger(ipFormat.ToArray()); - } - - public static BigInteger MaxIPofType(int type) - { - IPAddress ip = (type == 4) ? IPAddress.Parse("255.255.255.255") : IPAddress.Parse("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"); - List ipFormat = ip.GetAddressBytes().ToList(); - ipFormat.Reverse(); - ipFormat.Add(0); - return new BigInteger(ipFormat.ToArray()); - } - - public enum KnownProtocols - { - Min = 0, - Max = 142, - Any = 256 - } - - /*public static string Protocol2Str(int Protocol, string unkStr = "Unknow") - { - switch (Protocol) - { - case 0: return "HOPOPT (IPv6 Hop-by-Hop Option)"; - case 1: return "ICMP (Internet Control Message Protocol)"; - case 2: return "IGMP (Internet Group Management Protocol)"; - case 3: return "GGP (Gateway-to-Gateway)"; - case 4: return "IP (IP in IP (encapsulation))"; - case 5: return "Stream"; - case 6: return "TCP (Transmission Control Protocol)"; - case 7: return "CBT (Core Based Trees)"; - case 8: return "EGP (Exterior Gateway Protocol)"; - case 9: return "IGP (any private interior gateway)"; - case 10: return "BBN-RCC-MON (BBN RCC Monitoring)"; - case 11: return "NVP-II (Network Voice Protocol)"; - case 12: return "PUP"; - case 13: return "ARGUS"; - case 14: return "EMCON"; - case 15: return "XNET (Cross Net Debugger)"; - case 16: return "CHAOS"; - case 17: return "UDP (User Datagram Protocol)"; - case 18: return "Multiplexing"; - case 19: return "DCN-MEAS (DCN Measurement Subsystems)"; - case 20: return "HMP (Host Monitoring)"; - case 21: return "PRM (Packet Radio Measurement)"; - case 22: return "XNS-IDP (XEROX NS IDP)"; - case 23: return "TRUNK-1"; - case 24: return "TRUNK-2"; - case 25: return "LEAF-1"; - case 26: return "LEAF-2"; - case 27: return "RDP (Reliable Data Protocol)"; - case 28: return "IRTP (Internet Reliable Transaction Protocol)"; - case 29: return "ISO-TP4 (ISO Transport Protocol Class 4)"; - case 30: return "NETBLT (Bulk Data Transfer Protocol)"; - case 31: return "MFE-NSP (MFE Network Services Protocol)"; - case 32: return "MERIT-INP (MERIT Internodal Protocol)"; - case 33: return "DCCP (Datagram Congestion Control Protocol)"; - case 34: return "3PC (Third Party Connect Protocol)"; - case 35: return "IDPR (Inter-Domain Policy Routing Protocol)"; - case 36: return "XTP"; - case 37: return "DDP (Datagram Delivery Protocol)"; - case 38: return "IDPR-CMTP (IDPR Control Message Transport Proto)"; - case 39: return "TP++ (TP++ Transport Protocol)"; - case 40: return "IL (IL Transport Protocol)"; - case 41: return "Verkapselung von IPv6- in IPv4-Pakete"; - case 42: return "SDRP (Source Demand Routing Protocol)"; - case 43: return "IPv6-Route (Routing Header for IPv6)"; - case 44: return "IPv6-Frag (Fragment Header for IPv6)"; - case 45: return "IDRP (Inter-Domain Routing Protocol)"; - case 46: return "RSVP (Reservation Protocol)"; - case 47: return "GRE (Generic Routing Encapsulation)"; - case 48: return "MHRP (Mobile Host Routing Protocol)"; - case 49: return "BNA"; - case 50: return "ESP (Encapsulating Security Payload)"; - case 51: return "AH (Authentication Header)"; - case 52: return "I-NLSP (Integrated Net Layer Security TUBA)"; - case 53: return "SWIPE (IP with Encryption)"; - case 54: return "NARP (NBMA Address Resolution Protocol)"; - case 55: return "MOBILE (IP Mobility)"; - case 56: return "TLSP (Transport Layer Security Protocol)"; - case 57: return "SKIP"; - case 58: return "IPv6-ICMP (ICMP for IPv6)"; - case 59: return "IPv6-NoNxt (Kein nächster Header für IPv6)"; - case 60: return "IPv6-Opts (Destination Options for IPv6)"; - case 61: return "Jedes Host-interne Protokoll"; - case 62: return "CFTP"; - case 63: return "Jedes lokale Netz"; - case 64: return "SAT-EXPAK (SATNET and Backroom EXPAK)"; - case 65: return "KRYPTOLAN"; - case 66: return "RVD (MIT Remote Virtual Disk Protocol)"; - case 67: return "IPPC (Internet Pluribus Packet Core)"; - case 68: return "Jedes verteilte Dateisystem"; - case 69: return "SAT-MON (SATNET Monitoring)"; - case 70: return "VISA"; - case 71: return "IPCV (Internet Packet Core Utility)"; - case 72: return "CPNX (Computer Protocol Network Executive)"; - case 73: return "CPHB (Computer Protocol Heart Beat)"; - case 74: return "WSN (Wang Span Network)"; - case 75: return "PVP (Packet Video Protocol)"; - case 76: return "BR-SAT-MON (Backroom SATNET Monitoring)"; - case 77: return "SUN-ND (SUN ND PROTOCOL-Temporary)"; - case 78: return "WB-MON (WIDEBAND Monitoring)"; - case 79: return "WB-EXPAK (WIDEBAND EXPAK)"; - case 80: return "ISO-IP (ISO Internet Protocol)"; - case 81: return "VMTP"; - case 82: return "SECURE-VMTP"; - case 83: return "VINES"; - case 84: return "TTP"; - case 85: return "NSFNET-IGP (NSFNET-IGP)"; - case 86: return "DGP (Dissimilar Gateway Protocol)"; - case 87: return "TCF"; - case 88: return "EIGRP"; - case 89: return "OSPF"; - case 90: return "Sprite-RPC (Sprite RPC Protocol)"; - case 91: return "LARP (Locus Address Resolution Protocol)"; - case 92: return "MTP (Multicast Transport Protocol)"; - case 93: return "AX.25 (AX.25 Frames)"; - case 94: return "IPIP (IP-within-IP Encapsulation Protocol)"; - case 95: return "MICP (Mobile Internetworking Control Pro.)"; - case 96: return "SCC-SP (Semaphore Communications Sec. Pro.)"; - case 97: return "ETHERIP (Ethernet-within-IP Encapsulation)"; - case 98: return "ENCAP (Encapsulation Header)"; - case 99: return "Jeder private Verschlüsselungsentwurf"; - case 100: return "GMTP"; - case 101: return "IFMP (Ipsilon Flow Management Protocol)"; - case 102: return "PNNI (over IP)"; - case 103: return "PIM (Protocol Independent Multicast)"; - case 104: return "ARIS"; - case 105: return "SCPS"; - case 106: return "QNX"; - case 107: return "A/N (Active Networks)"; - case 108: return "IPComp (IP Payload Compression Protocol)"; - case 109: return "SNP (Sitara Networks Protocol)"; - case 110: return "Compaq-Peer (Compaq Peer Protocol)"; - case 111: return "IPX-in-IP (IPX in IP)"; - case 112: return "VRRP (Virtual Router Redundancy Protocol)"; - case 113: return "PGM (PGM Reliable Transport Protocol)"; - case 114: return "any 0-hop protocol"; - case 115: return "L2TP (Layer Two Tunneling Protocol)"; - case 116: return "DDX (D-II Data Exchange (DDX))"; - case 117: return "IATP (Interactive Agent Transfer Protocol)"; - case 118: return "STP (Schedule Transfer Protocol)"; - case 119: return "SRP (SpectraLink Radio Protocol)"; - case 120: return "UTI"; - case 121: return "SMP (Simple Message Protocol)"; - case 122: return "SM"; - case 123: return "PTP (Performance Transparency Protocol)"; - case 124: return "ISIS over IPv4"; - case 125: return "FIRE"; - case 126: return "CRTP (Combat Radio Transport Protocol)"; - case 127: return "CRUDP (Combat Radio User Datagram)"; - case 128: return "SSCOPMCE"; - case 129: return "IPLT"; - case 130: return "SPS (Secure Packet Shield)"; - case 131: return "PIPE (Private IP Encapsulation within IP)"; - case 132: return "SCTP (Stream Control Transmission Protocol)"; - case 133: return "FC (Fibre Channel)"; - case 134: return "RSVP-E2E-IGNORE"; - case 135: return "Mobility Header"; - case 136: return "UDPLite"; - case 137: return "MPLS-in-IP"; - case 138: return "manet (MANET Protocols)"; - case 139: return "HIP (Host Identity Protocol)"; - case 140: return "Shim6 (Shim6 Protocol)"; - case 141: return "WESP (Wrapped Encapsulating Security Payload)"; - case 142: return "ROHC (Robust Header Compression)"; - case 256: return "Unspecifyed"; - default: return unkStr; - } - }*/ - - public static string Protocol2Str(UInt32 Protocol) - { - switch (Protocol) - { - case 0: return "HOPOPT"; - case 1: return "ICMP"; - case 2: return "IGMP"; - case 3: return "GGP"; - case 4: return "IP"; - //case 5: return "Stream"; - case 6: return "TCP"; - case 7: return "CBT"; - case 8: return "EGP"; - case 9: return "IGP"; - case 10: return "BBN-RCC-MON"; - case 11: return "NVP-II"; - case 12: return "PUP"; - case 13: return "ARGUS"; - case 14: return "EMCON"; - case 15: return "XNET"; - case 16: return "CHAOS"; - case 17: return "UDP"; - //case 18: return "Multiplexing"; - case 19: return "DCN-MEAS"; - case 20: return "HMP"; - case 21: return "PRM"; - case 22: return "XNS-IDP"; - case 23: return "TRUNK-1"; - case 24: return "TRUNK-2"; - case 25: return "LEAF-1"; - case 26: return "LEAF-2"; - case 27: return "RDP"; - case 28: return "IRTP"; - case 29: return "ISO-TP4"; - case 30: return "NETBLT"; - case 31: return "MFE-NSP"; - case 32: return "MERIT-INP"; - case 33: return "DCCP"; - case 34: return "3PC"; - case 35: return "IDPR"; - case 36: return "XTP"; - case 37: return "DDP"; - case 38: return "IDPR-CMTP"; - case 39: return "TP++"; - case 40: return "IL"; - ///case 41: return "Verkapselung von IPv6- in IPv4-Pakete"; - case 42: return "SDRP"; - case 43: return "IPv6-Route"; - case 44: return "IPv6-Frag"; - case 45: return "IDRP"; - case 46: return "RSVP"; - case 47: return "GRE"; - case 48: return "MHRP"; - case 49: return "BNA"; - case 50: return "ESP"; - case 51: return "AH"; - case 52: return "I-NLSP"; - case 53: return "SWIPE"; - case 54: return "NARP"; - case 55: return "MOBILE"; - case 56: return "TLSP"; - case 57: return "SKIP"; - case 58: return "IPv6-ICMP"; - case 59: return "IPv6-NoNxt"; - case 60: return "IPv6-Opts"; - //case 61: return "Jedes Host-interne Protokoll"; - case 62: return "CFTP"; - //case 63: return "Jedes lokale Netz"; - case 64: return "SAT-EXPAK"; - case 65: return "KRYPTOLAN"; - case 66: return "RVD"; - case 67: return "IPPC"; - //case 68: return "Jedes verteilte Dateisystem"; - case 69: return "SAT-MON"; - case 70: return "VISA"; - case 71: return "IPCV"; - case 72: return "CPNX"; - case 73: return "CPHB"; - case 74: return "WSN"; - case 75: return "PVP"; - case 76: return "BR-SAT-MON"; - case 77: return "SUN-ND"; - case 78: return "WB-MON"; - case 79: return "WB-EXPAK"; - case 80: return "ISO-IP"; - case 81: return "VMTP"; - case 82: return "SECURE-VMTP"; - case 83: return "VINES"; - case 84: return "TTP"; - case 85: return "NSFNET-IGP"; - case 86: return "DGP"; - case 87: return "TCF"; - case 88: return "EIGRP"; - case 89: return "OSPF"; - case 90: return "Sprite-RPC"; - case 91: return "LARP"; - case 92: return "MTP"; - case 93: return "AX.25"; - case 94: return "IPIP"; - case 95: return "MICP"; - case 96: return "SCC-SP"; - case 97: return "ETHERIP"; - case 98: return "ENCAP"; - //case 99: return "Jeder private Verschlüsselungsentwurf"; - case 100: return "GMTP"; - case 101: return "IFMP"; - case 102: return "PNNI"; - case 103: return "PIM"; - case 104: return "ARIS"; - case 105: return "SCPS"; - case 106: return "QNX"; - case 107: return "A/N"; - case 108: return "IPComp"; - case 109: return "SNP"; - case 110: return "Compaq-Peer"; - case 111: return "IPX-in-IP"; - case 112: return "VRRP"; - case 113: return "PGM"; - case 114: return "any 0-hop protocol"; - case 115: return "L2TP"; - case 116: return "DDX"; - case 117: return "IATP"; - case 118: return "STP"; - case 119: return "SRP"; - case 120: return "UTI"; - case 121: return "SMP"; - case 122: return "SM"; - case 123: return "PTP"; - case 124: return "ISIS"; - case 125: return "FIRE"; - case 126: return "CRTP"; - case 127: return "CRUDP"; - case 128: return "SSCOPMCE"; - case 129: return "IPLT"; - case 130: return "SPS"; - case 131: return "PIPE"; - case 132: return "SCTP"; - case 133: return "FC"; - case 134: return "RSVP-E2E-IGNORE"; - case 135: return "Mobility Header"; - case 136: return "UDPLite"; - case 137: return "MPLS-in-IP"; - case 138: return "manet"; - case 139: return "HIP"; - case 140: return "Shim6"; - case 141: return "WESP"; - case 142: return "ROHC"; - case 256: return "???"; - default: return "#" + Protocol.ToString(); - } - } - - public static readonly Dictionary KnownIcmp4Types = new Dictionary() { - {0,"Echo Reply"}, - {3,"Destination Unreachable"}, - {4,"Source Quench"}, - {5,"Redirect"}, - {8,"Echo Request"}, - {9,"Router Advertisement"}, - {10,"Router Solicitation"}, - {11,"Time Exceeded"}, - {12,"Parameter Problem"}, - {13,"Timestamp (erleichtert die Zeitsynchronisation)"}, - {14,"Timestamp Reply"}, - {15,"Information Request"}, - {16,"Information Reply"}, - {17,"Address Mask Request"}, - {18,"Address Mask Reply"}, - {19,"Reserved (for Security)"}, - {30,"Traceroute"}, - {31,"Datagram Conversion Error"}, - {32,"Mobile Host Redirect"}, - {33,"Ursprünglich IPv6 Where-Are-You (ersetzt durch ICMPv6)"}, - {34,"Ursprünglich IPv6 I-Am-Here (ersetzt durch ICMPv6)"}, - {35,"Mobile Registration Request"}, - {36,"Mobile Registration Reply"}, - {37,"Domain Name Request"}, - {38,"Domain Name Reply"}, - {39,"SKIP"}, - {40,"Photuris"}, - {41,"ICMP messages utilized by experimental mobility protocols such as Seamoby"}, - }; - - public static readonly Dictionary KnownIcmp6Types = new Dictionary() { - {1, "Destination Unreachable"}, - {2, "Packet Too Big"}, - {3, "Time Exceeded"}, - {4, "Parameter Problem"}, - {128, "Echo Request"}, - {129, "Echo Reply"}, - {130, "Multicast Listener Query"}, - {131, "Version 1 Multicast Listener Report"}, - {132, "Multicast Listener Done"}, - {133, "Router Solicitation"}, - {134, "Router Advertisement"}, - {135, "Neighbor Solicitation"}, - {136, "Neighbor Advertisement"}, - {137, "Redirect"}, - {138, "Router Renumbering"}, - {139, "ICMP Node Information Query"}, - {140, "ICMP Node Information Response"}, - {141, "Inverse Neighbor Discovery Solicitation Message"}, - {142, "Inverse Neighbor Discovery Advertisement Message"}, - {143, "Version 2 Multicast Listener Report"}, - {144, "Home Agent Address Discovery Request Message"}, - {145, "Home Agent Address Discovery Reply Message"}, - {146, "Mobile Prefix Solicitation"}, - {147, "Mobile Prefix Advertisement"}, - {148, "Certification Path Solicitation Message"}, - {149, "Certification Path Advertisement Message"}, - {150, "ICMP messages utilized by experimental mobility protocols such as Seamoby"}, - {151, "Multicast Router Advertisement"}, - {152, "Multicast Router Solicitation"}, - {153, "Multicast Router Termination"}, - {155, "RPL Control Message"}, - }; - - public static bool IsLocalHost(IPAddress addr) - { - return (addr.Equals(IPAddress.Loopback) || addr.Equals(IPAddress.IPv6Loopback)); - } - - public static bool IsMultiCast(IPAddress addr) - { - // ipv4 multicast: 224.0.0.0 to 239.255.255.255 - // ipv6 multicast: ff00:: to ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff - if (addr.IsIPv6Multicast) - return true; - byte[] addressBytes = addr.GetAddressBytes(); - if (addressBytes.Length == 4) - return addressBytes[0] >= 224 && addressBytes[0] <= 239; - return false; - } -} diff --git a/PrivateWin10/API/ProcFunc.cs b/PrivateWin10/API/ProcFunc.cs deleted file mode 100644 index 06cc43e..0000000 --- a/PrivateWin10/API/ProcFunc.cs +++ /dev/null @@ -1,163 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - - -static class ProcFunc -{ - public static int CurID = System.Diagnostics.Process.GetCurrentProcess().Id; - - public const int SystemPID = 4; // on windows system is has always PID 4 - - [DllImport("kernel32.dll")] - public static extern IntPtr OpenProcess(uint processAccess, bool bInheritHandle, int processId); - - [DllImport("psapi.dll")] - public static extern uint GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, [Out] StringBuilder lpBaseName, [In] [MarshalAs(UnmanagedType.U4)] int nSize); - - [DllImport("advapi32.dll", SetLastError = true)] - public static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle); - - [DllImport("kernel32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - public static extern bool CloseHandle(IntPtr hObject); - - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static string GetProcessFileNameByPID(int pid) - { - if (pid == ProcFunc.SystemPID) - return MiscFunc.NtOsKrnlPath; - - // todo add cache, may be? - var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, pid); - if (processHandle == IntPtr.Zero) - return null; - - string result = null; - - const int lengthSb = 4096+1; - var sb = new StringBuilder(lengthSb); - if (GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, lengthSb) > 0) - { - result = sb.ToString(); - } - - CloseHandle(processHandle); - - return result; - } - - - [DllImport("kernel32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool GetProcessTimes(IntPtr hProcess, out long lpCreationTime, out long lpExitTime, out long lpKernelTime, out long lpUserTime); - - public static long GetProcessCreationTime(int pid) - { - var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, pid); - if (processHandle == IntPtr.Zero) - return 0; - - long RawCreationTime; - long RawExitTime; - long RawKernelTime; - long RawUserTime; - GetProcessTimes(processHandle, out RawCreationTime, out RawExitTime, out RawKernelTime, out RawUserTime); - - CloseHandle(processHandle); - - return RawCreationTime; - } - - [DllImport("advapi32", CharSet = CharSet.Unicode)] - public static extern bool ConvertStringSidToSid([In, MarshalAs(UnmanagedType.LPWStr)] string pStringSid, ref IntPtr pSID); - - [DllImport("advapi32", CharSet = CharSet.Unicode)] - public static extern bool ConvertSidToStringSid(IntPtr pSID, [In, Out, MarshalAs(UnmanagedType.LPWStr)] ref string pStringSid); - - [StructLayout(LayoutKind.Sequential)] - public struct TOKEN_APPCONTAINER_INFORMATION - { - public IntPtr Sid; - } - - [DllImport("ntdll.dll")] - public static extern uint NtQueryInformationToken([In] IntPtr TokenHandle, [In] uint TokenInformationClass, [In] IntPtr TokenInformation, [In] int TokenInformationLength, [Out] [Optional] out int ReturnLength); - - static public string GetAppPackageSidByPID(int PID) - { - //var process = System.Diagnostics.Process.GetProcessById(PID); // throws error if pid is not found - var processHandle = OpenProcess(0x1000/*PROCESS_QUERY_LIMITED_INFORMATION*/, false, PID); - if (processHandle == IntPtr.Zero) - return null; - - string strSID = null; - - IntPtr tokenHandle = IntPtr.Zero; - if (OpenProcessToken(processHandle, 8, out tokenHandle)) - { - int retLen; - NtQueryInformationToken(tokenHandle, 31 /*TokenAppContainerSid*/, IntPtr.Zero, 0, out retLen); - - IntPtr buffer = Marshal.AllocHGlobal((int)retLen); - ulong status = NtQueryInformationToken(tokenHandle, 31 /*TokenAppContainerSid*/, buffer, retLen, out retLen); - if (status >= 0) - { - var appContainerInfo = (TOKEN_APPCONTAINER_INFORMATION)Marshal.PtrToStructure(buffer, typeof(TOKEN_APPCONTAINER_INFORMATION)); - - ConvertSidToStringSid(appContainerInfo.Sid, ref strSID); - } - Marshal.FreeHGlobal(buffer); - - CloseHandle(tokenHandle); - } - - CloseHandle(processHandle); - - return strSID; - } - - /* - [DllImport("Kernel32.dll")] - private static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize); - - // Note: we can not access a module of a otehr bitness as we are so we need a native solution - public static string GetMainModuleFileName(this Process process) - { - var fileNameBuilder = new StringBuilder(1024); - uint bufferLength = (uint)fileNameBuilder.Capacity + 1; - try - { - return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) ? fileNameBuilder.ToString() : null; - } - catch - { - return null; - } - } - */ - - static public string GetPathFromCmdLine(string commandLine) - { - if (commandLine[0] == '"') - { - int pos = commandLine.IndexOf('"', 1); - if (pos != -1) - return commandLine.Substring(1, pos - 1); - } - else - { - int pos = commandLine.IndexOf(' '); - if (pos != -1) - return commandLine.Substring(0, pos); - } - return commandLine; - } -} diff --git a/PrivateWin10/API/ProcessUtilities.cs b/PrivateWin10/API/ProcessUtilities.cs deleted file mode 100644 index f6d616b..0000000 --- a/PrivateWin10/API/ProcessUtilities.cs +++ /dev/null @@ -1,321 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; - - -// All offset values below have been tested on Windows 7 & 8 only -// but you can use WinDbg "dt ntdll!_PEB" command and search for ProcessParameters offset to find the truth, depending on the OS version -public static class ProcessUtilities -{ - public static readonly bool Is64BitProcess = IntPtr.Size > 4; - public static readonly bool Is64BitOperatingSystem = Is64BitProcess || Is64BitChecker.InternalCheckIsWow64(); - - public static string GetCurrentDirectory(int processId) - { - try - { - return GetProcessParametersString(processId, PEB_OFFSET.CurrentDirectory); - } - catch - { - return null; - } - } - - public static string GetCurrentDirectory(this Process process) - { - if (process == null) - throw new ArgumentNullException("process"); - - return GetCurrentDirectory(process.Id); - } - - #region GetCommandLine - //public static string GetCommandLine(int processId) - //{ - // return null;// GetProcessParametersString(processId, Is64BitOperatingSystem ? 0x70 : 0x40); - //} - - //public static string GetCommandLine(this Process process) - //{ - // if (process == null) - // throw new ArgumentNullException("process"); - - // return GetCommandLine(process.Id); - //} - #endregion - - private static string GetProcessParametersString(int processId, PEB_OFFSET Offset) - { - IntPtr handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId); - if (handle == IntPtr.Zero) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - bool IsWow64Process = Is64BitChecker.InternalCheckIsWow64(); - bool IsTargetWow64Process = Is64BitChecker.GetProcessIsWow64(handle); - bool IsTarget64BitProcess = Is64BitOperatingSystem && !IsTargetWow64Process; - - long offset = 0; - long processParametersOffset = IsTarget64BitProcess ? 0x20 : 0x10; - switch (Offset) - { - case PEB_OFFSET.CurrentDirectory: - offset = IsTarget64BitProcess ? 0x38 : 0x24; - break; - case PEB_OFFSET.CommandLine: - default: - return null; - } - - try - { - long pebAddress = 0; - if (IsTargetWow64Process) // OS : 64Bit, Cur : 32 or 64, Tar: 32bit - { - IntPtr peb32 = new IntPtr(); - - int hr = NtQueryInformationProcess(handle, (int)PROCESSINFOCLASS.ProcessWow64Information, ref peb32, IntPtr.Size, IntPtr.Zero); - if (hr != 0) throw new Win32Exception(hr); - pebAddress = peb32.ToInt64(); - - IntPtr pp = new IntPtr(); - if (!ReadProcessMemory(handle, new IntPtr(pebAddress + processParametersOffset), ref pp, new IntPtr(Marshal.SizeOf(pp)), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - UNICODE_STRING_32 us = new UNICODE_STRING_32(); - if (!ReadProcessMemory(handle, new IntPtr(pp.ToInt64() + offset), ref us, new IntPtr(Marshal.SizeOf(us)), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - if ((us.Buffer == 0) || (us.Length == 0)) - return null; - - string s = new string('\0', us.Length / 2); - if (!ReadProcessMemory(handle, new IntPtr(us.Buffer), s, new IntPtr(us.Length), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - return s; - } - else if (IsWow64Process)//Os : 64Bit, Cur 32, Tar 64 - { - PROCESS_BASIC_INFORMATION_WOW64 pbi = new PROCESS_BASIC_INFORMATION_WOW64(); - int hr = NtWow64QueryInformationProcess64(handle, (int)PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, Marshal.SizeOf(pbi), IntPtr.Zero); - if (hr != 0) throw new Win32Exception(hr); - pebAddress = pbi.PebBaseAddress; - - long pp = 0; - hr = NtWow64ReadVirtualMemory64(handle, pebAddress + processParametersOffset, ref pp, Marshal.SizeOf(pp), IntPtr.Zero); - if (hr != 0) - throw new Win32Exception(hr); - - UNICODE_STRING_WOW64 us = new UNICODE_STRING_WOW64(); - hr = NtWow64ReadVirtualMemory64(handle, pp + offset, ref us, Marshal.SizeOf(us), IntPtr.Zero); - if (hr != 0) - throw new Win32Exception(hr); - - if ((us.Buffer == 0) || (us.Length == 0)) - return null; - - string s = new string('\0', us.Length / 2); - hr = NtWow64ReadVirtualMemory64(handle, us.Buffer, s, us.Length, IntPtr.Zero); - if (hr != 0) - throw new Win32Exception(hr); - - return s; - } - else// Os,Cur,Tar : 64 or 32 - { - PROCESS_BASIC_INFORMATION pbi = new PROCESS_BASIC_INFORMATION(); - int hr = NtQueryInformationProcess(handle, (int)PROCESSINFOCLASS.ProcessBasicInformation, ref pbi, Marshal.SizeOf(pbi), IntPtr.Zero); - if (hr != 0) throw new Win32Exception(hr); - pebAddress = pbi.PebBaseAddress.ToInt64(); - - IntPtr pp = new IntPtr(); - if (!ReadProcessMemory(handle, new IntPtr(pebAddress + processParametersOffset), ref pp, new IntPtr(Marshal.SizeOf(pp)), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - UNICODE_STRING us = new UNICODE_STRING(); - if (!ReadProcessMemory(handle, new IntPtr((long)pp + offset), ref us, new IntPtr(Marshal.SizeOf(us)), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - if ((us.Buffer == IntPtr.Zero) || (us.Length == 0)) - return null; - - string s = new string('\0', us.Length / 2); - if (!ReadProcessMemory(handle, us.Buffer, s, new IntPtr(us.Length), IntPtr.Zero)) - throw new Win32Exception(Marshal.GetLastWin32Error()); - - return s; - } - } - finally - { - CloseHandle(handle); - } - } - - private const int PROCESS_QUERY_INFORMATION = 0x400; - private const int PROCESS_VM_READ = 0x10; - - [StructLayout(LayoutKind.Sequential)] - private struct PROCESS_BASIC_INFORMATION - { - public IntPtr Reserved1; - public IntPtr PebBaseAddress; - public IntPtr Reserved2_0; - public IntPtr Reserved2_1; - public IntPtr UniqueProcessId; - public IntPtr Reserved3; - } - - [StructLayout(LayoutKind.Sequential)] - private struct UNICODE_STRING - { - public short Length; - public short MaximumLength; - public IntPtr Buffer; - } - - // for 32-bit process in a 64-bit OS only - [StructLayout(LayoutKind.Sequential)] - private struct PROCESS_BASIC_INFORMATION_WOW64 - { - public long Reserved1; - public long PebBaseAddress; - public long Reserved2_0; - public long Reserved2_1; - public long UniqueProcessId; - public long Reserved3; - } - - // for 32-bit process - [StructLayout(LayoutKind.Sequential)] - private struct UNICODE_STRING_WOW64 - { - public short Length; - public short MaximumLength; - public long Buffer; - } - - [StructLayout(LayoutKind.Sequential)] - private struct UNICODE_STRING_32 - { - public short Length; - public short MaximumLength; - public int Buffer; - } - - [DllImport("ntdll.dll")] - private static extern int NtQueryInformationProcess(IntPtr ProcessHandle, int ProcessInformationClass, ref PROCESS_BASIC_INFORMATION ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); - - //ProcessWow64Information, // q: ULONG_PTR - [DllImport("ntdll.dll")] - private static extern int NtQueryInformationProcess(IntPtr ProcessHandle, int ProcessInformationClass, ref IntPtr ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); - - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref IntPtr lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING_32 lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); - - //[DllImport("kernel32.dll", SetLastError = true)] - //private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref UNICODE_STRING_WOW64 lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("kernel32.dll", SetLastError = true)] - private static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer, IntPtr dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("kernel32.dll", SetLastError = true)] - private static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId); - - [DllImport("kernel32.dll")] - private static extern bool CloseHandle(IntPtr hObject); - - // for 32-bit process in a 64-bit OS only - [DllImport("ntdll.dll")] - private static extern int NtWow64QueryInformationProcess64(IntPtr ProcessHandle, int ProcessInformationClass, ref PROCESS_BASIC_INFORMATION_WOW64 ProcessInformation, int ProcessInformationLength, IntPtr ReturnLength); - - [DllImport("ntdll.dll")] - private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, ref long lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("ntdll.dll")] - private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, ref UNICODE_STRING_WOW64 lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); - - [DllImport("ntdll.dll")] - private static extern int NtWow64ReadVirtualMemory64(IntPtr hProcess, long lpBaseAddress, [MarshalAs(UnmanagedType.LPWStr)] string lpBuffer, long dwSize, IntPtr lpNumberOfBytesRead); - - - - // ref: http://www.microsoft.com/whdc/system/Sysinternals/MoreThan64proc.mspx - public enum PROCESSINFOCLASS : int - { - ProcessBasicInformation = 0, // 0, q: PROCESS_BASIC_INFORMATION, PROCESS_EXTENDED_BASIC_INFORMATION - ProcessWow64Information = 26, // q: ULONG_PTR - } - - [Flags] - public enum PEB_OFFSET - { - CurrentDirectory = 0, - DllPath, - ImagePathName, - CommandLine, - WindowTitle, - DesktopInfo, - ShellInfo, - RuntimeData, - TypeMask = 0xffff, - Wow64 = 0x10000, - }; - - public class Is64BitChecker - { - [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool wow64Process); - - public static bool GetProcessIsWow64(IntPtr hProcess) - { - if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6) - { - bool retVal; - if (!IsWow64Process(hProcess, out retVal)) - { - return false; - } - return retVal; - } - else - { - return false; - } - } - - public static bool InternalCheckIsWow64() - { - if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6) - { - using (Process p = Process.GetCurrentProcess()) - { - bool retVal; - if (!IsWow64Process(p.Handle, out retVal)) - { - return false; - } - return retVal; - } - } - else - { - return false; - } - } - } - -} \ No newline at end of file diff --git a/PrivateWin10/API/RegistryMonitor.cs b/PrivateWin10/API/RegistryMonitor.cs deleted file mode 100644 index 3fd87ca..0000000 --- a/PrivateWin10/API/RegistryMonitor.cs +++ /dev/null @@ -1,392 +0,0 @@ -using System; -using System.ComponentModel; -using System.IO; -using System.Threading; -using System.Runtime.InteropServices; -using Microsoft.Win32; - - -/// -/// RegistryMonitor allows you to monitor specific registry key. -/// -/// -/// If a monitored registry key changes, an event is fired. You can subscribe to these -/// events by adding a delegate to . -/// The Windows API provides a function -/// -/// RegNotifyChangeKeyValue, which is not covered by the -/// class. imports -/// that function and encapsulates it in a convenient manner. -/// -/// -/// -/// This sample shows how to monitor HKEY_CURRENT_USER\Environment for changes: -/// -/// public class MonitorSample -/// { -/// static void Main() -/// { -/// RegistryMonitor monitor = new RegistryMonitor(RegistryHive.CurrentUser, "Environment"); -/// monitor.RegChanged += new EventHandler(OnRegChanged); -/// monitor.Start(); -/// -/// while(true); -/// -/// monitor.Stop(); -/// } -/// -/// private void OnRegChanged(object sender, EventArgs e) -/// { -/// Console.WriteLine("registry key has changed"); -/// } -/// } -/// -/// -public class RegistryMonitor : IDisposable -{ - #region P/Invoke - - [DllImport("advapi32.dll", SetLastError = true)] - private static extern int RegOpenKeyEx(IntPtr hKey, string subKey, uint options, int samDesired, - out IntPtr phkResult); - - [DllImport("advapi32.dll", SetLastError = true)] - private static extern int RegNotifyChangeKeyValue(IntPtr hKey, bool bWatchSubtree, - RegChangeNotifyFilters dwNotifyFilter, IntPtr hEvent, - bool fAsynchronous); - - [DllImport("advapi32.dll", SetLastError = true)] - private static extern int RegCloseKey(IntPtr hKey); - - private const int KEY_QUERY_VALUE = 0x0001; - private const int KEY_NOTIFY = 0x0010; - private const int STANDARD_RIGHTS_READ = 0x00020000; - - private static readonly IntPtr HKEY_CLASSES_ROOT = new IntPtr(unchecked((int) 0x80000000)); - private static readonly IntPtr HKEY_CURRENT_USER = new IntPtr(unchecked((int) 0x80000001)); - private static readonly IntPtr HKEY_LOCAL_MACHINE = new IntPtr(unchecked((int) 0x80000002)); - private static readonly IntPtr HKEY_USERS = new IntPtr(unchecked((int) 0x80000003)); - private static readonly IntPtr HKEY_PERFORMANCE_DATA = new IntPtr(unchecked((int) 0x80000004)); - private static readonly IntPtr HKEY_CURRENT_CONFIG = new IntPtr(unchecked((int) 0x80000005)); - private static readonly IntPtr HKEY_DYN_DATA = new IntPtr(unchecked((int) 0x80000006)); - - /// - /// Filter for notifications reported by . - /// - [Flags] - public enum RegChangeNotifyFilters - { - /// Notify the caller if a subkey is added or deleted. - Key = 1, - /// Notify the caller of changes to the attributes of the key, - /// such as the security descriptor information. - Attribute = 2, - /// Notify the caller of changes to a value of the key. This can - /// include adding or deleting a value, or changing an existing value. - Value = 4, - /// Notify the caller of changes to the security descriptor - /// of the key. - Security = 8, - } - - #endregion - - #region Event handling - - /// - /// Occurs when the specified registry key has changed. - /// - public event EventHandler RegChanged; - - /// - /// Raises the event. - /// - /// - ///

- /// OnRegChanged is called when the specified registry key has changed. - ///

- /// - /// When overriding in a derived class, be sure to call - /// the base class's method. - /// - ///
- protected virtual void OnRegChanged() - { - EventHandler handler = RegChanged; - if (handler != null) - handler(this, null); - } - - /// - /// Occurs when the access to the registry fails. - /// - public event ErrorEventHandler Error; - - /// - /// Raises the event. - /// - /// The which occured while watching the registry. - /// - ///

- /// OnError is called when an exception occurs while watching the registry. - ///

- /// - /// When overriding in a derived class, be sure to call - /// the base class's method. - /// - ///
- protected virtual void OnError(Exception e) - { - ErrorEventHandler handler = Error; - if (handler != null) - handler(this, new ErrorEventArgs(e)); - } - - #endregion - - #region Private member variables - - private IntPtr _registryHive; - private string _registrySubName; - private object _threadLock = new object(); - private Thread _thread; - private bool _disposed = false; - private ManualResetEvent _eventTerminate = new ManualResetEvent(false); - - private RegChangeNotifyFilters _regFilter = RegChangeNotifyFilters.Key | RegChangeNotifyFilters.Attribute | - RegChangeNotifyFilters.Value | RegChangeNotifyFilters.Security; - - #endregion - - /// - /// Initializes a new instance of the class. - /// - /// The registry key to monitor. - public RegistryMonitor(RegistryKey registryKey) - { - InitRegistryKey(registryKey.Name); - } - - /// - /// Initializes a new instance of the class. - /// - /// The name. - public RegistryMonitor(string name) - { - if (name == null || name.Length == 0) - throw new ArgumentNullException("name"); - - InitRegistryKey(name); - } - - /// - /// Initializes a new instance of the class. - /// - /// The registry hive. - /// The sub key. - public RegistryMonitor(RegistryHive registryHive, string subKey) - { - InitRegistryKey(registryHive, subKey); - } - - /// - /// Disposes this object. - /// - public void Dispose() - { - Stop(); - _disposed = true; - GC.SuppressFinalize(this); - } - - /// - /// Gets or sets the RegChangeNotifyFilter. - /// - public RegChangeNotifyFilters RegChangeNotifyFilter - { - get { return _regFilter; } - set - { - lock (_threadLock) - { - if (IsMonitoring) - throw new InvalidOperationException("Monitoring thread is already running"); - - _regFilter = value; - } - } - } - - #region Initialization - - private void InitRegistryKey(RegistryHive hive, string name) - { - switch (hive) - { - case RegistryHive.ClassesRoot: - _registryHive = HKEY_CLASSES_ROOT; - break; - - case RegistryHive.CurrentConfig: - _registryHive = HKEY_CURRENT_CONFIG; - break; - - case RegistryHive.CurrentUser: - _registryHive = HKEY_CURRENT_USER; - break; - - case RegistryHive.DynData: - _registryHive = HKEY_DYN_DATA; - break; - - case RegistryHive.LocalMachine: - _registryHive = HKEY_LOCAL_MACHINE; - break; - - case RegistryHive.PerformanceData: - _registryHive = HKEY_PERFORMANCE_DATA; - break; - - case RegistryHive.Users: - _registryHive = HKEY_USERS; - break; - - default: - throw new InvalidEnumArgumentException("hive", (int)hive, typeof (RegistryHive)); - } - _registrySubName = name; - } - - private void InitRegistryKey(string name) - { - string[] nameParts = name.Split('\\'); - - switch (nameParts[0]) - { - case "HKEY_CLASSES_ROOT": - case "HKCR": - _registryHive = HKEY_CLASSES_ROOT; - break; - - case "HKEY_CURRENT_USER": - case "HKCU": - _registryHive = HKEY_CURRENT_USER; - break; - - case "HKEY_LOCAL_MACHINE": - case "HKLM": - _registryHive = HKEY_LOCAL_MACHINE; - break; - - case "HKEY_USERS": - _registryHive = HKEY_USERS; - break; - - case "HKEY_CURRENT_CONFIG": - _registryHive = HKEY_CURRENT_CONFIG; - break; - - default: - _registryHive = IntPtr.Zero; - throw new ArgumentException("The registry hive '" + nameParts[0] + "' is not supported", "value"); - } - - _registrySubName = String.Join("\\", nameParts, 1, nameParts.Length - 1); - } - - #endregion - - /// - /// true if this object is currently monitoring; - /// otherwise, false. - /// - public bool IsMonitoring - { - get { return _thread != null; } - } - - /// - /// Start monitoring. - /// - public void Start() - { - if (_disposed) - throw new ObjectDisposedException(null, "This instance is already disposed"); - - lock (_threadLock) - { - if (!IsMonitoring) - { - _eventTerminate.Reset(); - _thread = new Thread(new ThreadStart(MonitorThread)); - _thread.IsBackground = true; - _thread.Start(); - } - } - } - - /// - /// Stops the monitoring thread. - /// - public void Stop() - { - if (_disposed) - throw new ObjectDisposedException(null, "This instance is already disposed"); - - lock (_threadLock) - { - Thread thread = _thread; - if (thread != null) - { - _eventTerminate.Set(); - thread.Join(); - } - } - } - - private void MonitorThread() - { - try - { - ThreadLoop(); - } - catch (Exception e) - { - OnError(e); - } - _thread = null; - } - - private void ThreadLoop() - { - IntPtr registryKey; - int result = RegOpenKeyEx(_registryHive, _registrySubName, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_NOTIFY, - out registryKey); - if (result != 0) - throw new Win32Exception(result); - - try - { - AutoResetEvent _eventNotify = new AutoResetEvent(false); - WaitHandle[] waitHandles = new WaitHandle[] {_eventNotify, _eventTerminate}; - while (!_eventTerminate.WaitOne(0, true)) - { - result = RegNotifyChangeKeyValue(registryKey, true, _regFilter, _eventNotify.Handle, true); - if (result != 0) - throw new Win32Exception(result); - - if (WaitHandle.WaitAny(waitHandles) == 0) - { - OnRegChanged(); - } - } - } - finally - { - if (registryKey != IntPtr.Zero) - { - RegCloseKey(registryKey); - } - } - } -} \ No newline at end of file diff --git a/PrivateWin10/API/ServiceHelper.cs b/PrivateWin10/API/ServiceHelper.cs deleted file mode 100644 index f7e6c24..0000000 --- a/PrivateWin10/API/ServiceHelper.cs +++ /dev/null @@ -1,652 +0,0 @@ -using Microsoft.Win32; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.ServiceProcess; -using System.Text; -using System.Threading; -using System.Threading.Tasks; - - -public static class ServiceHelper -{ - private const int STANDARD_RIGHTS_REQUIRED = 0xF0000; - private const int SERVICE_WIN32_OWN_PROCESS = 0x00000010; - private const int ERROR_INSUFFICIENT_BUFFER = 0x0000007a; - private const uint SERVICE_NO_CHANGE = 0xFFFFFFFF; - private const int SC_STATUS_PROCESS_INFO = 0; - - - - #region OpenSCManager - [DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true)] - static extern IntPtr OpenSCManager(string machineName, string databaseName, ScmAccessRights dwDesiredAccess); - #endregion - - #region OpenService - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] - static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, ServiceAccessRights dwDesiredAccess); - #endregion - - #region CreateService - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] - private static extern IntPtr CreateService(IntPtr hSCManager, string lpServiceName, string lpDisplayName, ServiceAccessRights dwDesiredAccess, int dwServiceType, ServiceBootFlag dwStartType, ServiceError dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lp, string lpPassword); - #endregion - - #region CloseServiceHandle - [DllImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - static extern bool CloseServiceHandle(IntPtr hSCObject); - #endregion - - #region QueryServiceConfig - [StructLayout(LayoutKind.Sequential)] - public class ServiceConfigInfo - { - [MarshalAs(UnmanagedType.U4)] - public UInt32 ServiceType; - [MarshalAs(UnmanagedType.U4)] - public UInt32 StartType; - [MarshalAs(UnmanagedType.U4)] - public UInt32 ErrorControl; - [MarshalAs(UnmanagedType.LPWStr)] - public String BinaryPathName; - [MarshalAs(UnmanagedType.LPWStr)] - public String LoadOrderGroup; - [MarshalAs(UnmanagedType.U4)] - public UInt32 TagID; - [MarshalAs(UnmanagedType.LPWStr)] - public String Dependencies; - [MarshalAs(UnmanagedType.LPWStr)] - public String ServiceStartName; - [MarshalAs(UnmanagedType.LPWStr)] - public String DisplayName; - } - - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] - public static extern int QueryServiceConfig(IntPtr service, IntPtr queryServiceConfig, int bufferSize, ref int bytesNeeded); - #endregion - - - #region ChangeServiceConfig - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)] - private static extern bool ChangeServiceConfig(IntPtr hService, UInt32 dwServiceType, ServiceBootFlag dwStartType, UInt32 dwErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, string lpDependencies, string lpServiceStartName, string lpPassword, string lpDisplayName); - #endregion - - #region QueryServiceStatus - [StructLayout(LayoutKind.Sequential)] - public class SERVICE_STATUS - { - public int dwServiceType = 0; - public ServiceState dwCurrentState = 0; - public int dwControlsAccepted = 0; - public int dwWin32ExitCode = 0; - public int dwServiceSpecificExitCode = 0; - public int dwCheckPoint = 0; - public int dwWaitHint = 0; - } - - [DllImport("advapi32.dll", SetLastError = true)] - private static extern int QueryServiceStatus(IntPtr hService, SERVICE_STATUS lpServiceStatus); - #endregion - - #region QueryServiceStatusEx - [StructLayout(LayoutKind.Sequential)] - public sealed class SERVICE_STATUS_PROCESS - { - [MarshalAs(UnmanagedType.U4)] - public uint dwServiceType; - [MarshalAs(UnmanagedType.U4)] - public uint dwCurrentState; - [MarshalAs(UnmanagedType.U4)] - public uint dwControlsAccepted; - [MarshalAs(UnmanagedType.U4)] - public uint dwWin32ExitCode; - [MarshalAs(UnmanagedType.U4)] - public uint dwServiceSpecificExitCode; - [MarshalAs(UnmanagedType.U4)] - public uint dwCheckPoint; - [MarshalAs(UnmanagedType.U4)] - public uint dwWaitHint; - [MarshalAs(UnmanagedType.U4)] - public uint dwProcessId; - [MarshalAs(UnmanagedType.U4)] - public uint dwServiceFlags; - } - - [DllImport("advapi32.dll", SetLastError = true)] - internal static extern bool QueryServiceStatusEx(IntPtr hService, int infoLevel, IntPtr lpBuffer, uint cbBufSize, out uint pcbBytesNeeded); - #endregion - - #region DeleteService - [DllImport("advapi32.dll", SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool DeleteService(IntPtr hService); - #endregion - - #region ControlService - [DllImport("advapi32.dll")] - private static extern int ControlService(IntPtr hService, ServiceControl dwControl, SERVICE_STATUS lpServiceStatus); - #endregion - - #region StartService - [DllImport("advapi32.dll", SetLastError = true)] - private static extern int StartService(IntPtr hService, int dwNumServiceArgs, int lpServiceArgVectors); - #endregion - - public static void Uninstall(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess); - - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess); - if (service == IntPtr.Zero) - throw new ApplicationException("Service not installed."); - - try - { - StopService(service); - - if (!DeleteService(service)) - throw new ApplicationException("Could not delete service " + Marshal.GetLastWin32Error()); - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(scm); - } - } - - public static bool ServiceIsInstalled(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect); - - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus); - - if (service == IntPtr.Zero) - return false; - - CloseServiceHandle(service); - return true; - } - finally - { - CloseServiceHandle(scm); - } - } - - public static void Install(string serviceName, string displayName, string fileName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.AllAccess); - - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.AllAccess); - - if (service == IntPtr.Zero) - service = CreateService(scm, serviceName, displayName, ServiceAccessRights.AllAccess, SERVICE_WIN32_OWN_PROCESS, ServiceBootFlag.AutoStart, ServiceError.Normal, fileName, null, IntPtr.Zero, null, null, null); - - if (service == IntPtr.Zero) - throw new ApplicationException("Failed to install service."); - - CloseServiceHandle(service); - } - finally - { - CloseServiceHandle(scm); - } - } - - public static void ChangeStartMode(string serviceName, ServiceBootFlag mode) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect | ScmAccessRights.EnumerateService); - - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryConfig | ServiceAccessRights.ChangeConfig); - if (service == IntPtr.Zero) - throw new ApplicationException("Could not open service."); - - try - { - if (!ChangeServiceConfig(service, SERVICE_NO_CHANGE, mode, SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, null, null, null)) - throw new ApplicationException("Could not configure service " + Marshal.GetLastWin32Error()); - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(scm); - } - } - - public static ServiceConfigInfo GetServiceInfo(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect | ScmAccessRights.EnumerateService); - - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryConfig | ServiceAccessRights.ChangeConfig); - if (service == IntPtr.Zero) - throw new ApplicationException("Could not open service."); - - try - { - int bytesNeeded = 0; - if (QueryServiceConfig(service, IntPtr.Zero, 0, ref bytesNeeded) == 0 && bytesNeeded == 0) - throw new ApplicationException("Could not query service configuration" + Marshal.GetLastWin32Error()); - - IntPtr qscPtr = Marshal.AllocCoTaskMem(bytesNeeded); - try - { - if (QueryServiceConfig(service, qscPtr, bytesNeeded, ref bytesNeeded) == 0) - throw new ApplicationException("Could not query service configuration" + Marshal.GetLastWin32Error()); - - return (ServiceConfigInfo)Marshal.PtrToStructure(qscPtr, typeof(ServiceConfigInfo)); - } - finally - { - Marshal.FreeCoTaskMem(qscPtr); - } - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(scm); - } - } - - public static ServiceConfigInfo GetServiceInfoSafe(string serviceName) - { - try - { - return GetServiceInfo(serviceName); - } - catch - { - return null; - } - } - - public static bool StartService(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect); - - bool ret = false; - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus | ServiceAccessRights.Start); - if (service == IntPtr.Zero) - throw new ApplicationException("Could not open service."); - - try - { - StartService(service); - ret = true; - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(scm); - } - return ret; - } - - public static bool StopService(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect); - - bool ret = false; - try - { - IntPtr service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus | ServiceAccessRights.Stop); - if (service == IntPtr.Zero) - throw new ApplicationException("Could not open service."); - - try - { - StopService(service); - ret = true; - } - finally - { - CloseServiceHandle(service); - } - } - finally - { - CloseServiceHandle(scm); - } - return ret; - } - - private static void StartService(IntPtr service) - { - SERVICE_STATUS status = new SERVICE_STATUS(); - StartService(service, 0, 0); - var changedStatus = WaitForServiceStatus(service, ServiceState.StartPending, ServiceState.Running); - if (!changedStatus) - throw new ApplicationException("Unable to start service"); - } - - private static void StopService(IntPtr service) - { - SERVICE_STATUS status = new SERVICE_STATUS(); - ControlService(service, ServiceControl.Stop, status); - var changedStatus = WaitForServiceStatus(service, ServiceState.StopPending, ServiceState.Stopped); - if (!changedStatus) - throw new ApplicationException("Unable to stop service"); - } - - public static ServiceState GetServiceState(string serviceName) - { - SERVICE_STATUS_PROCESS ssp = GetServiceStatus(serviceName); - if(ssp == null) - return ServiceState.NotFound; - return (ServiceState)ssp.dwCurrentState; - } - - public static SERVICE_STATUS_PROCESS GetServiceStatus(string serviceName) - { - IntPtr scm = OpenSCManager(ScmAccessRights.Connect); - IntPtr zero = IntPtr.Zero; - IntPtr service = IntPtr.Zero; - - try - { - service = OpenService(scm, serviceName, ServiceAccessRights.QueryStatus); - - UInt32 dwBytesAlloc = 0; - UInt32 dwBytesNeeded = 36; - do - { - dwBytesAlloc = dwBytesNeeded; - // Allocate required buffer and call again. - zero = Marshal.AllocHGlobal((int)dwBytesAlloc); - if (QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO, zero, dwBytesAlloc, out dwBytesNeeded)) - { - var ssp = new SERVICE_STATUS_PROCESS(); - Marshal.PtrToStructure(zero, ssp); - return ssp; - } - // retry with new size info - } while (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER && dwBytesAlloc < dwBytesNeeded); - } - finally - { - if (zero != IntPtr.Zero) - Marshal.FreeHGlobal(zero); - if(service != IntPtr.Zero) - CloseServiceHandle(service); - CloseServiceHandle(scm); - } - return null; - } - - private static bool WaitForServiceStatus(IntPtr service, ServiceState waitStatus, ServiceState desiredStatus) - { - SERVICE_STATUS status = new SERVICE_STATUS(); - - QueryServiceStatus(service, status); - if (status.dwCurrentState == desiredStatus) return true; - - int dwStartTickCount = Environment.TickCount; - int dwOldCheckPoint = status.dwCheckPoint; - - while (status.dwCurrentState == waitStatus) - { - // Do not wait longer than the wait hint. A good interval is - // one tenth the wait hint, but no less than 1 second and no - // more than 10 seconds. - - int dwWaitTime = status.dwWaitHint / 10; - - if (dwWaitTime < 1000) dwWaitTime = 1000; - else if (dwWaitTime > 10000) dwWaitTime = 10000; - - Thread.Sleep(dwWaitTime); - - // Check the status again. - - if (QueryServiceStatus(service, status) == 0) break; - - if (status.dwCheckPoint > dwOldCheckPoint) - { - // The service is making progress. - dwStartTickCount = Environment.TickCount; - dwOldCheckPoint = status.dwCheckPoint; - } - else - { - if (Environment.TickCount - dwStartTickCount > status.dwWaitHint) - { - // No progress made within the wait hint - break; - } - } - } - return (status.dwCurrentState == desiredStatus); - } - - private static IntPtr OpenSCManager(ScmAccessRights rights) - { - IntPtr scm = OpenSCManager(null, null, rights); - if (scm == IntPtr.Zero) - throw new ApplicationException("Could not connect to service control manager."); - - return scm; - } - - - public enum ServiceState - { - Unknown = -1, // The state cannot be (has not been) retrieved. - NotFound = 0, // The service is not known on the host server. - Stopped = 1, - StartPending = 2, - StopPending = 3, - Running = 4, - ContinuePending = 5, - PausePending = 6, - Paused = 7 - } - - [Flags] - public enum ScmAccessRights - { - Connect = 0x0001, - CreateService = 0x0002, - EnumerateService = 0x0004, - Lock = 0x0008, - QueryLockStatus = 0x0010, - ModifyBootConfig = 0x0020, - StandardRightsRequired = 0xF0000, - AllAccess = (StandardRightsRequired | Connect | CreateService | - EnumerateService | Lock | QueryLockStatus | ModifyBootConfig) - } - - [Flags] - public enum ServiceAccessRights - { - QueryConfig = 0x1, - ChangeConfig = 0x2, - QueryStatus = 0x4, - EnumerateDependants = 0x8, - Start = 0x10, - Stop = 0x20, - PauseContinue = 0x40, - Interrogate = 0x80, - UserDefinedControl = 0x100, - Delete = 0x00010000, - StandardRightsRequired = 0xF0000, - AllAccess = (StandardRightsRequired | QueryConfig | ChangeConfig | - QueryStatus | EnumerateDependants | Start | Stop | PauseContinue | - Interrogate | UserDefinedControl) - } - - public enum ServiceBootFlag - { - Start = 0x00000000, - SystemStart = 0x00000001, - AutoStart = 0x00000002, - DemandStart = 0x00000003, - Disabled = 0x00000004 - } - - public enum ServiceControl - { - Stop = 0x00000001, - Pause = 0x00000002, - Continue = 0x00000003, - Interrogate = 0x00000004, - Shutdown = 0x00000005, - ParamChange = 0x00000006, - NetBindAdd = 0x00000007, - NetBindRemove = 0x00000008, - NetBindEnable = 0x00000009, - NetBindDisable = 0x0000000A - } - - public enum ServiceError - { - Ignore = 0x00000000, - Normal = 0x00000001, - Severe = 0x00000002, - Critical = 0x00000003 - } - - private static string GetServiceImagePath(string serviceName) - { - RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\services\" + serviceName); - if (regkey.GetValue("ImagePath") == null) - return null; - return regkey.GetValue("ImagePath").ToString(); - } - - public class ServiceInfo{ - public ServiceInfo(ServiceController sc) - { - ServiceName = sc.ServiceName; - var ImagePath = GetServiceImagePath(sc.ServiceName); - ServicePath = ImagePath != null ? ProcFunc.GetPathFromCmdLine(ImagePath) : ""; - DisplayName = sc.DisplayName; - if (sc.Status == ServiceControllerStatus.Stopped) - LastKnownPID = -1; - else - { - var ssp = GetServiceStatus(sc.ServiceName); - LastKnownPID = ssp != null ? (int)ssp.dwProcessId : -1; - } - } - public string ServiceName; - public string ServicePath; - public string DisplayName; - public int LastKnownPID; - } - - private static DateTime ServiceCacheTime = DateTime.MinValue; - private static MultiValueDictionary ServiceCacheByPID = new MultiValueDictionary(); - private static Dictionary ServiceCache = new Dictionary(); - private static ReaderWriterLockSlim ServiceCacheLock = new ReaderWriterLockSlim(); - - - private static void RefreshServices() - { - ServiceCacheLock.EnterWriteLock(); - ServiceCacheTime = DateTime.Now; - ServiceCache.Clear(); - ServiceCacheByPID.Clear(); - foreach (ServiceController sc in ServiceController.GetServices()) - { - ServiceInfo info = new ServiceInfo(sc); - if(!ServiceCache.ContainsKey(sc.ServiceName)) // should not happen but in case - ServiceCache.Add(sc.ServiceName, info); - if (info.LastKnownPID != -1) - ServiceCacheByPID.Add(info.LastKnownPID, info); - } - // this takes roughly 30 ms - ServiceCacheLock.ExitWriteLock(); - } - - public static List GetServicesByPID(int pid) - { - bool doUpdate = false; - if (pid == -1) // -1 means get all and we always want a fresh list - doUpdate = true; - else - { - ServiceCacheLock.EnterReadLock(); - doUpdate = ServiceCacheTime <= DateTime.FromFileTimeUtc(ProcFunc.GetProcessCreationTime(pid)).ToLocalTime(); - ServiceCacheLock.ExitReadLock(); - } - if (doUpdate) - RefreshServices(); - - ServiceCacheLock.EnterReadLock(); - CloneableList values; - if (pid == -1) - values = ServiceCacheByPID.GetAllValues(); - else if (!ServiceCacheByPID.TryGetValue(pid, out values)) - values = null; - ServiceCacheLock.ExitReadLock(); - return (values != null && values.Count == 0) ? null : values; - } - - public static List GetAllServices() - { - ServiceCacheLock.EnterReadLock(); - bool doUpdate = ServiceCacheTime < DateTime.Now.AddSeconds(-30); - ServiceCacheLock.ExitReadLock(); - if (doUpdate) - RefreshServices(); - - ServiceCacheLock.EnterReadLock(); - List list = ServiceCache.Values.ToList(); - ServiceCacheLock.ExitReadLock(); - return list; - } - - public static string GetServiceName(string name) - { - if (name == null || name.Length == 0) - return ""; - - ServiceCacheLock.EnterReadLock(); - ServiceInfo info = null; - bool found = ServiceCache.TryGetValue(name, out info); - ServiceCacheLock.ExitReadLock(); - - if (!found) - { - ServiceController sc = new ServiceController(name); - try { info = new ServiceInfo(sc); } catch { } - sc.Close(); - - ServiceCacheLock.EnterWriteLock(); - if (info != null && !ServiceCache.ContainsKey(sc.ServiceName)) // should not happen but in case - ServiceCache.Add(sc.ServiceName, info); - ServiceCacheLock.ExitWriteLock(); - } - - return info == null ? "" : info.DisplayName; - } -} \ No newline at end of file diff --git a/PrivateWin10/API/TokenManipulator.cs b/PrivateWin10/API/TokenManipulator.cs deleted file mode 100644 index 8e02e13..0000000 --- a/PrivateWin10/API/TokenManipulator.cs +++ /dev/null @@ -1,121 +0,0 @@ -using System; -using System.Runtime.InteropServices; - - -public class TokenManipulator -{ - - - [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] - internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, - ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen); - - - [DllImport("kernel32.dll", ExactSpelling = true)] - internal static extern IntPtr GetCurrentProcess(); - - - [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)] - internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr - phtok); - - - [DllImport("advapi32.dll", SetLastError = true)] - internal static extern bool LookupPrivilegeValue(string host, string name, - ref long pluid); - - - [StructLayout(LayoutKind.Sequential, Pack = 1)] - internal struct TokPriv1Luid - { - public int Count; - public long Luid; - public int Attr; - } - - internal const int SE_PRIVILEGE_DISABLED = 0x00000000; - internal const int SE_PRIVILEGE_ENABLED = 0x00000002; - internal const int TOKEN_QUERY = 0x00000008; - internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; - - public const string SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"; - public const string SE_AUDIT_NAME = "SeAuditPrivilege"; - public const string SE_BACKUP_NAME = "SeBackupPrivilege"; - public const string SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"; - public const string SE_CREATE_GLOBAL_NAME = "SeCreateGlobalPrivilege"; - public const string SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"; - public const string SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"; - public const string SE_CREATE_SYMBOLIC_LINK_NAME = "SeCreateSymbolicLinkPrivilege"; - public const string SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"; - public const string SE_DEBUG_NAME = "SeDebugPrivilege"; - public const string SE_ENABLE_DELEGATION_NAME = "SeEnableDelegationPrivilege"; - public const string SE_IMPERSONATE_NAME = "SeImpersonatePrivilege"; - public const string SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"; - public const string SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"; - public const string SE_INC_WORKING_SET_NAME = "SeIncreaseWorkingSetPrivilege"; - public const string SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"; - public const string SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"; - public const string SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"; - public const string SE_MANAGE_VOLUME_NAME = "SeManageVolumePrivilege"; - public const string SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"; - public const string SE_RELABEL_NAME = "SeRelabelPrivilege"; - public const string SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"; - public const string SE_RESTORE_NAME = "SeRestorePrivilege"; - public const string SE_SECURITY_NAME = "SeSecurityPrivilege"; - public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; - public const string SE_SYNC_AGENT_NAME = "SeSyncAgentPrivilege"; - public const string SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"; - public const string SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"; - public const string SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"; - public const string SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"; - public const string SE_TCB_NAME = "SeTcbPrivilege"; - public const string SE_TIME_ZONE_NAME = "SeTimeZonePrivilege"; - public const string SE_TRUSTED_CREDMAN_ACCESS_NAME = "SeTrustedCredManAccessPrivilege"; - public const string SE_UNDOCK_NAME = "SeUndockPrivilege"; - public const string SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"; - - public static bool AddPrivilege(string privilege) - { - try - { - bool retVal; - TokPriv1Luid tp; - IntPtr hproc = GetCurrentProcess(); - IntPtr htok = IntPtr.Zero; - retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); - tp.Count = 1; - tp.Luid = 0; - tp.Attr = SE_PRIVILEGE_ENABLED; - retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid); - retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); - return retVal; - } - catch (Exception ex) - { - throw ex; - } - - } - public static bool RemovePrivilege(string privilege) - { - try - { - bool retVal; - TokPriv1Luid tp; - IntPtr hproc = GetCurrentProcess(); - IntPtr htok = IntPtr.Zero; - retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok); - tp.Count = 1; - tp.Luid = 0; - tp.Attr = SE_PRIVILEGE_DISABLED; - retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid); - retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); - return retVal; - } - catch (Exception ex) - { - throw ex; - } - - } -} diff --git a/PrivateWin10/API/UwpFunc.cs b/PrivateWin10/API/UwpFunc.cs deleted file mode 100644 index e3a2f6b..0000000 --- a/PrivateWin10/API/UwpFunc.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Input; -using PrivateWin10.Controls; - -public class UwpFunc -{ - - const long APPMODEL_ERROR_NO_PACKAGE = 15700L; - - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); - - static public bool IsRunningAsUwp() - { - if (IsWindows7OrLower) - { - return false; - } - else - { - int length = 0; - StringBuilder sb = new StringBuilder(0); - int result = GetCurrentPackageFullName(ref length, sb); - - sb = new StringBuilder(length); - result = GetCurrentPackageFullName(ref length, sb); - - return result != APPMODEL_ERROR_NO_PACKAGE; - } - } - - /* - +------------------------------------------------------------------------------+ - | | PlatformID | Major version | Minor version | - +------------------------------------------------------------------------------+ - | Windows 95 | Win32Windows | 4 | 0 | - | Windows 98 | Win32Windows | 4 | 10 | - | Windows Me | Win32Windows | 4 | 90 | - | Windows NT 4.0 | Win32NT | 4 | 0 | - | Windows 2000 | Win32NT | 5 | 0 | - | Windows XP | Win32NT | 5 | 1 | - | Windows 2003 | Win32NT | 5 | 2 | - | Windows Vista | Win32NT | 6 | 0 | - | Windows 2008 | Win32NT | 6 | 0 | - | Windows 7 | Win32NT | 6 | 1 | - | Windows 2008 R2 | Win32NT | 6 | 1 | - | Windows 8 | Win32NT | 6 | 2 | - | Windows 8.1 | Win32NT | 6 | 3 | - +------------------------------------------------------------------------------+ - | Windows 10 | Win32NT | 10 | 0 | - +------------------------------------------------------------------------------+ - */ - - static public bool IsWindows7OrLower - { - get - { - int versionMajor = Environment.OSVersion.Version.Major; - int versionMinor = Environment.OSVersion.Version.Minor; - double version = versionMajor + (double)versionMinor / 10; - return version <= 6.1; - } - } - - static public bool IsWindows8 - { - get - { - int versionMajor = Environment.OSVersion.Version.Major; - int versionMinor = Environment.OSVersion.Version.Minor; - double version = versionMajor + (double)versionMinor / 10; - return version == 6.2 || version == 6.3; - } - } - - [Serializable()] - public class AppInfo - { - public string Name; - public string Logo; - public string ID; - public string SID; - } - - internal static void AddBinding(System.Windows.Controls.Control ctrl, KeyGesture keyGesture, ExecutedRoutedEventHandler executed) - { - RoutedCommand cmd = new RoutedCommand(); - cmd.InputGestures.Add(keyGesture); - ctrl.CommandBindings.Add(new CommandBinding(cmd, executed)); - } -} diff --git a/PrivateWin10/App.xaml.cs b/PrivateWin10/App.xaml.cs index f4a5c54..6e6a57f 100644 --- a/PrivateWin10/App.xaml.cs +++ b/PrivateWin10/App.xaml.cs @@ -16,6 +16,10 @@ using System.Threading; using System.Threading.Tasks; using System.Windows; +using MiscHelpers; +using System.Drawing; +using TweakEngine; +using PrivateAPI; namespace PrivateWin10 { @@ -38,57 +42,18 @@ public partial class App : Application public static string appPath = ""; public static string dataPath = ""; public static bool isPortable = false; - public static int Session = 0; public static AppLog Log = null; - public static Priv10Engine engine = null; + public static PresetManager presets = null; public static TweakManager tweaks = null; public static TrayIcon TrayIcon = null; public static MainWindow MainWnd = null; public static Priv10Client client = null; - public static Priv10Host host = null; - enum StartModes - { - Normal = 0, - Service, - Engine - } - - public enum EventIDs : long - { - Undefined = 0x0000, - - // generic - Exception, - AppError, - AppWarning, - AppInfo, - - TweakBegin = 0x0100, - TweakChanged, - TweakFixed, - TweakError, - TweakEnd = 0x01FF, - - FirewallBegin = 0x0200, - RuleChanged, - RuleDeleted, - RuleAdded, - //FirewallNewProg - FirewallEnd = 0x02FF, - } - - public enum EventFlags : short - { - DebugEvents = 0x0100, - AppLogEntries = 0x0200, - Notifications = 0x0400, // Show a Notification - PopUpMessages = 0x0800, // Show a PopUp Message - } + public static Process EngineProc = null; [STAThread] public static void Main(string[] args) @@ -103,24 +68,12 @@ public static void Main(string[] args) if (TestArg("-dbg_log")) AppDomain.CurrentDomain.FirstChanceException += FirstChanceExceptionHandler; - StartModes startMode = StartModes.Normal; // Normal GUI Mode - if (TestArg("-svc")) - startMode = StartModes.Service; - else if (TestArg("-engine")) - startMode = StartModes.Engine; - Log = new AppLog(Key); - AppLog.ExceptionLogID = (long)EventIDs.Exception; - AppLog.ExceptionCategory = (short)EventFlags.DebugEvents; + AppLog.ExceptionLogID = (long)Priv10Logger.EventIDs.Exception; + AppLog.ExceptionCategory = (short)Priv10Logger.EventFlags.DebugEvents; - if (startMode == StartModes.Normal) - { - Log.EnableLogging(); - Log.LoadLog(); - } - // When running as worker we need the windows event log - else if (!Log.UsingEventLog()) - Log.SetupEventLog(Key); + Log.EnableLogging(); + Log.LoadLog(); // load current version exePath = Process.GetCurrentProcess().MainModule.FileName; //System.Reflection.Assembly.GetExecutingAssembly().Location; @@ -161,101 +114,60 @@ public static void Main(string[] args) if(AdminFunc.IsAdministrator()) FileOps.SetAnyDirSec(dataPath); - App.LogInfo("PrivateWin10 Process Started, Mode {0}.", startMode.ToString()); + // setup custom assembly resolution for x86/x64 synamic compatybility + //AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler; - Session = Process.GetCurrentProcess().SessionId; + Thread.CurrentThread.Name = "Gui"; - // setup custom assembly resolution for x86/x64 synamic compatybility - AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolveHandler; + client = new Priv10Client(); - // is the process starting as a service/worker? - if (startMode != StartModes.Normal) + if (!AdminFunc.IsAdministrator()) { - engine = new Priv10Engine(); - if (startMode == StartModes.Service) + if (AdminFunc.SkipUacRun(App.Key, App.args)) + return; + + if (App.GetConfigInt("Startup", "ShowSetup", 1) == 1) { - using (Priv10Service svc = new Priv10Service()) - ServiceBase.Run(svc); + AppLog.Debug("Trying to restart as admin..."); + if (Restart(true)) + return; } - else - engine.Run(); - return; } - Thread.CurrentThread.Name = "Gui"; - - client = new Priv10Client(); - - // Encure wie have the required privilegs - //if (!AdminFunc.IsDebugging()) + AppLog.Debug("Trying to connect to Engine..."); + int conRes = client.Connect(1000); + if (conRes == 0) { - AppLog.Debug("Trying to connect to Engine..."); - int conRes = client.Connect(1000); - if (conRes == 0) + if (Priv10Service.IsInstalled()) { if (!AdminFunc.IsAdministrator()) { - AppLog.Debug("Trying to obtain Administrative proivilegs..."); - if (AdminFunc.SkipUacRun(App.Key, App.args)) - return; - - AppLog.Debug("Trying to start with 'runas'..."); - // Restart program and run as admin - string arguments = "\"" + string.Join("\" \"", args) + "\""; - ProcessStartInfo startInfo = new ProcessStartInfo(exePath, arguments); - startInfo.UseShellExecute = true; - startInfo.Verb = "runas"; - try - { - Process.Start(startInfo); - return; // we restarted as admin - } - catch - { - //MessageBox.Show(Translate.fmt("msg_admin_rights", mName), mName); - //return; // no point in cintinuing without admin rights or an already running engine - } + MessageBox.Show(Translate.fmt("msg_admin_rights_svc", Title, SvcName), Title); + Restart(true); + return; } - else if (Priv10Service.IsInstalled()) - { - AppLog.Debug("Trying to start service..."); - if (Priv10Service.Startup()) - { - AppLog.Debug("Trying to connect to service..."); - if (client.Connect() != 0) - AppLog.Debug("Connected to service..."); - else - AppLog.Debug("Failed to connect to service..."); - } - else - AppLog.Debug("Failed to start service..."); - } + AppLog.Debug("Trying to start service..."); + if (!Priv10Service.Startup()) + AppLog.Debug("Failed to start service..."); + } - else if (conRes == -1) + else if (App.GetConfigInt("Firewall", "Enabled", 0) != 0) { - MessageBox.Show(Translate.fmt("msg_dupliate_session", Title), Title); - return; // no point in cintinuing without admin rights or an already running engine + AppLog.Debug("Trying to start engine process..."); + StartEngine(); } - } - // + + AppLog.Debug("Trying to connect to service..."); + if (client.Connect() != 0) + AppLog.Debug("Connected to service..."); + else + AppLog.Debug("Failed to connect to service..."); + } tweaks = new TweakManager(); - - // if we couldn't connect to the engine start it and connect - if (!client.IsConnected() && AdminFunc.IsAdministrator()) - { - AppLog.Debug("Starting Engine Thread..."); - - engine = new Priv10Engine(); - - engine.Start(); - - AppLog.Debug("... engine started."); - - client.Connect(); - } + presets = new PresetManager(); var app = new App(); app.InitializeComponent(); @@ -265,7 +177,6 @@ public static void Main(string[] args) MainWnd = new MainWindow(); TrayIcon = new TrayIcon(); - TrayIcon.Action += TrayAction; TrayIcon.Visible = (GetConfigInt("Startup", "Tray", 0) != 0) || App.TestArg("-autorun"); if (!App.TestArg("-autorun") || !TrayIcon.Visible) @@ -275,15 +186,47 @@ public static void Main(string[] args) TrayIcon.DestroyNotifyicon(); - client.Close(); - + presets.Store(); tweaks.Store(); - if (engine != null) - engine.Stop(); + if (EngineProc != null) + StopEngine(); + else + client.Close(); } - static private Assembly AssemblyResolveHandler(object sender, ResolveEventArgs args) + public static bool StartEngine() + { + AppLog.Debug("Starting Engine..."); + + //ProcessStartInfo startInfo = new ProcessStartInfo(exePath, "-engine"); + ProcessStartInfo startInfo = new ProcessStartInfo(appPath + "\\PrivateService.exe", "-engine"); + startInfo.UseShellExecute = true; + startInfo.Verb = "runas"; + try + { + EngineProc = Process.Start(startInfo); + + AppLog.Debug("... engine started."); + return true; + } + catch + { + MessageBox.Show(Translate.fmt("msg_engine_fail"), Title); + return false; + } + } + + public static void StopEngine() + { + client.Quit(); + client.Close(); + if (!EngineProc.WaitForExit(30000)) + EngineProc.Kill(); + EngineProc = null; + } + + /*static private Assembly AssemblyResolveHandler(object sender, ResolveEventArgs args) { //This handler is called only when the common language runtime tries to bind to the assembly and fails. @@ -315,7 +258,7 @@ static private Assembly AssemblyResolveHandler(object sender, ResolveEventArgs a //Return the loaded assembly. return MyAssembly; - } + }*/ static private void FirstChanceExceptionHandler(object source, FirstChanceExceptionEventArgs e) { @@ -350,9 +293,9 @@ static public string GetResourceStr(string resourcePath) if (resourcePath.Length > 2 && resourcePath.Substring(0, 2) == "@{") { - if (App.engine != null) - resourceStr = App.engine?.PkgMgr?.GetAppResourceStr(resourcePath) ?? resourcePath; - else + //if (App.engine != null) + // resourceStr = App.engine?.PkgMgr?.GetAppResourceStr(resourcePath) ?? resourcePath; + //else // xxxxx resourceStr = App.client.GetAppPkgRes(resourcePath); } else @@ -368,6 +311,8 @@ static public string GetResourceStr(string resourcePath) static bool ExecuteCommands() { + bool bDone = false; + if (TestArg("-help") || TestArg("/?")) { string Message = "Available command line options\r\n"; @@ -401,8 +346,6 @@ static bool ExecuteCommands() return true; } - - bool bDone = false; if (TestArg("-uninstall")) { @@ -589,54 +532,10 @@ static bool ExecuteCommands() Thread.Sleep(1000); } } - + return bDone; } - static void TrayAction(object sender, TrayIcon.TrayEventArgs args) - { - if (MainWnd == null || !MainWnd.FullyLoaded) - return; - - switch (args.Action) - { - case TrayIcon.Actions.ToggleWindow: - { - if (MainWnd.IsVisible) - MainWnd.Hide(); - else - MainWnd.Show(); - break; - } - case TrayIcon.Actions.ToggleNotify: - { - if (MainWnd.notificationWnd.IsVisible) - MainWnd.notificationWnd.HideWnd(); - else if (!MainWnd.notificationWnd.IsEmpty()) - MainWnd.notificationWnd.ShowWnd(); - break; - } - case TrayIcon.Actions.CloseApplication: - { - if (Priv10Service.IsInstalled() && AdminFunc.IsAdministrator()) - { - MessageBoxResult res = MessageBox.Show(Translate.fmt("msg_stop_svc"), App.Title, MessageBoxButton.YesNoCancel, MessageBoxImage.Question); - switch (res) - { - case MessageBoxResult.Yes: - if(!Priv10Service.Terminate()) - MessageBox.Show(Translate.fmt("msg_stop_svc_err"), App.Title, MessageBoxButton.OK, MessageBoxImage.Stop); - break; - case MessageBoxResult.Cancel: - return; - } - } - Application.Current.Shutdown(); - break; - } - } - } - ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Misc Helpers @@ -729,6 +628,49 @@ public static List IniEnumSections(string INIPath = null) return TextHelpers.SplitStr(new String(chars, 0, size), "\0"); } + + public static void StoreWnd(Window wnd, string name) + { + App.SetConfig("GUI", name + "WndPos", wnd.Left + ":" + wnd.Top); + App.SetConfig("GUI", name + "WndSize", wnd.Width + ":" + wnd.Height); + } + + public static bool LoadWnd(Window wnd, string name) + { + Rectangle rect = new Rectangle(); + + string wndPos = App.GetConfig("GUI", name + "WndPos", null); + if (wndPos == null || wndPos.Length == 0) + return false; + + var LT = TextHelpers.Split2(wndPos, ":"); + rect.X = MiscFunc.parseInt(LT.Item1); + rect.Y = MiscFunc.parseInt(LT.Item2); + + string wndSize = App.GetConfig("GUI", name + "WndSize", null); + if (wndSize != null || wndSize.Length == 0) + { + var WH = TextHelpers.Split2(wndSize, ":"); + rect.Width = MiscFunc.parseInt(WH.Item1); + rect.Height = MiscFunc.parseInt(WH.Item2); + } + else + { + rect.Width = (int)wnd.Width; + rect.Height = (int)wnd.Height; + } + + if (!MiscFunc.IsOnScreen(rect)) + return false; + + wnd.Left = rect.Left; + wnd.Top = rect.Top; + wnd.Width = rect.Width; + wnd.Height = rect.Height; + + return true; + } + ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Start & Restart @@ -750,7 +692,7 @@ public static bool IsAutoStart() return (subKey != null && subKey.GetValue(App.Key) != null); } - public static void Restart(bool RunAs = false/*, bool bService = false*/) + public static bool Restart(bool RunAs = false/*, bool bService = false*/) { /*if (bService && Priv10Service.IsInstalled()) { @@ -767,55 +709,16 @@ public static void Restart(bool RunAs = false/*, bool bService = false*/) { Process.Start(startInfo); Environment.Exit(-1); + return true; } catch { //MessageBox.Show(Translate.fmt("msg_admin_req", mName), mName); - App.LogWarning("Failed to restart Application"); + Priv10Logger.LogWarning("Failed to restart Application"); + return false; } } - ///////////////////////////////////////////////////////////////////////////////////////////////////////// - // Event Logging - - static public void LogCriticalError(string message, params object[] args) - { -#if DEBUG - Debugger.Break(); -#endif - LogError("Critical Error: " + message, args); - } - - static public void LogError(string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Error, (long)EventIDs.AppError, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); - } - - static public void LogError(App.EventIDs eventID, Dictionary Params, App.EventFlags flags, string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Error, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); - } - - static public void LogWarning(string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Warning, (long)EventIDs.AppWarning, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); - } - - static public void LogWarning(App.EventIDs eventID, Dictionary Params, App.EventFlags flags, string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Warning, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); - } - - static public void LogInfo(string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Information, (long)EventIDs.AppInfo, (short)EventFlags.AppLogEntries, args.Length == 0 ? message : string.Format(message, args)); - } - - static public void LogInfo(App.EventIDs eventID, Dictionary Params, App.EventFlags flags, string message, params object[] args) - { - AppLog.Add(EventLogEntryType.Information, (long)eventID, (short)flags, args.Length == 0 ? message : string.Format(message, args), Params); - } - ///////////////////////////////////////////////////////////////////////////////////////////////////////// // Licensing diff --git a/PrivateWin10/Common/AdminFunc.cs b/PrivateWin10/Common/AdminFunc.cs index 8c9f396..f375338 100644 --- a/PrivateWin10/Common/AdminFunc.cs +++ b/PrivateWin10/Common/AdminFunc.cs @@ -1,4 +1,5 @@  +using MiscHelpers; using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/PrivateWin10/Common/AppLog.cs b/PrivateWin10/Common/AppLog.cs deleted file mode 100644 index 808f0f9..0000000 --- a/PrivateWin10/Common/AppLog.cs +++ /dev/null @@ -1,271 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Diagnostics.Eventing.Reader; -using System.Linq; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Threading; - -public class AppLog : IDisposable -{ - static private AppLog mInstance = null; - public static AppLog GetInstance() { return mInstance; } - - private int mLogLimit = 200; - private string mLogName = null; - private static ReaderWriterLockSlim mLocker = new ReaderWriterLockSlim(); - - EventLogWatcher mEventWatcher = null; - - public struct LogEntry - { - public EventLogEntryType entryType; - public short categoryID; - public long eventID; - public string strMessage; - public Dictionary Params; - //public byte[] binData; - public DateTime timeGenerated; - - public void SetData(string[] dataStr) - { - strMessage = dataStr[0]; - if (dataStr.Length < 2) - return; - - var keys = dataStr[1].Split('|'); - if (keys.Length != dataStr.Length - 2) - return; - - Params = new Dictionary(); - for (int i = 2; i < dataStr.Length; i++) - Params.Add(keys[i - 2], dataStr[i]); - } - - public object[] GetData() - { - if (Params == null || Params.Count == 0) - return new string[] { strMessage }; - - string[] dataStr; - dataStr = new string[Params.Count + 2]; - dataStr[0] = strMessage; // first entry is the message text - dataStr[1] = string.Join("|", Params.Keys); // second entry is the list of parameter names that follow - - int i = 2; - foreach (var str in Params.Values) - dataStr[i++] = str; - - return dataStr; - } - } - - private List mLogList = null; - - public class LogEventArgs : EventArgs - { - public LogEntry entry; - - public LogEventArgs(LogEntry entry) - { - this.entry = entry; - } - }; - - public event EventHandler LogEvent; - - public AppLog(string LogName = null) - { - mInstance = this; - if (LogName != null && EventLog.Exists(LogName)) - mLogName = LogName; - } - - public void Dispose() - { - if (mEventWatcher != null) - { - mEventWatcher.EventRecordWritten -= new EventHandler(OnLogEntry); - mEventWatcher.Dispose(); - mEventWatcher = null; - } - } - - public void EnableLogging() - { - mLogList = new List(); - - if (mLogName != null) - { - mEventWatcher = new EventLogWatcher(new EventLogQuery(mLogName, PathType.LogName)); - mEventWatcher.EventRecordWritten += new EventHandler(OnLogEntry); - mEventWatcher.Enabled = true; - } - } - - public bool SetupEventLog(string LogName) - { - if (mLogName != null) - return true; - - try - { - if (!EventLog.SourceExists(LogName)) - EventLog.CreateEventSource(LogName, LogName); - mLogName = LogName; - return true; - } - catch { } - return false; - } - - public void RemoveEventLog(string LogName) - { - try { EventLog.Delete(LogName); } catch { } - try { EventLog.DeleteEventSource(LogName); } catch { } - } - - public bool UsingEventLog() - { - return mLogName != null; - } - - public static void Add(EventLogEntryType entryType, long eventID, short categoryID, string strMessage, Dictionary Params = null/*, byte[] binData = null*/) - { - Console.WriteLine("Log: " + strMessage); - - if (mInstance != null) - mInstance.AddLogEntry(entryType, eventID, categoryID, strMessage, Params/*, binData*/); - } - - private void AddLogEntry(EventLogEntryType entryType, long eventID, short categoryID, string strMessage, Dictionary Params = null/*, byte[] binData = null*/) - { - LogEntry Entry = new LogEntry(); - Entry.entryType = entryType; - Entry.categoryID = categoryID; - Entry.eventID = eventID; - Entry.strMessage = strMessage; - Entry.Params = Params; - //Entry.binData = binData; - Entry.timeGenerated = DateTime.Now; - - if (mLogName == null) - AddToLog(Entry); - else - EventLog.WriteEvent(mLogName, new EventInstance(Entry.eventID, Entry.categoryID, Entry.entryType), /*Entry.binData,*/ Entry.GetData()); - } - - private void AddToLog(LogEntry Entry) - { - LogEvent?.Invoke(this, new LogEventArgs(Entry)); - - if (mLogList == null) - return; - - mLocker.EnterWriteLock(); - mLogList.Add(Entry); - while (mLogList.Count > mLogLimit) - mLogList.RemoveAt(0); - mLocker.ExitWriteLock(); - } - - private void OnLogEntry(object obj, EventRecordWrittenEventArgs arg) - { - if (arg.EventRecord == null || arg.EventRecord.Properties.Count == 0) - return; - - try - { - LogEntry Entry = new LogEntry(); - Entry.eventID = arg.EventRecord.Id; - Entry.categoryID = (short)arg.EventRecord.Task; - switch (arg.EventRecord.Level.Value) - { - case 2: Entry.entryType = EventLogEntryType.Error; break; - case 3: Entry.entryType = EventLogEntryType.Warning; break; - case 4: - default: Entry.entryType = EventLogEntryType.Information; break; - } - Entry.timeGenerated = arg.EventRecord.TimeCreated.Value; - string[] dataStr = new string[arg.EventRecord.Properties.Count]; - for(int i = 0; i < arg.EventRecord.Properties.Count; i++) - dataStr[i] = arg.EventRecord.Properties[i].Value.ToString(); - Entry.SetData(dataStr); - //Entry.binData = - - AddToLog(Entry); - } - catch { } - } - - public void LoadLog() - { - if (mLogName == null) - return; - - mLocker.EnterWriteLock(); - try - { - mLogList = new List(); - - EventLog eventLog = new EventLog(mLogName); - for(int idx = (eventLog.Entries.Count > mLogLimit ? eventLog.Entries.Count - mLogLimit : 0); idx < eventLog.Entries.Count; idx++) - { - EventLogEntry logEntry = eventLog.Entries[idx]; - - if (logEntry.ReplacementStrings.Length == 0) - continue; - - LogEntry Entry = new LogEntry(); - Entry.eventID = logEntry.InstanceId; - Entry.categoryID = logEntry.CategoryNumber; - Entry.entryType = logEntry.EntryType; - Entry.timeGenerated = logEntry.TimeGenerated; - Entry.SetData(logEntry.ReplacementStrings); - //Entry.binData = logEntry.Data; - - mLogList.Add(Entry); - } - } - catch { } - mLocker.ExitWriteLock(); - } - - public List GetFullLog() - { - List log = null; - if (mLogList != null) - { - mLocker.EnterReadLock(); - log = new List(mLogList); - mLocker.ExitReadLock(); - } - return log; - } - - static public long ExceptionLogID = 0; - static public long ExceptionCategory = 0; - - static public void Exception(Exception ex) - { -#if DEBUG - Debugger.Break(); -#endif - - var st = new StackTrace(); - var sf = st.GetFrame(1); - var name = sf.GetMethod().Name; - - String message = "Exception in " + name + ": " + ex.Message; - Dictionary values = new Dictionary(); - values.Add("Exception", ex.ToString()); - Add(EventLogEntryType.Error, ExceptionLogID, (short)ExceptionCategory, message, values); - } - - static public void Debug(string message, params object[] args) - { - Console.WriteLine(args.Length == 0 ? message : string.Format(message, args)); - } -} \ No newline at end of file diff --git a/PrivateWin10/Common/ClonableDictionary.cs b/PrivateWin10/Common/ClonableDictionary.cs deleted file mode 100644 index 1ddd557..0000000 --- a/PrivateWin10/Common/ClonableDictionary.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -public class CloneableDictionary : Dictionary -{ - public CloneableDictionary Clone() // shallow copy! - { - CloneableDictionary clone = new CloneableDictionary(); - foreach (KeyValuePair kvp in this) - { - clone.Add(kvp.Key, kvp.Value); - } - return clone; - } -} \ No newline at end of file diff --git a/PrivateWin10/Common/ClonableList.cs b/PrivateWin10/Common/ClonableList.cs deleted file mode 100644 index 7feee4e..0000000 --- a/PrivateWin10/Common/ClonableList.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -public class CloneableList : List -{ - public CloneableList Clone() // shallow copy! - { - CloneableList clone = new CloneableList(); - foreach (TValue v in this) - { - clone.Add(v); - } - return clone; - } -} \ No newline at end of file diff --git a/PrivateWin10/Common/HttpTask.cs b/PrivateWin10/Common/HttpTask.cs deleted file mode 100644 index 30265db..0000000 --- a/PrivateWin10/Common/HttpTask.cs +++ /dev/null @@ -1,359 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Threading; - - -public class HttpTask -{ - //const int DefaultTimeout = 2 * 60 * 1000; // 2 minutes timeout - const int BUFFER_SIZE = 1024; - private byte[] BufferRead; - private HttpWebRequest request; - private HttpWebResponse response; - private Stream streamResponse; - private Stream streamWriter; - private Dispatcher mDispatcher; - private string mUrl; - private string mDlPath; - private string mDlName; - private int mLength = -1; - private int mOffset = -1; - private bool Canceled = false; - private DateTime lastTime; - - public string DlUrl { get { return mUrl; } } - public string DlPath { get { return mDlPath; } } - public string DlName { get { return mDlName; } } - - public HttpTask(string Url, string DlPath, string DlName = null) - { - mUrl = Url; - mDlPath = DlPath; - mDlName = DlName; - - BufferRead = null; - request = null; - response = null; - streamResponse = null; - streamWriter = null; - mDispatcher = Dispatcher.CurrentDispatcher; - } - - // Abort the request if the timer fires. - /*private static void TimeoutCallback(object state, bool timedOut) - { - if (timedOut) - { - HttpWebRequest request = state as HttpWebRequest; - if (request != null) - request.Abort(); - } - }*/ - - public bool Start() - { - Canceled = false; - try - { - // Create a HttpWebrequest object to the desired URL. - request = (HttpWebRequest)WebRequest.Create(mUrl); - //myHttpWebRequest.AllowAutoRedirect = false; - - /** - * If you are behind a firewall and you do not have your browser proxy setup - * you need to use the following proxy creation code. - - // Create a proxy object. - WebProxy myProxy = new WebProxy(); - - // Associate a new Uri object to the _wProxy object, using the proxy address - // selected by the user. - myProxy.Address = new Uri("http://myproxy"); - - - // Finally, initialize the Web request object proxy property with the _wProxy - // object. - myHttpWebRequest.Proxy=myProxy; - ***/ - - BufferRead = new byte[BUFFER_SIZE]; - mOffset = 0; - - // Start the asynchronous request. - IAsyncResult result = (IAsyncResult)request.BeginGetResponse(new AsyncCallback(RespCallback), this); - - // this line implements the timeout, if there is a timeout, the callback fires and the request becomes aborted - //ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), request, DefaultTimeout, true); - return true; - } - catch (Exception e) - { - Console.WriteLine("\nMain Exception raised!"); - Console.WriteLine("\nMessage:{0}", e.Message); - } - return false; - } - - public void Cancel() - { - Canceled = true; - if (request != null) - request.Abort(); - } - - private void Finish(int Success, int ErrCode, Exception Error = null) - { - // Release the HttpWebResponse resource. - if (response != null) - { - response.Close(); - if (streamResponse != null) - streamResponse.Close(); - if (streamWriter != null) - streamWriter.Close(); - - } - response = null; - request = null; - streamResponse = null; - BufferRead = null; - - if (Success == 1) - { - try - { - if (File.Exists(mDlPath + @"\" + mDlName)) - File.Delete(mDlPath + @"\" + mDlName); - File.Move(mDlPath + @"\" + mDlName + ".tmp", mDlPath + @"\" + mDlName); - } - catch - { - AppLog.Debug("Failed to rename download {0}", mDlPath + @"\" + mDlName + ".tmp"); - mDlName += ".tmp"; - } - - try { File.SetLastWriteTime(mDlPath + @"\" + mDlName, lastTime); } catch { } // set last mod time - } - else if (Success == 2) - { - AppLog.Debug("File already downloaded {0}", mDlPath + @"\" + mDlName); - } - else - { - try { File.Delete(mDlPath + @"\" + mDlName + ".tmp"); } catch { } // delete partial file - AppLog.Debug("Failed to download file {0}", mDlPath + @"\" + mDlName); - } - - Finished?.Invoke(this, new FinishedEventArgs(Success > 0 ? 0 : Canceled ? -1 : ErrCode, Error)); - } - - static public string GetNextTempFile(string path, string baseName) - { - for (int i = 0; i < 10000; i++) - { - if (!File.Exists(path + @"\" + baseName + "_" + i + ".tmp")) - return baseName + "_" + i; - } - return baseName; - } - - private static void RespCallback(IAsyncResult asynchronousResult) - { - int Success = 0; - int ErrCode = 0; - Exception Error = null; - HttpTask task = (HttpTask)asynchronousResult.AsyncState; - try - { - // State of request is asynchronous. - task.response = (HttpWebResponse)task.request.EndGetResponse(asynchronousResult); - - ErrCode = (int)task.response.StatusCode; - - Console.WriteLine("The server at {0} returned {1}", task.response.ResponseUri, task.response.StatusCode); - - string fileName = Path.GetFileName(task.response.ResponseUri.ToString()); - task.lastTime = DateTime.Now; - - Console.WriteLine("With headers:"); - foreach (string key in task.response.Headers.AllKeys) - { - Console.WriteLine("\t{0}:{1}", key, task.response.Headers[key]); - - if (key.Equals("Content-Length", StringComparison.CurrentCultureIgnoreCase)) - { - task.mLength = int.Parse(task.response.Headers[key]); - } - else if (key.Equals("Content-Disposition", StringComparison.CurrentCultureIgnoreCase)) - { - string cd = task.response.Headers[key]; - fileName = cd.Substring(cd.IndexOf("filename=") + 9).Replace("\"", ""); - } - else if (key.Equals("Last-Modified", StringComparison.CurrentCultureIgnoreCase)) - { - task.lastTime = DateTime.Parse(task.response.Headers[key]); - } - } - - //Console.WriteLine(task.lastTime); - - if (task.mDlName == null) - task.mDlName = fileName; - - FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName); - if (testInfo.Exists && testInfo.LastWriteTime == task.lastTime && testInfo.Length == task.mLength) - { - task.request.Abort(); - Success = 2; - } - else - { - // prepare download filename - if (!Directory.Exists(task.mDlPath)) - Directory.CreateDirectory(task.mDlPath); - if (task.mDlName.Length == 0 || task.mDlName[0] == '?') - task.mDlName = GetNextTempFile(task.mDlPath, "Download"); - - FileInfo info = new FileInfo(task.mDlPath + @"\" + task.mDlName + ".tmp"); - if (info.Exists) - info.Delete(); - - // Read the response into a Stream object. - task.streamResponse = task.response.GetResponseStream(); - - task.streamWriter = info.OpenWrite(); - - // Begin the Reading of the contents of the HTML page and print it to the console. - task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task); - return; - } - } - catch (WebException e) - { - if (e.Response != null) - { - string fileName = Path.GetFileName(e.Response.ResponseUri.AbsolutePath.ToString()); - - if (task.mDlName == null) - task.mDlName = fileName; - - FileInfo testInfo = new FileInfo(task.mDlPath + @"\" + task.mDlName); - if (testInfo.Exists) - Success = 2; - } - - if(Success == 0) - { - ErrCode = -2; - Error = e; - Console.WriteLine("\nRespCallback Exception raised!"); - Console.WriteLine("\nMessage:{0}", e.Message); - Console.WriteLine("\nStatus:{0}", e.Status); - } - } - catch (Exception e) - { - ErrCode = -2; - Error = e; - Console.WriteLine("\nRespCallback Exception raised!"); - Console.WriteLine("\nMessage:{0}", e.Message); - } - task.mDispatcher.Invoke(new Action(() => { - task.Finish(Success, ErrCode, Error); - })); - } - - private int mOldPercent = -1; - - private static void ReadCallBack(IAsyncResult asyncResult) - { - int Success = 0; - int ErrCode = 0; - Exception Error = null; - HttpTask task = (HttpTask)asyncResult.AsyncState; - try - { - int read = task.streamResponse.EndRead(asyncResult); - // Read the HTML page and then print it to the console. - if (read > 0) - { - task.streamWriter.Write(task.BufferRead, 0, read); - task.mOffset += read; - - int Percent = task.mLength > 0 ? (int)((Int64)100 * task.mOffset / task.mLength) : -1; - if (Percent != task.mOldPercent) - { - task.mOldPercent = Percent; - task.mDispatcher.Invoke(new Action(() => { - task.Progress?.Invoke(task, new ProgressEventArgs(Percent)); - })); - } - - // setup next read - task.streamResponse.BeginRead(task.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), task); - return; - } - else - { - // this is done on finisch - //task.streamWriter.Close(); - //task.streamResponse.Close(); - Success = 1; - } - - } - catch (Exception e) - { - ErrCode = -3; - Error = e; - Console.WriteLine("\nReadCallBack Exception raised!"); - Console.WriteLine("\nMessage:{0}", e.Message); - } - task.mDispatcher.Invoke(new Action(() => { - task.Finish(Success, ErrCode, Error); - })); - } - - public class FinishedEventArgs : EventArgs - { - public FinishedEventArgs(int ErrCode = 0, Exception Error = null) - { - this.ErrCode = ErrCode; - this.Error = Error; - } - public string GetError() - { - if (Error != null) - return Error.ToString(); - switch(ErrCode) - { - case 0: return "Ok"; - case -1: return "Canceled"; - default: return ErrCode.ToString(); - } - } - public bool Success { get { return ErrCode == 0; } } - public bool Cancelled { get { return ErrCode == -1; } } - - public int ErrCode = 0; - public Exception Error = null; - } - public event EventHandler Finished; - - public class ProgressEventArgs : EventArgs - { - public ProgressEventArgs(int Percent) - { - this.Percent = Percent; - } - public int Percent = 0; - } - public event EventHandler Progress; -} diff --git a/PrivateWin10/Common/IconExtractor.cs b/PrivateWin10/Common/IconExtractor.cs deleted file mode 100644 index d7acc52..0000000 --- a/PrivateWin10/Common/IconExtractor.cs +++ /dev/null @@ -1,380 +0,0 @@ -/* - * IconExtractor/IconUtil for .NET - * Copyright (C) 2014 Tsuda Kageyu. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER - * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.IO; -using System.Runtime.InteropServices; -using System.Security; -using System.Text; -using System.Windows.Forms; - - -public class IconExtractor -{ - //////////////////////////////////////////////////////////////////////// - // Constants - - // Flags for LoadLibraryEx(). - - private const uint LOAD_LIBRARY_AS_DATAFILE = 0x00000002; - - // Resource types for EnumResourceNames(). - - private readonly static IntPtr RT_ICON = (IntPtr)3; - private readonly static IntPtr RT_GROUP_ICON = (IntPtr)14; - - private const int MAX_PATH = 260; - - //////////////////////////////////////////////////////////////////////// - // Fields - - private byte[][] iconData = null; // Binary data of each icon. - - //////////////////////////////////////////////////////////////////////// - // Public properties - - /// - /// Gets the full path of the associated file. - /// - public string FileName - { - get; - private set; - } - - /// - /// Gets the count of the icons in the associated file. - /// - public int Count - { - get { return iconData == null ? 0 : iconData.Length; } - } - - /// - /// Initializes a new instance of the IconExtractor class from the specified file name. - /// - /// The file to extract icons from. - public IconExtractor(string fileName) - { - Initialize(fileName); - } - - /// - /// Extracts an icon from the file. - /// - /// Zero based index of the icon to be extracted. - /// A System.Drawing.Icon object. - /// Always returns new copy of the Icon. It should be disposed by the user. - public Icon GetIcon(int index) - { - if (index < 0 || Count <= index) - throw new ArgumentOutOfRangeException("index"); - - // Create an Icon based on a .ico file in memory. - - using (var ms = new MemoryStream(iconData[index])) - { - return new Icon(ms); - } - } - - /// - /// Extracts an icon from the file. - /// - /// Zero based index of the icon to be extracted. - /// Size of the icon to extract - /// A System.Drawing.Icon object. - /// Always returns new copy of the Icon. It should be disposed by the user. - public Icon GetIcon(int index, Size size) - { - if (index < 0 || Count <= index) - throw new ArgumentOutOfRangeException("index"); - - // Create an Icon based on a .ico file in memory. - - using (var ms = new MemoryStream(iconData[index])) - { - return new Icon(ms, size); - } - } - - /// - /// Extracts all the icons from the file. - /// - /// An array of System.Drawing.Icon objects. - /// Always returns new copies of the Icons. They should be disposed by the user. - public Icon[] GetAllIcons() - { - var icons = new List(); - for (int i = 0; i < Count; ++i) - icons.Add(GetIcon(i)); - - return icons.ToArray(); - } - - private void Initialize(string fileName) - { - if (fileName == null) - return; - - IntPtr hModule = IntPtr.Zero; - try - { - hModule = LoadLibraryEx(fileName, IntPtr.Zero, LOAD_LIBRARY_AS_DATAFILE); - if (hModule == IntPtr.Zero) - return; - - FileName = GetFileName(hModule); - - // Enumerate the icon resource and build .ico files in memory. - - var tmpData = new List(); - - ENUMRESNAMEPROC callback = (h, t, name, l) => - { - // Refer the following URL for the data structures used here: - // http://msdn.microsoft.com/en-us/library/ms997538.aspx - - // RT_GROUP_ICON resource consists of a GRPICONDIR and GRPICONDIRENTRY's. - - var dir = GetDataFromResource(hModule, RT_GROUP_ICON, name); - - // Calculate the size of an entire .icon file. - - int count = BitConverter.ToUInt16(dir, 4); // GRPICONDIR.idCount - int len = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count - for (int i = 0; i < count; ++i) - len += BitConverter.ToInt32(dir, 6 + 14 * i + 8); // GRPICONDIRENTRY.dwBytesInRes - - using (var dst = new BinaryWriter(new MemoryStream(len))) - { - // Copy GRPICONDIR to ICONDIR. - - dst.Write(dir, 0, 6); - - int picOffset = 6 + 16 * count; // sizeof(ICONDIR) + sizeof(ICONDIRENTRY) * count - - for (int i = 0; i < count; ++i) - { - // Load the picture. - - ushort id = BitConverter.ToUInt16(dir, 6 + 14 * i + 12); // GRPICONDIRENTRY.nID - var pic = GetDataFromResource(hModule, RT_ICON, (IntPtr)id); - - // Copy GRPICONDIRENTRY to ICONDIRENTRY. - - dst.Seek(6 + 16 * i, SeekOrigin.Begin); - - dst.Write(dir, 6 + 14 * i, 8); // First 8bytes are identical. - dst.Write(pic.Length); // ICONDIRENTRY.dwBytesInRes - dst.Write(picOffset); // ICONDIRENTRY.dwImageOffset - - // Copy a picture. - - dst.Seek(picOffset, SeekOrigin.Begin); - dst.Write(pic, 0, pic.Length); - - picOffset += pic.Length; - } - - tmpData.Add(((MemoryStream)dst.BaseStream).ToArray()); - } - - return true; - }; - EnumResourceNames(hModule, RT_GROUP_ICON, callback, IntPtr.Zero); - - iconData = tmpData.ToArray(); - } - finally - { - if (hModule != IntPtr.Zero) - FreeLibrary(hModule); - } - } - - private byte[] GetDataFromResource(IntPtr hModule, IntPtr type, IntPtr name) - { - // Load the binary data from the specified resource. - - IntPtr hResInfo = FindResource(hModule, name, type); - if (hResInfo == IntPtr.Zero) - throw new Win32Exception(); - - IntPtr hResData = LoadResource(hModule, hResInfo); - if (hResData == IntPtr.Zero) - throw new Win32Exception(); - - IntPtr pResData = LockResource(hResData); - if (pResData == IntPtr.Zero) - throw new Win32Exception(); - - uint size = SizeofResource(hModule, hResInfo); - if (size == 0) - throw new Win32Exception(); - - byte[] buf = new byte[size]; - Marshal.Copy(pResData, buf, 0, buf.Length); - - return buf; - } - - private string GetFileName(IntPtr hModule) - { - // Alternative to GetModuleFileName() for the module loaded with - // LOAD_LIBRARY_AS_DATAFILE option. - - // Get the file name in the format like: - // "\\Device\\HarddiskVolume2\\Windows\\System32\\shell32.dll" - - string fileName; - { - var buf = new StringBuilder(MAX_PATH); - int len = GetMappedFileName( - GetCurrentProcess(), hModule, buf, buf.Capacity); - if (len == 0) - throw new Win32Exception(); - - fileName = buf.ToString(); - } - - // Convert the device name to drive name like: - // "C:\\Windows\\System32\\shell32.dll" - - for (char c = 'A'; c <= 'Z'; ++c) - { - var drive = c + ":"; - var buf = new StringBuilder(MAX_PATH); - int len = QueryDosDevice(drive, buf, buf.Capacity); - if (len == 0) - continue; - - var devPath = buf.ToString(); - if (fileName.StartsWith(devPath)) - return (drive + fileName.Substring(devPath.Length)); - } - - return fileName; - } - - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hFile, uint dwFlags); - - [DllImport("kernel32.dll", SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern bool FreeLibrary(IntPtr hModule); - - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public static extern bool EnumResourceNames(IntPtr hModule, IntPtr lpszType, ENUMRESNAMEPROC lpEnumFunc, IntPtr lParam); - - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public static extern IntPtr FindResource(IntPtr hModule, IntPtr lpName, IntPtr lpType); - - [DllImport("kernel32.dll", SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern IntPtr LoadResource(IntPtr hModule, IntPtr hResInfo); - - [DllImport("kernel32.dll", SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern IntPtr LockResource(IntPtr hResData); - - [DllImport("kernel32.dll", SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo); - - [DllImport("kernel32.dll", SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern IntPtr GetCurrentProcess(); - - [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public static extern int QueryDosDevice(string lpDeviceName, StringBuilder lpTargetPath, int ucchMax); - - [DllImport("psapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public static extern int GetMappedFileName(IntPtr hProcess, IntPtr lpv, StringBuilder lpFilename, int nSize); - - - [UnmanagedFunctionPointer(CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Unicode)] - [SuppressUnmanagedCodeSecurity] - public delegate bool ENUMRESNAMEPROC(IntPtr hModule, IntPtr lpszType, IntPtr lpszName, IntPtr lParam); - - - - - public class PickerDialog : CommonDialog - { - - [DllImport("shell32.dll", EntryPoint = "#62", CharSet = CharSet.Unicode, SetLastError = true)] - [SuppressUnmanagedCodeSecurity] - public static extern bool SHPickIconDialog(IntPtr hWnd, StringBuilder pszFilename, int cchFilenameMax, out int pnIconIndex); - - private const int MAX_PATH = 260; - - [DefaultValue(default(string))] - public string FileName - { - get; - set; - } - - [DefaultValue(0)] - public int IconIndex - { - get; - set; - } - - protected override bool RunDialog(IntPtr hwndOwner) - { - var buf = new StringBuilder(FileName, MAX_PATH); - int index; - - bool ok = SHPickIconDialog(hwndOwner, buf, MAX_PATH, out index); - if (ok) - { - FileName = Environment.ExpandEnvironmentVariables(buf.ToString()); - IconIndex = index; - } - - return ok; - } - - public override void Reset() - { - FileName = null; - IconIndex = 0; - } - } -} - diff --git a/PrivateWin10/Common/ImgFunc.cs b/PrivateWin10/Common/ImgFunc.cs deleted file mode 100644 index 79dd6ae..0000000 --- a/PrivateWin10/Common/ImgFunc.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.IO; -using System.Linq; -using System.Runtime.InteropServices; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Interop; -using System.Windows.Media; -using System.Windows.Media.Imaging; - -public static class ImgFunc -{ - private static Dictionary IconCache = new Dictionary(); - private static ReaderWriterLockSlim IconCacheLock = new ReaderWriterLockSlim(); - - public static ImageSource ExeIcon16 = GetIcon(MiscFunc.NtOsKrnlPath, 16); - - public static ImageSource GetIcon(string path, double size) - { - string key = path + "@" + size.ToString(); - - ImageSource image = null; - IconCacheLock.EnterReadLock(); - bool bFound = IconCache.TryGetValue(key, out image); - IconCacheLock.ExitReadLock(); - if(bFound) - return image; - - try - { - var pathIndex = TextHelpers.Split2(path, "|"); - - IconExtractor extractor = new IconExtractor(pathIndex.Item1); - int index = MiscFunc.parseInt(pathIndex.Item2); - if (index < extractor.Count) - image = ToImageSource(extractor.GetIcon(index, new System.Drawing.Size((int)size, (int)size))); - - if (image == null) - { - if(File.Exists(MiscFunc.NtOsKrnlPath)) // if running in WOW64 this does not exist - image = ToImageSource(Icon.ExtractAssociatedIcon(MiscFunc.NtOsKrnlPath)); - else // fall back to an other icon - image = ToImageSource(Icon.ExtractAssociatedIcon(MiscFunc.Shell32Path)); - } - - image.Freeze(); - } - catch(Exception err) - { - AppLog.Exception(err); - } - - IconCacheLock.EnterWriteLock(); - if (!IconCache.ContainsKey(key)) - IconCache.Add(key, image); - IconCacheLock.ExitWriteLock(); - return image; - } - - public delegate ImageSource IconExtract(string path, double size); - - public static IAsyncResult GetIconAsync(string path, double size, Func cb) - { - IconExtract iconExtract = new IconExtract(ImgFunc.GetIcon); - return iconExtract.BeginInvoke(path, size, new AsyncCallback((IAsyncResult asyncResult) => { - ImageSource icon = (asyncResult.AsyncState as IconExtract).EndInvoke(asyncResult); - cb(icon); - }), iconExtract); - } - - - [DllImport("gdi32.dll", SetLastError = true)] - private static extern bool DeleteObject(IntPtr hObject); - - public static ImageSource ToImageSource(this Icon icon) - { - Bitmap bitmap = icon.ToBitmap(); - IntPtr hBitmap = bitmap.GetHbitmap(); - ImageSource wpfBitmap = Imaging.CreateBitmapSourceFromHBitmap(hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); - if (!DeleteObject(hBitmap)) - throw new Win32Exception(); - return wpfBitmap; - } -} diff --git a/PrivateWin10/Common/MiscFunc.cs b/PrivateWin10/Common/MiscFunc.cs deleted file mode 100644 index 239fed4..0000000 --- a/PrivateWin10/Common/MiscFunc.cs +++ /dev/null @@ -1,393 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Diagnostics; -using System.Drawing; -using System.IO; -using System.Linq; -using System.Management; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using System.Security.Principal; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Forms; -using System.Windows.Interop; -using System.Windows.Media; -using System.Windows.Media.Imaging; - - -internal struct LASTINPUTINFO -{ - public uint cbSize; - public uint dwTime; -} - -static class MiscFunc -{ - public static UInt64 GetUTCTime() - { - return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; - } - - public static UInt64 GetUTCTimeMs() - { - return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds; - } - - - public static UInt64 DateTime2Ms(DateTime dateTime) - { - return (UInt64)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds; - } - - static public List EnumAllFiles(string sourcePath) - { - List files = new List(); - - foreach (string fileName in Directory.GetFiles(sourcePath)) - files.Add(fileName); - - foreach (string dirName in Directory.GetDirectories(sourcePath)) - { - if ((new DirectoryInfo(dirName).Attributes & FileAttributes.ReparsePoint) != 0) - continue; // skip junctions - - files.AddRange(EnumAllFiles(dirName)); - } - - return files; - } - - public static TValue GetOrCreate(this IDictionary dict, TKey key) - where TValue : new() - { - TValue val; - - if (!dict.TryGetValue(key, out val)) - { - val = new TValue(); - dict.Add(key, val); - } - - return val; - } - - public static bool Exec(string cmd, string args, bool hidden = true) - { - try - { - Process process = new Process(); - process.StartInfo.UseShellExecute = false; - if (hidden) - { - process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; - process.StartInfo.CreateNoWindow = true; - } - process.StartInfo.FileName = cmd; - process.StartInfo.Arguments = args; - process.StartInfo.Verb = "runas"; // run as admin - process.Start(); - process.WaitForExit(); - return true; - } - catch (Exception err) - { - AppLog.Exception(err); - } - return false; - } - - public static UInt64 GetCurTick() - { - return (UInt64)DateTime.Now.Ticks / 10000; // ticks in ms - } - - /*internal static void ActiveSleep(int ms) - { - DateTime until = DateTime.Now.Add(new TimeSpan(0, 0, 0, 0, ms)); - while (until >= DateTime.Now) - System.Windows.Forms.Application.DoEvents(); - }*/ - - [DllImport("User32.dll")] - private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); - - [DllImport("Kernel32.dll")] - private static extern uint GetLastError(); - - public static uint GetIdleTime() // in seconds - { - LASTINPUTINFO lastInPut = new LASTINPUTINFO(); - lastInPut.cbSize = (uint)Marshal.SizeOf(lastInPut); - if (!GetLastInputInfo(ref lastInPut)) - { - throw new Exception(GetLastError().ToString()); - } - return ((uint)Environment.TickCount - lastInPut.dwTime)/1000; - } - - public static int parseInt(string str, int def = 0) - { - int ret; - if (int.TryParse(str, out ret)) - return ret; - return def; - } - - public static double parseDouble(string str, double def = 0) - { - double ret; - if (double.TryParse(str, out ret)) - return ret; - return def; - } - - public static bool? parseBool(string str, bool? def = false) - { - if (str.Length == 0) - return def; - - bool ret; - if (bool.TryParse(str, out ret)) - return ret; - return def; - } - - public static string NtOsKrnlPath = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\ntoskrnl.exe"); - - public static string Shell32Path = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\System32\shell32.dll"); - - public static string parsePath(string path) - { - try - { - if (path.Contains(@"\device\mup\")) - return @"\" + path.Substring(11, path.Length - 11); - string[] strArray = path.Split(new char[1]{'\\'}, StringSplitOptions.RemoveEmptyEntries); - string vol = @"\" + strArray[0] + @"\" + strArray[1]; - path = path.Replace(vol, GetDriveLetter(vol)); - if (path.Contains('~')) - path = Path.GetFullPath(path); - return path; - } - catch (Exception err) - { - AppLog.Exception(err); - } - return ""; - } - - [DllImport("kernel32.dll")] - public static extern UInt64 GetTickCount64(); - - [DllImport("kernel32.dll")] - public static extern uint QueryDosDevice(string lpDeviceName, [In, Out] char[] lpTargetPath, int ucchMax); - - private static Dictionary> DriveLetterCache = new Dictionary>(); - private static ReaderWriterLockSlim DriveLetterCacheLock = new ReaderWriterLockSlim(); - - private static string GetDriveLetter(string longPath) - { - Tuple temp; - DriveLetterCacheLock.EnterReadLock(); - if (DriveLetterCache.TryGetValue(longPath.ToLower(), out temp)) - { - if (temp.Item2 > GetTickCount64()) - { - DriveLetterCacheLock.ExitReadLock(); - return temp.Item1; - } - DriveLetterCache.Remove(longPath.ToLower()); - } - DriveLetterCacheLock.ExitReadLock(); - - string ret = null; - char[] lpTargetPath = new char[260 + 1]; - for (char ltr = 'A'; ltr <= 'Z'; ltr++) - { - uint size = QueryDosDevice(ltr + ":", lpTargetPath, 260); - if (size > 0 && longPath.Equals(new String(lpTargetPath, 0, (int)size-2), StringComparison.OrdinalIgnoreCase)) - { - ret = ltr + ":"; - break; - } - } - - if (ret == null) - return "?:"; - - DriveLetterCacheLock.EnterWriteLock(); - if(DriveLetterCache.ContainsKey(longPath.ToLower()) == false) - DriveLetterCache.Add(longPath.ToLower(), new Tuple(ret, GetTickCount64() + 1*60*1000)); // cahce values for 1 minutes - DriveLetterCacheLock.ExitWriteLock(); - return ret; - } - - public static string GetExeDescription(string appPath) - { - string descr = null; - if (File.Exists(appPath)) - { - try - { - FileVersionInfo info = FileVersionInfo.GetVersionInfo(appPath); - descr = info?.FileDescription; - } - catch { } - } - return (descr != null && descr.Length > 0) ? descr : ""; - } - - - /*public static string GetServiceNameByPID(int pid) - { - ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT Name FROM Win32_Service WHERE ProcessId = " + pid); - foreach (ManagementObject queryObj in searcher.Get()) - { - string ret = queryObj["Name"].ToString(); - return ret; - } - return null; - }*/ - - public static bool StrCmp(string value, string test) - { - if(value == null) - return test == null; - if (test == null) - return false; - return value.Equals(test, StringComparison.OrdinalIgnoreCase); - } - - /*public static string GetServiceName(string name) - { - ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT DisplayName FROM Win32_Service WHERE Name = \"" + name + "\""); - foreach (ManagementObject queryObj in searcher.Get()) - { - return queryObj["DisplayName"].ToString(); - } - return ""; - }*/ - - [DllImport("shlwapi.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] - public static extern int SHLoadIndirectString(string pszSource, StringBuilder pszOutBuf, int cchOutBuf, IntPtr ppvReserved); - - public static string GetResourceStr(string resourcePath) - { - StringBuilder buffer = new StringBuilder(4096); - int result = SHLoadIndirectString(resourcePath, buffer, buffer.Capacity, IntPtr.Zero); - if (result == 0) - return buffer.ToString(); - return resourcePath; - } - - public static string GetResourceStr(string path, string resID) - { - return GetResourceStr("@{" + path + "? " + resID + "}"); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - static public string GetCurrentMethod() - { - try - { - var st = new StackTrace(); - var sf = st.GetFrame(1); - return sf.GetMethod().Name; - } - catch - { - return "Unknown"; - } - } - - static public bool IsOnScreen(Rectangle formRectangle) - { - Screen[] screens = Screen.AllScreens; - foreach (Screen screen in screens) - { - if (screen.WorkingArea.Contains(formRectangle)) - { - return true; - } - } - return false; - } - - public static bool IsValidRegex(string pattern) - { - if (string.IsNullOrEmpty(pattern)) return false; - - try - { - Regex.Match("", pattern); - } - catch (ArgumentException) - { - return false; - } - - return true; - } - - public static bool IsEqual(T L, T R) - { - if (L == null) - return (R == null); - return L.Equals(R); - } - - public static T Max(T x, T y) - { - return (Comparer.Default.Compare(x, y) > 0) ? x : y; - } - - public static T Min(T x, T y) - { - return (Comparer.Default.Compare(x, y) < 0) ? x : y; - } - - public static class ClipboardNative - { - [DllImport("user32.dll")] - private static extern bool OpenClipboard(IntPtr hWndNewOwner); - - [DllImport("user32.dll")] - private static extern bool CloseClipboard(); - - [DllImport("user32.dll")] - private static extern bool SetClipboardData(uint uFormat, IntPtr data); - - private const uint CF_UNICODETEXT = 13; - - public static bool CopyTextToClipboard(string text) - { - if (!OpenClipboard(IntPtr.Zero)) - { - return false; - } - - var global = Marshal.StringToHGlobalUni(text); - - SetClipboardData(CF_UNICODETEXT, global); - CloseClipboard(); - - //------------------------------------------- - // Not sure, but it looks like we do not need - // to free HGLOBAL because Clipboard is now - // responsible for the copied data. (?) - // - // Otherwise the second call will crash - // the app with a Win32 exception - // inside OpenClipboard() function - //------------------------------------------- - // Marshal.FreeHGlobal(global); - - return true; - } - } -} diff --git a/PrivateWin10/Common/MultiValueDictionary.cs b/PrivateWin10/Common/MultiValueDictionary.cs deleted file mode 100644 index 4c184c8..0000000 --- a/PrivateWin10/Common/MultiValueDictionary.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -public class MultiValueDictionary : Dictionary> -{ - public MultiValueDictionary() : base() - { - } - - public MultiValueDictionary Clone() // shallow copy! - { - MultiValueDictionary clone = new MultiValueDictionary(); - foreach (KeyValuePair> kvp in this) - { - clone.Add(kvp.Key, kvp.Value.Clone()); - } - return clone; - } - - public void Add(TKey key, TValue value) - { - CloneableList container = null; - if (!this.TryGetValue(key, out container)) - { - container = new CloneableList(); - base.Add(key, container); - } - container.Add(value); - } - - public bool ContainsValue(TKey key, TValue value) - { - bool toReturn = false; - CloneableList values = null; - if (this.TryGetValue(key, out values)) - { - toReturn = values.Contains(value); - } - return toReturn; - } - - public void Remove(TKey key, TValue value) - { - CloneableList container = null; - if (this.TryGetValue(key, out container)) - { - container.Remove(value); - if (container.Count <= 0) - { - this.Remove(key); - } - } - } - - public CloneableList GetValues(TKey key, bool returnEmptySet = true) - { - CloneableList toReturn = null; - if (!base.TryGetValue(key, out toReturn) && returnEmptySet) - { - toReturn = new CloneableList(); - } - return toReturn; - } - - public CloneableList GetOrAdd(TKey key) - { - CloneableList toReturn = null; - if (!base.TryGetValue(key, out toReturn)) - { - toReturn = new CloneableList(); - base.Add(key, toReturn); - } - return toReturn; - } - - public int GetCount() - { - int Count = 0; - foreach (KeyValuePair> pair in this) - Count += pair.Value.Count; - return Count; - } - - public TValue GetAt(int index) - { - int Count = 0; - foreach (KeyValuePair> pair in this) - { - if (Count + pair.Value.Count > index) - return pair.Value[index - Count]; - Count += pair.Value.Count; - } - throw new IndexOutOfRangeException(); - } - - public TKey GetKey(int index) - { - int Count = 0; - foreach (KeyValuePair> pair in this) - { - if (Count + pair.Value.Count > index) - return pair.Key; - Count += pair.Value.Count; - } - throw new IndexOutOfRangeException(); - } - - public CloneableList GetAllValues() - { - CloneableList toReturn = new CloneableList(); - foreach (List values in this.Values) - { - foreach (TValue value in values) - toReturn.Add(value); - } - return toReturn; - } -} diff --git a/PrivateWin10/Common/TextHelpers.cs b/PrivateWin10/Common/TextHelpers.cs deleted file mode 100644 index 69517ab..0000000 --- a/PrivateWin10/Common/TextHelpers.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; - - -public class TextHelpers -{ - public static String GetLeft(ref String Line, String sep = " ") - { - int pos = Line.IndexOf(sep); - String ret; - if (pos == -1) - { - ret = Line; - Line = ""; - } - else - { - ret = Line.Substring(0, pos); - Line = Line.Remove(0, pos+1); - } - return ret; - } - - public static String get2nd(String Line, String sep = ":") - { - int pos = Line.IndexOf(sep); - if (pos == -1) - return ""; - return Line.Substring(pos + 1).Trim(); - } - - public static String get1st(String Line, String sep = ":") - { - int pos = Line.IndexOf(sep); - if (pos == -1) - return Line; - return Line.Substring(0, pos).Trim(); - } - - public static Tuple Split2(string str, String sep = ":", bool rev = false) - { - int pos = rev ? str.LastIndexOf(sep) : str.IndexOf(sep); - if (pos == -1) - return new Tuple(str.Trim(),""); - return new Tuple(str.Substring(0, pos).Trim(), str.Substring(pos + 1).Trim()); - } - - public static bool CompareWildcard(string str, string find) - { - if (str == null) - return false; - //var like = "^" + Regex.Escape(find).Replace("_", ".").Replace("%", ".*") + "$"; - var like = Regex.Escape(find).Replace("_", ".").Replace("\\*", ".*"); - return Regex.IsMatch(str, like, RegexOptions.IgnoreCase); - } - - public static List TokenizeStr(string input) - { - List output = new List(); - foreach (string str in Regex.Matches(input, @"[\""].+?[\""]|[^ ]+").Cast().Select(m => m.Value).ToList()) - { - if (str.Length > 2 && str.ElementAt(0) == '"') - output.Add(str.Substring(1, str.Length - 2)); - else - output.Add(str); - } - return output; - } - - public static List SplitStr(string str, string sep, bool bKeepEmpty = false) - { - List strList = new List(); - String[] spearator = { sep }; - foreach (var curStr in str.Split(spearator, StringSplitOptions.None)) - { - var tmpStr = curStr.Trim(); - if (tmpStr.Length > 0 || bKeepEmpty) - strList.Add(tmpStr); - } - return strList; - } -} - diff --git a/PrivateWin10/Common/WinConsole.cs b/PrivateWin10/Common/WinConsole.cs deleted file mode 100644 index f9a5bb3..0000000 --- a/PrivateWin10/Common/WinConsole.cs +++ /dev/null @@ -1,100 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Runtime.InteropServices; -using System.IO; -using Microsoft.Win32.SafeHandles; - - -static class WinConsole -{ - static public bool Initialize(bool alwaysCreateNewConsole = true) - { - if (AttachConsole(ATTACH_PARRENT) != 0) - return true; - if (!alwaysCreateNewConsole) - return false; - if(AllocConsole() != 0) - { - InitializeOutStream(); - InitializeInStream(); - return true; - } - return false; - } - - private static void InitializeOutStream() - { - var fs = CreateFileStream("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, FileAccess.Write); - if (fs != null) - { - var writer = new StreamWriter(fs) { AutoFlush = true }; - Console.SetOut(writer); - Console.SetError(writer); - } - } - - private static void InitializeInStream() - { - var fs = CreateFileStream("CONIN$", GENERIC_READ, FILE_SHARE_READ, FileAccess.Read); - if (fs != null) - { - Console.SetIn(new StreamReader(fs)); - } - } - - private static FileStream CreateFileStream(string name, uint win32DesiredAccess, uint win32ShareMode, FileAccess dotNetFileAccess) - { - var file = new SafeFileHandle(CreateFileW(name, win32DesiredAccess, win32ShareMode, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero), true); - if (!file.IsInvalid) - { - var fs = new FileStream(file, dotNetFileAccess); - return fs; - } - return null; - } - - #region Win API Functions and Constants - [DllImport("kernel32.dll", - EntryPoint = "AllocConsole", - SetLastError = true, - CharSet = CharSet.Auto, - CallingConvention = CallingConvention.StdCall)] - private static extern int AllocConsole(); - - [DllImport("kernel32.dll", - EntryPoint = "AttachConsole", - SetLastError = true, - CharSet = CharSet.Auto, - CallingConvention = CallingConvention.StdCall)] - private static extern UInt32 AttachConsole(UInt32 dwProcessId); - - [DllImport("kernel32.dll", - EntryPoint = "CreateFileW", - SetLastError = true, - CharSet = CharSet.Auto, - CallingConvention = CallingConvention.StdCall)] - private static extern IntPtr CreateFileW( - string lpFileName, - UInt32 dwDesiredAccess, - UInt32 dwShareMode, - IntPtr lpSecurityAttributes, - UInt32 dwCreationDisposition, - UInt32 dwFlagsAndAttributes, - IntPtr hTemplateFile - ); - - private const UInt32 GENERIC_WRITE = 0x40000000; - private const UInt32 GENERIC_READ = 0x80000000; - private const UInt32 FILE_SHARE_READ = 0x00000001; - private const UInt32 FILE_SHARE_WRITE = 0x00000002; - private const UInt32 OPEN_EXISTING = 0x00000003; - private const UInt32 FILE_ATTRIBUTE_NORMAL = 0x80; - private const UInt32 ERROR_ACCESS_DENIED = 5; - - private const UInt32 ATTACH_PARRENT = 0xFFFFFFFF; - - #endregion -} diff --git a/PrivateWin10/Common/WpfFunc.cs b/PrivateWin10/Common/WpfFunc.cs deleted file mode 100644 index 3f2f946..0000000 --- a/PrivateWin10/Common/WpfFunc.cs +++ /dev/null @@ -1,286 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Controls.Ribbon; -using System.Windows.Media; -using PrivateWin10; - -public class WpfFunc -{ - public class ViewModelHelper : INotifyPropertyChanged//, IDisposable - { - /*public void Dispose() - { - }*/ - - protected void SetProperty(string Name, T newValue, ref T curValue) - { - if (Equals(newValue, curValue)) - return; - curValue = newValue; - RaisePropertyChanged(Name); - } - - protected void SetPropertyCmb(string Name, ContentControl newValue, ref ContentControl curValue, ref string curText) - { - SetProperty(Name, newValue, ref curValue); - if (curValue != null) - curText = curValue.Content.ToString(); - } - - - #region INotifyPropertyChanged Members - - public event PropertyChangedEventHandler PropertyChanged; - - /// - /// Raises the PropertyChanged event if needed. - /// - /// The name of the property that changed. - protected virtual void RaisePropertyChanged(string propertyName) - { - if (PropertyChanged != null) - { - PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); - } - } - - #endregion - } - - static public ContentControl CmbPick(ComboBox box, string tag) - { - if (tag == null) - return null; - for (int i = 0; i < box.Items.Count; i++) - { - ContentControl item = (box.Items[i] as ContentControl); - if (item.Tag != null && item.Tag.ToString().Equals(tag)) - return item; - } - return null; - } - - static public bool CmbSelect(ComboBox box, string tag) - { - ContentControl item = CmbPick(box, tag); - if (item != null) - { - box.SelectedItem = item; - return true; - } - return false; - - } - - static public RibbonGalleryItem CmbPick(RibbonGalleryCategory cat, string tag) - { - if (tag == null) - return null; - for (int i = 0; i < cat.Items.Count; i++) - { - RibbonGalleryItem item = (cat.Items[i] as RibbonGalleryItem); - if (item.Tag != null && item.Tag.ToString().Equals(tag)) - return item; - } - return null; - } - - static public bool CmbSelect(RibbonGallery gal, string tag) - { - ContentControl item = CmbPick(gal.Items[0] as RibbonGalleryCategory, tag); - if (item != null) - { - gal.SelectedItem = item; - return true; - } - return false; - } - - static public ComboBoxItem CmbAdd(ComboBox box, string text, object tag) - { - var item = new ComboBoxItem() { Content = text, Tag = tag }; - box.Items.Add(item); - return item; - } - - static public void CmbAdd(RibbonGallery gal, string text, object tag) - { - (gal.Items[0] as RibbonGalleryCategory).Items.Add(new RibbonGalleryItem { Content = text, Tag = tag }); - } - - public static void StoreWnd(Window wnd, string name) - { - App.SetConfig("GUI", name + "WndPos", wnd.Left + ":" + wnd.Top); - App.SetConfig("GUI", name + "WndSize", wnd.Width + ":" + wnd.Height); - } - - public static bool LoadWnd(Window wnd, string name) - { - Rectangle rect = new Rectangle(); - - string wndPos = App.GetConfig("GUI", name + "WndPos", null); - if (wndPos == null || wndPos.Length == 0) - return false; - - var LT = TextHelpers.Split2(wndPos, ":"); - rect.X = MiscFunc.parseInt(LT.Item1); - rect.Y = MiscFunc.parseInt(LT.Item2); - - string wndSize = App.GetConfig("GUI", name + "WndSize", null); - if (wndSize != null || wndSize.Length == 0) - { - var WH = TextHelpers.Split2(wndSize, ":"); - rect.Width = MiscFunc.parseInt(WH.Item1); - rect.Height = MiscFunc.parseInt(WH.Item2); - } - else - { - rect.Width = (int)wnd.Width; - rect.Height = (int)wnd.Height; - } - - if (!MiscFunc.IsOnScreen(rect)) - return false; - - wnd.Left = rect.Left; - wnd.Top = rect.Top; - wnd.Width = rect.Width; - wnd.Height = rect.Height; - - return true; - } - - public static List SplitAndValidate(string Values, ref bool? duplicate) - { - if (Values == null) - return null; - List ValueList = new List(); - foreach (string Value in Values.Split(',')) - { - string temp = Value.Trim(); - if (duplicate != null && ValueList.Contains(temp)) - { - duplicate = true; - return null; - } - ValueList.Add(temp); - } - if (ValueList.Count == 0) - return null; - return ValueList; - } - - public static T FindChild(DependencyObject parentObj) where T : DependencyObject - { - if (parentObj == null) - return null; - - try - { - if (parentObj is T) - return parentObj as T; - - for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(parentObj); i++) - { - T childObj = FindChild(System.Windows.Media.VisualTreeHelper.GetChild(parentObj, i)); - if (childObj != null) - return childObj; - } - } - catch (Exception err) - { - AppLog.Exception(err); - } - return null; - } - - public static MenuItem AddMenu(ItemsControl menu, string label, RoutedEventHandler handler, object icon = null, object Tag = null) - { - var item = new MenuItem() { Header = label, Tag = Tag }; - if(handler != null) - item.Click += handler; - if (icon != null) - item.Icon = new System.Windows.Controls.Image() { Source = icon as ImageSource }; - menu.Items.Add(item); - return item; - } - - private void DumpVisualTree(DependencyObject parent, int level) - { - string typeName = parent.GetType().Name; - string name = (string)(parent.GetValue(FrameworkElement.NameProperty) ?? ""); - - Console.WriteLine(string.Format("{0}: {1}", typeName, name)); - - if (parent == null) return; - - for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) - { - DependencyObject child = VisualTreeHelper.GetChild(parent, i); - DumpVisualTree(child, level + 1); - } - } - - private void DumpLogicalTree(object parent, int level) - { - string typeName = parent.GetType().Name; - string name = null; - DependencyObject doParent = parent as DependencyObject; - // Not everything in the logical tree is a dependency object - if (doParent != null) - { - name = (string)(doParent.GetValue(FrameworkElement.NameProperty) ?? ""); - } - else - { - name = parent.ToString(); - } - - Console.WriteLine(string.Format("{0}: {1}", typeName, name)); - - if (doParent == null) return; - - foreach (object child in LogicalTreeHelper.GetChildren(doParent)) - { - DumpLogicalTree(child, level + 1); - } - } -} - -public static class DependencyObjectExtensions -{ - private static readonly PropertyInfo InheritanceContextProp = typeof(DependencyObject).GetProperty("InheritanceContext", BindingFlags.NonPublic | BindingFlags.Instance); - - public static IEnumerable GetParents(this DependencyObject child) - { - while (child != null) - { - var parent = LogicalTreeHelper.GetParent(child); - if (parent == null) - { - if (child is FrameworkElement) - { - parent = VisualTreeHelper.GetParent(child); - } - if (parent == null && child is ContentElement) - { - parent = ContentOperations.GetParent((ContentElement)child); - } - if (parent == null) - { - parent = InheritanceContextProp.GetValue(child, null) as DependencyObject; - } - } - child = parent; - yield return parent; - } - } -} \ No newline at end of file diff --git a/PrivateWin10/Controls/AddressControl.xaml.cs b/PrivateWin10/Controls/AddressControl.xaml.cs index 2140138..b3f3fa6 100644 --- a/PrivateWin10/Controls/AddressControl.xaml.cs +++ b/PrivateWin10/Controls/AddressControl.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/ControlList.cs b/PrivateWin10/Controls/ControlList.cs new file mode 100644 index 0000000..e734527 --- /dev/null +++ b/PrivateWin10/Controls/ControlList.cs @@ -0,0 +1,211 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; + +namespace PrivateWin10 +{ + public interface IControlItem + { + event RoutedEventHandler Click; + + void SetFocus(bool set = true); + //bool GetFocus(); + + void DoUpdate(U value); + } + + public class ControlList where T: UserControl, IControlItem + { + public SortedDictionary Items = new SortedDictionary(); + public List SelectedItems = new List(); + public event EventHandler SelectionChanged; + public bool SingleSellection = false; + + ScrollViewer ItemScroll; + Grid ItemGrid; + Func ItemFactory; + Func GetGuid; + Action> Sorter; + Func Filter; + + public ControlList(ScrollViewer itemScroll, FuncitemFactory, Func getGuid, Action> sorter = null, Func filter = null) + { + ItemScroll = itemScroll; + ItemScroll.PreviewKeyDown += process_KeyEventHandler; + + ItemGrid = (Grid)ItemScroll.Content; + + ItemFactory = itemFactory; + GetGuid = getGuid; + Sorter = sorter; + Filter = filter; + } + + public void UpdateItems(List newItems) + { + SortedDictionary OldItems = new SortedDictionary(Items); + + if (newItems != null) + { + foreach (U value in newItems) + { + T item; + if (Items.TryGetValue(GetGuid(value), out item)) + { + item.DoUpdate(value); + OldItems.Remove(GetGuid(value)); + } + else + item = AddItem(value); + } + } + + foreach (var guid in OldItems.Keys) + { + Items.Remove(guid); + T item; + if (OldItems.TryGetValue(guid, out item)) + ItemGrid.Children.Remove(item); + } + + SortAndFitlerList(); + } + + public T AddItem(U value) + { + T item = ItemFactory(value); + Items.Add(GetGuid(value), item); + //item.Tag = process; + item.VerticalAlignment = VerticalAlignment.Top; + item.HorizontalAlignment = HorizontalAlignment.Stretch; + item.Margin = new Thickness(1, 1, 1, 1); + //item.MouseDown += new MouseButtonEventHandler(process_Click); + item.Click += new RoutedEventHandler(process_Click); + + ItemGrid.Children.Add(item); + + return item; + } + + public void UpdateItem(U value) + { + T item; + if (Items.TryGetValue(GetGuid(value), out item)) + item.DoUpdate(value); + else + item = AddItem(value); + } + + public void SortAndFitlerList() + { + List OrderList = Items.Values.ToList(); + + Sorter?.Invoke(OrderList); + + for (int i = 0; i < OrderList.Count; i++) + { + if (i >= ItemGrid.RowDefinitions.Count) + ItemGrid.RowDefinitions.Add(new RowDefinition()); + RowDefinition row = ItemGrid.RowDefinitions[i]; + //ProcessControl item = tmp.Item2; + T item = OrderList[i]; + if (row.Height.Value != item.Height) + row.Height = GridLength.Auto; //new GridLength(item.Height + 2); + Grid.SetRow(item, i); + + item.Visibility = Filter?.Invoke(item) == true ? Visibility.Collapsed : Visibility.Visible; + } + + while (OrderList.Count < ItemGrid.RowDefinitions.Count) + ItemGrid.RowDefinitions.RemoveAt(OrderList.Count); + } + + void process_Click(object sender, RoutedEventArgs e) + { + T curItem = (T)sender; + + if (!SingleSellection && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) + { + if (SelectedItems.Contains(curItem)) + { + curItem.SetFocus(false); + SelectedItems.Remove(curItem); + } + else + { + curItem.SetFocus(true); + SelectedItems.Add(curItem); + } + } + else + { + foreach (T cur in SelectedItems) + cur.SetFocus(false); + SelectedItems.Clear(); + SelectedItems.Add(curItem); + curItem.SetFocus(true); + } + + SelectionChanged?.Invoke(this, new EventArgs()); + } + + void process_KeyEventHandler(object sender, KeyEventArgs e) + { + if ((e.Key == Key.Up || e.Key == Key.Down) )//&& !((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)) + { + T curItem = default(T); + if (SelectedItems.Count > 0) + { + foreach (T cur in SelectedItems) + cur.SetFocus(false); + curItem = SelectedItems[SelectedItems.Count - 1]; + SelectedItems.Clear(); + } + + e.Handled = true; + int curRow = Grid.GetRow(curItem); + if (e.Key == Key.Up) + { + while (curRow > 0) + { + curRow--; + T cut = ItemGrid.Children.Cast().First((c) => Grid.GetRow(c) == curRow); + if (cut.Visibility == Visibility.Visible) + { + curItem = cut; + ItemScroll.ScrollToVerticalOffset(ItemScroll.VerticalOffset - (curItem.ActualHeight + 2)); + break; + } + } + } + else if (e.Key == Key.Down) + { + while (curRow < ItemGrid.Children.Count - 1) + { + curRow++; + T curProg = ItemGrid.Children.Cast().First((c) => Grid.GetRow(c) == curRow); + if (curProg.Visibility == Visibility.Visible) + { + curItem = curProg; + ItemScroll.ScrollToVerticalOffset(ItemScroll.VerticalOffset + (curItem.ActualHeight + 2)); + break; + } + } + } + + if (curItem == null) + return; + + curItem.SetFocus(true); + SelectedItems.Add(curItem); + + SelectionChanged?.Invoke(this, new EventArgs()); + } + } + } +} diff --git a/PrivateWin10/Controls/Converters.cs b/PrivateWin10/Controls/Converters.cs index 10ff429..2d89fce 100644 --- a/PrivateWin10/Controls/Converters.cs +++ b/PrivateWin10/Controls/Converters.cs @@ -1,4 +1,5 @@ -using System; +using MiscHelpers; +using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; diff --git a/PrivateWin10/Controls/DnsBlockListsControl.xaml b/PrivateWin10/Controls/Dns/DnsBlockListsControl.xaml similarity index 100% rename from PrivateWin10/Controls/DnsBlockListsControl.xaml rename to PrivateWin10/Controls/Dns/DnsBlockListsControl.xaml diff --git a/PrivateWin10/Controls/DnsBlockListsControl.xaml.cs b/PrivateWin10/Controls/Dns/DnsBlockListsControl.xaml.cs similarity index 96% rename from PrivateWin10/Controls/DnsBlockListsControl.xaml.cs rename to PrivateWin10/Controls/Dns/DnsBlockListsControl.xaml.cs index cde0c95..4c156c4 100644 --- a/PrivateWin10/Controls/DnsBlockListsControl.xaml.cs +++ b/PrivateWin10/Controls/Dns/DnsBlockListsControl.xaml.cs @@ -14,6 +14,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/DnsFilterListControl.xaml b/PrivateWin10/Controls/Dns/DnsFilterListControl.xaml similarity index 100% rename from PrivateWin10/Controls/DnsFilterListControl.xaml rename to PrivateWin10/Controls/Dns/DnsFilterListControl.xaml diff --git a/PrivateWin10/Controls/DnsFilterListControl.xaml.cs b/PrivateWin10/Controls/Dns/DnsFilterListControl.xaml.cs similarity index 97% rename from PrivateWin10/Controls/DnsFilterListControl.xaml.cs rename to PrivateWin10/Controls/Dns/DnsFilterListControl.xaml.cs index 28f806c..8cd7c4c 100644 --- a/PrivateWin10/Controls/DnsFilterListControl.xaml.cs +++ b/PrivateWin10/Controls/Dns/DnsFilterListControl.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/DnsLogList.xaml b/PrivateWin10/Controls/Dns/DnsLogList.xaml similarity index 100% rename from PrivateWin10/Controls/DnsLogList.xaml rename to PrivateWin10/Controls/Dns/DnsLogList.xaml diff --git a/PrivateWin10/Controls/DnsLogList.xaml.cs b/PrivateWin10/Controls/Dns/DnsLogList.xaml.cs similarity index 97% rename from PrivateWin10/Controls/DnsLogList.xaml.cs rename to PrivateWin10/Controls/Dns/DnsLogList.xaml.cs index 9b55d98..3ce4967 100644 --- a/PrivateWin10/Controls/DnsLogList.xaml.cs +++ b/PrivateWin10/Controls/Dns/DnsLogList.xaml.cs @@ -16,6 +16,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/DnsQueryLogControl.xaml b/PrivateWin10/Controls/Dns/DnsQueryLogControl.xaml similarity index 100% rename from PrivateWin10/Controls/DnsQueryLogControl.xaml rename to PrivateWin10/Controls/Dns/DnsQueryLogControl.xaml diff --git a/PrivateWin10/Controls/DnsQueryLogControl.xaml.cs b/PrivateWin10/Controls/Dns/DnsQueryLogControl.xaml.cs similarity index 96% rename from PrivateWin10/Controls/DnsQueryLogControl.xaml.cs rename to PrivateWin10/Controls/Dns/DnsQueryLogControl.xaml.cs index 9f5a842..a1a85cd 100644 --- a/PrivateWin10/Controls/DnsQueryLogControl.xaml.cs +++ b/PrivateWin10/Controls/Dns/DnsQueryLogControl.xaml.cs @@ -14,6 +14,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/FirewallLogList.xaml.cs b/PrivateWin10/Controls/FirewallLogList.xaml.cs index 1f6522e..a68378f 100644 --- a/PrivateWin10/Controls/FirewallLogList.xaml.cs +++ b/PrivateWin10/Controls/FirewallLogList.xaml.cs @@ -16,6 +16,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/FirewallRuleList.xaml.cs b/PrivateWin10/Controls/FirewallRuleList.xaml.cs index edf12d7..f26167e 100644 --- a/PrivateWin10/Controls/FirewallRuleList.xaml.cs +++ b/PrivateWin10/Controls/FirewallRuleList.xaml.cs @@ -17,6 +17,7 @@ using System.Windows.Shapes; using PrivateWin10.Pages; using PrivateWin10.Windows; +using MiscHelpers; namespace PrivateWin10.Controls { diff --git a/PrivateWin10/Controls/NetworkSocketList.xaml.cs b/PrivateWin10/Controls/NetworkSocketList.xaml.cs index 802fff8..2bcc0fe 100644 --- a/PrivateWin10/Controls/NetworkSocketList.xaml.cs +++ b/PrivateWin10/Controls/NetworkSocketList.xaml.cs @@ -16,6 +16,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using MiscHelpers; namespace PrivateWin10.Controls { @@ -329,13 +330,13 @@ public string Access { get { return string.Join(",", profiles.ToArray().Reverse()); } } - public UInt64 Upload { get { return sock.Stats.UploadRate.ByteRate; } } + public UInt64 Upload { get { return sock.UploadRate; } } - public UInt64 Download { get { return sock.Stats.UploadRate.ByteRate; } } + public UInt64 Download { get { return sock.DownloadRate; } } - public UInt64 Uploaded { get { return sock.Stats.SentBytes; } } + public UInt64 Uploaded { get { return sock.SentBytes; } } - public UInt64 Downloaded { get { return sock.Stats.ReceivedBytes; } } + public UInt64 Downloaded { get { return sock.ReceivedBytes; } } void UpdateValue(ref T value, T new_value, string Name) { @@ -357,15 +358,10 @@ public void Update(NetworkSocket new_sock) if(sock.Update(new_sock)) NotifyPropertyChanged("DestAddress"); - if (!sock.Stats.Equals(new_sock.Stats)) - { - sock.Stats = new_sock.Stats; - NotifyPropertyChanged("Upload"); - NotifyPropertyChanged("Download"); - NotifyPropertyChanged("Uploaded"); - NotifyPropertyChanged("Downloaded"); - // todo: other - } + UpdateValue(ref sock.UploadRate, new_sock.UploadRate, "Upload"); + UpdateValue(ref sock.DownloadRate, new_sock.DownloadRate, "Download"); + UpdateValue(ref sock.SentBytes, new_sock.SentBytes, "Uploaded"); + UpdateValue(ref sock.ReceivedBytes, new_sock.ReceivedBytes, "Downloaded"); } #region INotifyPropertyChanged Members diff --git a/PrivateWin10/Controls/ConnectionNotify.xaml b/PrivateWin10/Controls/Notify/ConnectionNotify.xaml similarity index 91% rename from PrivateWin10/Controls/ConnectionNotify.xaml rename to PrivateWin10/Controls/Notify/ConnectionNotify.xaml index de6cd0c..5b8f05a 100644 --- a/PrivateWin10/Controls/ConnectionNotify.xaml +++ b/PrivateWin10/Controls/Notify/ConnectionNotify.xaml @@ -60,12 +60,13 @@ - + + -