Bios Password

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Saturday, January 2, 2010

Opening Ports in Windows Firewall from Batch files

Posted on 6:23 PM by Unknown

All of our systems run a single application/service that is auto-(re)-installed from a user login event script.

No biggie.  It’s kinda overkill but it is a critical application that could be deleted accidently.

It does require that we add some Windows Firewall port exceptions for custom IP addresses so it can have a clear shot through the Windows Firewall if the user accidentally or purposefully (say our laptop users) enable the firewall.

All of my own built system images for some time have had the custom port opening rules added in by default.

However, some older systems didn’t deploy with my image and didn’t have the port configured by default.  Since we are not an AD shop, it has meant going, upon request, to the users’ systems, verifying the application/service is running correctly, and then manually going into the Windows Firewall GUI to set the open port rule and custom scope.

Usually it’s not a big deal but sometimes it can be as it is disruptive to an end-user when we drop in and start suddenly adding Firewall port rules/exceptions to the system from the GUI.  Some end users are fairly PC savvy and it can generate some raised eyebrows and questions that add more time to the service job.

Recently I had completed another such assignment and wondered if I could just skip all the drama of having someone watch me set custom Windows Firewall port rules.  Maybe I could just make a silent-running and innocuous batch file that could quietly do all the work for me in the background while I focused on checking other system things.

Sure enough…I could…and it’s pretty easy as well.

GUI-based Windows Firewall Port Exceptions

Some applications and services need to be able to get out through the firewall.  Sometime “mothership” applications need to signal down to the client-side application/service.  Firewalls work to prevent those communications.

However if the communications inbound/outbound are legitimate and mission-critical, you need to open up a hole to talk-through.

Normally opening a hole in your security wall is dangerous as maybe someone unwanted could sneak in.  Windows uses communication “holes” called ports.  Actually I guess they are more like “channels” or frequencies rather than ports (windows) on the side of a ship.  Certain programs and services only talk on certain ports/channels.  Generally the firewall locks these down so the call can’t go through.  However, these can be opened up so that anyone/anything could talk on that port.

Sites like GRC | ShieldsUP! or McAfee’s Test Your Firewall, or SecurityMetrics Free Port Scan or PC Flanks can be used for free to test your system for open ports.

But what if you don’t want’ to leave a port open to everyone, even though you need to?  You can then set one more level of protection on your port by setting a custom rule to only allow traffic of a certain type and/or trusted IP address (or range) to flow through.

Firewalls are great but they can interfere with applications, games, and other communications from trusted programs that by design need to reach the network/Internet. 

  • Windows Firewall may block some programs from communicating over the Internet after you install Windows XP Service Pack 2 – Microsoft Help and Support Article ID: 842242

Luckily Windows XP and higher does allow you to set these kind of special exceptions.  For most folks using the GUI method is simple enough and easy to do.

  • Open a port in Windows Firewall – Microsoft

Unlike an exception, which is only open during the time that it is needed, a port stays open all the time, so be sure to close ports that you don't need anymore.

  1. Open Windows Firewall by clicking the Start button, clicking Control Panel, clicking Security, and then clicking Windows Firewall.

  2. Click Allow a program through Windows Firewall.  If you are prompted for an administrator password or confirmation, type the password or provide confirmation.

  3. Click Add port.

  4. In the Name box, type a name that will help you remember what the port is used for.

  5. In the Port number box, type the port number.

  6. Click TCP or UDP, depending on the protocol.

  7. To change scope for the port, click Change scope, and then click the option that you want to use. ("Scope" refers to the set of computers that can use this port opening.)

  • How to manually open ports in Internet Connection Firewall in Windows XP – Microsoft Help and Support Article ID: 308127

Which works just fine but takes a while to click through, enter the correct values (assuming you still remember them), and isn’t very discreet, particularly if the rule name sounds suspicious.

But a silent-running batch-file now that would be quick, surgical, and endoscopic.

I did find that Gammadyne’s Free DOS Utilities offers a free command-line tool FIREWALL.EXE for adding exceptions to the Windows Firewall but it wasn’t quite as granular as I was looking for.

Luckily, Windows XP (and a more advanced one in Vista/Windows 7) does contain just the command-line tool I needed.

NETSH to the CLI Firewall configuring rescue!

It didn’t take me too much effort to find the NETSH command and the wonderful tricks it could do:

  • Michael Howard’s Web Log : The joy of netsh. – Michael Howard’s Web Log

  • Using Netsh with Windows Firewall – Windows Networking

  • Configuring network settings from command line - LanToolbox.

  • Enable or disable Windows firewall from command prompt – Help Desk Geek

