LoginSignup
8
9

More than 5 years have passed since last update.

Cで作ったOSXのキーロガーのログを時系列でグラフ表示

Posted at

概要

前回作成したCで作るOSXのキーロガーで記録したログを時系列で表示してみる。

ログの形式

前回作成したキーロガーでは以下のようにタイムスタンプと押下されたキーコードのログが出力される。

$ head -n 5 keystrokes.log 
1401328736 124
1401328737 125
1401328737 44
1401328737 44
1401328738 1

Rubyで1分ごとのキー押下数をカウント

Time#strftimeで分まで切り出してそれをhasheql?で使い回して横着した。

keystrokes.rb
require 'date'

class Rank

  attr_reader :time

  def initialize(time)
    @time = time
  end

  def to_s
    @time.strftime("%Y-%m-%d %H:%M")
  end

  def hash
    @hash ||= to_s.hash
  end

  def eql?(other)
    self.hash == other.hash
  end

end

{}.tap{ |ranks|
  ARGF.readlines.each{ |line|
    time = DateTime.strptime(line.split.first, '%s').to_time
    rank = Rank.new(time)
    ranks[rank] ||= 0
    ranks[rank] += 1
  }
}.to_a.sort_by{ |rank|
  rank[0].time
}.each{ |number, rank|
  puts "#{rank.to_s}\t#{number}"
}

実行してみる。

cat keystrokes.log | ruby keystrokes.rb | head -n 5
5   2014-05-29 10:58
122 2014-05-29 10:59
105 2014-05-29 11:00
60  2014-05-29 11:01
62  2014-05-29 11:02

ふむ。

Gnuplotに食わせる

plot<を使うと食べてくれる。

keystrokes.plt
set xdata time
set timefmt "%Y-%m-%d %H:%M"
set xtics format "%H"
set xtics 60*60

set grid
set style fill solid
set xlabel "Timeline."
set ylabel "The number of keystrokes."

set terminal png
set output "keystrokes.png"

plot "<cat keystrokse.log | ruby keystrokes.rb" using 2:1:1 with i lc rgb "royalblue" notitle

これをGnuplotで実行する。

$ gnuplot keystrokes.plt

結果

(´。・ω・。)<進捗どうですか…。

keystrokes.png

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9