Exemplo n.º 1
0
        static string NetShArgumentsForRule(int port, RuleDirections direction, TransportProtocols protocol)
        {
            string strArguments = "";

            if (Functions.OSSupportsAdvancedFirewallInNetSH)
            {
                strArguments += "advfirewall firewall add rule name=" + "\"" + "Remote Potato - " + port.ToString() + " " + direction.ToString() + "\"";
                strArguments += " dir=" + ((direction == RuleDirections.Inbound) ? "in" : "out");
                strArguments += " action=allow";
                strArguments += " protocol=" + ((protocol == TransportProtocols.TCP) ? "TCP" : "UDP");
                strArguments += " localport=" + port.ToString();
            }
            else
            {
                // XP old syntax
                strArguments += "firewall add portopening name=" + "\"" + "Remote Potato - " + port.ToString() + " " + direction.ToString() + "\"";
                strArguments += " protocol=" + ((protocol == TransportProtocols.TCP) ? "TCP" : "UDP");
                strArguments += " port=" + port.ToString();
            }

            return(strArguments);
        }
Exemplo n.º 2
0
 public Rule(Map parent, int pos, RuleTypes type, RuleDirections dir, string pattern, string replacement)
     : this(parent, pos)
 {
     this.Type = type;
     this.Direction = dir;
     this.Pattern = pattern;
     this.Replacement = replacement;
 }
Exemplo n.º 3
0
 public Rule AddRule(RuleTypes type, RuleDirections dir, string pattern, string replacement)
 {
     Rule r = new Rule(this, this.NextRulePos, type, dir, pattern, replacement);
     rules.Add(this.NextRulePos, r);
     this.NextRulePos ++;
     return r;
 }
Exemplo n.º 4
0
        static bool AddFirewallRule(int port, RuleDirections direction, TransportProtocols protocol)
        {
            System.Diagnostics.Process process = null;
            System.Diagnostics.ProcessStartInfo processStartInfo;

            processStartInfo = new System.Diagnostics.ProcessStartInfo();
            processStartInfo.FileName = "netsh.exe";
            processStartInfo.Arguments = NetShArgumentsForRule(port, direction, protocol);
            if (Functions.OSSupportsAdvancedFirewallInNetSH)
                processStartInfo.Verb = "runas";
            processStartInfo.UseShellExecute = true;
            //processStartInfo.UseShellExecute = false;
            //processStartInfo.CreateNoWindow = true;
            //processStartInfo.RedirectStandardOutput = true;
            processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            try
            {
                process = System.Diagnostics.Process.Start(processStartInfo);

                DateTime begunProcess = DateTime.Now;
                bool processTimeout = false;
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(200);
                    TimeSpan elapsed = (DateTime.Now - begunProcess);
                    if (elapsed.TotalSeconds > 25)
                    {
                        processTimeout = true;
                        break;
                    }
                }

                if (processTimeout)
                {
                    Functions.WriteLineToLogFile("FirewallHelper: NOT OK - TimeOut");
                    return false;
                }
                else if (process.ExitCode != 0)
                {
                    string processOutput = process.StandardOutput.ReadToEnd();

                    Functions.WriteLineToLogFile("FirewallHelper: NOT OK (error code " + process.ExitCode.ToString() );
                    Functions.WriteLineToLogFile(processOutput);

                    return false;
                }
            }
            catch (Exception ex)
            {
                Functions.WriteLineToLogFile("FirewallHelper: Exception trying to add a firewall rule using netsh.");
                Functions.WriteExceptionToLogFile(ex);
                return false;
            }
            finally
            {
                if (process != null)
                {
                    process.Dispose();
                }
            }

            Functions.WriteLineToLogFile("FirewallHelper: Rule Added 0 OK");
            return true;
        }
Exemplo n.º 5
0
        static string NetShArgumentsForRule(int port, RuleDirections direction, TransportProtocols protocol)
        {
            string strArguments = "";

            if (Functions.OSSupportsAdvancedFirewallInNetSH)
            {
                strArguments += "advfirewall firewall add rule name=" + "\"" + "Remote Potato - " + port.ToString() + " " + direction.ToString() + "\"";
                strArguments += " dir=" + ((direction == RuleDirections.Inbound) ? "in" : "out");
                strArguments += " action=allow";
                strArguments += " protocol=" + ((protocol == TransportProtocols.TCP) ? "TCP" : "UDP");
                strArguments += " localport=" + port.ToString();
            }
            else
            {
                // XP old syntax
                strArguments += "firewall add portopening name=" + "\"" + "Remote Potato - " + port.ToString() + " " + direction.ToString() + "\"";
                strArguments += " protocol=" + ((protocol == TransportProtocols.TCP) ? "TCP" : "UDP");
                strArguments += " port=" + port.ToString();
            }

            return strArguments;
        }
Exemplo n.º 6
0
        static bool AddFirewallRule(int port, RuleDirections direction, TransportProtocols protocol)
        {
            System.Diagnostics.Process          process = null;
            System.Diagnostics.ProcessStartInfo processStartInfo;

            processStartInfo           = new System.Diagnostics.ProcessStartInfo();
            processStartInfo.FileName  = "netsh.exe";
            processStartInfo.Arguments = NetShArgumentsForRule(port, direction, protocol);
            if (Functions.OSSupportsAdvancedFirewallInNetSH)
            {
                processStartInfo.Verb = "runas";
            }
            processStartInfo.UseShellExecute = true;
            //processStartInfo.UseShellExecute = false;
            //processStartInfo.CreateNoWindow = true;
            //processStartInfo.RedirectStandardOutput = true;
            processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

            try
            {
                process = System.Diagnostics.Process.Start(processStartInfo);

                DateTime begunProcess   = DateTime.Now;
                bool     processTimeout = false;
                while (!process.HasExited)
                {
                    System.Threading.Thread.Sleep(200);
                    TimeSpan elapsed = (DateTime.Now - begunProcess);
                    if (elapsed.TotalSeconds > 25)
                    {
                        processTimeout = true;
                        break;
                    }
                }

                if (processTimeout)
                {
                    Functions.WriteLineToLogFile("FirewallHelper: NOT OK - TimeOut");
                    return(false);
                }
                else if (process.ExitCode != 0)
                {
                    string processOutput = process.StandardOutput.ReadToEnd();

                    Functions.WriteLineToLogFile("FirewallHelper: NOT OK (error code " + process.ExitCode.ToString());
                    Functions.WriteLineToLogFile(processOutput);

                    return(false);
                }
            }
            catch (Exception ex)
            {
                Functions.WriteLineToLogFile("FirewallHelper: Exception trying to add a firewall rule using netsh.");
                Functions.WriteExceptionToLogFile(ex);
                return(false);
            }
            finally
            {
                if (process != null)
                {
                    process.Dispose();
                }
            }

            Functions.WriteLineToLogFile("FirewallHelper: Rule Added 0 OK");
            return(true);
        }