david wong

Hey! I'm David, a security consultant at Cryptography Services, the crypto team of NCC Group . This is my blog about cryptography and security and other related topics that I find interesting.

Here we go again

posted September 2014

So here's to a new school year in Bordeaux. My initial plan was to do my first year in Bordeaux and do my second year in Rennes. I liked Bordeaux so much that I decided to stay here instead, for better or worse.

First, I found a new place. It's way better than my last place (which was really, really bad). And I couldn't have asked for a better location. I'm right in the middle of everything. Bordeaux is small enough that I basically have to walk less than 5 minutes to go to restaurants, shops, supermarkets, the laundry, my friends, etc... Life is easy :)

my place

A few weeks ago an article has been written about my website 3pages in Telerama (a nationwide paper). It's small but that's something :)

telerama

Also I started classes last week. But I'll make another post about that!

comment on this story

Slim

posted August 2014

I talked about Slim the other day. I wanted to do a similar project not so long ago that I would have called weblang.

I first thought about an indented language with no symbols to declare html elements. Something like that:

p
  a 'more info' href: 'http://www.google.com'
ul
  li
    hey !

But then I thought, how will I distinguish markups from text. If I want to write ul without it being translated to <ul></ul>, how do I do that?
And if I want to write several lines of text, will I have to indent them all the time ?

That's why I quickly thought the language would need brackets and a symbol to distinguish markup from plain text (I used $).

Slim is somehow what I had imagined at the beginning and it's working!

The above example in Slim would be written as such:

p
  a href="http://www.google.com" more info
ul
  li
    | hey !

Not so far from what I had in mind :)

If you're not convinced yet, try this html to Slim conversion app on a heavy html page of yours that you can't really understand anymore and you'll see how amazing it is!

comment on this story

Unit Tests in Rails

posted August 2014

I've heard about unit test. They seems to be extremely important for a crypto application, but for a web app? Do I really need them?

mimoo: Hey I'm beginning with Rails, should I worry about unit test yet?
eladmeidar: tests are always important, in fact, it's more important than anything else
mimoo: it seems so boring though
fowlduck: don't bother with tests if you don't want to
fowlduck: remember to be conscious of frustration in pain while you develop, though, and try to figure out ways to avoid it later
fowlduck: you will almost certainly eventually feel frustration due to lack of tests
fowlduck: but wait until you feel it, if you want to
fowlduck: it's a good pain to feel. you don't quickly forget it
fowlduck: I like grapes
eladmeidar: i like grapes too

