<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3922701899129876234</id><updated>2024-07-04T08:40:32.057+03:00</updated><category term="authentication"/><category term="crypto"/><category term="hash"/><category term="hmac"/><category term="md5"/><category term="sha-1"/><title type='text'>random bit generator</title><subtitle type='html'>☞ the possibilities are endless!</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://rbitg.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3922701899129876234/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://rbitg.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/07658315434668184753</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3922701899129876234.post-2352044293116119176</id><published>2010-12-15T22:06:00.019+02:00</published><updated>2013-03-26T19:13:15.809+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="authentication"/><category scheme="http://www.blogger.com/atom/ns#" term="crypto"/><category scheme="http://www.blogger.com/atom/ns#" term="hash"/><category scheme="http://www.blogger.com/atom/ns#" term="hmac"/><category scheme="http://www.blogger.com/atom/ns#" term="md5"/><category scheme="http://www.blogger.com/atom/ns#" term="sha-1"/><title type='text'>Using HMAC to protect web-requests from forging</title><content type='html'>In this article I will try to demonstrate, how a simple combination of cryptographic primitives (hashing and basic XOR encryption) &amp;mdash; HMAC, can be used to construct a primitive (single request) message authentication protocol, which is useful to control, who can access a personalized web service without a need to fully authenticate a user, when requirements allow for some specific issues associated with it.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Why authenticate?&lt;/h4&gt;&lt;br /&gt;
Sometimes you don&#39;t want to let people &quot;get any ideas&quot; and tamper with the request data, forging requests you don&#39;t want to serve. One particular example is that you may want to give access to a service for a user, but neither you, nor the user, want to have another point of password entry in the system and you don&#39;t want any 3rd parties to get in the way. The user wants to just get whatever he needs by simply clicking on a link in an e-mail or IM and you want that only authorized users can do that.&lt;br /&gt;
&lt;br /&gt;
This is a kind of problem I had to deal with last week. A web-application was set up to accept users&#39; orders to perform a service for them (let&#39;s say, to write a summary about a news). A request would be tied to a pair: a user ID and a news ID. News ID specifies the news item for which the summary to be written for, user ID refers to the customer&#39;s account. Before the bright idea of authenticating such requests came to my mind, the requests would be thus structured like this: &lt;br /&gt;
&lt;pre&gt;http://some.address.net/app?UserId=31231&amp;amp;NewsId=12312434&lt;/pre&gt; But what will happen, if a &#39;nonconformist&#39; user comes and decides to cause us some trouble, or a malicious 3&lt;sup&gt;rd&lt;/sup&gt; party gets to know about the unsecured service? If it&#39;s a common HTTP GET request with no prior authentication, it&#39;s pretty easy to substitute the numbers with something else &amp;mdash; other numbers, or even arbitrary strings, something completely unexpected by the application. Even, if pairs of allowed user and news IDs are to be stored in a database, the result would be that an arbitrary 3rd party may guess such pairs and make an unsolicited request on that user&#39;s behalf. &lt;em&gt;Not good!&lt;/em&gt; So, what do we do? &lt;br /&gt;
&lt;br /&gt;
We need to authenticate the request. A usual, straightforward way is to ask the user for username, password and establish a secure session. But why create all the hassle for the user with entering the password yet another time, if the user is already authenticated in another realm and this order is ought to be just a simple one-off request? &lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;HMAC used as intended&lt;/h4&gt;&lt;br /&gt;
This is what message authentication codes (MACs), and hash-based MACs (HMACs) in particular, are invented for: to make sure messages are only supplied by the intended source, not a malicious 3rd party. By supplying the request with a cryptographically secure message authentication code, we can be sure the message cannot be tampered with (not in a reasonable amount of time and effort, at least). This is how it works: &lt;br /&gt;
&lt;br /&gt;
First, let&#39;s identify the actors here. Alice — the message sender (in the example above this is the intended user); Bob — the message receiver (in my example it is the ordering server) and, finally, the tricky and mischievous Mallory trying to imitate Alice to fool Bob into accepting his (or her?) malformed messages. &lt;br /&gt;
&lt;br /&gt;
Just generating a hash of the message will not work: Mallory can easily do that, too (and it&#39;s easy to guess, even if Mallory has no prior knowledge about the specific method being used to hash the messages). We need to somehow combine a secret code with the message, so that resulting hash will only match, if both the message and the secret code match. &lt;br /&gt;
&lt;br /&gt;
Now, the secret can be any set of data (a timestamp XORed with a static byte array may even do the trick), the safety concerns are the same as with passwords and generally cryptographic key strengths &amp;mdash; the longer and less predictable the key, the better. A rule of thumb is to use something that is genuinely random and cannot be guessed and is long enough, so that it would be infeasible to simply &#39;brute force&#39; it. &lt;br /&gt;
&lt;br /&gt;
So, why not just use a simple &#39;keyed hash&#39; and concatenate the secret with the message and hash it with MD5? Because it is considered unsafe. HMAC is a keyed hash, too, but it&#39;s more robust, than a na&amp;iuml;ve implementation. More about it can be found in this article: &quot;&lt;a href=&quot;http://rdist.root.org/2009/10/29/stop-using-unsafe-keyed-hashes-use-hmac/&quot;&gt;Stop using unsafe keyed hashes, use HMAC&lt;/a&gt;&quot;. HMAC is better, because it doesn&#39;t just hash the initial key-message pair, but it also hashes the resulting hash again with a part of the secret key. This way finding a collision for the resulting hash is not enough to break the code: the attacker would have to find a collision in the inner hash, which is potentially much more complex, than just finding a collision for a hash value. [&lt;a href=&quot;http://en.wikipedia.org/wiki/HMAC#Security&quot;&gt;HMAC Security&lt;/a&gt;]&lt;br /&gt;
&lt;br /&gt;
HMAC computation is now part of many mainstream standard libraries and runtime environments, like .NET, Python, Java, so you don&#39;t have to implement the HMAC algorithm yourself (although it&#39;s quite simple), apart from using an existing API. I was using the C#/.NET &lt;strong&gt;System.Security.Cryptography&lt;/strong&gt; API. It&#39;s very straightforward to use, so I will leave this as an exercise for you to figure it out.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Other scenarios, where HMACs may help&lt;/h4&gt;&lt;br /&gt;
The example I presented in the beginning doesn&#39;t quite match the basic pattern, but it&#39;s just a variation of it. In that case Alice is my service, Bob is the service&#39;s intended user, authorized only for that particular service, and Mallory is an unintended 3rd party, completely unauthorized to use the service.&lt;br /&gt;
&lt;br /&gt;
Alice generates a secret, stores it and generates and appends an HMAC to the URL; it then sends the URL to Bob, so Bob can get what he needs on Alice. That HMAC will also confine Bob&#39;s authorized actions to that particular service, e.g. if the URL is a link to an article named &quot;Bob is Awarded 1st Place on Some Competition&quot;, he will only be able to access that, but not, say &quot;Dick reports Bob cheating on Some Competition&quot; (all names are coincidental, bla-bla-bla...). If that URL looked like this:&lt;br /&gt;
&lt;pre&gt;http://alice.articles.com/GetArticle?
id=1&amp;user=Bob&amp;auth=ceaeb2847bdd12d36ec355bea01f88b3abc1e58d&lt;/pre&gt;then if &lt;code&gt;id=1&lt;/code&gt; is substituted with &lt;code&gt;id=2&lt;/code&gt;, for example, the HMAC will not be valid anymore and it will be too hard to forge. This will be true in the case with Mallory, too: unless Mallory enters the right HMAC along with the Id and user name used to generate the authentication code, his forged request&#39;s authentication will fail too. &lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Caveat&lt;/h4&gt;&lt;br /&gt;
HMAC in the URL above becomes the &#39;password&#39; and, thus, if the full URL used by Bob to access the service becomes known to Mallory, then the service will be accessible to him, too. An attack performed by Mallory in this case is called &lt;a href=&quot;http://en.wikipedia.org/wiki/Replay_attack&quot;&gt;a replay attack&lt;/a&gt;. This is deliberately left so to keep the protocol simple (the actual request contains all the necessary data to both validate it and execute without need for any prior negotiation or data exchange). But because of that Bob must not be interested in sharing that link and must be aware of the risks associated with sharing the link. &lt;br /&gt;
&lt;br /&gt;
Mallory may also get access to Bob&#39;s service access URLs by other means: due to mistakes in delivery software, human error or deliberate attacks on Bob&#39;s communication. So the services provided in this regard to Bob should not be so valuable to Mallory as to encourage him to attack Bob or his computers to gain access to his service links. &lt;br /&gt;
&lt;br /&gt;
This problem can be reduced somewhat by adding a parameter to the message, which changes on every request and invalidates the message, once it&#39;s used. It doesn&#39;t solve this problem completely, though. Only a proper authentication protocol will overcome this issue.&lt;br /&gt;
&lt;br /&gt;
This simple protocol is still useful though where the service provided to Bob is either too much specific to him (i.e. personalized and limited, but doesn&#39;t contain any sensitive data to raise privacy concerns), or is not very valuable to others alone (without something else only accessible to Bob in a properly authenticated realm, for example.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;Conclusion&lt;/h4&gt;&lt;br /&gt;
HMAC is a valuable tool in securing communications over the Internet. They&#39;re used in many security protocols, for example &lt;a href=&quot;http://en.wikipedia.org/wiki/Kerberos_(protocol)&quot;&gt;Kerberos&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/OAuth&quot;&gt;OAuth&lt;/a&gt;. What I&#39;ve shown here is just a very simple application of this cryptographic function.</content><link rel='replies' type='application/atom+xml' href='http://rbitg.blogspot.com/feeds/2352044293116119176/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://rbitg.blogspot.com/2010/12/using-hmac-to-protect-web-requests-from.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3922701899129876234/posts/default/2352044293116119176'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3922701899129876234/posts/default/2352044293116119176'/><link rel='alternate' type='text/html' href='http://rbitg.blogspot.com/2010/12/using-hmac-to-protect-web-requests-from.html' title='Using HMAC to protect web-requests from forging'/><author><name>Anonymous</name><uri>http://www.blogger.com/profile/07658315434668184753</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>