Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 2 min read

Your API Signed Requests With sha256 of the Secret Plus the Query and Anyone Could Add Parameters

The partner API had a simple scheme. Take your shared secret. Stick the query string on the end. Hash it with SHA-256. Send the hash as the signature. sig = sha256(secret + query) No secret on the wire. No library

The partner API
had a simple scheme.

Take your shared secret.

Stick the query string
on the end.

Hash it with SHA-256.

Send the hash
as the signature.

sig = sha256(secret + query)

No secret on the wire.

No library needed.

It looked careful.

Then a support ticket
showed a refund request
that nobody had signed.

The signature was valid.

The original parameters
were all there.

So was one more,
added at the end:

&account=88231

Here is the mechanism.

SHA-256 reads its input
in blocks.

After each block
it keeps a small internal state.

The final hash
is simply that state,
written out.

So anyone holding a hash
is holding the machine
exactly where it stopped.

They load that state,
feed in more data,
and get a valid hash
for the longer message.

Without ever knowing the secret.

They do need
the secret's length.

That is a guess
between one and a hundred.

They also have to include
the padding bytes
the hash added
at the old ending.

Those land
in the middle of the query
as a little binary junk.

Most query parsers
shrug,
skip it,
and read the parameter
that comes after.

Often the last copy
of a repeated parameter
wins.

The attack has a name.

Length extension.

It works on MD5,
SHA-1,
and SHA-256.

In 2009
Thai Duong and Juliano Rizzo
used it
against Flickr's API,
which signed calls
with MD5 of the secret
plus the parameters.

One signed URL
from a log file
is all an attacker needs.

The fix is the function
built for this job:

HMAC-SHA256.

It hashes twice,
with the key on both sides,
so the state you can see
is not the state
you could continue.

Every language
has it in the standard library.

Then compare the signature
in constant time,
and sign the method and path
as well as the query.

A hash says
the data has not changed.

A MAC says
who wrote it.

They are not
the same promise.

โ€“ Serguey Asael Shinder

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.