5.63 用wf.msc阻断特定程序外连 Q: 在Win10上用wf.msc阻断某特定程序外连,意外发现未生效,Tcpview、Process Monitor 均看到TCP连接。 A: bluerust 2021-03-11 检查针对该程序的外连阻断规则,程序路径中是否出现了环境变量,比如%USERPROFILE%。 假设当前用户是scz,但wf.msc规则生效时%USERPROFILE%可能指向其他帐户,比如 Administrator,而不是scz,这使得阻断规则达不到预期效果。 %ProgramFiles%这种环境变量永远只有一种解释,wf.msc用了也无所谓。 为了确保wf.msc规则符合预期,指定程序时尽量不在路径表达中出现环境变量。 wf.msc在此有个大坑等着你。有时在wf.msc中指定目标程序时,尽管没有主动使用环 境变量,但wf.msc会自作聪明地帮你使用环境变量。要点是,不要点击"浏览"弹出文 件对话框后选中目标程序,也不要在这个文件对话框中直接贴目标程序绝对路径,这 两种操作方式都可能触发wf.msc的自作聪明;最保险的办法是在"此程序"处直接贴目 标程序绝对路径,避免使用文件对话框,此时wf.msc不会帮转环境变量。 Firewall Rule Properties Page: Programs and Services Tab - [2009-11-17] https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc755082(v=ws.11) 这里有一段警示 Important Do not use environment variable strings that resolve only in the context of a certain user (for example, %USERPROFILE%). When these strings are evaluated by the service at runtime, the service is not running in the context of the user. The use of these strings can produce unexpected results. D: scz 2021-03-11 假设wf.msc指定的程序路径中出现了环境变量,比如 %USERPROFILE%\AppData\Local\SomeApp\some.exe 用netsh查看该条规则 netsh.exe advfirewall firewall show rule name="Block SomeApp" dir=out verbose 此时看到的程序绝对路径被解释成 C:\Windows\ServiceProfiles\LocalService\AppData\Local\SomeApp\some.exe 显然这不符合预期,也无法阻断 C:\Users\scz\AppData\Local\SomeApp\some.exe wf.msc中的环境变量太具有欺骗性了,毕竟是它自己帮转的,真是个大坑。 可以用netsh.exe命令行阻断,不会自动替换成环境变量: netsh.exe advfirewall firewall add rule name="Block SomeApp" enable=yes dir=out action=block profile=any program="C:\Users\scz\AppData\Local\SomeApp\some.exe"