01 Aug

Breaking B0rken ElGamal KeygenMe, part 2

In part 1 of the tutorial, I explained how badly initialized PRNG causes a serious problems and allows us to find the private key. In this part of tutorial, I’ll show how to use another weakness in crackme to find private key without using bruteforce.

Looking deeper in the keygenme

If you analyze serial check in more details, you’ll notice part of code that processes blacklisted names and keys:

It looks like we have 2 blacklisted names & corresponding serials. We can quickly patch crackme and verify that these names and serials really work.

But did you also notice that those serials look a lot alike? That’s weird.. Let’s take a closer look at them:

First part of the serial is exactly the same! Let’s go back to ElGamal basics and think a bit.

Revisiting ElGamal signing algorithm

I’m sure you already remember the algorithm from my previous blog post. But here it is again.. smile

To sign a message M, one would:

  • Make a hash of message, H(M). In this crackme, it’s SHA1 of the username
  • Generate a random number K where K<P-1
  • Calculate R=G^K (mod P)
  • Calculate S=(H(M)-RX)*K^(-1) (mod P-1)
  • The signature will be the pair C(R,S).

So, in our case, R is always the same. G and P are hardcoded in the crackme. That means.. K is always the same. And that doesn’t sound right! smile

Quick look in Wikipedia confirms that it’s a really bad idea:

The signer must be careful to choose a different k uniformly at random for each signature and to be certain that k, or even partial information about k, is not leaked. Otherwise, an attacker may be able to deduce the secret key x with reduced difficulty, perhaps enough to allow a practical attack. In particular, if two messages are sent using the same value of k and the same key, then an attacker can compute x directly.[1]

The problem of reusing k and the attack itself is explained in Stinson’s “Cryptography Theory And Practice”, pages 290-291. Unfortunately, normal person has no chance to understand that “explanation”.

Few Google searches later I found 2 writeups from Boston Key Party CTF 2015 which were slightly better:

So, let’s try to get something useful out of them.

Reused K and recovering private key

As I said earlier, cryptography is a dark magic – if you don’t spend years studying it, you can’t understand it. So, I just took the code from those CTF writeups and added few more comments to it. And no, I still don’t know why it works. smile


Wise man once said:

Knowledge is of two kinds. We know a subject ourselves, or we know where we can find information upon it.

You don’t need to be a crypto wizard to solve crackmes – you just need to know where to find the necessary information. But if you’re implementing cryptography in your software, better ask someone who understands those things.

Have fun!

3 thoughts on “Breaking B0rken ElGamal KeygenMe, part 2

  1. Thank you soooooo much kao!
    Crypto is something REALLY scary for me!

    You explained two very interesting approaches … but I’ll need to sit down with a good coffee thermos and read it again and again, lol :P

    Thanks a lot for sharing your knowledge.

    Best Regards,

    P.S. Of course, greetings to SmilingWolf too :)

Leave a Reply

Your email address will not be published.

nine + six ==