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.

Tags: , , ,

31 Responses to “Cracking PIN and Password Locks on Android”

  1. Android Pattern Lock Stumps FBI « « Fix-Singh - Gadget RepairsFix-Singh – Gadget Repairs Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  2. Android Pattern Lock Stumps FBI | Tux Doc Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  3. Android Pattern Lock Stumps FBI | Got2.Me Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  4. Android Pattern Lock Stumps FBI | Android News Center Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  5. Android Pattern Lock Stumps FBI | News24 Says:

    [...] the FBI is apparently "unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques" to crack the phone and grab its [...]

  6. Android Pattern Lock Stumps FBI | androidless.net Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  7. Android pattern lock flummoxes FBI | Android News Center Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  8. Android Pattern Lock Stumps FBI Says:

    [...] is apparently “unable, or reluctant to use commercially accessible forensics collection or widely documented hardware-hacking techniques” to moment a phone and squeeze a data.Soghoian also wonders if a [...]

  9. Android Pattern Lock Stumps FBI Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  10. Android Pattern Lock Stumps FBI | GambiaPage Says:

    [...] the FBI is apparently “unable, or unwilling to use commercially available forensics tools or widely documented hardware-hacking techniques” to crack the phone and grab its [...]

  11. Int'l Bleu Says:

    I just hope Google doesn't allow the FBI to mock the us constitution which was set forth by our forefathers. it's more than them trying to unlock a pimp's smart phone riding on this. there are laws that govern this kinda stuff. what about due-process? due-diligence? right to privacy? search and seizure? i've read many of articles and the actual search warrant regarding this matter, it's all he say-she say, or mere assumptions from this man's past-life. nothing factual.

    if Google provides entry into this man's phone; where does our society go from here? who's rights are violated next? o yea, it will continue, think about it.... IT MAY BE YOU! are we a democracy? or, are we becoming a communist society? hnmmmmm!

  12. El bloqueo por patrón de Android detiene al FBI » PC World Perú Says:

    [...] quiere usar herramientas forenses disponibles a nivel comercial o técnicas para hackear hardware ampliamente documentadas” para crackear el teléfono y obtener la [...]

  13. Anonymous Says:

    [...] Dank,Greetz Lp Das Video ist nicht auffindbar.Eventuell spielst Du aber auf diesen Link hier an?http://forensics.spreitzenbarth.de/2012/02/28/cracking-pin-and-password-locks-on-android/T S__________Ich bin ein [...]

  14. SaferMobile » Monday’s Mobile Threat Report Says:

    [...] very first line of defense, but know that this is a feeble defense at best.  Handy instructions on how to crack a pin and password lock on Android are here, how to crack the pattern lock here, and for good measure, see how easy it is to crack the face [...]

  15. floyd Says:

    Didn't you mean:

    If you deal with a rooted smartphone OR USB debugging is enabled

    Because USB debugging allows easy access to all files with adb shell. Rooted phones often have options to make entire file system backups to the SD card (just boot into backup mode). I'm refering to: "just have to dump the file". How?

  16. mspreitz Says:

    Hi floyd,

    no, in this case I really mean AND.

    when you have a rooted device with enabled USB debugging you can run the following commands on your PC (with installed android sdk):
    $ adb pull /data/system/password.key .
    $ adb pull /data/data/com.android.providers.settings/databases/settings.db .

    if your device is not rooted and USB debugging is enabled this commands won't work because you have insufficient access rights for these two folders

    if you have a rooted device with disabled USB debugging you can try to enable USB debugging remotely or boot into recovery mode, create a android backup and restore the files from this backup on your local PC.

    I hope this answers your question.

    Best Regards,
    Michael

  17. Apple und Google entsperren Smartphones für Strafverfolger | Edv-Sicherheitskonzepte.de – News Blog aus vielen Bereichen Says:

    [...] Zugriffsschutz auszuhebeln. Auch bei einigen Android-Versionen ist bereits dokumentiert, wie man Passcode-Sperre und Mustereingabe umgeht. Während der Aufwand bei gerooteten Geräten überschaubar ist, ist das [...]

  18. Whatevs Says:

    @mspreitz

    If the phone is rooted, debugging disabled, encrypted using ICS, does that change anything?

  19. mspreitz Says:

    @Whatevs

    unfortunately, I hadn't the chance to get my hands on an ICS running device yet.

  20. PsyRead Says:

    For a newbie but interested in learning this. Can you tell me how to bruteforce ?

    Some automatic way to bruteforce using the information acquired?

    My phone is nexus S, rooted and running Icecream sandwitch

  21. mspreitz Says:

    Hi PsyRead,

    I'm not sure how it is working in ICS, as far as I have seen the Android source code right now, there were some modifications in this area (e.g. no md5 hashes anymore) and our own scripts inside ADEL are not working with Android 4.0.3 at present. But we are working on a solution!

    Best regards,
    Michael

  22. Geo Says:

    Hi Michael,

    In your comments, you're talking about enable USB debugging remotely. How is it possible? Do you have some example?

    Thanks a lot
    Geo

  23. Cracking The Pattern Lock Android Says:

    [...] [...]

  24. “Handy” ist out, jetzt ist Smartphone – die neue Gefahr? Says:

    [...] ge”JTAG”ten Android-Phones zeigt, wie schnell man um die Passwortsperre herumkommt: Link. Doch ist es schon einmal eine erstes Hindernis und man kann nicht mal eben an das Ding heran, [...]

  25. Jon Says:

    If the phone is already locked, how do you get access to these files?

  26. mspreitz Says:

    It depends on the phone type. On some phones you can use a modified recovery image with enabled adb and root shell, on other phones you can extract this data with the help of a JTAG box.

  27. sobieski Says:

    How do you perform the brute force operation?

    I tried writing a script calling GetBytes(20) against the .NET library Security.Cryptography.PasswordDeriveBytes, but I'm not sure how many iterations to use in the constructor.

  28. Jeff Pang Says:

    Is there any way to take the dump, overwrite the password and the salt with your own generated password and salt, then upload it back into the phone?

  29. TouchID is Hacked, But Wait a Minute…cmdshft.com | cmdshft.com Says:

    […] to truly not be a hypocrite ask if it’s easier than hacking Android’s security measures. No, it […]

  30. Anthony Says:

    I got the device rooted through a custom Odin download and can access a rooted adb shell from stock recovery. However, it seems that I still can't access /data/system or /data/data. It appears like they're not there. USB debugging isn't enabled and that's the entire reason why I'm attempting to access it via recovery adb shell. Have any ideas before I whip out the JTAG interface?

  31. mspreitz Says:

    Hi Anthony,

    have you tried to mount these partitions manually?

    Best regards,
    Michael

Leave a Reply


%d bloggers like this: