Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure access to my web service from my code only?

I am writing a very simple web service for my iPhone app. Let's say this is a http page that returns a random number at http://mysite/getRand. How do I ensure that this page can only be accessed from my iPhone app and not from other clients? I've thought of doing some simple password mechanism but that can easily be sniffed by capturing what my app sends out.

The reason for this is to lower the load of my server by only allowing legitimate requests.

like image 430
erotsppa Avatar asked May 11 '09 16:05

erotsppa


5 Answers

You can't really do this. Your application can be disassembled and whatever secret is in the binary can be replicated in a malicious application.

Another attack you should be aware of is people settings the hosts file to a location they control and then installing a root certificate that allows them to provide a signature for that domain. Your application would do the post with the secret, and they'd just be able to read out the secret. They could extract the password from any complicated encryption system within the binary in this way.

Most of the ideas in this thread are vulnerable to this attack.

That said, the likelihood of somebody caring enough to disassemble your application is probably fairly remote.

I'd just keep it simple. Have a password that's hardcoded in to your application. To prevent someone just looking at the resources and trying every string, make it the XOR of two strings or the result of an AES decrypt of a particular fixed string.

Obviously, you should do the request over SSL otherwise an attacker can just sniff the traffic.

Yes, a determined attacker will circumvent the scheme but like any DRM scheme, that's always been the case. The trick is to make it too much effort to be worth it.

like image 65
Simon Johnson Avatar answered Nov 10 '22 12:11

Simon Johnson


To follow up on Simon's idea, you could very easily have a key string in your application, then send the device ID, and then the DeviceID XOR'ed (or some other simple algorithm for string encryption) with your key string.

Since you know the key value to use, it's trivial for you to "decrypt" this string on the sever side and verify that the values match.

This way, the password is different for each user's device, and the "key" string is never sent over the wires of the great unwashed internets. :-)

Yes, this would by no means be impossible to figure out, but like others have said, the idea is not to make it impossible. The idea is to make it more trouble than it is worth.

like image 33
mmc Avatar answered Nov 10 '22 11:11

mmc


I would use the https protocol with client-side keys too. You can use one client key for everyone or you can even generate a different key for each client and "register" them at your server.

I suppose that it's a lot of work for small project, but it sounds like the appropriate thing to do if you need authentication.

You should check that keys aren't seen easily by mobile phone owner. And remember that somebody will be able to hack it in any case.

like image 35
m_vitaly Avatar answered Nov 10 '22 10:11

m_vitaly


Here's one thought - send up the device ID along with requests from your app.

Monitor the device ID's used - if you see a ton of requests from different IP's near or at the same time, that device is probably being used as a fixed key in the requests sent to you - block it.

For those that actually send the real device ID from other apps (not yours), you can monitor usage trends to see if the calls match the pattern of how your app performs - like one call being used by a device before some initialization call you would normally expect, and so on - block those too.

Basically by being able to shift rules around patterns of use, you can better adjust to someone trying to use your service by making sure it's not a fixed target like some random use key would be.

You may also want to use a simple use key as well as a first line of defense, and then layer on the traffic analysis approach. Also custom http header values you look for are another simple way to trip up a naive attacker.

like image 1
Kendall Helmstetter Gelner Avatar answered Nov 10 '22 12:11

Kendall Helmstetter Gelner


I am assuming you don't want to use SSL? If you do then you can open HTTPS session and then pass some secret key in the request.

If you don't want SSL your options are limited: to have pseudo security I suggest both authentication and authorization methods and a third to reduce overall traffic:

Authentication: Generator in client application that creates secret keys by combining with a key file. The keyfile can be updated every so often for greater security: lets say you update the key file once a week. To re-cap: Generator combines in app secret with out of app key file to generate a 3rd key for transmission used in authentication. The server would then be able to authenticate.

Authorization: Of course you also want to lock out rogue applications. Here it would be best to have authorization mechanism with the site. Don't replace keyfiles for unless the client logs in. Track key files to users. etc.

Traffic reduction: If you are receiving obscene amount of traffic or if you suspect someone trying to DOS your server, you can also have both the server and clients sync to request/response on a procedurally generated URL that can change often. It is wasteful to open/close so many HTTPS sessions if someone is just flooding you with requests.

like image 1
Klathzazt Avatar answered Nov 10 '22 10:11

Klathzazt