吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 28652|回复: 269
收起左侧

[原创工具] 别动我电脑

    [复制链接]
xiaomingtt 发表于 2023-9-26 15:36
本帖最后由 xiaomingtt 于 2023-9-27 10:56 编辑

更新了一个版本,主要增加了设置功能。
2023-09-27_103936.png
程序运行后自动添加到系统启动项。第一次运行会出现设置界面,在前5个选项卡选择一种验证方式,并设置好验证内容,下次程序运行的时候,不符合验证条件,就触发设置操作。
具体触发什么操作以及等待时间,可在设置页面修改。触发的操作除了可以从下拉菜单选择,还可以手工输入。
设置完后还想重新设置,可在程序后面添加任意参数。如未编译执行:python bdwdn.py 0;如已编译执行:bdwdn.exe 123
https://www.aliyundrive.com/s/BPE7wSTAaxw提取码: 86hg
新源码地址:https://www.52pojie.cn/forum.php ... 838600&pid=48103851


虽然是单位电脑,但上面总有很多不想让同事看到的东西,尤其是外网电脑。但同事来借,又抹不开面不借。
不借电脑或者让别人打消借你电脑的念头,就是电脑坏了,于是之前做了【VBS】指定任意USB设备作为机器锁 - 『编程语言区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn
最近在学Python,于是做了一版功能更丰富的。
2023-09-26_153050.png 2023-09-26_153111.png 2023-09-26_153140.png
本来准备完全让chatgpt来写,但几个函数后就放弃了,还不如我手搓效率高,但写完让chatgpt做代码优化还是不错的选择。
下载地址:https://www.aliyundrive.com/s/avR8oRuf5Fm提取码: wb50

运行程序后,可以从5个条件中选择一个,设置完下次重启电脑的时候,如果电脑不满足设置的条件,5分钟后就会自动关机,程序后台运行,无界面。
第一个条件是电脑存在指定进程,如设置“notepad.exe”,开机后5分钟内打开记事本,程序就会自动退出,不让电脑关机。
第二个条件是存在指定文件,设置一个文件完整路径,如果无指定文件就关机。
第三个条件是存在指定U盘,通过读取优盘ID,判断优盘是否存在,开机5分钟后没有插入指定优盘就关机。
第四个条件是存在指定USB设备,可以是优盘、键盘、鼠标、打印机、HUB等任意USB设备,程序运行5分钟,指定设备没有接入电脑就关机。设置列表中将很多内置USB设备都列出来了,需要自己判断是否为外置USB设备。
第五个是开机要在键盘上按一组按键,相当于密码,但不需要输入框,在任意位置输入都可以。设置的时候先点开始记录,可在界面预览输入的内容,点击停止记录后可确定组合键的内容。开机5分钟内在键盘按出组合键就可以退出程序阻止关机。
程序只有第一次运行的时候有界面,设置好以后只会在后台运行,随系统启动。如果需要修改,可以在注册表编辑器中删除HKEY_CURRENT_USER\Software\bdwdn,再次运行程序即可弹出界面(下一版本将增加设置功能)。

具体作用参见这个帖子【新提醒】U盘解锁/锁定,适用于学校、公司、以及任何存在授权使用的场景 - 『原创发布区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|[url]www.52pojie.cn[/url]

免费评分

