1

在我们开始之前...

我对 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 运行时,它会在输出“记录击键...”后立即关闭。

帮助?

4

1 回答 1

0

来看看,是 PC 上的防病毒软件导致程序崩溃。这可以正常工作,并且从一开始就可以预期。

于 2019-09-11T21:28:33.010 回答