(extracts from #RubyOnRails on freenode)

comment on this story

Some Rails' magic.

posted August 2014

If you use Rails you know that Rails also has some pretty weird routes for default.

routes

And if you're like me you're wondering how is the browser understanding those requests. The requests are correct, and should be used but nowadays browsers only understand GET and POST queries. That's why they are often not used correctly and have lost a lot of meaning in web development.

So first, here's how Rails is doing it, by including jQuery_ujs, a jQuery plugin specially made for Rails that permits the use of those other verbs (DELETE, PUT, PATCH...).

What you write in Rails looks like that by the way.

<%= link_to 'Destroy', controller, data: {:confirm => 'Are you sure?'}, :method => :delete %>

And what it does is that it creates a link that will actually send a hidden form using POST with an input telling Rails it's actually a DELETE.

It's a hack. But it feels nice.

PS: And that's not all, jQuery_ujs does other useful things.

Rails is not a Ruby framework. It's a ruby+javascript+css framework.

comment on this story

Ruby on Rails

posted August 2014

I just pulled an all nighter on Rails. I like it. I really really like it.

I like Ruby. I like how everything makes sense. I like how things are pretty and I like how everything was setup for the perfect web developing experience.

Before that I used Django, and I didn't like it. Oh sure I didn't use it enough to have a real opinion on it. But my first impression was bad. The way it handled the static files, the namespaces ( /templates/blog/blog/ ), the way everything seemed to be made up and counter-intuitive. I didn't like it because it felt limited and forced, almost unnatural.

And well before that I used CodeIgniter, which I really like because it's dead simple and it leaves you a huge amount of liberties. This blog is made in CodeIgniter by the way.

But back to Rails. Here's what I like:

  • There is a convention. And it feels nice to be guided for once. There is a way to do things and you feel like you are coding correctly when you respect them. For example a controller should be a plural noun in CamelCase, like "Bears", and its respective model should be the singular noun, in this case "Bear". Database tables have to use the snake_case. And on and on.

  • There is beauty in ruby. The unless, the ?, the symbols, the syntax, the blocks, the... I like it. It looks beautiful and feels good to write.

  • Everything is already prepared for you. There is CoffeeScript, Sass, jQuery, jQuery-ujs, turbo-link, ... and There is so much that is just waiting to be served. A gem away.

I feel dumb for having waited so long to take the leap. I heard of Rails years ago but couldn't see the point. I remember thinking Ruby was weird. Boy... Things are gonna get so much easier when I'll have a good understanding of this beast!

comment on this story

Where have I been?

posted August 2014

I've been on holidays, not doing much, mostly enjoying what is sadly my very last summer holiday sheds a tear.

But as usual, when I'm not productive I get all grumpy and I feel like I'm losing precious time.

Before holidays: "I'll have plenty of time to learn and code!"
During holidays: "Man I'm just gonna watch another episode of this new tv show"

So these past few weeks I chose to put android, google glass, unity and oculus asides. I'll deal with them later.

Now It's time to learn. And you can't create without learning the technologies first!

So the first thing I did was take a look at React and MongoDB. After spending a few hours with React I knew I didn't need it and fell in love with Angular. MongoDB seems pretty cool and it's my first time with a noSQL database (I followed the awesome Andrew Burgess tutorial on Tutsplus).

I've been reading a lot about Rails lately and I'm trying to gather all the info I need before starting my next project which will involve those technologies that I've never used before:

  • Ruby on Rails

  • Slim

  • Angular

  • CoffeeScript

I already have a simple but useful project in mind.

I start school on September the 1st and I also want to be able to spend a week with GOlang before having too many things to do what I want.

comment on this story

BadUSB

posted August 2014

An interesting read about how any usb device could be a potential threat. Some scary extracts:

Once reprogrammed, benign devices can turn malicious in many ways, including:

  • A device can emulate a keyboard and issue commands on behalf of the logged-in user, for example to exfiltrate files or install malware. Such malware, in turn, can infect the controller chips of other USB devices connected to the computer.
  • The device can also spoof a network card and change the computer’s DNS setting to redirect traffic.
  • A modified thumb drive or external hard disk can – when it detects that the computer is starting up – boot a small virus, which infects the computer’s operating system prior to boot.

And a scarier one...

No effective defenses from USB attacks are known.

Once infected, computers and their USB peripherals can never be trusted again.

Some proof of concept should be introduced in a week at the incoming Black Hat convention. This is gonna be good :)

EDIT:

There's actually something similar that you can already buy: The USB Rubber Duck

rubber duck

comment on this story

80s computer hacking: A Supercut

posted July 2014

Pretty funny, and it's sad to see that it hasn't evolved much (besides some rare exceptions like 24 or The Social Network). For example that hacking scene in the last James Bond Skyfall. Never forget.

comment on this story

A Conversation with Elon Musk

posted May 2014

I've always disliked paypal but after watching that video I have a new image of Elon Musk. The guy is pretty humble, clever and knows how to explain an idea. The opposite of a Linus Torvald.

What's also really amazing to me is how diversified his vocabulary is. Here are some words I learned thanks to this video:

  • belabor: argue or elaborate (a subject) in excessive detail.

  • farcical: of or resembling a farce, especially because of absurd or ridiculous aspects.
comment on this story

Coinbase: 10$ of bitcoins for students in the US

posted May 2014

If you're a college student in the US today might be your lucky day. Coinbase is offering 10$ in bitcoin to students from some american universities. I guess if yours is not accepted you can ask them directly.

To support bitcoin awareness among college students, today we are announcing a bitcoin giveaway: we are gifting $10 worth of bitcoin to students who create a new Coinbase account using their .edu email address.

Here you go

comment on this story

Bruteforce Apr1 hashes.

posted May 2014

One of my professor organized a Hacking Week this semester but I didn't have time to do it. Since I'm in holidays I thought I would take a look at it and write a bit about how I solved them.

Here's the Crypto Challenge number 2 (out of 5) from this CTF (Capture The Flag):

user0:$apr1$oTsx8NNn$bAjDZHpM7tCvHermlXKfZ0 user1:$apr1$UxOdpNtW$funTxZxL/8y3m8STvonWj0
user2:$apr1$w7YNTrjQ$0/71H7ze5o9/jCnKLt0mj0 user3:$apr1$AIw2h09/$Ti0TRlU9mDpCGm5zg.ZDP. user4:$apr1$048HynE6$io7TkN7FwrBk6PmMzMuyC. user5:$apr1$T2QG6cUw$eIPlGIXG6KZsn4ht/Kpff0 user6:$apr1$2aLkQ0oD$YRb6aFYMkzPoUCj70lsdX0