参与人数 69吾爱币 +66 热心值 +57 收起 理由
Szyk + 1 + 1 鼓励转贴优秀软件安全工具和文档!
catoo1 + 1 谢谢@Thanks!
456852jun + 1 + 1 热心回复!
p009p + 1 + 1 谢谢@Thanks!
skywalkesd + 1 + 1 大佬,PHP固定资产网站什么更新一下啊,急需使用微信盘点功能
1989xiaodoudou + 1 我很赞同!
yeah1 + 1 + 1 谢谢@Thanks!
gqdsc + 1 + 1 必须支持下
jieshao1 + 1 + 1 谢谢@Thanks!
ghpopst + 1 + 1 谢谢@Thanks!
萧杀 + 1 + 1 谢谢@Thanks!
lslong29 + 1 + 1 我很赞同!
SHRS23 + 1 我很赞同!
ptjmzcr + 1 谢谢@Thanks!
zxyfy + 1 用心讨论,共获提升!
TravisScoot + 1 + 1 我很赞同!
爱的太迟 + 1 + 1 用心讨论,共获提升!
bugof52pj + 1 谢谢@Thanks!
wuboxun + 1 谢谢@Thanks!
yzhl + 1 + 1 谢谢@Thanks!
诸葛文诚 + 1 + 1 谢谢@Thanks!
挚爱红蓝 + 1 热心回复!
kyokusanagi2000 + 1 谢谢@Thanks!
nakasou + 2 + 1 谢谢@Thanks!
Hcj665766 + 1 + 1 谢谢@Thanks!
guoruihotel + 1 + 1 谢谢@Thanks!
liupin924 + 1 谢谢@Thanks!
qq8808 + 1 + 1 谢谢@Thanks!
long8586 + 1 + 1 谢谢@Thanks!
精妹 + 1 我很赞同!
wtwvip + 1 + 1 我很赞同!
ag129 + 1 + 1 谢谢@Thanks!
jie520yun + 1 + 1 我很赞同!
h07799486 + 1 + 1 谢谢@Thanks!
134wap + 1 + 1 谢谢@Thanks!
cshdsj + 1 谢谢@Thanks!
扬肉包子 + 1 + 1 谢谢@Thanks!
qiaoyong + 1 + 1 热心回复!
hetiwz + 1 + 1 我很赞同!
laoxiao + 1 + 1 我很赞同!
wojaiyh + 1 谢谢@Thanks!
Natu + 1 + 1 谢谢@Thanks!
lyslxx + 1 + 1 我很赞同!
resu + 1 + 1 用心讨论,共获提升!
tel18654660612 + 1 + 1 大老牛逼啊
hijk1 + 1 + 1 我很赞同!
yaan + 1 + 1 我很赞同!
liji7716 + 1 + 1 我很赞同!
smfc + 1 我很赞同!
Y33 + 1 谢谢@Thanks!
JF300666 + 1 + 1 谢谢@Thanks!
328D93 + 1 + 1 热心回复!
hwh425 + 1 用心讨论,共获提升!
kingfu大量 + 1 我很赞同!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
luozi1653 + 1 + 1 热心回复!
Davie123 + 1 + 1 谢谢@Thanks!
Muhei998 + 1 小白来参观
nephalem_ + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
神奇的大表哥 + 1 我很赞同!
Fishycat + 1 + 1 我很赞同!
ZXY2018 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
金苹果 + 1 + 1 我很赞同!
kinghuashi + 1 + 1 我很赞同!
coolpengfei + 1 + 1 我很赞同!
tenvten + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
玄觞 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
hackabi + 1 + 1 如果U盘丢了怎么办{:1_918:}
beize1226 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| xiaomingtt 发表于 2024-2-1 10:18
真正的我 发表于 2024-2-1 10:02
恳请 博主是否可以加入 再没有插入指定U盘的时候 强制恢复初始化....... WIN10 LTSC 系统

初始化是啥意思?

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
真正的我 + 1 + 1 我很赞同!

查看全部评分

 楼主| xiaomingtt 发表于 2023-9-27 10:54
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-

import sys
import os
import psutil
import wmi
from pynput import keyboard
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtGui
from PyQt5.QtCore import *
import winreg
import threading
import time
from pathlib import Path
from ctypes import windll
import re
import subprocess

