dpz1983 2011-01-10 01:28
浏览 20
已采纳

允许在目录中进行文件访问的安全字符

I've got a PHP script which allows uses to delete certain files in a directory by specifying the file name (via a drop down - but thats easy enough for someone malicious to change). I'm 'cleansing' the file name by doing the following

if(preg_match(/'/^[a-zA-Z0-9.]$/'/,$file)) {
    # do stuff to this particular file
}

I'm fairly sure that should prevent anyone from getting up to anything nasty, but as the people here have vast amounts of knowledge more then I, I thought I'd ask -- is there a hole here, or will this keep the nasty away?

  • 写回答

4条回答 默认 最新

  • douyi0219 2011-01-10 01:44
    关注

    I'm not sure whether your regex will really help: There are valid characters outside that range that you won't be able to delete this way.

    One thing that I would do is do a realpath() on the full final path, and check whether it still is a child of your allowed file path. That will prevent ../../ directory traversal attacks, even if they use some special characters. That should already provide fairly good security.

    You could also additionally scan the directory using glob() and check the results to see whether the requested file is actually in there (that is impossible to circumvent even with the most sneaky directory traversal.)

    If you want to be totally paranoid about this, you could use a different approach altogether: Don't transfer file names, but list indexes of a list that you specified before. If for example you show this list to the user and save it in a temporary text file or database record:

    1. Readme.txt
    2. License
    3. Readme.doc

    and then pass only the (random) ID of the text file or database record, and the number of the file you want to delete:

    delete.php?list=xasdafdas&index=3
    

    you should have a solution that is pretty invulnerable against any conceivable kind of injection and file name tampering.

    You would have to store an individual list for every request, as the files can change.

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

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算