dtq81142 2014-11-26 18:07
浏览 115
已采纳

PHP安全问题

I'm using multiple services to accept mobile payments for stuff like virtual currency.

Many companies will include an MD5 signature in the POST or GET callback which I can calculate to verify that the request is authentic and then reward the user with the purchased credits.

This method is very secure as it's nearly impossible to guess the signature.

Other companies will not provide a signature and just tell me to check if the call is from their server IP like the following code:

<?php 

  if(!in_array($_SERVER['REMOTE_ADDR'],array('xxx.xx.x.xx'))) {
     header('HTTP/1.0 403 Forbidden');
     die('Error: Unknown IP');
  }

?>

Is this IP check secure enough?? Isn't it now very easy to spoof an IP address and make a GET or a POST request using that IP?

  • 写回答

2条回答 默认 最新

  • douli0531 2014-11-26 20:20
    关注

    The other answers are incorrect. So I'll write my own.

    With the exception of exceedingly rare situations, REMOTE_ADDR is 100% trust worthy. It comes from the TCP connection to the server, so it's practically impossible to forge without actually compromising something on the network (like the router the IP belongs to) or without having your server misconfigured (severely, Apache doesn't even let you misconfigure it like that).

    So, there are two questions that I can see:

    Is it safe to trust REMOTE_ADDR

    Yes.

    If the REMOTE_ADDR variable in PHP indicates the request came from their server, then it came from their server.

    If you're using a remote proxy, then X-HTTP-FORWARDED-FOR is not to be trusted. That's where you can get into problems if you're not careful.

    Is MD5() of the request safer than REMOTE_ADDR verification

    NO!!!

    It's a lot easier to forge an MD5 signature than it is to forge an IP address (which requires you to breach specific network hardware). And if the attacker breaches the network hardware, the game is over anyway.

    What's The Best Solution?

    The best solution is three fold:

    1. Use HTTPS with Certificate Pinning

      On your app, store the public key of their server. Then force the peer verification to use that certificate. That means that an attacker would need to steal the certificate of the remote server to be able to connect.

    2. Verify IP Addresses

      Using REMOTE_ADDR

    3. Sign requests using HMAC+SHA2

      Use HMAC with SHA-256 or SHA-512.

    But yes, the IP check alone is quite secure.

    To go deeper, we'd need to go into what types of attacks you're defending from.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号