These were great and gave me the basics that I needed to see my goal was possible to accomplish.

Then I found Penn State U’s fantastic page How To Add Programs and Ports to Windows XP SP2 Firewall Exceptions List. Not only did it have some basics, but it also has a unpackable collection of batch-files tailor-made for configuring Windows Firewall rules with these techniques; including both prompted and silent-running batch files with netsh commands.  It provides some great examples to use as starting points.

In the end, the heart of my own custom Windows XP Firewall batch file will contain a line like this:

netsh firewall add portopening TCP <my target port #> OPRule_<port#> ENABLE ALL CUSTOM <ip address #1,ip address #2>

where <my target port #"> is the specific one that our application/service communicates through, and where <ip address #1,ip address #2> are the specific IP addresses used the the “mothership” application that only will be allowed to solicit requests to the local client application through that specific port.

NETSH CLI References

The links above were great but they didn’t really help me understand and add the details like “ALL” which set the Windows Firewall port exception rule for all profiles on the system. Nor did it help me with the “CUSTOM” argument to detail which IP’s I needed to open up when setting the rule.  While the examples found showed how to open up a port number, it didn’t deal with setting the port to use the specific IP ranges that I wanted to only allow.

For those details I had to turn to these wonderful resources.

  • Appendix B: Netsh Command Syntax for the Netsh Firewall Context – Microsoft TechNet.

In my case specifically this portion (though the Appendix is rich with Netsh CLI goodness) is what I was interested in.

…

add portopening

Used to create a port-based exception.

Syntax:

Note Some parts of the following code snippet have been displayed in multiple lines only for better readability. These should be entered in a single line.

add portopening
[ protocol = ] TCP|UDP|ALL    
[ port = ] 1-65535    
[ name = ] name    
[ [ mode = ] ENABLE|DISABLE       
    [ scope = ] ALL|SUBNET|CUSTOM      
    [ addresses = ] addresses      
    [ profile = ] CURRENT|DOMAIN|STANDARD|ALL      
    [ interface = ] name ] 
Adds firewall port configuration. 
Parameters: 
protocol - Port protocol.    
    TCP  - Transmission Control Protocol (TCP).    
    UDP  - User Datagram Protocol (UDP).    
    ALL  - All protocols. 
port - Port number. 
name - Port name. 
mode - Port mode (optional).    
    ENABLE  - Allow through firewall (default).    
    DISABLE - Do not allow through firewall. 
scope - Port scope (optional).    
    ALL    - Allow all traffic through firewall (default).    
    SUBNET - Allow only local network (subnet) traffic through firewall.
    CUSTOM - Allow only specified traffic through firewall. 
addresses - Custom scope addresses (optional). 
profile   - Configuration profile (optional).    
    CURRENT  - Current profile (default).    
    DOMAIN   - Domain profile.    
    STANDARD - Standard profile.    
    ALL      - All profiles. 
interface - Interface name (optional). 
Remarks: 'profile' and 'interface' may not be specified together. 'scope' and 'interface' may not be specified together. 'scope' must be 'CUSTOM' to specify 'addresses'. 
Examples:    
     add portopening TCP 80 MyWebPort    
     add portopening UDP 500 IKE ENABLE ALL    
     add portopening ALL 53 DNS ENABLE CUSTOM 157.60.0.1,172.16.0.0/16,10.0.0.0/255. 0.0.0,LocalSubnet    
     add portopening protocol = TCP port = 80 name = MyWebPort    
     add portopening protocol = UDP port = 500 name = IKE mode = ENABLE scope = ALL    
     add portopening protocol = ALL port = 53 name = DNS mode = ENABLE scope = CUSTOM addresses = 157.60.0.1,172.16.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet

You can also use “netsh set portopening” to modify an existing port rule or “netsh delete portopening” to remove one from a batch-file or command line.

  • Download details: Deploying Windows Firewall Settings for Microsoft Windows XP with Service Pack 2 – This Microsoft white paper “…describes the methods used to deploy Windows Firewall settings in a managed environment.”   I found it to be a helpful reference.

  • Chapter 8 - Locking Up the Ports : Windows Firewall – PDF – I found this amended chapter on the Google.  Had some great tips and examples of Netsh in action in Windows.

  • Microsoft Windows XP - Using Netsh - Windows XP Professional Product Documentation.  Turns out that Netsh can operate like a “nested” command tool, similar to DiskPart.  Run NETSH alone and you go into the Netsh> command level and can then pass arguments accordingly.  Similarly, when using in batch-files above, just pass the entire netsh command string with arguments and it will work just fine.

