Cracking PIN and Password Locks on Android

As you may know it is possible to get around the pin and password lock on an Android smartphone. In this post we will describe the following two ways to get around it:

  • on a rooted smartphone
  • with the help of the JTAG interface

Some Background Information

Since version 2.2 Android provides the option of a numeric PIN or alphanumeric password as an alternative to screen lock. Both pass phrases are required to be between 4 and 16 digits or characters in length.

     

Android stores this pattern in a special file called password.key in /data/system/. As storing the pattern in plain text wouldn’t be very save, this time Android stores an salted SHA1-hashsum and MD5-hashsum of the PIN or password. The numeric PIN and the alphanumeric passwords are processed in the same way (see the following code snippet).

 public byte[] passwordToHash(String password) {
        if (password == null) {
            return null;
        }
        String algo = null;
        byte[] hashed = null;
        try {
            byte[] saltedPassword = (password + getSalt()).getBytes();
            byte[] sha1 = MessageDigest.getInstance(algo = "SHA-1").digest(saltedPassword);
            byte[] md5 = MessageDigest.getInstance(algo = "MD5").digest(saltedPassword);
            hashed = (toHex(sha1) + toHex(md5)).getBytes();
        } catch (NoSuchAlgorithmException e) {
            Log.w(TAG, "Failed to encode string because of missing algorithm: " + algo);
        }
        return hashed;
}

Due to the fact that the hash is salted this time, its unfeasible to crack the password with help of a dictionary attack. For cracking the password it is important to get the salt and enough time for attempting a brute force attack. The salt is a string of the hexadecimal representation of a random 64-bit integer. To get this salt, there are two ways from which you can choose.

On a Rooted Smartphone:

If you deal with a rooted smartphone and USB debugging is enabled, cracking of the pattern lock is quite simple. You just have to dump the file /data/system/password.key and the salt, which is stored in a SQLite database under the lockscreen.password_salt key. The corresponding database can be found in /data/data/com.android.providers.settings/databases and is called settings.db (see the figure below). After you got both information you just need to start brute forcing the password.

With the Help of the JTAG Interface:

If you deal with a stock or at least unrooted smartphone this whole process is a bit more complicated. First of all, you need special hardware like a Riff-Box and an JIG-adapter or some soldering skills. After you have gained a physical dump of the complete memory chip the chase for the password lock can start. To find the hashsums of the passphrase you need to have the following points in mind:

  • The dump of the memory is broken into chunks of 2048 bytes
  • The password.key file contains two hashes, together 72 bytes long:
    • a SHA-1 hash (20 bytes long)
    • a MD5 hash (16 bytes long)
  • These hashes only contain the characters 0-9 and A-F
  • The following 1960 bytes of the chunk are zeros
  • The remaining 16 bytes of the chunk are random

Finding the SQLite-database an the salt in it is way harder as finding the hashes. As SQLite stores all data in plain text we have one first reference point – the lockscreen.password_salt string. When we find this string in our dump, we should be very close to the actual salt. At this point it is important to understand the SQLite-File-Format.

Using this information we can create two rulesets to find the position of the salt as well as the actual salt (refer to the figure below for a better understanding):

  • Search for the string “lockscreen.password_salt”.
  • The byte directly in front has to be between 0x0F and 0x35. This byte represents the length of our salt and is called byteA for a better understanding of the rest of this article.
  • In front of this byte, there has to be a byte with 0x3D (indicates a serial type representing a string with a length of 24). This is the length of our string we searched for.
  • In front of this byte has to be a zero byte

If the first ruleset applies, we have found the right position in our dump and we can now start to extract the salt.

  • Decoding byteA gives us the length of the salt and has to between 1 and 20 bytes.
  • Now we have to extract this amount of bytes directly after the string “lockscreen.password_salt”
  • These bytes are the salt!

After we got both information (hashes and salt) we can again start our brute force attack! In our test we could crack PIN’s (with up to 10 digits) and simple passwords (with up to 5 chars) within one hour.

Update:

Have a look at the update to this post for more resent information and automated scripts!

54 Replies to “Cracking PIN and Password Locks on Android”

  1. hi there i have a Samsung Galaxy s4 phone model SGH-i337m (canada version on rogers network)
    OS is 4.2.2 jellybean
    It is password protected with numbers, not a pattern
    It is not rooted and USB debugging is not enabled

    I have tried everything such as Odin, external SD card flash, sdk tools recovery, shell commands, TWRP, CWM, DrFone, androidPatternLockMaster, etc. I believe my last option (unless i’ve missed something) is a Jtag. I however don’t possess the hardware or the knowledge of how to do this.

    The phone has very valuable information and photos to me. Please assist me. Thanks in advance

  2. hi,i’m confused with the JTAG connection.why should i immediately dump the flash when the phone is just reboting.thanks for reply.

  3. sorry dear i dont understand the following examples …….! ?????????

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.