dsgawmla208057 2013-11-19 16:29
浏览 107
已采纳

在MySQL中使用md5时安全性逃逸

In this code

UPDATE table SET field = MD5('{$_POST['variable']}') WHERE id = 1;

Since the updated value is md5'ed, should I still take steps to prevent from injection? Like addslashes() or something like that?

  • 写回答

5条回答 默认 最新

  • duanpa1898 2013-11-19 16:36
    关注

    Since the updated value is md5'ed, should I still take steps to prevent from injection?

    In fact the variable in your code has not been MD5'ed when it goes into the query string, so the same injection issues still apply just as they would with any other variable.

    If you had done $hashedVar = md5($var) in PHP and then added $hashedVar into the query string, then your question may have been a bit more legitimate, but the way you're doing it with the MD5() as part of the query string itself, the answer is yes, it most certainly does need to be escaped to avoid injection.

    However some additional notes:

    1. addslashes() is not the correct method to use for escaping PHP strings into SQL. You should use the real escape string function applicable to the DB API that you're using.

      If you're using mysqli or PDO as your DB API, your best option for this is Prepapred Statements. If you're using the older mysql_xxx() functions then you don't have this option, but you should update to one of the other APIs, as the mysql extension is deprecarted.

    2. MD5 is not a secure hashing algorithm for passwords. If you're using plain unsalted MD5 for passwords then you are insecure already, before you even start thinking about injection attacks. You should use a better hashing algorithm such as bcrypt.

      Recent PHP versions include a set of password_xx() functions which are designed specifically for providing the best available quality hashing with minimal effort. (these functions are also available for older PHP versions, via a third-party library).

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

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)