How to Check Authenticity of an AJAX Request

后端 未结 15 852
悲&欢浪女
悲&欢浪女 2021-01-31 08:47

I am designing a web site in which users solve puzzles as quickly as they can. JavaScript is used to time each puzzle, and the number of milliseconds is sent to the server via A

相关标签:
15条回答
  • 2021-01-31 09:08

    You asked a bunch of questions in your original question, I'm only going to answer one of them:

    Am I going to have to use something that gets compiled, like Flash? (Yikes.)

    Yes. Given your criteria: 1) 100% accurate, and 2) No possibility of user interference, you have to use a compiled binary.

    Doesn't have to be flash though - I'd suggest a java applet if the thought of Flash makes you say "Yikes".

    0 讨论(0)
  • 2021-01-31 09:11

    I do not think there is a perfect solution. Here is an alternative that makes it harder for cheater but at the same time an unlucky honest solver may lose out.

    Get many samples of roundtrip time measurements from each specific devices/location/other combination apriori for each user based on their other interaction with your site. You will also have these measurements for the entire population. You could also be very subtle get the timestamps for when a particular DNS lookup happened from their ISP's resolver (random hostname and you host the authoritative DNS server for that domain).

    Once you have this, perform all measurements on the server side (puzzle returned to user, solution received) and subtract out the network time based on previous observations.

    Note that even in other solutions you have server load, client load (slow processor, etc.), etc that affect timing.

    Make sure you have XSRF protection on puzzle submission page :)

    0 讨论(0)
  • 2021-01-31 09:13

    Depending on the server side implementation you have, you could put the timing functionality on the server side. Record the time that the webpage request was made (you could put that into a database if you liked) and then when the answer is received get the current time and perform some arithmetic to get the duration of the answer. You could store the time in the session object if you liked instead of the database as well although I don't know too much about its integrity in there.

    0 讨论(0)
  • 2021-01-31 09:15

    Just a quick thought: why don't you use an iFrame to include the game and it's javascripts and let them reside on the server you have your server side implementation running. Any ajax request should then be sent by the same IP as your server side IP is which would solve the problem of identifying the source. Of course you have to take further measures but already gained a lot of confidence in your "client" side requests. Remember the windowsLive services login and many more like it are based on javascript and the usage of iFrames and are considered secure enough.

    0 讨论(0)
  • 2021-01-31 09:16

    As several others have pointed out:

    1. You must use server time, because client time is vulnerable to manipulation.
    2. Checking the time on the server will potentially penalize people with slow network connections, or people that are far away.

    The answer to the problem is to use a time synchronization protocol between the client and the server similar to the protocol that NTP uses. Working together, the client and the server determine the amount of delay caused by network latency. This is then factored into the times given to each user.

    NTP's algorithms are complicated and have been developed over years. But a simple approach is below; I think that the protocol should work, but you may wish to test it.

    Have the client measure the round-trip time with two successive HTTP XMLRPC pings. Each ping returns a different nonce. The second ping requires the nonce from the first ping, which assures that they are sequential. The puzzle time starts when the second HTTP ping is sent from the client. The server timestamps each request and assumes that the puzzle is displayed 1/2 way between the receipt of the first and the second request.

    When the puzzle is finished the client pings twice again, following the same protocol as before. The server knows when it receives each request and it knows the time delta. Now take half the time delta and subtract that from when the first ping of the second set is received. That can be safely assumed to be the time that the puzzle was completed.

    0 讨论(0)
  • 2021-01-31 09:17

    This approach obviously makes assumptions and is not invincible. All calculations are done on the client, and the server does some background checks to find out if the request could have been forged. Like any other client-based approach, this is not deterministic but makes it very hard for a lying client.

    The main assumption is that long-lived HTTP connections are much faster for transmitting data, even negligible in some cases depending on the application context. It is used in most online trading systems as stock prices can change multiple times within a second, and this is the fastest way to transmit current price to users. You can read up more about HTTP Streaming or Comet here.

    Start by creating a full-duplex ajax connection between the client and server. The server has a dedicated line to talk to the client, and the client can obviously talk to the server. The server sends the puzzle, and other messages to the client on this dedicated line. The client is supposed to confirm the receipt of each message to the server along with its local timestamp.

    On the server generate random tokens (could be just distinct integers) after the puzzle has been sent, record the time when each token was generated, and pass it over to the client. The client sees the message, and is supposed to immediately relay this token back along with it's local time of receipt. To make it unpredictable for the client, generate these server tokens at random intervals, say between 1 and n ms.

    There would be three types of messages that the client sends to the server:

    PUZZLE_RECEIVED
    TOKEN_RECEIVED
    PUZZLE_COMPLETED
    

    And two types of messages that the server sends to the client:

    PUZZLE_SENT
    TOKEN_SENT
    

    There could be a lot of time variation in the messages send from the client to the server, but much lesser in the other direction (and that's a very fair assumption, hey - we have to start somewhere).

    Now when the server receives a receipt to a message it sent, record the client time contained in that message. Since the token was also relayed back in this message, we can match it with the corresponding token on the server. At the end of the puzzle, the client sends a PUZZLE_COMPLETED message with local time to the server. The time to complete the puzzle would be:

    PUZZLE_COMPLETED.time - PUZZLE_RECEIVED.time
    

    Then double check by calculating the time difference in each message's sent vs received times.

    PUZZLE_RECEIVED.time - PUZZLE_SENT.time
    TOKEN_RECEIVED.time - TOKEN_SENT.time
    

    A high variance in these times implies that the response could have been forged. Besides simple variance, there is lots of statistical analysis you can do on this data to look for odd patterns.

    0 讨论(0)
提交回复
热议问题