donglian6625 2014-05-17 14:10
浏览 36

安全存储私人图像

I am in need of a secure solution for storing a larger (unknown) amount of private images accessed only by a certain user. This question is not surrounding things like logins, sessions or anything like that - solely surrounding images and them being stored.

I'll start out with explaining what I am trying to achieve and then go on to things I've thought about doing.

Private images only accessible by a certain user

A user logs in to my site to upload and view images provided and uploaded by him/her. The images should only be accessible by that user - therefore a login is required. When a image is requested for viewing, the rendering is handled by a PHP-script validating the user's authority and bringing the actual image from the safe directory.

What I know, assume and think about.

Indexing images for ease of managment

Every image that gets uploaded is indexed in a database for management, i.e.

Title: "Me on a scooter"
User: {0E4A759A-CC31-4B0D-97E1-EEFB28F0BF86}
Filename: asuohLAFUUHJSFUhaSGFUOAHSGUA
Extension: .jpg

When the user wants to view the image, the server validates and checks if the user is indeed the owner of the image.

Keeping the public away from the images

I've read some articles explaining that it's good practice to keep files outside of the document root directory. Though I understand the concept, I don't fully understand how to do it in practice. My assumption:

/var/www/myFolder/ //here's where the html files are stored
/mySecureFolder/ //here's where the content are stored outside of the root, or on a higher level

So whenever the server should display an image it looks for it in the /mySecureFolder/ and checks if the file exists before rendering it for the user.

Assumption #2

Creating and placing an .htaccess file blocking everyone but localhost in the /mySecureFolder/ is what I should do to keep others out.

Question #1 The PHP process is running as the user www-data, is it safe to make the owner group of /mySecureFolder/ www-data?

Assumption #3 - Chmod

To allow none but one specific system user i should CHMOD the folder recursivly with 0770, allowing only the owner user and the owner group to read, write and execute permissions.

Question #2

Is using .htaccess to deny access to everyone but localhost equivalent to placing the secure folder outside the root (/var/www/myFolder/) folder?

Assumption #4 Overkill

Encrypting every image stored on the filesystem is overdoing it and - if the directory permissions are set up correctly - unnecessary.

My attack scenario is only outer attacks, hackers cannot access the server trough SSH nor via physically being at the server. The possible attacks are therefore outer attacks coming from the code I created - like injections and so on.

I hope that the possible answers might come in handy for future bypassers, therefore I would appreciate if you help me out with formatting this question to ensure it being as clear as possible.

  • 写回答

2条回答 默认 最新

  • dougou2937 2014-05-17 14:14
    关注

    The best way is to store the images outside of /www - and have them in their own directory.

    Then use readfile() to 'serve' the images to the authorised user. No need to CHMOD or use htaccess etc - as it is outside the www folder and cannot be accessed except through your application.

    Something like this would work:

    function show_image($file_name= "")
    {
        // Ensure no funny business names to prevent directory transversal etc.
        $file_name = str_replace ('..', '', $file_name);
        $file_name = str_replace ('/', '', $file_name);
    
        // now do the logic to check user is logged in - put your own code here
        if (LoggedInUserCanAccessThisFile())
        {
            // Serve file via readfile()
            // Images are stored in a specific user folder - so only the own user can get their own images
            readfile('../image_storage/' . getLoggedInUserID() . '/' . $file_name);
        }
    }
    

    You can read more about readfile() here from php.net

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)