busylog
·

add Windows Firewall Rules to Block UWP Applications

to create the firewall rule, you need to get the package SID.
the normal method is DeriveAppContainerSidFromAppContainerName.
but thankfully there's an alternate implementation from https://github.com/metablaster/WindowsFirewallRuleset/issues/6 that could convert PackageFamilyName to SID, and I ported the method to python3.

New-NetFirewallRule -Displayname RULENAME -enabled false -action block -direction out -package SID
# Get-AppxPackage|select Name,PackageFamilyName

import hashlib
# s is PackageFamilyName
def AppSid(s):
  # unicode without bom
  a = hashlib.sha256(s.lower().encode('utf16')[2:]).digest()
  r = []
  for i in range(0,28,4): # not 32, last part not needed
     r.append(int.from_bytes(a[i:i+4],'little'))
  return 'S-1-15-2-' + '-'.join(map(str,r))

AppSid("Microsoft.MicrosoftEdge_8wekyb3d8bbwe")
# 'S-1-15-2-3624051433-2125758914-1423191267-1740899205-1073925389-3782572162-737981194'
bookmark_add
添加收藏
评论
登录后评论
社区准则 博客 联系 反馈 状态
主题