eatabrick.org

For to be to make you smarter. For to be to get you dead.

Keyboard 2014-11-05

I have recently been investigating building or purchasing a 60% keyboard. Before committing to this investment, I wanted to get an idea of how much I actually use the keys outside of the 60% layout. There may have been some kind of tool to show me this but I decided to whip up a very rudimentary keylogger and let it run for a few hours while I used my computer.

#!/bin/sh

id=$(xinput --list |grep 'USB Keyboard' |head -1 |grep -oP '(?<=id=)\d+')
xinput --test $id >> ~/Documents/keylog.txt

I did say it was rudimentary. Better data might be attained with a better method of logging keystrokes, as xinput seems to regard holding a key down as a long series of press and release events rather than a single one. After collecting the data for a few hours I created a script to put the data into a useful format for me to digest.

#!/usr/bin/env perl

package KeyMap;

use 5.010;
use strict;
use warnings;

use SVG;

sub new {
  my ($class, @args) = @_;

  my $self = bless {
    presses => [],
    total   => 0,
    svg     => SVG->new(
      width  => 940,
      height => 300,
      @args,
    ),
  }, $class;
}

sub svg { shift->{svg} }

sub make_key {
  my ($self, $x, $y, $w, $h, $text, $color) = @_;

  my ($r, $g, $b) = @$color;
  my $tcol = $color->[3] ? '#fff' : '#000';

  $self->svg->rectangle(
    x      => $x + 2,
    y      => $y + 2,
    width  => $w - 4,
    height => $h - 4,
    rx     => 5,
    ry     => 5,
    style  => {
      stroke => '#000',
      fill   => "rgb($r,$g,$b)",
    });

  $self->svg->text(
    x     => $x + $w / 2,
    y     => $y + $h / 2 + 3,
    style => {
      'fill'        => $tcol,
      'text-align'  => 'center',
      'text-anchor' => 'middle',
      'font-family' => 'Noto Sans',
      'font-size'   => '10px',
    },
  )->cdata($text);
}

sub make_row {
  my ($self, $x, $y, @keys) = @_;

  foreach (@keys) {
    $_ = [ $_, 1, 1 ] unless ref $_;
    my ($code, $w, $h) = @$_;
    $w ||= 1;
    $h ||= 1;

    $self->make_key($x, $y, $w * 40, $h * 40,
      sprintf('%0.2f', $self->percent($code)),
      $self->color($code));
    $x += $w * 40;
  }
}

sub add_key_press {
  my ($self, $code) = @_;

  $self->{presses}[$code]++;
  $self->{total}++;

  delete $self->{max_percent};
}

sub presses { shift->{presses}[shift] || 0 }
sub total { shift->{total} }

sub max_percent {
  my ($self) = @_;

  return $self->{max_percent} if exists $self->{max_percent};

  my $max = 0;
  for (@{ $self->{presses} }) {
    $max = $_ if $_ and $_ > $max;
  }

  return $self->{max_percent} = $max / $self->total * 100;
}

sub percent {
  my ($self, $code) = @_;

  return $self->presses($code) / $self->total * 100;
}

sub color {
  my ($self, $code) = @_;

  # Crazy exponential scaling gotten experimentally
  my $scale = ($self->percent($code) / $self->max_percent) ** 0.3;

  my ($r, $gb, $t);

  if ($scale > 0.66) {
    $r = 255 - int(128 * ($scale - 0.66) / 0.33);
    $gb = 0;
    $t = 1;
  } else {
    $r = 255;
    $gb = 255 - int(255 * $scale / 0.66);
    $t = 0;
  }

  return [ $r, $gb, $gb, $t ];
}

sub render {
  my ($self) = @_;

  # base keys
  $self->make_row(20, 80, 49, 10 .. 21, [ 22, 2 ]);
  $self->make_row(20, 120, [ 23, 1.5 ],  24 .. 35, [ 51, 1.5 ]);
  $self->make_row(20, 160, [ 66, 1.75 ], 38 .. 48, [ 36, 2.25 ]);
  $self->make_row(20, 200, [ 50, 2.25 ], 52 .. 61, [ 62, 2.75 ]);
  $self->make_row(
    20,
    240,
    [ 37,  1.25 ],
    [ 133, 1.25 ],
    [ 64,  1.25 ],
    [ 65,  6.25 ],
    [ 108, 1.25 ],
    [ 134, 1.25 ],
    [ 135, 1.25 ],
    [ 105, 1.25 ]);

  # function keys
  $self->make_row(20,  20, 9);
  $self->make_row(100, 20, 67 .. 70);
  $self->make_row(280, 20, 71 .. 74);
  $self->make_row(460, 20, 75, 76, 95, 96);

  # navigation
  $self->make_row(630, 20,  107, 78,  127);
  $self->make_row(630, 80,  118, 110, 112);
  $self->make_row(630, 120, 119, 115, 117);
  $self->make_row(670, 200, 111);
  $self->make_row(630, 240, 113, 116, 114);

  # number pad
  $self->make_row(760, 80, 77, 106, 63, 82);
  $self->make_row(760, 120, 79 .. 81, [ 86,  1, 2 ]);
  $self->make_row(760, 160, 83 .. 85);
  $self->make_row(760, 200, 87 .. 89, [ 104, 1, 2 ]);
  $self->make_row(760, 240, [ 90, 2 ], 91);
}

sub xmlify {
  my ($self, @args) = @_;

  return $self->svg->xmlify(@args);
}

package main;

use strict;
use warnings;
use autodie;

my $INPUT  = "$ENV{HOME}/Documents/keylog.txt";
my $OUTPUT = "$ENV{HOME}/Documents/keyfreq.svg";

