用Perl和xev创建连续击键力学监视器

上传人:ldj****22 文档编号:35915846 上传时间:2018-03-22 格式:PDF 页数:11 大小:350.98KB
返回 下载 相关 举报
用Perl和xev创建连续击键力学监视器_第1页
第1页 / 共11页
用Perl和xev创建连续击键力学监视器_第2页
第2页 / 共11页
用Perl和xev创建连续击键力学监视器_第3页
第3页 / 共11页
用Perl和xev创建连续击键力学监视器_第4页
第4页 / 共11页
用Perl和xev创建连续击键力学监视器_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《用Perl和xev创建连续击键力学监视器》由会员分享,可在线阅读,更多相关《用Perl和xev创建连续击键力学监视器(11页珍藏版)》请在金锄头文库上搜索。

1、 版权所有 IBM 公司 2008商标 用 Perl 和 xev 创建连续击键力学监视器第 1 页,共 11用 Perl 和 xev 创建连续击键力学监视器通过连续处理 X Window System 级别中的击键属性来分析谁在使用计算机Nathan Harrington 程序员 EMC2008 年 11 月 28 日了解如何使用 Perl、xev 和自定义算法,根据特有的键入模式监视当前使用键盘的人员。击键力学 是一个相对崭新的领域,可以通过对键入模式进行统计分析来识别使用键盘的人员。以前 发布在 developerWorks 中的文章已经展示了如何将击键力学的概念集成到应用程序中,以及修改

2、 Gnome Display Manager(GDM)以要求使用正确密码及 “正确键入的” 密码的实际示例。本文提供 的代码和工具可以使您超越单一的击键力学应用,而是连续监视整个 X Window System 环境以掌握 键入者的特有模式。在阅读本文后,您将可以创建一个连续的击键力学监视器,该监视器可以在没有检测到特有键入模 式时锁定 X Window System 会话。所选方法的注意事项跟踪每个键按下情况的最有效方法也许就是使用内核级键盘记录工具(例如 THC-vlogger 或 ttyrpld)。不幸的是,这些程序是为较旧的内核级别设计的,或者目前很难在现代 Linux 发行版中 使用

3、。键盘设备跟踪程序(例如 uberkey)是另外一种好方法,但是其释放击键和计时的不精确性使 其不适合本文介绍的应用。虽然不适用于控制台或远程会话,但是 xev 为检测在 X Window System 中运行的任意应用程序的键 盘事件提供了一种健壮且轻量级的方法。xev 会话中的每个事件都是随高精度的时间值一同输出的。在本文中,我们将在具体时窗内使用该 时间值记录 R、S 和 T 键的 “停顿” 时间。停顿时间是用户的手指按住按键的时间段。将为 X Window System 桌面中的每个应用程序记录这个相对简单的度量。在开发击键 “签名” 时,最好使用大量数据。使用模式需要匹配最常见的计算

4、机使用情况。用下面介 绍的数据跟踪选项进行试验以获得各种样例使用数据。然后,开发的签名将被转换为密码散列并存 储到磁盘中,以供稍后在监视阶段中进行比较。developerW Perl 和 xev 创建连续击键力学监视器第 2 页,共 11硬件和软件要求2000 年以后生产的所有 PC 都应当能够处理本文中提供的代码。您将需要 X Window System 以及 xev 程序(请参阅 参考资料)。您需要使用 mkpasswd 程序(附带于大多数 Linux 分发版中)才能生成 击键签名的密码散列。需要 Perl 模块 X11:GUITest、threads 和 Thread:Queue。UNI

5、X 和 Linux 用 户请注意:如果您刚开始安装 Perl 模块,使用 Andreas J. Konig 的 CPAN 模块可以自动安装其他模块 (请参阅 参考资料)。continuousKeystrokes.pl 程序记录在 X Window System 会话中按下的每个击键的简单方法是启动与 xwininfo -root -tree 所列出 的每个窗口绑定的 xev 程序。在理论上,这种方法将对于少量窗口有效,但是最终,将达到 X 客户 机的最大数目,并且需要重新编译 X Window System 才能增加允许的 X 客户机数目。更合理的解决 方案是跟踪持有焦点的当前窗口并把单个 x

