以下是一个Python程序,用于禁止微信登录和联网,并定时查杀微信进程:
import subprocess
import time
import sys
def check_admin():
"""检查是否以管理员权限运行"""
try:
return subprocess.check_output(['net', 'session'], shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError:
print("请以管理员身份运行此程序!")
sys.exit(1)
def manage_firewall_rule(action='add'):
"""管理防火墙规则"""
wechat_path = r"C:\Program Files (x86)\Tencent\WeChat\WeChat.exe"
for direction in ['in', 'out']:
subprocess.run([
'netsh', 'advfirewall', 'firewall',
action, 'rule',
f'name="Block WeChat {direction}"',
f'dir={direction}',
f'program="{wechat_path}"',
'action=block'
], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def kill_wechat():
"""结束微信进程"""
subprocess.run(
['taskkill', '/f', '/im', 'WeChat.exe', '/t'],
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
def main():
check_admin() # 检查管理员权限
manage_firewall_rule('add') # 添加防火墙规则
try:
while True:
kill_wechat()
time.sleep(2)
except KeyboardInterrupt:
print("\n正在清理防火墙规则...")
manage_firewall_rule('delete')
print("程序已安全退出")
if __name__ == "__main__":
main()
使用说明:
- 保存代码为
block_wechat.py
- 右键使用"以管理员身份运行"
- 按
Ctrl+C
退出时会自动清理防火墙规则
功能特点:
- 自动检测管理员权限
- 创建入站/出站防火墙规则
- 每2秒强制结束微信进程(包括子进程)
- 退出时自动清理防火墙规则
- 静默运行无多余输出
注意事项:
- 微信默认安装路径为
C:\Program Files (x86)\Tencent\WeChat\
- 如果安装路径不同,需要修改
wechat_path
变量 - 该程序需要持续后台运行才能保持阻止效果
- 防火墙规则更改可能需要稍等片刻生效
补充说明:
- 程序退出时会自动删除创建的防火墙规则
- 使用强制结束进程参数
/t
可以终止进程树 - 通过Windows任务计划程序可以设置为开机启动
- 可根据需要调整检测间隔时间(修改sleep参数)
请谨慎使用此类程序,确保符合当地法律法规和网络安全规范。