Rails 3 and lighttpd

This was performed on Archlinux with lighttpd 1.4.28 and rails 3.0.3


Required packages:

  • lighttpd,
  • fcgi,
  • ruby,
  • and their dependencies…


Ruby Setup

Required gems:

  • fcgi,
  • bundler

(if you are behind a proxy, the magic gem command is :

# gem install GEM -r -p "http://[PROXY_URL]:[PROXY_PORT]"


Once you have that you need to create a “dispatch.fcgi” script to do all the rails magic. I found an example one at http://stackoverflow.com/questions/3296206/rails-3-and-fcgi .


require 'rubygems'require 'fcgi'

require_relative '../config/environment'

class Rack::PathInfoRewriter  

  def initialize(app)    
    @app = app  

  def call(env)    
    parts = env['REQUEST_URI'].split('?')    
    env['PATH_INFO'] = parts[0]    
    env['QUERY_STRING'] = parts[1].to_s    

Rack::Handler::FastCGI.run  Rack::PathInfoRewriter.new(YOUR_APP_NAME::Application)

Running a “bundle install” from your app root will make sure all the necessary gems are available for local use. Follow these instructions and run “ruby public/dispatch.fcgi”, if you get no errors, voila!

Lighttpd Setup

Now, to set up lighttpd you need to merge this with your config:

server.modules   += ( "mod_fastcgi", "mod_rewrite" )

$HTTP["host"] == "localhost" {        

  server.document-root       =   "/path/to/your/app/public/"

  server.dir-listing         =   "disable"        
  server.error-handler-404   =   "/dispatch.fcgi"

  fastcgi.server             =   ( 
                                   ".fcgi" => ( 
                                     "localhost" => (
                                       "min-procs" => 1,
                                       "max-procs" => 1,
                                       "socket" => "/tmp/ruby-beholder.socket",                
                                       "bin-path" => "/path/to/your/app/public/dispatch.fcgi",                
                                       "bin-environment" => ( "RAILS_ENV" => "development" )        

A quick “sudo /etc/rc.d/lighttpd restart” and a check of the error logs will tell you if it has worked

CSLU Demo Prep

Well, this is getting rather fun, but quite time consuming.

My alarm clock is ticking away and my sleep time dwindling as I type, but I just don;t feel the urge to sleep yet. I’ve got a talk/demo I’m preparing for Wednesday for CSLU and it is getting bigger and bigger workload wise.

I’m hoping this translates to geeky hacky fun though.

The talk is on languages and paradigms, so I’m introducing a load of languages (8 I think, off the top of my head), describing the basic constructs, and then getting the room to build a “99 bottles” program using 3 of the given languages.

The fun, but time consuming, part of this is that I’m building cheat sheets for each language. Done the first one tonight, and it was for C, so quite detailed. Hoping I can cut/paste a lot of stuff for similar languages, and hoping I don’t have to fall back to a stock one. It is nice to make a custom one for the group, and also cements my own knowledge, especially of the languages I am unfamiliar with.

After the 99 bottles task, I am going to set some slightly longer challenges which can be made easier using certain paradigms. I have an OO one, which can be done procedurally. It’s based around a game scenario. For procedural I will probably do a processing batch type thing. For functional I need to do a little more research.

With the department allowing me to set up a load of VMs for everyone to use too, this should be pretty fun on Wednesday … so long as I get it all finished in time!

Conference season

September time is conference time it seems and I am currently at UbiComp2010. The UCSE2010 workshop convened yesterday which was very successful and discussion driven.

One of the main technical company sponsor was in the workshop, Autodesk, and gave an interesting talk on how they are opening their whole building’s Building Information Model(BIM) to the internet. I would point you to Digital 210 King for more information.

2 weeks ago I was at IPIN, which was hosted for the first time at ETH Zurick. Very successful conference, with lots of content (200+ talks!).

For all those interested, I am putting my slides on slideshare, those being my IPIN talk (30 min ish) and my UCSE talk (5 min). The UCSE presentation has had a slide removed for confidentiality reasons, so the short presentation is even shorter.

Please navigate to http://www.slideshare.net/CarlEllis to view.


Cyber Security Challenge

To all those interested, here is a copy of my email with my solution.

To whom it may concern,

Decoded this from the last challenge!

Congratulations  youve found and completed the REAL challenge. Your win
code is  cyb3r=s3cur1ty*ch@ll3nge+26-07-2010.

Please email this code to our team at
media@cybersecuritychallenge.org.uk. If youre the first person to do so,
and can prove you meet the eligibility criteria (British citizen
currently resident in the UK) we will be in touch to advise how to claim
your prize. Well done and good luck in the Cyber Security Challenge
competitions taking place throughout the rest of the year

Figured I would go through the process:

Task one

Copied the data into a file.

Created the following perl script:

#!/usr/bin/perluse MIME::Base64;

while(<STDIN>){    print decode_base64($_);}

I then piped the data through the script into another file

$ cat data | ./base64.pl > output

At a hunch, checked if it was an image.

$ evince output


Task two

Noticed the squiggles, zoomed in a lot and noticed it covered 2 pixel strips, so converted the image to a bitmap, then created a very hacky C file to grab the data (after checking the wiki page for the  file format). It is quite hacky, but combined with an online binary to ascii converter, worked well. The C file was as follows:

#include <stdlib.h>
#include <stdio.h>

void * imgdata;int datao(int x, int y);

int main(int argc, char *argv[]){

  FILE * fp;
  int w, h, o, off, i;
  int r, g, b, re;

  //Read binary data
  fp = fopen("output.bmp", "rb");

  fseek(fp, 0x0a, SEEK_SET);
  o = getc(fp);
  printf("1n", o);

  off = o + 1052 * 173;
  fseek(fp, off, SEEK_SET);
  for(i = 0; i < 350; i++){
    r = getc(fp);
    g = getc(fp);
    b = getc(fp);    

    if (r+g+b > 600)
      re = 1;
      re = 0;

      printf("%d", re);
      printf("%d", re);

  printf("n2n", o);

  //section section
  for(i = 3; i <= 172; i++){
    off = o + (1052 * i) + 1047;
    fseek(fp, off, SEEK_SET);    

    r = getc(fp);
    g = getc(fp);
    b = getc(fp);    

    if (r+g+b > 600)
      re = 0;
      re = 1;

      printf("%d", re);
      printf("%d", re);

  printf("n3n", o);

  //third section  

  off = o;
  for(i = 349; i >= 0 ; i--){
    fseek(fp, off + (i*3), SEEK_SET);
    r = getc(fp);
    g = getc(fp);
    b = getc(fp);    

    if (r+g+b > 600)
      re = 0;
      re = 1;

      printf("%d", re);
      printf("%d", re);  

  printf("n4n", o);

  //forth section  

  for(i = 172; i >= 3; i--){
    off = o + (1052 * i) + 3 ;
    fseek(fp, off, SEEK_SET);    

    r = getc(fp);
    g = getc(fp);
    b = getc(fp);    

    if (r+g+b > 600)
      re = 0;
      re = 1;

      printf("%d", re);
      printf("%d", re);

  return 0;

//offst from data start

int datao(int x, int y){

  int width = 350;
  int height = 175;

  int offset = (y * width * 3) + x*3;
  return offset;

From this I got the following text:

Cyrnfr sbyybj guvf yvax:

Which after running through the following perl script:


use MIME::Base64;

while(<stdin>){  $_ =~ tr/a-zA-Z/n-za-mN-ZA-M/;  print $_;}

Which led me to …

Task three

This one was tricky. I looked for ascii values on the data, reversed, big endian, little endian, looked for 7bit data compacted into 8, 7bit and parity (odd/even) and then only looking at bits which  passed the test.

Early on I noticed there were no ‘3’s in the data, but didn’t see the shocking clue.

Then I tried some basic frequency analysis, but as there were 56 unique entries it was probably a polyalphabetic cypher if it was. Tried the kasiski technique, ended up with random numbers, but tried none the less. I was convinced that the page name was a key, or something simple, like “cypher”. Alas, it was not true. Whilst looking at the binary for the 500th time looking for patterns of shifts which you could be employing, but finally I saw the solution. Turns out I was doing the rookie mistake of starting out complex.

Following is the ruby file used to decode the data:

class Cypherc

    attr_accessor :bytearray, :rawdata

    def initialize

        rawdata =  "68edcdec4e2c8eae8d2c8e2dedcd6e04d2042fedae52ceac04..."

        @rawdata = rawdata

        bytearray = []

        (0 ... rawdata.size/2).each do |i|

            bytearray &lt;&lt; "0x#{rawdata.slice!(0,2)}"


        @bytearray = bytearray


    def differencekeysize(length)

        diff = []

        initial = @bytearray[0]

        (1...@bytearray.size).each do |i|

            if (i%length &lt; 1)

                d = Integer(@bytearray[i]) - Integer(initial)

                initial = @bytearray[i]

                diff &lt;&lt; d



        return diff


    def asciicharacters

        ac = {}

        (0...@bytearray.size).each do |i|

            if(Integer(bytearray[i]) &lt; 128)

                puts "#{i} : #{@bytearray[i]}"

                ac[i] = @bytearray[i]



        return ac


    def paritycheck(byte, type = 1)

        #number of 1s

        binary = Integer(byte)

        tocheck = binary &amp; 0x7f

        par = binary &gt;&gt; 7

        ones = 0

        (0..7).each { |i|

            ones += tocheck &amp; 0x01

            tocheck = tocheck &gt;&gt; 1


        ok = false

        if(type == 1)

            if par == 1 and ones.odd?

                ok = true

            elsif par == 0 and ones.even?

                ok = true


        elsif(type == 2)

            if par == 1 and ones.even?

                ok = true

            elsif par == 0 and ones.odd?

                ok = true



        return ok


    def bitvalue(byte)

        return byte &amp; 0x7f


    def diffscan(index)

        diffs = []

        tocheck = @bytearray[index]

        (index+1 ... @bytearray.size).each{|b|

            diffs &lt;&lt; Integer(@bytearray[b]) - Integer(tocheck)


        return diffs


    def ngrams(n)

        bins = {}

        (0...@bytearray.size - n).each { |i|

            key = @bytearray.slice(i, n).to_s

            if bins[key].nil?

                bins[key] = 1


                bins[key] += 1



        return bins


    def bincheck

        bins = {}

        ks = 10

        (0 ... rawdata.size/ks).each {|i|

            bins[i] = rawdata.slice(ks*i, ks)

            t = []

            (0 ... bins[i].size/2).each do |j|

                t &lt;&lt; "0x#{bins[i].slice(j*2,2)}"


            bins[i] = t


        comp = []

        bins.each{ |k,v| comp &lt;&lt; bins[k][0] }


    def shiftbits(byte)

        binary = Integer(byte)

        bottom = binary &gt;&gt; 5

        top = (binary &amp; 0x1f) &lt;&lt; 3

        return bottom ^ top


    def decode

        @bytearray.each { |b|

            print "#{shiftbits(b).chr}"




c = Cypherc.new


Those are my workings, 3 hours on the steg stuff probably, and much too long on the last challenge.

Looking forward to more challenges 🙂

Carl Ellis

Cardiff and Lands End

So I decided to go on a week long tour of the good bit of the south, namely, the southwest. First though, a stop off at Cardiff to see an old friend.

As Cardiff had been explored before, and I had the car, first on the list of places to go was a pilgrimage to Barry Island, the place where every Welshman should be scarred with. Not quite sure why it is called an Island, as it was inside a cove, but those southern welshies are fairly off most of the time, like those Taven Ferry folk. Apart from children, dodgy amusements, fish and chip shops, and a dirty beach, there really wasn’t much there. Some nice photo opportunities though, soon to be on my public Picasa profile. After a scramble up the headland, we decided to go and find a real beach!

The first one we hit … had a power station firmly attached to it. Nice. So biting the bullet we continued westward, edging towards Bridgend. Luckily, before we hit that town a sign for Southerndown Beach. This was a brilliant little surfer beach and had some excellent photo spots from atop the cliffs and within the ruined castle on the opposite hilltop. (Photos https://picasaweb.google.com/Carlc75/Cardiff)

Finally, we head back to Cardiff for food, narrowly missing the general 10pm restaurant deadline by a mere 45min, we opted for Dominos pizza. Dropping the car off in a 24hour place, I then endured the scariest walk of my life to the flat. Cardiff is scary compared to little old Lancaster, deserted hilltops, dense woodland, etc.

After that, I left for Cornwall! First stop was Newquay and a cliff walk from Watergate Bay to Porth, which was stunning! If you want to learn to surf, this seems like the place. Next day I moved towards a small village called Treen, near Penzance, via most of the southern coast. Amazing little village called Mousehole, where on the beach people had stacked rather flimsy towers of beach rocks, ready to be washed by the tide. It was like an art installation, but seemed so natural. (Photos https://picasaweb.google.com/Carlc75/Mousehole)

Treen has a history about it, near its headland is what is called the Logain Stone, which you can rock with your hands due to fine balance! Or you used to be able to, until some chap from the navy in the 1800s decided he wanted to prove the navy could topple it. After much complain by the local tourism industry, he paid to have it put back, but it has never rocked as easily since, which is something I can attest to after daring the rather leery scramble to the rock. (Photos  https://picasaweb.google.com/Carlc75/LogainsRock)

Next day found me at Lands End, doing the usual touristy stuff, but I did get some dirty looks off passers by as I did some undaring bouldering to get a good photo. Then to St. Ives to wonder the streets. (Photos https://picasaweb.google.com/Carlc75/LandsEnd)

Today back to Newquay, and a day at the Eden Project (Photos https://picasaweb.google.com/Carlc75/EdenProject). Brilliant place, if a little inundated with small children, but then, it is for them. Big kids have to fit in I suppose! After chilling out in Watergate Bay again, Im in the pub writing this (Cornish Ale has nothing compared to Lancastrian). Tomorrow, I go bouldering at Carn Brea. Woo. Then back into the Shire.

Battery Monitor – rbatmon

I use a rather spartan windowing manager called awesome in all of my machines. This has been a fine setup until I used it on my netbook due to one small issue, battery monitors.

On my desktop machines and the laptop I use gkrellm to monitor cpu and memory and for the laptop it has a handy battery usage label. With the netbook however, screen real estate is quite valuable, so I opted for finding something to sit in the system tray.

After a quick look it seems there was nothing which was lightweight or simple or not requiring me to install the entirety of gnome.

In the end I made my own called rbatmon, then packaged it up for use in the AUR. If you have a substandard flavour of linux, not to worry, You can grab the script from my githib page here.

The most interesting challenge of this was building a package for the first time. There is a great package for Archlinux called abs (available on pacman) which fills /usr/share/pacman with some example PKGBUILDS for standard sources of gettings code from VCS’s.

Page on my site regarding this is here.

AUR page is here.


Website Layout

I have modified the layout of jimhi to be user friendly. It still has a bit of improvement to be had, but at least you can find pages other than the start one now!

I shall put a feature in the site which puts it back into ridiculous interaction mode soon.

Very basic Makefile for LaTeX documents

So I wanted to make a basic makefile for my tex documents, so I took the Makefile from http://www.acoustics.hut.fi/u/mairas/UltimateLatexMakefile/ and modified it to be amazingly simple.

Heres the Make file:


LATEX= latex
BIBTEX= bibtex
DVIPS= dvips
PS2PDF= ps2pdf

SRC := $(shell egrep -l '^[^%]*\begin{document}' *.tex | sed -e 's/.tex//')TRG = $(SRC).dviPSF = $(SRC).ps

all : pdf

dvi :  $(LATEX) $(SRC) && $(BIBTEX) $(SRC) && $(LATEX) $(SRC) && $(LATEX) $(SRC)

ps : dvi  $(DVIPS) $(TRG)

pdf : ps  $(PS2PDF) $(PSF)

clean :  rm *.bbl *.aux *.blg *.dvi *.log *pdf *.ps

Fairly simple and lets you choose your toolchain.