0
点赞
收藏
分享

微信扫一扫

Python 写个程序实现电脑微信禁止登录禁止联网,定时每2秒查杀微信进程

以下是一个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()

使用说明:

  1. 保存代码为 block_wechat.py
  2. 右键使用"以管理员身份运行"
  3. Ctrl+C 退出时会自动清理防火墙规则

功能特点:

  1. 自动检测管理员权限
  2. 创建入站/出站防火墙规则
  3. 每2秒强制结束微信进程(包括子进程)
  4. 退出时自动清理防火墙规则
  5. 静默运行无多余输出

注意事项:

  1. 微信默认安装路径为 C:\Program Files (x86)\Tencent\WeChat\
  2. 如果安装路径不同,需要修改 wechat_path 变量
  3. 该程序需要持续后台运行才能保持阻止效果
  4. 防火墙规则更改可能需要稍等片刻生效

补充说明:

  • 程序退出时会自动删除创建的防火墙规则
  • 使用强制结束进程参数 /t 可以终止进程树
  • 通过Windows任务计划程序可以设置为开机启动
  • 可根据需要调整检测间隔时间(修改sleep参数)

请谨慎使用此类程序,确保符合当地法律法规和网络安全规范。

举报

相关推荐

0 条评论