port blocking

  • Thread starter Thread starter JustinC
  • Start date Start date
J

JustinC

Does anyone know of a way, using C#, to programmatically block a
Windows port? I've looked into some low level solutions like NDIS, but
am hoping to find something that uses C#. Any help would be greatly
appreciated.
 
Hi,

JustinC said:
Does anyone know of a way, using C#, to programmatically block a
Windows port? I've looked into some low level solutions like NDIS, but
am hoping to find something that uses C#. Any help would be greatly
appreciated.

No, there is nothing like that that I know of, I would suggest you check WMI
first , additionally see if the firewall that comes with SP2 has any API
you can use.
 
Thanks Ignacio, I have been looking through WMI, it's just hard to know
where to even start. There is some stuff is SP2 and even more
planned/coming for the future. I was hoping to find something .NET
based b/c our app needs to work on XP as well as 2000.

I'll keep looking through WMI and I'll post an answer if I find one.
 
| Thanks Ignacio, I have been looking through WMI, it's just hard to know
| where to even start. There is some stuff is SP2 and even more
| planned/coming for the future. I was hoping to find something .NET
| based b/c our app needs to work on XP as well as 2000.
|
| I'll keep looking through WMI and I'll post an answer if I find one.
|

The easiest is to shell-out (built-in) firewall configuration to the
netsh.exe command line utility.

For instance running:
netsh firewall show portopening
returns all ports open, while..

netsh firewall add portopening TCP 80 myport80

opens port 80 for TCP traffic

or:

add portopening protocol = TCP port = 135 name = DNS mode = ENABLE
scope = CUSTOM addresses =
192.168.0.1,192.168.0.0/16,10.0.0.0/255.0.0.0,LocalSubnet

for more info run - netsh firewall ?

Willy.
 
Willy,

I've been reading through the docs on netsh and it looks to be exactly
what I want, but I haven't been able to successfully block a port yet.

Specifically, I want to be able to turn on/off port 8080 to block
internet traffic. Does netsh allow you to completely block a port and
then later re-enable it? If so, what would be the syntax to do that?

Thanks again for your help.
 
| Willy,
|
| I've been reading through the docs on netsh and it looks to be exactly
| what I want, but I haven't been able to successfully block a port yet.
|
| Specifically, I want to be able to turn on/off port 8080 to block
| internet traffic. Does netsh allow you to completely block a port and
| then later re-enable it? If so, what would be the syntax to do that?
|
| Thanks again for your help.
|

Here are some examples:

Add an entry to all profiles, name = Web, default mode = DISABLE for port
tcp/8080
netsh firewall add portopening protocol = TCP port = 8080 name = Web mode =
DISABLE profile = all

Disable port
netsh firewall set portopening TCP 8080 Web DISABLE

Enable the port
netsh firewall set portopening TCP 8080 Web ENABLE

Show status of all profiles in verbose mode
netsh firewall show portopening ENABLE


Willy.
 
ok, that's what I was doing, it just wasn't doing what I was hoping it
would do. I'm trying to find a way to programmatically block all
internet traffic to a computer without blocking other network traffic.
I guess even with port 80 and 8080 blocked, outgoing traffic must
either produce an exception to allow incoming traffic, or another port
is being used. Thanks for all the help. Back to the drawing board!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top