NETSH in Vista/Windows 7

Netsh is still present in Vista/Windows 7 but advances in the Windows Firewall design have demanded it be expanded to keep with the times.

In XP, you have to call the “netsh firewall” context when placing your add or set or delete portopening commands and arguments (along with all the other firewall-specific supported netsh commands). 

In Vista and Windows 7 that changes to now require "netsh advfirewall firewall" context to control Windows Firewall behavior.

  • How to use the "netsh advfirewall firewall" context instead of the "netsh firewall" context to control Windows Firewall behavior in Windows Server 2008 and in Windows Vista – Microsoft Help and Support Article ID: 947709.

    The netsh advfirewall firewall command-line context is available in Windows Server 2008 and in Windows Vista. This context provides the functionality for controlling Windows Firewall behavior that was provided by the netsh firewall context in earlier Windows operating systems.

    This context also provides functionality for more precise control of firewall rules. These rules include the following per-profile settings:

    Domain

    Private

    Public

      The netsh firewall command-line context might be deprecated in a future version of the Windows operating system. We recommend that you use the netsh advfirewall firewall context to control firewall behavior.

      Note The netsh firewall command line is not recommended for use in Windows Vista.

    And then there is this…

    • Netsh Commands for Windows Firewall with Advanced Security – Microsoft TechNet.

      Applies To: Windows 7,Windows Server 2008,Windows Server 2008 R2,Windows Vista

      Netsh advfirewall is a command-line tool for Windows Firewall with Advanced Security that helps with the creation, administration, and monitoring of Windows Firewall and IPsec settings and provides an alternative to console-based management. This can be useful in the following situations:

      When deploying Windows Firewall with Advanced Security settings to computers on a wide area network (WAN), commands can be used interactively at the Netsh command prompt to provide better performance than gnraphical utilities when used across slow-speed network links.

      When deploying Windows Firewall with Advanced Security settings to a large number of computers, commands can be used in batch mode at the Netsh command prompt to help script and automate recurring administrative tasks that must be performed.

      You must have the required permissions to run the netsh advfirewall commands:

      If you are a member of the Administrators group, and User Account Control is enabled on your computer, then run the commands from a command prompt with elevated permissions. To start a command prompt with elevated permissions, find the icon or Start menu entry that you use to start a command prompt session, right-click it, and then click Run as administrator.

    From there (at least in my specific need) you then need to move on to either the

    • Netsh AdvFirewall Firewall Commands page on Microsoft TechNet for the specific command line arguments required.

    or the

    • Download details: Introduction to Windows Firewall with Advanced Security – Microsoft Downloads offers a great whitepaper detailing the changes and examples of managing the Vista/Win7 firewall, including from the command line.

    Good stuff all the way round.

    Can’t wait to start deploying…

    Claus V.

    Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
    Posted in command-line interface, firewalls, Microsoft, networking, security, Vista, Vista mods, Windows 7, XP, XP mods | No comments
    Newer Post Older Post Home

    0 comments:

    Post a Comment

    Subscribe to: Post Comments (Atom)

    Popular Posts

    • Finally! Time to Post! New material list
      After a recent text from my bro reminding me it has been since March since I’ve done a blog post, I was finally able to clear the schedule a...
    • Oscar watch Linkpost
      Alvis and Lavie are watching the Oscars tonight and I’m along for the ride. I wasn’t able to come even close to getting out some of the pos...
    • New Year’s Day - First Post 2011
      Same day I came out with my first post after a long drought, I fell upon this article Blogging Seems To Have Peaked, Says Pew Report over a...
    • Utility Gumbo
      There’s a lot in this pot.  Probably something everyone can find to enjoy. I’m serving it up tonight out of the back of the truck on the s...
    • iodd : Multi-boot madness!
      Like many computer technicians and responders, I seem to always have at hand a collection of bootable media; CD’s, DVD’s, USB-HDD’s, flash m...
    • Ubuntu 13.10 Upgrade - Lessons Learned & VIDMA utility found
      A few weeks ago a new release of Ubuntu came out. Naturally that meant it was update time! I have been getting pretty good at this now so ...
    • Interesting Malware in Email Attempt - URL Scanner Links
      Last weekend I spent some time with extended family helping confirm for them that their on-line email account got hacked and had been used t...
    • Windows 8 Linkage: A Bit Behind the Ball
      CC attribution: behind the eight ball by Ed Schipul on flickr . OK. Confession time. I’m more than a bit exhausted this weekend. Besides a...
    • Lego MiniFig Extravaganza
      picture clipped from Wired’s clip from Gizmodo clip… Thanks in no small part to the Windows 7 RC release, XPM mode research, and a big “l...
    • This Week in Security and Forensics: Beware the cake!
      Cube Party! image used with permission from John Walker at "rockpapershotgun.com" Yeah, the cake is a Portal thing.  Let’s d...

    Categories

    • Active Directory
    • anti-virus software
    • Apple
    • architecture
    • art
    • AVG
    • Blogger
    • blogging
    • books
    • boot-cd's
    • browsers
    • cars
    • cell-phones
    • cheat sheets
    • Chrome/Chromium
    • command-line interface
    • cooking
    • crafts
    • crazy
    • curmudgeon
    • DHC
    • Dr. Who
    • E-P1
    • Education
    • family
    • Firefox
    • firewalls
    • For the Gentleman
    • forensics
    • Gmail
    • Google
    • graphics
    • hacks
    • hardware
    • humor
    • hurricanes
    • imagex
    • Internet Explorer
    • iOS
    • iPhone
    • iPod
    • iTunes
    • Kindle
    • Learning
    • Link Fest
    • Linux
    • malware tools
    • Microsoft
    • movies
    • music
    • networking
    • NewsFox
    • NFAT
    • Nook
    • Opera
    • organization
    • PDF's
    • photography
    • politics
    • PowerShell
    • recipes
    • Remote Support
    • RSS
    • science
    • Scripting
    • search engines
    • security
    • Shuttle SFF
    • software
    • Texana
    • Thunderbird
    • troubleshooting
    • TrueCrypt
    • tutorials
    • utilities
    • VBscript
    • video
    • Virtual PC
    • virtualization
    • viruses
    • Vista
    • Vista mods
    • wallpapers
    • Win FE
    • Win PE
    • Win RE
    • Windows 7
    • Windows 8
    • Windows Home Server
    • Windows Live Writer
    • Windows Phone
    • writing
    • XP
    • XP mods
    • Xplico

    Blog Archive

    • ►  2013 (83)
      • ►  November (8)
      • ►  October (8)
      • ►  September (14)
      • ►  August (6)
      • ►  July (10)
      • ►  June (10)
      • ►  April (11)
      • ►  March (6)
      • ►  February (7)
      • ►  January (3)
    • ►  2012 (96)
      • ►  December (8)
      • ►  November (4)
      • ►  October (9)
      • ►  September (8)
      • ►  August (12)
      • ►  July (4)
      • ►  June (3)
      • ►  May (7)
      • ►  April (13)
      • ►  March (3)
      • ►  February (5)
      • ►  January (20)
    • ►  2011 (41)
      • ►  December (8)
      • ►  November (7)
      • ►  September (4)
      • ►  August (4)
      • ►  July (2)
      • ►  June (6)
      • ►  March (5)
      • ►  February (1)
      • ►  January (4)
    • ▼  2010 (69)
      • ►  December (1)
      • ►  October (3)
      • ►  September (2)
      • ►  August (13)
      • ►  July (17)
      • ►  June (3)
      • ►  May (3)
      • ►  April (3)
      • ►  March (11)
      • ►  February (1)
      • ▼  January (12)
        • Rapid-Strike Linkfest: Micro Edition
        • One day only deal: WinPatrol PLUS $0.99 – January ...
        • Security and Forensics Roundup: Heavy Version #6
        • Mega Linkfest – Dog-pile Style
        • Forensic Memory Capture roundup
        • Windows Firewall, the netsh command, and verbose =...
        • How I banished YouTube pink-psychedelic-dust casti...
        • A Valca Oops! How to REALLY enable (hack) Windows ...
        • Lavie’s Laptop Teardown
        • Windows Process Tools – Freeware Linkfest
        • Windows Things…
        • Opening Ports in Windows Firewall from Batch files
    • ►  2009 (177)
      • ►  December (20)
      • ►  November (11)
      • ►  October (7)
      • ►  September (7)
      • ►  August (21)
      • ►  July (17)
      • ►  June (7)
      • ►  May (18)
      • ►  April (9)
      • ►  March (17)
      • ►  February (23)
      • ►  January (20)
    • ►  2008 (35)
      • ►  December (23)
      • ►  November (12)
    Powered by Blogger.

    About Me

    Unknown
    View my complete profile