How to prevent brute force file downloading on S3? - amazon-web-services

I'm storing user images on S3 which are readable by default.
I need to access the images directly from the web as well.
However, I'd like to prevent hackers from brute forcing the URL and downloading my images.
For example, my S3 image url is at http://s3.aws.com/test.png
They can brute force test and download all the contents?
I cannot set the items inside my buckets to be private because I need to access directly from the web.
Any idea how to prevent it?

Using good security does not impact your ability to "access directly from the web". All content in Amazon S3 can be accessed from the web if appropriate permissions are used.
By default, all content in Amazon S3 is private.
Permissions to access content can then be assigned in several ways:
Directly on the object (eg make an object 'public')
Via a Bucket Policy (eg permit access to a subdirectory if accessed from a specific range of IP addresses, during a particular time of day, but only via HTTPS)
Via a policy assigned to an IAM User (which requires the user to authenticate when accessing Amazon S3)
Via a time-limited Pre-signed URL
The most interesting is the Pre-Signed URL. This is a calculated URL that permits access to an Amazon S3 object for a limited period of time. Applications can generate a Pre-signed URL and include the link in a web page (eg as part of a <img> tag). That way, your application determines whether a user is permitted to access an object and can limit the time duration that the link will work.

You should keep your content secure, and use Pre-signed URLs to allow access only for authorized visitors to your web site. You do have to write some code to make it work, but it's secure.

Related

AWS S3 Per Bucket Permission for non-AWS accounts

