Posts Tagged ‘hack mcu source code’
Patching IC Code: One Byte, One Checksum, One Brick
Reading code and changing code are different jobs with different risks, and the second one is where devices are destroyed. A read either produces data or does not. A modification produces a binary that is either accepted by the device or rejected, and rejection on a production unit is not a failure to be retried.
The technical content of patching is small: change a few bytes and recompute whatever the device checks. The judgement is in knowing what the device checks, and in choosing an edit that does not disturb anything else.
What an edit can be
Three kinds of edit exist, and they carry very different risk.
An equal-length substitution replaces bytes in place, changes no address and shifts nothing. It is the only edit that is locally contained, and it is the one to prefer whenever the desired change can be expressed that way.
An insertion adds code and requires everything after it to move, which means every relative branch that crosses the insertion point must be corrected and every absolute address in a table must be updated. It is possible and it is rarely worth it.
A redirection replaces a call with a call to a new location where a longer routine lives. It keeps the original region intact and costs only the space for the new routine, which has to be placed somewhere unused.
| Edit type | Addresses change | Risk | Typical use |
|---|---|---|---|
| Equal-length substitution | No | Low | Changing a condition, a constant, a branch |
| Redirection via a trampoline | One call site | Medium | Adding behaviour without moving code |
| Insertion | Many | High | Rarely justified |
| Data-only change | No | Low | Parameters, strings, calibration |
The first and fourth rows account for almost every successful patch. A condition that should be inverted is one instruction; a licence check that should pass is one branch; a limit that should be higher is one constant.
The conditions that can be inverted in one instruction
Most access checks compile to a conditional branch whose sense decides the outcome. Inverting that sense is the smallest possible edit and the one with the best record.
On a fixed-width RISC architecture the change is a single instruction word with the same length, which means the following instructions are unaffected and no address moves. On a variable-length architecture it is usually still possible to choose an encoding of the same length.
| Original | Patched | Effect |
|---|---|---|
| Branch if equal | Branch if not equal | Invert the check |
| Branch if not equal to zero | Branch if equal to zero | Same, opposite sense |
| Call the check, then test | Call, then unconditional pass | Skip the decision |
| Load a limit constant | Load a different constant | Change a threshold |
The third row is subtly different: it removes the decision rather than inverting it, and it leaves the call in place so that any side effects of the check still occur. That matters when the check also increments a counter or logs an event, because removing it entirely changes behaviour elsewhere.
What the device checks
Four integrity mechanisms appear in practice, and they determine whether a patch survives.
No check is the simplest case and is more common than expected on older designs and on products where the code is read from an external flash by a loader that does not verify.
A checksum is usually a sum or an XOR over a region. It is trivially recomputed once the algorithm and the region are known, and both are usually visible in the code as a loop.
A CRC requires the polynomial, which is either in the code as a constant or is a standard one. Recomputation is a few lines, and the recomputed value goes into the location the checker reads.
A signature is a cryptographic check over the image using a key that is not in the image. It cannot be satisfied by recomputation, and a patch to a signed image is not possible without either the key or a bypass of the verification itself.
| Mechanism | Recomputable | Where the value lives | Effort |
|---|---|---|---|
| None | Not needed | — | None |
| Sum or XOR | Yes | A field near the end or a header | Minutes |
| CRC | Yes, with the polynomial | A field, usually last four bytes | Hours |
| Signature | No | A field, verified against a public key | Key or bypass |
The fourth row is where most modern products sit, and it changes the shape of the work. A signed image cannot be patched; the verification has to be defeated, which means finding the branch that consumes the result and patching that instead. That is still an equal-length substitution, but it is a patch to the verifier rather than to the thing verified, and it survives only if the verifier is not itself checked.
The five reasons a patched device does not boot
When a patch is applied and the device fails to start, the cause is almost always one of five things, and they are distinguishable.
The integrity check failed. The device computed a value over the image and compared it against a stored one, and the stored one no longer matches. The symptom is a device that does nothing, or that enters a recovery mode.
The patch corrupted a neighbouring literal. A substitution that was not actually equal-length, or that overwrote part of a pool, produces code that executes garbage. The symptom is a crash at a specific point rather than at boot.
The patch changed a value that the startup code depends on. A constant used for a stack size, a clock divider or a memory layout is not obviously a constant until the device stops working.
The patch broke an alignment requirement. Some architectures require certain instructions or data to be aligned, and a write that crosses a boundary produces a hard fault.
The device is checking itself. A watchdog that expects a specific sequence, a self-test that compares a region against a copy, or a check that the reset vector is unchanged will all detect a modification that the integrity check missed.
| Symptom | Likely cause | Confirmation |
|---|---|---|
| No response at all | Integrity check | Read back and recompute |
| Crash at a fixed point | Corrupted neighbour | Disassemble around the patch |
| Boots then resets | Self-test or watchdog | Compare the region against its copy |
| Hard fault immediately | Alignment | Check the address and width |
| Works but misbehaves | Changed a startup constant | Inspect the value’s users |
The verification that makes a patch safe
A patch is a hypothesis until it is tested on a device that is not the customer’s only unit.
The verification has three parts. First, the patched image is disassembled around the change and the surrounding twenty instructions are checked to confirm that nothing else moved. Second, the integrity value is recomputed and the recomputation is checked against the value the device reads. Third, the patched image is written to a sacrificial unit and the intended change is observed along with the absence of unintended ones.
| Verification step | Catches | Cost |
|---|---|---|
| Disassemble around the change | Corrupted neighbours, misalignment | Minutes |
| Recompute the integrity value | Check failures | Minutes |
| Run on a sacrificial unit | Everything else | One device |
| Exercise the modified path | Logic errors in the patch | Hours |
The fourth row is the one most often skipped and the one that finds the interesting failures. A patched check that now passes might also skip the initialisation that the check performed, and the device works until it needs the thing that was never initialised.
When there is no second unit
Most patching advice assumes a sacrificial device, and most real engagements have one unit that must survive.
The procedure changes in three ways. The image is read and archived before anything is written, so that the original can be restored byte for byte if the patch fails — and the archive is verified by reading it back and comparing, not by trusting the tool’s success message. The patch is then validated on a model rather than on hardware: writing the modified image to an equivalent part if one exists, or emulating the affected function if the core is simple enough. Finally the write is performed once, on the unit itself, with the verification steps above completed beforehand rather than after.
| Constraint | Standard procedure | Single-unit procedure |
|---|---|---|
| Restore path | Re-program from the original | Archived read plus verified read-back |
| Validation | A second device | A model, or a matching part |
| Write attempts | Many | One |
| Risk acceptance | Low | Explicit, in writing |
The last row is not a formality. A customer who is told that a patch has one attempt and that failure means a dead unit is a customer who will think about whether the patch is worth it, and that conversation is better held before the write than after.
Where this sits in the work
Patching requires the boundaries established in finding the code and the function structure established in MCU break. Without both, a patch is a byte written into an unknown region with an unknown purpose.
Where the integrity mechanism is a signature, the route changes from patching to bypass, and the bypass targets the verification code described in secure boot. Where the modification is intended to add behaviour rather than change it, the minimal-injection technique in hacking IC firmware is a better fit than an insertion, because it places new code in unused space rather than moving existing code.
What we deliver for a patching engagement is a patched image, a description of every byte changed with the reason, a recomputed integrity value, and the results of the three verification steps. What we decline is a patch promised without a sacrificial unit, because the first device is the one that teaches you whether the hypothesis was right.