One mistake, Three bugs: Comprehensive android pentesting.
The Android app's PIN lock stored its hash and salt in shared preferences with a hardcoded salt, and the attempt counter accepted negative or huge values. An attacker with a rooted device could brute-force the PIN, set a new PIN, or disable the lock entirely.
Published on medium.com
Kushal Jain
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
The target was a private bug bounty program, so the write-up is deliberately generalised and the app itself is not named. The only product detail given is that it belonged to the finance category. The feature under examination was the app's built-in applock: the screen that asks for a PIN before letting the user back into the application. It was the one part of the workflow that did not send any request to the server, which is what made it interesting.
The general point the author makes at the start is that API traffic is the usual place to hunt, but activities, intents and other client-side code can be vulnerable to bad coding practices even when the API is fine.
Setup
The work was done against an Android application from a private program. The tool used to read the compiled code was jadx. The precondition for every exploit described below is a rooted Android device, which the author acknowledges and which is why the findings were rated low severity.
Finding it
While walking through the application's workflow, the author noticed that the applock was the only feature that never sent a request to the server. That absence was the flag: if the check is not server-side, it is an activity, and the implementation has to be read in the code to understand it. When the APK was opened in jadx, the code was not fully obfuscated and was human readable.
Digging further, the app turned out to use lollipin, a PIN code library for Android apps. The library's documentation states that it stores the PIN's hash on the device itself, under the shared preferences folder. That is where the review moved from the app's own code to the library's storage format.
Exploitation
Analysing the application code revealed a parameter, DEFAULT_PASSWORD_SALT, holding a hardcoded value that was used to generate the salt for the SHA-1 hash of the PIN. The hashing method itself was also present in the code. The XML file in which lollipin stored its data completed the picture. Three separate issues followed from this one design.
Step 1: Reverse engineering the stored hash
The application was vulnerable to reverse engineering. When a user sets a PIN, a random SALT_PREFERENCE_KEY, an array of integers, is generated. When the user removes the PIN or sets a different one, SALT_PREFERENCE_KEY stays the same, so every new PIN is hashed with the same salt. Because the same SALT_PREFERENCE_KEY is reused for every PIN, an attacker can reverse engineer it and change the stored hash to a hash of their choosing. The attacker can therefore set any PIN they want, which changes the behaviour of the app lock.
Step 2: Brute forcing past the attempt limit
The intended workflow gives the user five chances to enter the correct PIN before being logged out of the app. The XML file, however, contains a parameter NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEY that is incremented by one on every wrong PIN. Once it reaches five, the app logs the user out. The flaw is that the value accepts negative integers. Setting it to -10,000 leaves enough attempts to brute force a 4-digit PIN, because each wrong guess only moves the counter one step closer to five.
Step 3: Buffer overflow to disable the lock
The same parameter, NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEY, could be given an exceptionally large integer, for example:
1000000000000000000000000
The application could not handle that value and disabled the app lock entirely, so the user was no longer asked for a PIN to get into the app. In addition, the attacker could set a new PIN from the app without knowing the old one, and SALT_PREFERENCE_KEY would still be the same when the new PIN was set up.
Root cause
The three issues all stem from how the PIN state is stored and handled on the device, in shared preferences. The salt used for the SHA-1 hash is hardcoded in DEFAULT_PASSWORD_SALT, and the hashing routine is readable in the code, so the stored hash can be recomputed and replaced. SALT_PREFERENCE_KEY stays the same when the PIN is removed or changed, so every new PIN is hashed with the same salt. The attempt counter NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEY is an unvalidated integer: it accepts negative values, which defeats the five-attempt limit, and it accepts values large enough that the app cannot handle them, which disables the lock.
Impact
An attacker with a rooted device could set an arbitrary PIN, brute force a 4-digit PIN despite the five-attempt limit, or bypass the app lock completely and set a new PIN without knowing the old one. All three findings were rated low severity because the only prerequisite was a rooted Android device. The author received a total of $600 for the three findings.
Fix
The report does not describe any fix that was applied.
Variants to test
- Look for features that make no network request at all, such as app locks, local authentication and onboarding screens. A positive result is a security decision that is made entirely in client code.
- Decompile with jadx and search for third-party authentication libraries. A positive result is a library whose storage format and hashing routine are documented and readable in the decompiled code.
- Inspect the shared preferences XML for hardcoded salts and hash parameters. A positive result is a salt constant in the code that matches the value used to produce the stored hash.
- Change the PIN and compare the salt in the XML before and after. A positive result is an unchanged salt across PIN changes.
- Edit the attempt counter to a negative value and make wrong guesses. A positive result is a counter that keeps incrementing without ever reaching the logout threshold.
- Edit the attempt counter to an exceptionally large integer. A positive result is a lock that disables itself instead of rejecting the value.
What happened, in plain words
A security researcher was testing an Android app from a private bug bounty program. A bug bounty program is a scheme where a company invites independent testers to look for security problems in its products and pays them for the ones they find. Because the program was private, the researcher could not name the app or the company. The only detail given is that the app belonged to the finance category, meaning it deals with money in some way.
The researcher found three separate security problems in one feature: the app's built-in applock. An applock is the screen that asks for a PIN (a short numeric password) before letting you back into the app. The three problems were:
- The stored PIN could be reverse engineered and replaced, so an attacker could set any PIN they wanted.
- The limit of five wrong PIN attempts could be bypassed by editing a counter, so an attacker could try thousands of PINs.
- A very large number in that same counter could switch the applock off completely.
All three were rated low severity, because the only way to carry them out was on a rooted Android device. Rooting is the process of removing the manufacturer's restrictions on an Android phone, giving the owner full control over the system, including files that normal apps cannot touch. The researcher received a total of $600 for the three findings.
The rest of this article walks through how the researcher found each problem, in the same order, explaining every concept from scratch along the way.
Why look beyond the network traffic
Most security testing of a mobile app focuses on the API. An API (Application Programming Interface) is the set of addresses and rules an app uses to talk to its server. Think of it like a restaurant: the app is the customer, the server is the kitchen, and the API is the waiter who carries orders back and forth. Testers usually watch what the waiter carries, because that is where data leaks and permission mistakes tend to show up.
But the researcher makes a broader point at the start: sometimes the API is perfectly fine, and the problem is in the app's own code on the phone. In Android, the pieces of an app that a user sees and interacts with are called activities. An activity is roughly one screen of the app, such as the login screen or the settings screen. Intents are the messages Android uses to move between activities, for example when one screen asks another to open. Both activities and intents can contain security mistakes if the code was written carelessly, even when every server request is secure.
So the lesson is: do not only watch the waiter. Sometimes the problem is in the dining room.
Finding the interesting feature
The researcher walked through the app's workflow, meaning the normal sequence of screens a user goes through, and noticed something odd. The applock was the only feature that never sent a request to the server. Every other feature talked to the server at some point; the applock did not.
That absence was the clue. If a security check never asks the server, then the server cannot be enforcing it. The check must be happening entirely on the phone, inside an activity, which means the logic lives in the app's code. To understand it, the researcher had to read that code.
To read the code of an Android app, you first need the app's installation file, called an APK (Android Package). An APK is like a zip archive containing the compiled app. The compiled code is not plain text, so a special tool is needed to turn it back into something readable. The researcher used a free tool called jadx, which takes an APK and produces readable source code.
When the APK was opened in jadx, the code was not fully obfuscated. Obfuscation is a technique where developers deliberately rename things in their code to meaningless labels, so that anyone reading it has a harder time understanding what it does. It is like writing a recipe in code words: "add 200 units of ingredient X" instead of "add 200 grams of flour". In this app, the obfuscation was incomplete, so the code was human readable.
Digging further, the researcher found that the app used lollipin, a third-party library for Android apps that provides a PIN code screen. A library is a bundle of ready-made code that developers can drop into their app instead of writing a feature from scratch. Using a library saves time, but it also means the library's behaviour becomes part of your app's behaviour.
The lollipin documentation states that it stores the PIN's hash on the device itself, in the shared preferences folder. Two concepts need explaining here.
A hash is the result of feeding data through a one-way mathematical function. You can turn a PIN into a hash easily, but you cannot turn the hash back into the PIN. It is like putting a document through a shredder: you can shred it in seconds, but you cannot reassemble it. Because of this, apps often store hashes instead of the original PIN, so that even if someone reads the stored value, they do not learn the PIN.
A salt is a random piece of extra data mixed into the PIN before hashing. Without a salt, the same PIN always produces the same hash, so an attacker could compare a stolen hash against a precomputed list of hashes for common PINs. With a salt, the same PIN produces a different hash for each different salt, which makes that comparison useless. The salt is not secret in principle, but it should be random and unique.
Shared preferences is Android's built-in storage for small pieces of app data, such as settings and flags. It is stored as an XML file (a text format that uses tags to structure data) inside the app's private data folder. On a normal phone, other apps cannot read that folder. On a rooted phone, the owner can.
That is where the review moved from the app's own code to the library's storage format.
Reading the code
Analysing the application code revealed a parameter called DEFAULT_PASSWORD_SALT, holding a hardcoded value. A parameter here means a named value in the code. Hardcoded means the value is written directly into the source code by the developer, rather than being generated fresh each time. That value was used to generate the salt for the SHA-1 hash of the PIN.
SHA-1 is a specific hashing algorithm, one of several standard formulas for turning data into a fixed-length fingerprint. The important point is that the hashing method itself was also present in the code, so anyone reading the code could see exactly how a PIN becomes a stored hash.
The XML file in which lollipin stored its data completed the picture. Between the hardcoded salt, the visible hashing method, and the readable storage file, the researcher had everything needed to understand and then manipulate the stored PIN. Three separate issues followed from this one design.
Step 1: Reverse engineering the stored hash
The first issue was that the application was vulnerable to reverse engineering. Reverse engineering means taking something apart to understand how it works, in this case reading the compiled app to recover its logic. The researcher describes the app as vulnerable to it because the code was readable and the storage format was documented.
Here is the mechanism. When a user sets a PIN, a random SALT_PREFERENCE_KEY is generated. This is an array of integers, meaning a list of whole numbers. When the user removes the PIN or sets a different one, SALT_PREFERENCE_KEY stays the same. So every new PIN is hashed with the same salt.
With the hardcoded DEFAULT_PASSWORD_SALT and the hashing method visible in the code, an attacker can recompute the stored hash and replace it with a hash of a PIN of their choosing. In plain terms: the attacker can work out what the app expects to find in its storage file, write their own value there, and the app will accept it. The attacker can therefore set any PIN they want, which changes the behaviour of the app lock.
Why does this matter? Because the whole point of the applock is that only the person who knows the PIN can get past it. If the stored value can be replaced, the lock no longer proves anything about who is holding the phone.
Step 2: Brute forcing past the attempt limit
The second issue was brute forcing. Brute forcing means trying every possible value until one works. For a 4-digit PIN there are only 10,000 possibilities (0000 to 9999), which a computer can try very quickly if nothing stops it.
The intended workflow gives the user five chances to enter the correct PIN before being logged out of the app. That is a sensible defence: five guesses out of 10,000 is nowhere near enough to find the PIN by luck.
The XML file, however, contains a parameter called NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEY that is incremented by one on every wrong PIN. Incremented means increased by one. Once it reaches five, the app logs the user out.
The flaw is that the value accepts negative integers. Integers are whole numbers, and negative integers are the ones below zero, such as -1 or -10,000. Setting the counter to -10,000 leaves enough attempts to brute force a 4-digit PIN, because each wrong guess only moves the counter one step closer to five. Starting at -10,000, an attacker gets far more wrong guesses than the 10,000 possible PINs before the logout triggers.
Think of it like a bouncer who counts how many times you have been turned away, and throws you out when the count reaches five. If you can start the count at minus ten thousand, you can be turned away ten thousand times before the bouncer notices.
Step 3: Buffer overflow to disable the lock
The third issue was a buffer overflow. A buffer is a fixed-size area of memory set aside to hold a value. A buffer overflow happens when a value is too large for that area, so it spills over into places it was not meant to go. In this case, the same parameter, NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEY, could be given an exceptionally large integer, for example:
1000000000000000000000000
This is a one followed by twenty-four zeros, far larger than any normal attempt counter would ever need to be. The application could not handle that value and disabled the app lock entirely, so the user was no longer asked for a PIN to get into the app. In addition, the attacker could set a new PIN from the app without knowing the old one, and SALT_PREFERENCE_KEY would still be the same when the new PIN was set up.
Why does this matter? Because it turns a lock into no lock at all. The attacker does not need to guess the PIN or replace the hash; they simply make the app forget that it should be asking.
The common root cause
All three issues come from how the PIN state is stored and handled on the device, in shared preferences. The app never asks the server whether the PIN is correct, so the server cannot enforce any policy.
Breaking that down:
- The salt used for the SHA-1 hash is hardcoded in
DEFAULT_PASSWORD_SALT, and the hashing routine is readable in the code, so the stored hash can be recomputed and replaced. SALT_PREFERENCE_KEYstays the same when the PIN is removed or changed, so every new PIN is hashed with the same salt.- The attempt counter
NUMBER_OF_PASSCODE_ATTEMPTS_PREFERENCE_KEYis an unvalidated integer. Unvalidated means nobody checks whether the value makes sense. It accepts negative values, which defeats the five-attempt limit, and it accepts values large enough that the app cannot handle them, which disables the lock.
Impact
An attacker with a rooted device could set an arbitrary PIN, brute force a 4-digit PIN despite the five-attempt limit, or bypass the app lock completely and set a new PIN without knowing the old one.
All three findings were rated low severity because the only prerequisite was a rooted Android device. That is a fair rating: the only prerequisite was a rooted Android device. The author received a total of $600 for the three findings.
Fix
The report does not describe any fix that was applied.
In plain terms: the phone should not be the only judge of whether a PIN is correct. The server, or at least a part of the system the user cannot edit, should have a say.
Variants to test
If you want to look for similar problems yourself, these are the checks the findings suggest:
- Look for features that make no network request at all, such as app locks, local authentication and onboarding screens. A positive result is a security decision that is made entirely in client code.
- Decompile with jadx and search for third-party authentication libraries. A positive result is a library whose storage format and hashing routine are documented and readable in the decompiled code.
- Inspect the shared preferences XML for hardcoded salts and hash parameters. A positive result is a salt constant in the code that matches the value used to produce the stored hash.
- Change the PIN and compare the salt in the XML before and after. A positive result is an unchanged salt across PIN changes.
- Edit the attempt counter to a negative value and make wrong guesses. A positive result is a counter that keeps incrementing without ever reaching the logout threshold.
- Edit the attempt counter to an exceptionally large integer. A positive result is a lock that disables itself instead of rejecting the value.
Glossary
- API: the set of addresses and rules an app uses to talk to its server.
- APK: the installation file of an Android app, a package containing the compiled code.
- Activity: one screen of an Android app, such as the login screen or the settings screen.
- Applock: a screen that asks for a PIN before letting the user back into the app.
- Brute forcing: trying every possible value until one works.
- Buffer: a fixed-size area of memory set aside to hold a value.
- Buffer overflow: a situation where a value is too large for its buffer and spills into places it was not meant to go.
- Bug bounty program: a scheme where a company invites independent testers to find security problems and pays them for the ones they find.
- Client code: the part of an app that runs on the user's device, as opposed to the server.
- Decompile: turn a compiled app back into readable source code.
- Hardcoded: written directly into the source code by the developer, rather than generated fresh each time.
- Hash: the result of feeding data through a one-way mathematical function, easy to compute but not reversible.
- Incremented: increased by one.
- Integer: a whole number, positive, negative or zero.
- Intent: the message Android uses to move between activities.
- jadx: a free tool that turns an Android APK into readable source code.
- Library: a bundle of ready-made code that developers can drop into their app instead of writing a feature from scratch.
- lollipin: a third-party Android library that provides a PIN code screen.
- Obfuscation: renaming things in code to meaningless labels so that readers have a harder time understanding it.
- Parameter: a named value in code.
- PIN: a short numeric password.
- Reverse engineering: taking something apart to understand how it works, in this case reading a compiled app to recover its logic.
- Rooted device: an Android phone whose manufacturer restrictions have been removed, giving the owner full control over the system, including files normal apps cannot touch.
- Rotated: replaced with a fresh value.
- Salt: random extra data mixed into a PIN before hashing, so the same PIN produces a different hash each time.
- SHA-1: a specific hashing algorithm, one of several standard formulas for turning data into a fixed-length fingerprint.
- Shared preferences: Android's built-in storage for small pieces of app data, kept as an XML file in the app's private folder.
- Unvalidated: not checked for whether the value makes sense.
- XML: a text format that uses tags to structure data.
- Workflow: the normal sequence of screens a user goes through in an app.