class MyWindow(QMainWindow):
    reg_root=winreg.HKEY_CURRENT_USER
    reg_path=r"SOFTWARE\bdwdn"
    mode = "0"
    value = ""
    waittime = 5
    cmd = ""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("别动我电脑")
        self.setWindowIcon(QIcon('icon.ico'))
        self.setGeometry(100, 100, 800, 400)
        self.setFixedSize(self.size())
        self.center()
        self.tab_widget = QTabWidget(self)
        self.tab_widget.setGeometry(10, 10, 780, 380)
        self.tab_widget.currentChanged.connect(self.on_tab_changed)
        self.create_tab1()
        self.create_tab2()
        self.create_tab3()
        self.create_tab4()
        self.create_tab5()
        self.create_tab6()
        
    def center(self):
        frame = self.frameGeometry()
        screen = QDesktopWidget().availableGeometry().center()
        frame.moveCenter(screen)
        self.move(frame.topLeft())
        
    def create_tab1(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定进程")
        layout = QVBoxLayout(tab)
        self.xznrr_label = QLabel('从进程列表中选择:', tab)
        self.list_widget = QListWidget(tab)
        self.list_widget.itemClicked.connect(self.on_item_clicked)
        self.xznrn_label = QLabel('从文件选择:', tab)
        self.file_button = QPushButton('选择文件', tab)
        self.file_button.clicked.connect(self.select_exefile)
        self.xznr_label = QLabel('选中内容:', tab)
        self.xz_textbox = QLineEdit(tab)
        self.ok1_button = QPushButton('确定', tab)
        self.ok1_button.clicked.connect(self.ok1_click)
        layout.addWidget(self.xznrr_label)
        layout.addWidget(self.list_widget)
        layout.addWidget(self.xznrn_label)
        layout.addWidget(self.file_button)
        layout.addWidget(self.xznr_label)
        layout.addWidget(self.xz_textbox)
        layout.addWidget(self.ok1_button)

    def create_tab2(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定文件")
        layout = QVBoxLayout(tab)
        self.file_button = QPushButton('选择文件', tab)
        self.file_button.clicked.connect(self.select_file)
        self.xz2_textbox = QLineEdit(tab)
        self.ok2_button = QPushButton('确定', tab)
        self.ok2_button.clicked.connect(self.ok2_click)
        layout.addWidget(self.file_button)
        layout.addWidget(self.xz2_textbox)
        layout.addWidget(self.ok2_button)

    def create_tab3(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定优盘")
        layout = QVBoxLayout(tab)
        self.list3_widget = QListWidget(tab)
        self.list3_widget.itemClicked.connect(self.on_youpan_clicked)
        self.xznr_label3 = QLabel('选中内容:', tab)
        self.xz3_textbox = QLineEdit(tab)
        self.ok3_button = QPushButton('确定', tab)
        self.ok3_button.clicked.connect(self.ok3_click)
        layout.addWidget(self.list3_widget)
        layout.addWidget(self.xznr_label3)
        layout.addWidget(self.xz3_textbox)
        layout.addWidget(self.ok3_button)

    def create_tab4(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定USB外设")
        layout = QVBoxLayout(tab)
        self.list4_widget = QListWidget(tab)
        self.list4_widget.itemClicked.connect(self.on_usbdrv_clicked)
        self.xznr_label4 = QLabel('选中内容:', tab)
        self.xz4_textbox = QLineEdit(tab)
        self.ok4_button = QPushButton('确定', tab)
        self.ok4_button.clicked.connect(self.ok4_click)
        layout.addWidget(self.list4_widget)
        layout.addWidget(self.xznr_label4)
        layout.addWidget(self.xz4_textbox)
        layout.addWidget(self.ok4_button)

    def create_tab5(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "一组键盘按键")
        layout = QVBoxLayout(tab)
        self.input_label = QLabel('输入内容:', tab)
        self.input_label.setAlignment(Qt.AlignTop)
        self.input_label.setWordWrap(True)
        self.xz5_textbox = QLineEdit(tab)
        self.ok5_button = QPushButton('确定', tab)
        self.ok5_button.clicked.connect(self.ok5_click)
        self.record_button = QPushButton("开始记录", tab)
        self.record_button.clicked.connect(self.toggle_record)
        layout.addWidget(self.record_button)
        layout.addWidget(self.input_label)
        layout.addWidget(self.xz5_textbox)
        layout.addWidget(self.ok5_button)
        self.is_recording = False
        self.record_list = []

    def create_tab6(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "设置")
        layout = QVBoxLayout(tab)
        self.xznr1_label = QLabel('当前模式:', tab)
        self.setmode_textbox = QLineEdit(tab)
        self.xznr0_label = QLabel('当前设定值:', tab)
        self.setinfo_textbox = QLineEdit(tab)
        self.xznr2_label = QLabel('等待时间:', tab)
        self.time_textbox = QLineEdit(tab)
        self.time_textbox.setValidator(QtGui.QIntValidator(1, 99999))
        self.xznr3_label = QLabel('选择操作:', tab)
        self.cmd_combobox = QComboBox(tab)
        self.cmd_combobox.addItem("关机(shutdown -s -f -t 0)")
        self.cmd_combobox.addItem("注销(shutdown -l /f /t 0)")
        self.cmd_combobox.addItem("锁定(rundll32.exe user32.dll,LockWorkStation)")
        self.cmd_combobox.addItem("断网(netsh interface set interface 以太网 disabled)(需要将“以太网”改为实际网卡名)")
        self.cmd_combobox.addItem("禁用键鼠(windll.user32.BlockInput(True))(内置命令)(与按键组合不能同时使用)")
        self.cmd_combobox.setCurrentIndex(0)
        self.xznr4_label = QLabel('选中命令(可手工输入):', tab)
        self.cmd_textbox = QLineEdit(tab)
        self.ok6_button = QPushButton('确定', tab)
        self.ok6_button.clicked.connect(self.ok6_click)
        layout.addWidget(self.xznr1_label)
        layout.addWidget(self.setmode_textbox)
        layout.addWidget(self.xznr0_label)
        layout.addWidget(self.setinfo_textbox)
        layout.addWidget(self.xznr2_label)
        layout.addWidget(self.time_textbox)
        layout.addWidget(self.xznr3_label)
        layout.addWidget(self.cmd_combobox)
        layout.addWidget(self.xznr4_label)
        layout.addWidget(self.cmd_textbox)
        layout.addWidget(self.ok6_button)
        self.cmd_combobox.currentTextChanged.connect(self.get_selected_command)

    def get_selected_command(self):
        selected_command = self.cmd_combobox.currentText()
        pattern = r'((.*?))'
        contents = re.findall(pattern, selected_command)
        self.cmd_textbox.setText(contents[0])
        cmdmode = "in" if len(contents) > 1 and contents[1] == "内置命令" else "out"
        self.cmd = '["' + cmdmode +  '","' + contents[0] + '"]'

    def ok6_click(self):
        waittime = int(self.time_textbox.text())
        waittime = 5 if waittime < 1 else waittime
        self.time_textbox.setText(str(waittime))
        try:
            key = winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_ALL_ACCESS)
        except:
            key = winreg.CreateKey(self.reg_root, self.reg_path)
        finally:
            winreg.SetValueEx(key, "time", 0, winreg.REG_SZ, str(waittime))
            winreg.SetValueEx(key, "cmd", 0, winreg.REG_SZ, self.cmd)
            winreg.CloseKey(key)
            QMessageBox.information(None, '别动我电脑', '设置完成')
        
    def ok1_click(self):
        self.write_to_inifile(1, self.xz_textbox.text())
        self.setmode_textbox.setText("存在一个指定进程")
        self.setinfo_textbox.setText(self.xz_textbox.text())

    def ok2_click(self):
        self.write_to_inifile(2, self.xz2_textbox.text())
        self.setmode_textbox.setText("存在一个指定文件")
        self.setinfo_textbox.setText(self.xz2_textbox.text())

    def ok3_click(self):
        strValue = self.xz3_textbox.text()
        strValue = strValue.split("~")[-1]
        self.write_to_inifile(3, strValue)
        self.setmode_textbox.setText("存在一个U盘")
        self.setinfo_textbox.setText(self.xz3_textbox.text())

    def ok4_click(self):
        strValue = self.xz4_textbox.text()
        strValue = strValue.split("~")[-1]
        self.write_to_inifile(4, strValue)
        self.setmode_textbox.setText("存在一个USB设备")
        self.setinfo_textbox.setText(self.xz4_textbox.text())

    def ok5_click(self):
        self.write_to_inifile(5, self.xz5_textbox.text())
        self.setmode_textbox.setText("输入一组按键")
        self.setinfo_textbox.setText(self.xz5_textbox.text())
    
    def write_to_inifile(self, m, s):
        try:
            key = winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_ALL_ACCESS)
        except:
            key = winreg.CreateKey(self.reg_root, self.reg_path)
        finally:
            winreg.SetValueEx(key, "mode", 0, winreg.REG_SZ, str(m))
            winreg.SetValueEx(key, "value", 0, winreg.REG_SZ, s)
            winreg.CloseKey(key)
            QMessageBox.information(None, '别动我电脑', '设置完成')
            try:
                key = winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_READ)
                ys, _ = winreg.QueryValueEx(key, "time")
                winreg.CloseKey(key)
            except:
                with winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_ALL_ACCESS) as key:
                    winreg.SetValueEx(key, "time", 0, winreg.REG_SZ, '5')
                self.time_textbox.setText('5')
            try:
                key = winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_READ)
                cmd, _ = winreg.QueryValueEx(key, "cmd")
                winreg.CloseKey(key)
            except:
                with winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_ALL_ACCESS) as key:
                    winreg.SetValueEx(key, "cmd", 0, winreg.REG_SZ, '["out","shutdown -s -f -t 0"]')
                self.cmd_textbox.setText('shutdown -s -f -t 0')

    def toggle_record(self):
        if self.is_recording:
            self.record_button.setText("开始记录")
            self.listener.stop()
            self.is_recording = False
            self.xz5_textbox.setText(str(self.record_list))
        else:
            self.record_button.setText("停止记录")
            self.xz5_textbox.clear()
            self.record_list.clear()
            self.listener = keyboard.Listener(on_press=self.on_key_press)
            self.listener.start()
            self.is_recording = True

    def on_key_press(self, key):
        try:
            char = str(key)
            self.record_list.append(char)
        except AttributeError:
            pass
        self.input_label.setText(f"输入内容:{self.record_list}")

    def closeEvent(self, event):
        try:
            self.listener.stop()
        except:
            pass
        event.accept()

    def on_youpan_clicked(self, item):
        self.xz3_textbox.setText(item.text())

    def on_usbdrv_clicked(self, item):
        self.xz4_textbox.setText(item.text())

    def on_item_clicked(self, item):
        self.xz_textbox.setText(item.text())

    def show_youpan(self):
        ups = self.get_os_up()
        self.list3_widget.clear()
        for device in ups:
            str_list = device['Drive Letter'] + "~" + device['Size'] + "~" + device['SerialNumber']
            self.list3_widget.addItem(str_list)

    def show_usbdrv(self):
        usbdrv = self.get_usb_drv()
        self.list4_widget.clear()
        for usb in usbdrv:
            str_list = usb['Name'] + "~" + usb['Manufacturer'] + "~" + usb['DeviceID']
            self.list4_widget.addItem(str_list)

    def show_processes(self):
        process_names = [proc.info['name'] for proc in psutil.process_iter(['pid', 'name'])]
        process_names.sort(key=lambda x: x.lower())
        self.list_widget.clear()
        if process_names:
            self.list_widget.addItems(process_names)

    def select_file(self):
        filename, _ = QFileDialog.getOpenFileName(self, '选择文件', '.', 'All Files (*)')
        self.xz2_textbox.setText(filename)

    def select_exefile(self):
        filename, _ = QFileDialog.getOpenFileName(self, '选择exe文件', '.', 'Executable Files (*.exe)')
        filename = os.path.basename(filename)
        self.xz_textbox.setText(filename)

    def get_usb_drv(self):
        try:
            c = wmi.WMI()
        except Exception as e:
            return []
        usb_devices = [
            {prop.Name: prop.Value for prop in device.Dependent.Properties_}
            for device in c.Win32_USBControllerDevice()
        ]
        return usb_devices

    def get_os_up(self):
        c = wmi.WMI()
        usb_storage_devices = []
        for diskdrive in c.Win32_DiskDrive():
            if "USB" in diskdrive.InterfaceType:
                for partition in diskdrive.associators("Win32_DiskDriveToDiskPartition"):
                    for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                        usb_device = {}
                        for prop in diskdrive.Properties_:
                            usb_device[prop.Name] = prop.Value

                        size_in_bytes = int(diskdrive.Size)
                        if size_in_bytes < 1024 ** 3:
                            size = round(size_in_bytes / (1024 ** 2), 2)
                            unit = 'MB'
                        else:
                            size = round(size_in_bytes / (1024 ** 3), 2)
                            unit = 'GB'
                        usb_device['Size'] = str(size) + ' ' + unit
                        usb_device['Drive Letter'] = logical_disk.Caption
                        usb_storage_devices.append(usb_device)
        return usb_storage_devices

    def read_reg_config(self):
        strings = {'1': '进程', '2': '文件', '3': '优盘', '4': '设备', '5': '按键'}
        try:
            root_key = winreg.ConnectRegistry(None, self.reg_root)
            key = winreg.OpenKey(root_key, self.reg_path)
            mode,_ = winreg.QueryValueEx(key, "mode")
            value,_ = winreg.QueryValueEx(key, "value")
            waittime,_ = winreg.QueryValueEx(key, "time")
            cmd,_ = winreg.QueryValueEx(key, "cmd")
            winreg.CloseKey(key)
            print(mode,value,waittime)
            self.mode = mode
            self.value = value
            self.setinfo_textbox.setText(value)
            self.waittime = waittime
            self.cmd = cmd 
            if mode in ['1','2','3','4','5'] and value:
                corresponding_string = strings.get(mode)
                self.setmode_textbox.setText(corresponding_string)
                self.setinfo_textbox.setText(value)
                self.time_textbox.setText(waittime)
                self.cmd_textbox.setText(eval(cmd)[1])
                return True
            else:
                return False 
        except Exception as e:
            return False 
    
    def add_auto_startup(self):
        if getattr(sys, 'frozen', False):
            exepath = os.path.abspath(sys.executable)
        else:
            exepath = os.path.abspath(sys.argv[0])
        key_name = os.path.basename(exepath)
        reg_root=winreg.HKEY_CURRENT_USER
        reg_path=r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        try:
            with winreg.OpenKey(reg_root, reg_path, 0, winreg.KEY_READ) as key:
                location, _ = winreg.QueryValueEx(key, key_name)
                if location == '"' + exepath + '"':
                    is_need_startup = False 
                else:
                    is_need_startup = True  
        except:
            is_need_startup = True

        if is_need_startup:
            try:
                with winreg.OpenKey(reg_root, reg_path, 0, winreg.KEY_ALL_ACCESS) as key:
                    winreg.SetValueEx(key, key_name, 0, winreg.REG_SZ, '"' + exepath + '"')
            except:
                pass

    def on_tab_changed(self, index):
        if index == 2:
            self.show_youpan()
        elif index == 3:
            self.show_usbdrv()
        elif index == 1:
            self.show_processes()

    def shutdown_computer(self):
        def shutdown():
            try:
                delay_minutes = int(self.waittime)
                delay_minutes = 1 if delay_minutes < 1 else delay_minutes
                cmd = eval(self.cmd)
                cmdmode = cmd[0]
                command = cmd[1]
            except:
                delay_minutes = 5
                cmdmode = "out"
                command = "shutdown -s -f -t 0"
            while True:
                time.sleep(delay_minutes * 60)
                if cmdmode == "out":
                    subprocess.run(command, shell=True, capture_output=True, creationflags=subprocess.CREATE_NO_WINDOW)
                else:
                    eval(command)
        thread = threading.Thread(target=shutdown)
        thread.start()
        return thread

    def check_process_exists(self, process_name):
        for process in psutil.process_iter(['name']):
            if process.info['name'] == process_name:
                return True
        return False

    def check_file_exists(self, file_path):
        path = Path(file_path)
        return path.is_file()

    def verify_user(self):
        if self.mode=="1":
            while True:
                if self.check_process_exists(self.value):
                    os._exit(0)
                time.sleep(5)
        elif self.mode=="2":
            while True:
                if self.check_file_exists(self.value):
                    os._exit(0)
                time.sleep(5)
        elif self.mode=="3":
            while True:
                allup = self.get_os_up()
                for device in allup:
                    if device['SerialNumber'] == self.value:
                        os._exit(0)
                time.sleep(5)
        elif self.mode=="4":
            while True:
                allusb = self.get_usb_drv()
                for usb in allusb:
                    if usb['DeviceID'] == self.value:
                        os._exit(0)
                time.sleep(5)
        elif self.mode=="5":
            self.listener = keyboard.Listener(on_press=self.verify_keycode)
            self.listener.start()
            self.key_list = []
        else:
            pass

    def verify_keycode(self, key):
        lst = eval(self.value)
        key_char = str(key)
        self.key_list.append(key_char)
        if len(self.key_list)>len(lst):
            self.key_list.pop(0)
        if self.key_list==lst:
            self.listener.stop()
            os._exit(0)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    arguments = app.arguments()
    win = MyWindow()
    win.add_auto_startup()
    if win.read_reg_config() and len(arguments)<2:
        win.hide()
        shutdownthread = win.shutdown_computer()
        win.verify_user()
    else:
        win.show()
        win.show_processes()
    sys.exit(app.exec_())
 楼主| xiaomingtt 发表于 2023-9-26 15:38