You have 7 different users with their respective password hashed and you have to find them. It's just the 2nd out of 5 crypto problems, it's pretty basic, but I never brute forced passwords for real before (I remember using John The Ripper when I was in middle school but that's for script kiddies).

What's Apr1 ? It's a hash function that uses md5. And md5 is pretty weak, lots of rainbow tables on google.

This is how Apr1 looks in PHP according to Wikipedia, also the passwords are supposed to be alpha (a to z) in lowercase.

function apr1($mdp, $salt) {
    $max = strlen($mdp);
    $context = $mdp.'$apr1$'.$salt;
    $binary = pack('H32', md5($mdp.$salt.$mdp));
    for($i=$max; $i>0; $i-=16)
        $context .= substr($binary, 0, min(16, $i));
    for($i=$max; $i>0; $i>>=1)
        $context .= ($i & 1) ? chr(0) : $mdp{0};
    $binary = pack('H32', md5($context));
    for($i=0; $i<1000; $i++) {
        $new = ($i & 1) ? $mdp : $binary;
        if($i % 3) $new .= $salt;
        if($i % 7) $new .= $mdp;
        $new .= ($i & 1) ? $binary : $mdp;
        $binary = pack('H32', md5($new));
    }
     $hash = '';
    for ($i = 0; $i < 5; $i++) {
        $k = $i+6;
        $j = $i+12;
        if($j == 16) $j = 5;
        $hash = $binary{$i}.$binary{$k}.$binary{$j}.$hash;
    }
    $hash = chr(0).chr(0).$binary{11}.$hash;
    $hash = strtr(
        strrev(substr(base64_encode($hash), 2)),
        'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
        './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    );
    return '$apr1$'.$salt.'$'.$hash;
}

It seems pretty difficult to reverse. Let's not forget that hashes are one-way functions and that they also lose information. I don't know if they do lose information on a 7-letters-password though, but it seemed quite stupid to go down this road when I could just brute force it.

What language offers a good library to hash with Apr1? Well I didn't know, and I felt like maybe Unix could do it well for me.

Turns out that OpenSSL has a command line for it:

openssl passwd -apr1 -salt SALT PASSWD

A quick bash script later:

#!/bin/bash

test[1]='$apr1$oTsx8NNn$bAjDZHpM7tCvHermlXKfZ0'
salt[1]='oTsx8NNn'

test[2]='$apr1$UxOdpNtW$funTxZxL/8y3m8STvonWj0'
salt[2]='UxOdpNtW'

test[3]='$apr1$w7YNTrjQ$0/71H7ze5o9/jCnKLt0mj0'
salt[3]='w7YNTrjQ'

test[4]='$apr1$AIw2h09/$Ti0TRlU9mDpCGm5zg.ZDP.'
salt[4]='AIw2h09/'

test[5]='$apr1$048HynE6$io7TkN7FwrBk6PmMzMuyC.'
salt[5]='048HynE6'

test[6]='$apr1$T2QG6cUw$eIPlGIXG6KZsn4ht/Kpff0'
salt[6]='T2QG6cUw'

test[7]='$apr1$2aLkQ0oD$YRb6aFYMkzPoUCj70lsdX0'
salt[7]='2aLkQ0oD'

while read line          
do          
    if [ "${#line}" == 7 ]
    then
    for num in {1..7}
    do
        noob=$(openssl passwd -apr1 -salt $salt[$num] $line)
        if [ "$noob" == "$test[$num]" ];
        then
        echo $line;
        fi
    done
    fi
done < /usr/share/dict/words

I read the /user/share/dict/words that contains a simple dictionary of words on Unix, I try only the 7-letters-words.

The test ran in a few minutes and gave me nothing.

Well, I guess with a 7 letters password they must have used gibberish words. Let's try a real bruteforce:

for a in {a..z}
do
    for b in {a..z}
    do
        for c in {a..z}
        do
            for d in {a..z}
            do
                for e in {a..z}
                do
                    for f in {a..z}
                    do
                        for g in {a..z}
                        do
                            truc=$a$b$c$d$e$f$g;

                            for num in {1..7}
                            do
                            noob=$(openssl passwd -apr1 -salt $salt[$num] $truc)
                            if [ "$noob" == "$test[$num]" ];
                            then
                                echo $truc;
                            fi
                            done
                        done
                    done
                done
            done
        done
    done
done

It ran and ran and... nothing.

Well. Let's not spend too much on this. There is John The Ripper that does this well and even oclHashcat that does this with the GPU.

Let's create a john.conf with the following to limit the password to 7 letters:

[Incremental:Alpha7]
File = $JOHN/alpha.chr
MinLen = 7
MaxLen = 7
CharCount = 26

Let's launch John:

john -i=Alpha7 hackingweek.txt

(don't forget to put the hashed password in hackingweek.txt).

Wait and wait and wait.. and get the passwords =)

comment on this story