Posts Tagged ‘mram mcu firmware crack’
Credential Hunting in Firmware: Strings, Tables, and Three Ways to Hide
A firmware image is a large amount of data with a small amount of meaning. Somewhere in it there is a value that the device compares against, and finding that value is the whole of credential hunting.
The usual approach is a string search, and it works more often than a security engineer would like. The approach that works when it fails is different in kind: instead of searching for the value, find the comparison and read backwards.
The string search and its limits
A string search over a firmware image finds printable sequences of four characters or more. On a typical embedded image it returns a few hundred, of which a handful are interesting: default passwords, API keys, URLs, Wi-Fi credentials, service codes, debug commands.
| Finds | Misses |
|---|---|
| ASCII credentials of four characters or more | Values shorter than the threshold |
| Command strings and error messages | UTF-16 encoded strings |
| URLs and hostnames | Values split across a boundary |
| Format strings that reveal structure | Values assembled at runtime |
The second column is why a failed string search is not evidence that there is no credential. A four-digit PIN is four bytes and may fall below the tool’s threshold; a key stored as UTF-16 appears as interleaved nulls and looks like garbage; a value split into two parts at a boundary appears as two unrelated short strings.
The first fix is to lower the threshold to three and to search for UTF-16 as well as ASCII. The second is to search for short printable runs near comparison instructions rather than everywhere, which is the beginning of the second strategy.
Where comparisons live
The most reliable way to find a credential is to find the code that checks it, because the check has a shape that the data does not.
A string comparison calls a library routine, and the routine’s address appears in the call. A byte comparison is a loop, and the loop’s bound is the length of the secret. A single-value comparison is a load followed by a compare, and the immediate is the value — which is the best case, because the secret is in the instruction rather than in a table.
| Check shape | What it reveals | Where the secret is |
|---|---|---|
Call to strcmp |
The pointer arguments | A string in flash |
Call to memcmp |
The pointer arguments and the length | A buffer in flash, length known |
| Inline loop with a compare | The loop bound | A table, length known |
| Load and compare against an immediate | The value itself | In the instruction |
| Hash then compare | The hash, and the length | Nowhere; the plaintext is not stored |
The last row is the case that ends the search. A device that hashes the supplied value and compares the digest does not store the credential, and no search of the firmware finds it. What can be done instead is to find the hash input and either brute force it offline — which is fast, because the hash is now local — or attack the comparison.
The three hiding patterns
When a string search fails and the comparison leads to something unreadable, the value is hidden in one of three ways.
Segmentation splits the value into pieces stored apart and joined at run time. The pieces are short, so each falls below a string-search threshold, and the join is a copy or a concatenation that appears in the code.
| Pattern | Static signature | How to recover |
|---|---|---|
| Segmentation | Several short runs copied into one buffer | Follow the copy, read the sources |
| Encoding | A buffer transformed before comparison | Find the transform, invert it |
| Runtime assembly | Values computed from constants | Evaluate the expression |
| External fetch | A value read from an external device | Read the device |
Encoding is the second and the most common. A byte-wise XOR with a constant turns a readable string into unreadable bytes while keeping the length, and the XOR appears in the code as a loop or an inline operation on the buffer before the comparison. Inverting it is trivial once it is found, and the way to find it is to notice that the comparison’s argument is a buffer that was just written rather than a constant.
| Encoding | Detectable by | Inversion |
|---|---|---|
| XOR with a constant | Flat histogram shifted by a constant | XOR with the same value |
| Addition of a constant | Wrapped byte values | Subtract |
| Nibble swap | Histogram peaks moved by a factor of sixteen | Swap back |
| Base64 | Restricted alphabet, padding | Decode |
| Compression | High entropy, a table | Decompress |
The third pattern is assembly from constants, and it is used when the developer wants the value to exist nowhere in memory. A key built from four 32-bit constants combined with shifts and XORs appears in the code as arithmetic and in the data as four unrelated words.
Working from the call graph
The comparison is easier to find when the call graph is already resolved, and harder when the credential is checked in a place nobody expects.
Three locations account for most checks. The one everyone looks at is the authentication routine called from the login path. The one that is often missed is the check performed by an interrupt handler, because the credential is validated as it arrives rather than after it is complete. The third is a check inside a library, where the application passes a buffer and the library decides.
| Check location | How to find it | Why it is missed |
|---|---|---|
| Login path | Follow the command handler | Nothing; it is found first |
| Interrupt handler | Look for compares in the receive path | Not on the call graph of the command |
| Inside a library | Signature of the library function | Appears as a library block |
| In the bootloader | A separate image | Not in the application at all |
The fourth row is the one that ends a search in the wrong image. A device whose application has no credential check at all may have one in the bootloader, and the bootloader is a different program in a different region. Its presence is established by the region map in extracting MCU firmware, and its structure is the one described in secure boot.
Why the comparison is the better target
Attacking the comparison rather than the data has two advantages that are worth stating plainly.
The first is that the comparison must exist. Whatever the value is and however it is hidden, the device compares something against something, and that comparison is code. Data can be hidden; a decision cannot.
The second is that the comparison tells you the length, the type and often the algorithm. A memcmp with a length of sixteen says the secret is sixteen bytes. A hash-then-compare says the secret is not stored. A comparison against a value loaded from a peripheral address says the secret is in an external device.
| What the comparison reveals | Inference |
|---|---|
| Length immediate | Secret length |
| Library call identity | String or binary comparison |
| Hash routine preceding it | Secret not stored |
| Load from a peripheral | Secret in an external device |
When the credential is not in the firmware at all
A significant fraction of searches end with the conclusion that the credential is elsewhere, and reaching that conclusion quickly is as valuable as finding the value.
Three signs point outward. The firmware contains no comparison for the operation in question, only a command frame sent to another device. The device has a second package on a bus, and that package is a secure element. The device authenticates successfully even when its flash is erased and reprogrammed with a stub, which means the decision is not made in the flash.
| Sign | Conclusion | Route |
|---|---|---|
| Only a command frame, no comparison | Another device decides | Read the bus, attack the element |
| Hash-then-compare with no plaintext | Credential is only a digest | Offline search, or attack the comparison |
| Works with the flash erased | Decision is not in the firmware | Find the deciding device |
| No comparison found at all | The path was not located | Re-examine the call graph |
The third row is worth testing deliberately, because it is cheap and it saves entire engagements. Replacing the application with a minimal stub and observing whether authentication still succeeds tells you immediately whether the secret is in the code you are looking at.
What the service delivers
The output is a credential inventory: every value found, its location, how it is stored, and what it unlocks. For each value the inventory states whether it was found directly, recovered by inverting an encoding, or reconstructed from assembly.
| Item | Example | Status |
|---|---|---|
| Debug password | Eight characters, plaintext | Direct |
| Service code | Four digits, XOR-encoded | Inverted |
| API key | Thirty-two bytes, segmented | Reconstructed |
| Update signing | Public key only | Not available |
The last row is the honest one. A firmware image that contains only a public key does not contain the credential needed to sign, and no amount of searching changes that.
The location question for values that are not in the firmware is the subject of where the secret key lives, and the case where the secret is derived from the device rather than stored is in a brute force case study. Where the value is only a digest, the search becomes an offline one whose arithmetic is in when brute force works.