[Python] 纯文本查看 复制代码
import sys
import os
import psutil
import wmi
from pynput import keyboard
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import winreg
import threading
import time
from pathlib import Path

class MyWindow(QMainWindow):
    reg_root=winreg.HKEY_CURRENT_USER
    reg_path=r"SOFTWARE\bdwdn"
    mode = "0"
    value = ""
    
    def __init__(self):
        super().__init__()
        self.setWindowTitle("别动我电脑")
        self.setWindowIcon(QIcon('icon.ico'))
        self.setGeometry(100, 100, 800, 400)
        self.setFixedSize(self.size())
        self.center()
        self.tab_widget = QTabWidget(self)
        self.tab_widget.setGeometry(10, 10, 780, 380)
        self.tab_widget.currentChanged.connect(self.on_tab_changed)
        self.create_tab1()
        self.create_tab2()
        self.create_tab3()
        self.create_tab4()
        self.create_tab5()
        
    def center(self):
        frame = self.frameGeometry()
        screen = QDesktopWidget().availableGeometry().center()
        frame.moveCenter(screen)
        self.move(frame.topLeft())
        
    def create_tab1(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定进程")
        layout = QVBoxLayout(tab)
        self.xznrr_label = QLabel('从进程列表中选择:', tab)
        self.list_widget = QListWidget(tab)
        self.list_widget.itemClicked.connect(self.on_item_clicked)
        self.xznrn_label = QLabel('从文件选择:', tab)
        self.file_button = QPushButton('选择文件', tab)
        self.file_button.clicked.connect(self.select_exefile)
        self.xznr_label = QLabel('选中内容:', tab)
        self.xz_textbox = QLineEdit(tab)
        self.ok1_button = QPushButton('确定', tab)
        self.ok1_button.clicked.connect(self.ok1_click)
        layout.addWidget(self.xznrr_label)
        layout.addWidget(self.list_widget)
        layout.addWidget(self.xznrn_label)
        layout.addWidget(self.file_button)
        layout.addWidget(self.xznr_label)
        layout.addWidget(self.xz_textbox)
        layout.addWidget(self.ok1_button)

    def create_tab2(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定文件")
        layout = QVBoxLayout(tab)
        self.file_button = QPushButton('选择文件', tab)
        self.file_button.clicked.connect(self.select_file)
        self.xz2_textbox = QLineEdit(tab)
        self.ok2_button = QPushButton('确定', tab)
        self.ok2_button.clicked.connect(self.ok2_click)
        layout.addWidget(self.file_button)
        layout.addWidget(self.xz2_textbox)
        layout.addWidget(self.ok2_button)

    def create_tab3(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定优盘")
        layout = QVBoxLayout(tab)
        self.list3_widget = QListWidget(tab)
        self.list3_widget.itemClicked.connect(self.on_youpan_clicked)
        self.xznr_label3 = QLabel('选中内容:', tab)
        self.xz3_textbox = QLineEdit(tab)
        self.ok3_button = QPushButton('确定', tab)
        self.ok3_button.clicked.connect(self.ok3_click)
        layout.addWidget(self.list3_widget)
        layout.addWidget(self.xznr_label3)
        layout.addWidget(self.xz3_textbox)
        layout.addWidget(self.ok3_button)

    def create_tab4(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "存在指定USB外设")
        layout = QVBoxLayout(tab)
        self.list4_widget = QListWidget(tab)
        self.list4_widget.itemClicked.connect(self.on_usbdrv_clicked)
        self.xznr_label4 = QLabel('选中内容:', tab)
        self.xz4_textbox = QLineEdit(tab)
        self.ok4_button = QPushButton('确定', tab)
        self.ok4_button.clicked.connect(self.ok4_click)
        layout.addWidget(self.list4_widget)
        layout.addWidget(self.xznr_label4)
        layout.addWidget(self.xz4_textbox)
        layout.addWidget(self.ok4_button)

    def create_tab5(self):
        tab = QWidget()
        self.tab_widget.addTab(tab, "一组键盘按键")
        layout = QVBoxLayout(tab)
        self.input_label = QLabel('输入内容:', tab)
        self.input_label.setAlignment(Qt.AlignTop)
        self.input_label.setWordWrap(True)
        self.xz5_textbox = QLineEdit(tab)
        self.ok5_button = QPushButton('确定', tab)
        self.ok5_button.clicked.connect(self.ok5_click)
        self.record_button = QPushButton("开始记录", tab)
        self.record_button.clicked.connect(self.toggle_record)
        layout.addWidget(self.record_button)
        layout.addWidget(self.input_label)
        layout.addWidget(self.xz5_textbox)
        layout.addWidget(self.ok5_button)
        self.is_recording = False
        self.record_list = []

    def ok1_click(self):
        self.write_to_inifile(1, self.xz_textbox.text())

    def ok2_click(self):
        self.write_to_inifile(2, self.xz2_textbox.text())

    def ok3_click(self):
        strValue = self.xz3_textbox.text()
        strValue = strValue.split("~")[-1]
        self.write_to_inifile(3, strValue)

    def ok4_click(self):
        strValue = self.xz4_textbox.text()
        strValue = strValue.split("~")[-1]
        self.write_to_inifile(4, strValue)

    def ok5_click(self):
        self.write_to_inifile(5, self.xz5_textbox.text())
    
    def write_to_inifile(self, m, s):
        try:
            key = winreg.OpenKey(self.reg_root, self.reg_path, 0, winreg.KEY_ALL_ACCESS)
        except:
            key = winreg.CreateKey(self.reg_root, self.reg_path)
        finally:
            winreg.SetValueEx(key, "mode", 0, winreg.REG_SZ, str(m))
            winreg.SetValueEx(key, "value", 0, winreg.REG_SZ, s)
            winreg.CloseKey(key)

    def toggle_record(self):
        if self.is_recording:
            self.record_button.setText("开始记录")
            self.listener.stop()
            self.is_recording = False
            self.xz5_textbox.setText(str(self.record_list))
        else:
            self.record_button.setText("停止记录")
            self.xz5_textbox.clear()
            self.record_list.clear()
            self.listener = keyboard.Listener(on_press=self.on_key_press)
            self.listener.start()
            self.is_recording = True

    def on_key_press(self, key):
        try:
            char = str(key)
            self.record_list.append(char)
        except AttributeError:
            pass
        self.input_label.setText(f"输入内容:{self.record_list}")

    def closeEvent(self, event):
        self.listener.stop()
        event.accept()

    def on_youpan_clicked(self, item):
        self.xz3_textbox.setText(item.text())

    def on_usbdrv_clicked(self, item):
        self.xz4_textbox.setText(item.text())

    def on_item_clicked(self, item):
        self.xz_textbox.setText(item.text())

    def show_youpan(self):
        ups = self.get_os_up()
        self.list3_widget.clear()
        for device in ups:
            str_list = device['Drive Letter'] + "~" + device['Size'] + "~" + device['SerialNumber']
            self.list3_widget.addItem(str_list)

    def show_usbdrv(self):
        usbdrv = self.get_usb_drv()
        self.list4_widget.clear()
        for usb in usbdrv:
            str_list = usb['Name'] + "~" + usb['Manufacturer'] + "~" + usb['DeviceID']
            self.list4_widget.addItem(str_list)

    def show_processes(self):
        process_names = [proc.info['name'] for proc in psutil.process_iter(['pid', 'name'])]
        process_names.sort(key=lambda x: x.lower())
        self.list_widget.clear()
        if process_names:
            self.list_widget.addItems(process_names)

    def select_file(self):
        filename, _ = QFileDialog.getOpenFileName(self, '选择文件', '.', 'All Files (*)')
        self.xz2_textbox.setText(filename)

    def select_exefile(self):
        filename, _ = QFileDialog.getOpenFileName(self, '选择exe文件', '.', 'Executable Files (*.exe)')
        filename = os.path.basename(filename)
        self.xz_textbox.setText(filename)

    def get_usb_drv(self):
        try:
            c = wmi.WMI()
        except Exception as e:
            return []
        usb_devices = [
            {prop.Name: prop.Value for prop in device.Dependent.Properties_}
            for device in c.Win32_USBControllerDevice()
        ]
        return usb_devices

    def get_os_up(self):
        c = wmi.WMI()
        usb_storage_devices = []
        for diskdrive in c.Win32_DiskDrive():
            if "USB" in diskdrive.InterfaceType:
                for partition in diskdrive.associators("Win32_DiskDriveToDiskPartition"):
                    for logical_disk in partition.associators("Win32_LogicalDiskToPartition"):
                        usb_device = {}
                        for prop in diskdrive.Properties_:
                            usb_device[prop.Name] = prop.Value

                        size_in_bytes = int(diskdrive.Size)
                        if size_in_bytes < 1024 ** 3:
                            size = round(size_in_bytes / (1024 ** 2), 2)
                            unit = 'MB'
                        else:
                            size = round(size_in_bytes / (1024 ** 3), 2)
                            unit = 'GB'
                        usb_device['Size'] = str(size) + ' ' + unit
                        usb_device['Drive Letter'] = logical_disk.Caption
                        usb_storage_devices.append(usb_device)
        return usb_storage_devices

    def read_reg_config(self):
        try:
            root_key = winreg.ConnectRegistry(None, self.reg_root)
            key = winreg.OpenKey(root_key, self.reg_path)
            mode,_ = winreg.QueryValueEx(key, "mode")
            value,_ = winreg.QueryValueEx(key, "value")
            winreg.CloseKey(key)
            print(mode,value)
            self.mode = mode
            self.value = value
            if mode in ['1','2','3','4','5'] and value:
                return True
            else:
                return False 
        except Exception as e:
            return False 
    
    def add_auto_startup(self):
        if getattr(sys, 'frozen', False):
            exepath = os.path.abspath(sys.executable)
        else:
            exepath = __file__
            
        key_name = os.path.basename(exepath)
        reg_root=winreg.HKEY_CURRENT_USER
        reg_path=r"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
        try:
            with winreg.OpenKey(reg_root, reg_path, 0, winreg.KEY_READ) as key:
                location, _ = winreg.QueryValueEx(key, key_name)
                if location == '"' + exepath + '"':
                    is_need_startup = False 
                else:
                    is_need_startup = True  
        except:
            is_need_startup = True

        if is_need_startup:
            try:
                with winreg.OpenKey(reg_root, reg_path, 0, winreg.KEY_ALL_ACCESS) as key:
                    winreg.SetValueEx(key, key_name, 0, winreg.REG_SZ, '"' + exepath + '"')
            except:
                pass

    def on_tab_changed(self, index):
        if index == 2:
            self.show_youpan()
        elif index == 3:
            self.show_usbdrv()

    def shutdown_computer(self, delay_minutes=5):
        def shutdown():
            timer = threading.Timer(delay_minutes * 60, os.system, args=('shutdown /s /f /t 0',))
            timer.start()
        thread = threading.Thread(target=shutdown)
        thread.start()
        return thread

    def check_process_exists(self, process_name):
        for process in psutil.process_iter(['name']):
            if process.info['name'] == process_name:
                return True
        return False

    def check_file_exists(self, file_path):
        path = Path(file_path)
        return path.is_file()

    def verify_user(self):
        if self.mode=="1":
            while True:
                if self.check_process_exists(self.value):
                    os._exit(0)
                time.sleep(5)
        elif self.mode=="2":
            while True:
                if self.check_file_exists(self.value):
                    os._exit(0)
                time.sleep(5)
        elif self.mode=="3":
            while True:
                allup = self.get_os_up()
                for device in allup:
                    if device['SerialNumber'] == self.value:
                        os._exit(0)
                time.sleep(5)
        elif self.mode=="4":
            while True:
                allusb = self.get_usb_drv()
                for usb in allusb:
                    if usb['DeviceID'] == self.value:
                        os._exit(0)
                time.sleep(5)
        elif self.mode=="5":
            self.listener = keyboard.Listener(on_press=self.verify_keycode)
            self.listener.start()
            self.key_list = []
        else:
            pass

    def verify_keycode(self, key):
        lst = eval(self.value)
        key_char = str(key)
        self.key_list.append(key_char)
        if len(self.key_list)>len(lst):
            self.key_list.pop(0)
        if self.key_list==lst:
            self.listener.stop()
            os._exit(0)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = MyWindow()
    if win.read_reg_config():
        win.hide()
        shutdownthread = win.shutdown_computer(delay_minutes=5)
        win.verify_user()
    else:
        win.show()
        win.show_processes()
    win.add_auto_startup()
    sys.exit(app.exec_())
ringcoco 发表于 2023-9-26 15:57
没看明白什么意思
xuanxuanker 发表于 2023-9-26 15:58
效果是怎样 拔了鼠标就自动关机么
zhcj66 发表于 2023-9-26 16:05
不明白动作,怎么操作的
q6563265632 发表于 2023-9-26 16:08
别人用的话会出现什么情况,什么条件会触发呢?
superchb77 发表于 2023-9-26 16:12
我也想学PYTHON
莫纯皓 发表于 2023-9-26 16:33
是什么意思?能不能简单说下
keneng 发表于 2023-9-26 16:37
期待详细讲解效果。
TWYX 发表于 2023-9-26 16:37
没看明白什么意思
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-12-23 21:55

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表