6、ev 程序与该窗口绑定起来。然后将记录当前持有焦点的 窗口的每个键盘事件。清单 1 显示了用于跟踪当前焦点并创建击键签名的 continuousKeystrokes.pl 程序的开头。清单 1. continuousKeystrokes.pl 变量声明#! perl -w # continuousKeystrokes.pl - monitor dwell time of r,s,t for all X Window System use strict; use X11:GUITest qw( :ALL ); use threads; use Thread:Queue; die “specify

7、 mode, minimum samples“ unless _cnnew1ARGV = 2 ;my $sleepTime = 5; # seconds between key event processing runs my %windows = (); # hash of window keystrokes my samp = (); # most recent sample averages of keystrokes my $checkRng = 10; # fuzziness of dwell time matching my $userMatch = 0; # user or im

8、postor? my %keys = (); # average of key dwell timesmy $mode = $ARGV0; # record baseline or monitor matches my $minSamples = $ARGV1; # required base samples to match with my ( $salt, $hash ) = “; # read from keystroke.Signaturesif( $mode eq “monitor“ ) loadSignatureFile() 在模块 include 语句和初始变量声明之后,进入主控

9、制循环。在处于监视模式下时,将装入先前生成 的签名文件。清单 2 显示了主程序循环的开头。清单 2. 主程序循环的开始部分# ctrl-c to exit the program and drop the threads without error while(1) my activeId = GetInputFocus();my $foundPipe = 0;for my $key ( keys %windows )if( $key eq “activeId“ $windows$key input = createPipe( $res ) or die “no pipe “;$windows

10、$keypipeDef = 1;$foundPipe = 1;#if not a match#for each windows keyif( $foundPipe = 0 )# if pipe doesnt already exist, add a new onemy $key = “activeId“;if( !exists($windows$key) | $windows$keypipeDef = 0 )my $res = “xev -id $key |“;$windows$key input = createPipe( $res ) or die “no pipe “;$windows$

11、keypipeDef = 1;#if pipe doesnt already exist#foundpipe check第一个 for 循环将搜索目前没有绑定管道的窗口散列中的已有条目。如果找到这样一个条目,则创 建一个管道。维护一个由目前拥有管道的窗口组成的可用工作列表,将允许在一段时间内收集 xev 输出。xev 输出是非缓冲的,这会造成较少使用的窗口无法以足够快的速度填充输出缓冲。为了让输 出数据在窗口失去焦点后继续保留,然后重新获得这些数据,窗口散列将记录输出。清单 3 显示了 主逻辑循环的其余部分。清单 3. 主逻辑循环结束# read any available date from

12、 a pipefor my $xevPipe( keys %windows )next unless( $windows$xevPipepipeDef = 1 );while( $windows$xevPipeinput-pending )my $line = $windows$xevPipeinput-dequeue or next;$windows$xevPipekeyString .= $line;#while data to be added to the buffernext unless( exists( $windows$xevPipekeyString ) );next unl

13、ess( length( $windows$xevPipekeyString ) 8192 );compareSignature( getKeyAverages( $windows$xevPipekeyString ) );$windows$xevPipekeyString = “;#for windows keys# kill all xevs except currently monitoredfor my $key ( keys %windows )next unless( $key ne “activeId“ $windows$keypipeDef = 0;my $cmd = qqps

14、 -aef | grep $key | grep xev | perl -lane kill $F1;system($cmd);#for each windows keysleep( $sleepTime );#while main loopdeveloperW Perl 和 xev 创建连续击键力学监视器第 4 页,共 11在创建了管道(或者如果已经存在一个管道)后,每个管道的输出将被读入到该窗口记录的事件 变量中。如果记录了足够的数据,则整个缓冲将被传递给 getKeyAverages 子例程,然后再传递给 compareSignature 子例程。接下来,如果焦点事件发生更改,则终止旧的

15、 xev 程序。清单 4 显示了前两个子例程:loadSignatureFile 和 createPipe。清单 4. loadSignatureFile 和 createPipe 子例程sub loadSignatureFile open(INFILE,“keystroke.signatures“) or die “no signature file“;my $line =;die “empty file “ unless defined $line;chomp($line);( undef, undef, $salt, $hash ) = split $, $line;close(INFILE);#loadSignatureFilesub createPipe my $cmd = shift;my $queue = new Thread:Queue;asyncmy $pid = open my $pipe, $cmd or die $!;$queue-enqueue( $_ ) while ;$queue-enqueue( undef );-detach;# detach causes the threads to be silently terminated on program exitreturn $queue;#createPipeloadSigna

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号