About C # operating the firewall to prevent program connection,

Source: Internet
Author: User
Tags rekey

About C # operating the firewall to prevent program connection,

// Enable the Service and open the firewall public void OpenFileWall () {// 1. judge whether the current system is XP or Win7 RegistryKey rk = Registry. localMachine. openSubKey (@ "Software \ Microsoft \ Windows NT \ CurrentVersion"); var VersionName = rk. getValue ("ProductName "). toString (); rk. close (); RegistryKey key; // obtain the Firewall Service name var ServicerName = ""; if (VersionName. contains ("XP") {ServicerName = "SharedAccess"; key = Registry. localMachine. openSubKey (@ "SYSTEM \\ CurrentControlSet \ Services \ SharedAccess ", true);} else {ServicerName =" MpsSvc "; key = Registry. localMachine. openSubKey (@ "SYSTEM \ CurrentControlSet \ Services \ MpsSvc", true);} // 2. checks whether the firewall Startup type is disabled. If it is disabled, sets it to automatic var StartIndex = key. getValue ("Start "). toString (); if (StartIndex = "4") {ProcessStartInfo objProInfo = new ProcessStartInfo (); objProInfo. fileName = "cmd.exe"; objProInfo. createNoWindo W = false; objProInfo. windowStyle = ProcessWindowStyle. hidden; objProInfo. arguments = "/c SC config" + ServicerName + "start =" + "auto"; Process. start (objProInfo); // After the thread is suspended for 1 s, the service System is started. threading. thread. sleep (1000);} key. close (); // 3. determine whether the Firewall service starts ServiceController SC = new ServiceController (ServicerName); if (SC. status. equals (ServiceControllerStatus. stopped) | (SC. status. equals (ServiceContr OllerStatus. stopPending) {SC. start (); // suspend the thread for 1 s and enable firewall System. threading. thread. sleep (1000);} // 4. enable Firewall if (VersionName. contains ("XP") {RegistryKey rekey = Registry. localMachine. openSubKey (@ "SYSTEM \ CurrentControlSet \ Services \ SharedAccess \ Parameters \ FirewallPolicy \ StandardProfile", true); var Enablefilewall = rekey. getValue ("EnableFirewall "). toString (); if (Enablefilewall = "0") {rekey. SetValue ("EnableFirewall", 1);} rekey. close ();} else {inetfw1_y2 firewallPolicy = (inetfw1_y2) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. fw1_y2 "); // enable or disable <Advanced Security Windows Firewall>-firewall firewallPolicy In the VPC configuration file. set_FirewallEnabled (NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE, true); // enable or disable <Windows Firewall for Advanced Security>-firewall for public configuration files. set_FirewallEnabled (NET_FW_PROFILE_TYPE2_.NET _ FW_PROFILE2_PUBLIC, true) ;}// when the detection does not meet the conditions, access is blocked. Only the public void UnAllowOpenFW () of the program is allowed () {// determine whether the system belongs to xp or win7 using (RegistryKey rk = Registry. localMachine. openSubKey (@ "Software \ Microsoft \ Windows NT \ CurrentVersion") {var VersionName = rk. getValue ("ProductName "). toString (); if (VersionName. contains ("XP") {// 1. create an instance of the authentication program class INetFwAuthorizedApplication Fwapp = (INetFwAuthorizedApplication) Activator. CreateInstance (Type. getTypeFromProgID ("HNetCfg. fwAuthorizedApplication "); Fwapp. name = "360 secure browser"; // Fwapp. name = Application. productName; Fwapp. processImageFileName = "C: \ Documents ents and Settings \ Administrator \ Application Data \ 360se6 \ Application \ 360se.exe"; // Fwapp. processImageFileName = Application. executablePath; // whether to enable Fwapp. enabled = true; // 2. create an instance of the firewall management class and add the program to the firewall exception INetFwMgr net FwMgr = (INetFwMgr) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. fwMgr "); netFwMgr. localPolicy. currentProfile. authorizedApplications. add (Fwapp);} else {// 1. create an instance to prevent all outbound connections from inetfw1_y2 firewallPolicy = (inetfw1_y2) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. fw1_y2 "); // enable or disable the <Advanced Security Windows Firewall>-the outbound connection firewallPolicy of the VPC configuration file. set_DefaultOutboundAction (NET_FW_PROFILE_TY PE2_.NET_FW_PROFILE2_PRIVATE, NET_FW_ACTION_.NET_FW_ACTION_BLOCK); // enable or disable the <Advanced Security Windows Firewall>-Public configuration file's outbound connection firewallPolicy. set_DefaultOutboundAction (NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_BLOCK); // 2. create an outbound rule for the program and only run the connection of the program. INetFwRule2 OutBoundRule = (INetFwRule2) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. FWRule "); OutBoundRule. name = "360 security Browser"; // OutBoundRule. name = Application. productName; OutBoundRule. description = "360 secure browser"; // program path OutBoundRule. applicationName = "C :\\ Users \ Administrator \ AppData \ Roaming \ 360se6 \ Application \ 360se.exe"; // OutBoundRule. applicationName = Application. executablePath; OutBoundRule. direction = NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT; OutBoundRule. action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW; OutBoundRule. enabled = true; // 3. add the outbound rule firewallPolicy. rules. add (OutBoundRule) ;}}// check whether the conditions are met. Enable public void AllowOpenFW () {// determine whether the system belongs to xp or win7 using (RegistryKey rk = Registry. localMachine. openSubKey (@ "Software \ Microsoft \ Windows NT \ CurrentVersion") {var VersionName = rk. getValue ("ProductName "). toString (); if (VersionName. contains ("XP ")){

// Method MISSING} else {// 1. Create an instance and allow connections to all programs. Inetfw1_y2 firewallPolicy = (inetfw1_y2) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. fw1_y2 "); // enable or disable the <Advanced Security Windows Firewall>-the outbound connection firewallPolicy of the VPC configuration file. set_DefaultOutboundAction (login, NET_FW_ACTION_.NET_FW_ACTION_ALLOW); // enable or disable the <Advanced Security Windows Firewall>-Public configuration file for outbound connections to firewallPolicy. set_DefaultOutboundAction (NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC, NET_FW_ACTION_.NET_FW_ACTION_ALLOW); // 2. delete the outbound rule of this program. Delete the rule INetFwRule2 OutBoundRule = (INetFwRule2) Activator. createInstance (Type. getTypeFromProgID ("HNetCfg. FWRule "); OutBoundRule. name = "360 security Browser"; // OutBoundRule. name = Application. productName; firewallPolicy. rules. remove (OutBoundRule. name );}}}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.