open my $output, '>', $OUTPUT;
open my $input, '<', $INPUT;

my $map = KeyMap->new();
while (<$input>) {
  $map->add_key_press($1) if /^key release\s+(\d+)\s+$/;
}

close $input;

$map->render;
print $output $map->xmlify;

close $output;

This script reads the keylog and creates a "heat map" of which keys I actually pressed. Here is the generated image:

keyboard heat map

Update: I have regenerated the map with a few days of data to give a better view of my usage.

Some notes before interpreting the data:

I have my caps lock mapped to super (windows key) because it is the modifier I use for my window manager functions. I do not actually turn caps lock on and off that frequently.

I have my escape and tilde (left of number 1) keys swapped. I'm not sure I like this just yet as I type ~ more often than I had realized.

I only had the keylogger running while I was doing work-type things. I boot into Windows if I am going to play games and I did not log any data during those times although I'm not sure the differences would be significant.

All in all I think this really shows how little keys are used outside of the 60% layout area (at least by me during the time of this experiment). In any case, this has strengthened my resolve to acquire a 60% keyboard.

heartbleed 2014-04-09

In light of the recent heartbleed bug, I have taken an opportunity to review my server configuration and ensure that everything is up to snuff. To facilitate this, I used the wonderful Qualys SSL Labs tool. Their best practices seemed extremely reasonable to me so I set out to ensure I was following all of them

First of all, I had to make new server keys and certs because of the potential that they were compromised. Personally, I can never remember how to work the openssl command line tool so I have set up a makefile to do all that nonsense for me:

BITSIZE=4096
FILE=eatabrick.org

all: $(FILE).pem $(FILE).key dhparam.pem
  chown http:http $(FILE).*
  chmod 600 $(FILE).key

$(FILE).pem: $(FILE).csr intermediate.pem
  @echo "Copy and paste the following into your CA:"
  @cat $(FILE).csr
  @echo "Paste the certificate from your CA here (^D to finish):"
  @cat >$(FILE).pem
  @cat intermediate.pm >>$(FILE).pem

$(FILE).csr: $(FILE).key
  openssl req -new -key $(FILE).key -out $(FILE).csr

$(FILE).key:
  openssl genrsa -des3 -passout pass:x -out $(FILE).pass.key $(BITSIZE)
  openssl rsa -passin pass:x -in $(FILE).pass.key -out $(FILE).key
  rm $(FILE).pass.key

intermediate.pem:
  @echo "Paste any intermediate certs here (^D to finish):"
  @cat >intermediate.pem

dhparam.pem:
  openssl dhparam -out dhparam.pem $(BITSIZE)

clean:
  rm $(FILE).* intermediate.pem dhparam.pem

This makes my life much easier since I can just make clean all when it's time for new certs. The dhparam.pem rule is because nginx by default uses openssl's default DH parameters which are only 1024 bit and that will weaken the security of clients using ephemeral keys which kind of defeats the purpose.

With the certs in place, I have the following nginx configuration:

server {
  listen 80 default;
  listen [::]:80 default;

  server_name eatabrick.org www.eatabrick.org;
  rewrite ^/(.*) https://eatabrick.org/$1 permanent;
}

server {
  listen 443 ssl;
  listen [::]:443 ssl;

  server_name www.eatabrick.org;

  ssl_certificate /etc/nginx/ssl/eatabrick.org.pem;
  ssl_certificate_key /etc/nginx/ssl/eatabrick.org.key;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4:!3DES';
  ssl_session_cache shared:SSL:10m;
  ssl_dhparam /etc/nginx/ssl/dhparam.pem;

  ssl_stapling on;

  add_header Strict-Transport-Security max-age=31536000;
  add_header X-Frame-Options DENY;

  rewrite ^/(.*) https://eatabrick.org/$1 permanent;
}

server {
  listen 443 ssl default;
  listen [::]:443 ssl default;

  server_name eatabrick.org;

  ssl_certificate /etc/nginx/ssl/eatabrick.org.pem;
  ssl_certificate_key /etc/nginx/ssl/eatabrick.org.key;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4:!3DES';
  ssl_session_cache shared:SSL:10m;
  ssl_dhparam /etc/nginx/ssl/dhparam.pem;

  ssl_stapling on;

  add_header Strict-Transport-Security max-age=31536000;
  add_header X-Frame-Options DENY;

  access_log /var/log/nginx/eatabrick.org.access.log;
  error_log /var/log/nginx/eatabrick.org.error.log;

  root /srv/http/eatabrick.org/htdocs/;
  index index.html;
  error_page 404 /404.html;
}

This configuration has netted me a coveted A+ from Qualys:

SSL Labs Overall Rating: A+

However, I did have to make some compromises to do this.

First, I have disabled use of SSLv3 (because it is broken). This precludes IE6 from accessing eatabrick.org. I cannot think of anything less controversial than this. If for some reason you need to support people using IE6 you should review your life choices. According to Qualys, this also precludes YandexBot 3.0, which is apparently a bot from a Russian search engine. This is more unfortunate than not supporting IE6 but not so much that I am going to use a broken protocol.

Second, as mentioned earlier, I am using 4096 bit DH parameters to match my 4096 bit key. This apparently precludes Java 6u45 from connecting. As far as I am aware Java 6 is no longer supported so it's probably time for any clients using this to upgrade to something less broken.

Lastly, I have excluded 3DES from the list of ciphers my server is willing to use. This was probably not entirely necessary since 3DES is not really broken but it does use 112 (or 108) bit keys which are a tad too small for my taste so it got the axe. This precludes IE8 on Windows XP from connecting. As with the other sacrifices, this product is no longer supported by its makers so I see no reason for it to be supported by me.