Detect if windows firewall is blocking my program
Asked Answered
B

4

11

I have an application that communicates with a NetApp device through their api. With the windows firewall on, the api commands will fail. With the firewall off, the api commands work. I don't receive any message like "Windows Firewall is blocking this program".

Looking through the documentation I believe that I found the TCP ports that need to be open for the api commands to work. How can I programatically detect if the ports are blocked so I can display a message to the user on the potential problem?

Beak answered 4/8, 2011 at 14:27 Comment(0)
S
11

The firewall manager exposes itself via COM and implements an IsPortAllowed.

Sinaloa answered 4/8, 2011 at 14:40 Comment(2)
This only works for incoming UDP ports - adding an outgoing rule to block traffic goes undetected by this function. Additionally, if incoming UDP port is reported as blocked then it may still be possible to hole-punch the firewall.Pronounced
Any idea how to detect if outgoing UDP is blocked?Pronounced
R
8

You can do it like this I think: give it a try: Change 1433 for the port you want to check.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace CheckPortStatus
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                TcpClient tcp = new TcpClient();
                tcp.Connect("localhost", Convert.ToInt16(1433));
                Console.WriteLine("online");
            }
            catch (Exception ex)
            {
                Console.WriteLine("offline");
                Console.WriteLine(ex.Message);
            }
        }
    }
}

Also, to see which ports are available in your machine run:

C:>netstat -an |find /i "listening"

TCP 0.0.0.0:25 0.0.0.0:0 LISTENING

TCP 0.0.0.0:80 0.0.0.0:0 LISTENING

TCP 0.0.0.0:135 0.0.0.0:0 LISTENING

Riordan answered 4/8, 2011 at 14:40 Comment(2)
This tells you nothing about Windows Firewall. There's a million and one other reasons why this could fail.Hambrick
Do firewalls usually block access to localhost?Elery
C
2

To detect if the ports are blocked - on Win7 you can view the Window Firewall logs by opening Windows Firewall - click Advanced Settings on the left-side and then open the Monitoring branch.

Note on the Monitoring tab in the Logging Settings section there is an option to log to file which on my Win7 PC is %systemroot%\system32\LogFiles\Firewall\pfirewall.log - you could just parse this file. I have researched in the past and there are utilities out there to do this for you, however, at the end of the day it's just a standard format log file.

Census answered 4/8, 2011 at 14:33 Comment(2)
Sorry, I clarified the question, but how can I do this programatically?Beak
Why -1? My answer preceded the edit to do this programmatically.Census
F
0

I doubt that the firewall will mention that it's blocking the application, otherwise intruder can have a information on what's preventing him to access the system :-).

Usually, firewalls logs attempts to connect from and to the computer, successful or not, you can check it.

Update*

you may try Acknowledgement in the network. If you received none for certain amount of time, then you can safely say that there's a problem in the connection.

Foeticide answered 4/8, 2011 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.