This question is in the same line of thought than Is it possible to give token access to link to amazon s3 storage?.
Basically, we are building an app where groups of users can save pictures, that should be visible only to their own group.
We are thinking of using either a folder per user group, or it could even be an independent S3 bucket per user group.
The rules are very simple:
Any member of Group A should be able to add a picture to the Group A folder (or bucket)
Any member of Group A should be able to read all pictures of the Group A folder (or bucket)
No member of Group A should not have access to any of the pictures
However, the solution used by the post mentioned above (temporary pre-signed URLs) is not usable, as we need the client to be able to write files on his bucket as well as read the files on his bucket, without having any access to any other bucket. The file write part is the difficulty here and the reason why we cannot use pre-signed URLs.
Additionally, the solution from various AWS security posts that we read (for example https://aws.amazon.com/blogs/security/writing-iam-policies-grant-access-to-user-specific-folders-in-an-amazon-s3-bucket/) do not apply because they show how to control accesses for IAM groups of for other AWS accounts. In our case, a group of users does not have an IAM account...
The only solutions that we see so far are either insecure or wasteful
Open buckets to everybody and rely on obfuscating the folder / bucket names (lots of security issues, including the ability to brute force and read / overwrite anybody's files)
Have a back-end that acts as a facade between the app and S3, validating the accesses. S3 has no public access, the bucket is only opened to an IAM role that the back-end has. However this is a big waste of bandwidth, since all the data would transit on the EC2 instance(s) of that back-end
Any better solution?
Is this kind of customized access doable with S3?
The correct way to achieve your goal is to use Amazon S3 pre-signed URLs, which are time-limited URLs that provides temporary access to a private object.
You can also Upload objects using presigned URLs - Amazon Simple Storage Service.
The flow is basically:
Users authenticate to your back-end app
When a user wants to access a private object, the back-end verifies that they are permitted to access the object (using your own business logic, such as the Groups you mention). If they are allowed to access the object, the back-end generates a pre-signed URL.
The pre-signed URL is returned to the user's browser, such as putting it in a <img src="..."> tag.
When the user's browser requests the object, S3 verifies the signature in the pre-signed URL. If it is valid and the time period has not expired, S3 provides the requested object. (Otherwise, it returns Access Denied.)
A similar process is used when users upload objects:
Users authenticate to your back-end app
They request the opportunity to upload a file
Your back-end app generates an S3 Pre-signed URL that is included in the HTML page for upload
Your back-end should track the object in a database so it knows who performed the upload and keeps track of who is permitted to access the object (eg particular users or groups)
Your back-end app is fully responsible for deciding whether particular users can upload/download objects. It then hands-off the actual upload/download process to S3 via the pre-signed URLs. This reduces load on your server because all uploads/downloads go direct to/from S3.

Prevent anyone from accessing my bucket unless a certain condition

I have an ec2 instance with a load balancer and cloudfront attached and I want to prevent my s3 bucket files from being viewed unless the files are being requested on my website. How would I be able to do this? I've tried "referrer" (which doesn't work sometimes, and apprently not the best option) and I've tried using the "source ip" condition which just doesn't work, I've put in my website ip, my vpc ip from my load balancer, etc, just doesn't work (unless there's another way I have to do it, I would appreciate it if anyone told me). I just want a bucket policty that has a condition like so:
"Condition": {
** person is on my website **
}
If anyone has any ideas, that would be nice, thanks.
I can immediately think of 2 options:
Make your bucket private and instead reverse-proxy the images through your own website.
Make your bucket use Query String Authentication and have your website generate a short-lived QSA token (5 minutes?) for each visitor.
If your content is being served from Amazon S3 or Amazon CloudFront, you can use pre-signed URLs to grant time-limited access to private content.
For example, let's say that you have a photo-sharing website and all photos are private by default. Access can be provided as follows:
Users authenticate to your application
The user then requests access to a private object, or your application wishes to generate an HTML page that includes a link to a private object (eg in an <img> tag).
The application checks whether the user is permitted to access the object. If they are, the application generates a pre-signed URL and provides it in the HTML page or as a link.
The user's browser then uses the URL to request the private object, which sends the request to CloudFront or S3
CloudFront or S3 then checks whether the pre-signed URL is correctly signed and is still within the validity period. If so, it provides access to the object. If not, it returns Access Denied.
For more information, see:
Amazon S3 pre-signed URLs
Using Amazon CloudFront Signed URLs

Block s3 public url access for my image and only show the image in mobile app or website

I have upload all my images in s3 bucket and allowing it to show in my website using s3 url, but when I access the s3 url directly in browser it showing the image, I want to block those access
Can anyone help me on how to Block s3 public url access for my image and only show the image in mobile app or website.
All objects in Amazon S3 are private by default.
Access to objects can be granted in several ways:
A Bucket Policy can make a whole bucket (or a part of a bucket) public to everyone. It is also possible to specify restrictions, such as IP address and referer.
An Access Control List on an object can make the object public (for everyone)
An IAM Policy can grant access to objects for specific IAM Users
A pre-signed URL can provide temporary access to a private object
Based upon your question, I would recommend:
Keep the objects private (remove Bucket Policies and ACLs)
When a user wishes to access an image or other object, your application determines whether the user is permitted to access the object (this logic is totally up to you to write in your application)
If they are permitted, your application can create a pre-signed URL in a few lines of code, which will allow the mobile app or website to access the object for a limited time period that your app specifies (eg 5 minutes). After this time period, the URL will no longer provide access.
Thus, your application has full control over whether somebody is permitted to access the image, while still serving the content directly from Amazon S3 (eg in a web page via a <img> tag).
See: Share an Object with Others - Amazon Simple Storage Service

how to assign Amazon S3 objects permissions to a particular IAM group?

How to allow read/write/delete etc, permissions to users in a particular IAM group for a specific Amazon S3 object/file.
If you wish to control access to "millions" of individual files where access is not based upon the path (directory/folder) of the files, then you will need to create your own authentication method.
This can be done by using an Amazon S3 Pre-signed URL. Basically:
Users access your application
When they request access to a secure file (or, for example, when the application generates an HTML page that includes a link to such a file, or even a reference in an Image tag), the application generates a time-limited pre-signed URL
Users can use this link/URL to access the object in Amazon S3
After the expiry period, the URL no longer works
This gives your application full control over whether a user can access an object.
The only alternative if you were to use IAM would be to grant access based upon the path of the object. It is not a good method to assign access to individual objects.

Restrict Access to S3 bucket on AWS

I am storing files in a S3 bucket. I want the access to the files be restricted.
Currently, anyone with the URL to the file is able to access the file.
I want a behavior where file is accessed only when it is accessed through my application. The application is hosted on EC2.
Following are 2 possible ways I could find.
Use "referer" key in bucket policy.
Change "allowed origin" in CORS configuration
Which of the above 2 should be used, given the fact that 'referer' could be spoofed in the request header.
Also can cloudfront play a role over here?
I would recommend using a Pre-Signed URL that permits access to private objects stored on Amazon S3. It is a means of keeping objects secure, yet grant temporary access to a specific object.
It is created via a hash calculation based on the object path, expiry time and a shared Secret Access Key belonging to an account that has permission to access the Amazon S3 object. The result is a time-limited URL that grants access to the object. Once the expiry time passes, the URL does not return the object.
Start by removing existing permissions that grant access to these objects. Then generate Pre-Signed URLs to grant access to private content on a per-object basis, calculated every time you reference an S3 object. (Don't worry, it's fast to do!)
See documentation: Sample code in Java
When dealing with a private S3 bucket, you'll want to use an AWS SDK appropriate for your use case.
Here lies SDKs for many different languages: http://aws.amazon.com/tools/
Within each SDK, you can find sample calls to S3.
If you are trying to make private calls via browser-side JavaScript, you can use CORS.