Firewall Context Menu

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Protek
Posts: 2
Joined: 06 Sep 2018 16:32

Firewall Context Menu

#1 Post by Protek » 06 Sep 2018 16:44

Goal

I need to add Block Firewall Context Menu For .exe Files.

Code

Code: Select all

Reg Add "HKEY_CLASSES_ROOT\exefile\Shell\Firewall\Command" /T Reg_SZ /D "Elevate.exe CMD.exe /Q /C For %%%%A In (\"%%1\") Do Netsh Advfirewall Firewall Add Rule Name=\"%%~nA\" Dir=Out Action=Block Program=\"%%1\""
This code works perfectly fine when using it with filepath-that-doesnt-contain-space

If filepath containing spaces it will simply throw out this error. To debug, I added

Code: Select all

& pause
to previous code

Output

Code: Select all

A specified value is not valid.

Usage: add rule name=<string>
     dir=in|out
     ..
Press any key to continue. . .
Twice

Code: Select all

A specified value is not valid.

Usage: add rule name=<string>
     dir=in|out
     ..
Press any key to continue. . .
I am not sure why it ran the code twice. One theory I could come up with is that maybe it is trying add the folder with spaces in path as filename and also the filename to netsh?

While debugging using Echo I found out that it doesn't have "" inside the

Code: Select all

Netsh Rule Name="" and Program=""
which i think it requires if the path or filename has spaces in it. I believe the "Qoutes" that are missing are causing this issue probably.

I have also tried escaping and adding double quotes. Still doesn't work.

Code: Select all

Reg Add "HKEY_CLASSES_ROOT\exefile\Shell\Firewall\Command" /T Reg_SZ /D "Elevate.exe CMD.exe /Q /C For %%%%A In (\"\"%%1\"\") Do Netsh Advfirewall Firewall Add Rule Name=\"\"%%~nA\"\" Dir=Out Action=Block Program=\"\"%%1\"\""
Thank you all for your help, if you require more information or clarification please ask.

ShadowThief
Expert
Posts: 910
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Firewall Context Menu

#2 Post by ShadowThief » 06 Sep 2018 21:20

The doctor says to the patient, "What seems to be the trouble?"
Raising his arm, the patient says, "It hurts when I do this."
The doctor says, "Don't do that."

Protek
Posts: 2
Joined: 06 Sep 2018 16:32

Re: Firewall Context Menu

#3 Post by Protek » 07 Sep 2018 06:21

Fixed :D

Code: Select all

For %%F In ('doctor') Do (
Echo Says to the patient, "What seems to be the trouble?"
Echo Raising his arm, the patient says, "It hurts when I do this."
Echo The doctor says, "Ok lemme help you with"
)
Edit: Answer here: https://stackoverflow.com/questions/522 ... ll-context (Huge Thanks @michael_heath)

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Firewall Context Menu

#4 Post by Ed Dyreen » 21 Sep 2018 16:05

adding values to the registry using reg.exe can be done safely.

1) escape reg.exe's escape character "\" in data
2) escape double quotes in key and data
3) add enclosing quotes to key and data
4) delay special characters
5) delay special characters in quotes in key and data
6) add quote if uneven in key and data

Directly accessing reg.exe without a wrapper function is a pain when working with unknown values.

Post Reply