2

Goal

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


Sources

Block .EXE in Windows Firewall with context menu (Modified for my use)

Windows 10 | regedit | exefile shell command (firewall add rule) - not running (No answers there)


Code

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 & pause to the end of the previous code

Output

A specified value is not valid.

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

Twice

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 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.

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\"\""

I hope we can figure this out together as a team. I will edit and update the OP for more clarification or updates, if needed. Thank you to @micheal_heath for answering.

TT.
  • 15,774
  • 6
  • 47
  • 88
  • Please do not deface your question. You can delete it yourself, or ask to disassociate yourself from your question if you want. – TT. Nov 18 '18 at 18:17

1 Answers1

0
Reg Add "HKLM\Software\Classes\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\\\""^
 /F

Using elevate, you may need to escape the double quotes. It is made from C which uses argv for argument handling just like reg uses to parse arguments.

reg removes the \ from \" so the " is kept. The issue is that executing elevate with ", that is not escaped with \, is removing the ". Thus paths with spaces now have no outer " to keep them from being split.

Solve this by adding another \ preceding \". If only use one \ to become \\", escapes the following \ and so the " is removed. So add yet another \ to become \\\" to escape the \ and to escape the " so that the \ is kept and the " is kept.

So reg parses \\\", elevate parses \" and cmd parses ".

Inserted into the registry looks like:

Elevate.exe CMD.exe /Q /C For %%A In (\"%1\") Do Netsh Advfirewall Firewall Add Rule Name=\"%~nA\" Dir=Out Action=Block Program=\"%1\"
michael_heath
  • 5,262
  • 2
  • 12
  • 22