Amazon S3 bucket policy to restrict IP Address - amazon-web-services

I created bucket policy below, but I can't access any images from server I set up the other day(got 403). How can I allow specific IP address in the condition?
{
"Version": "2012-10-17",
"Id": "Policy1527266936788",
"Statement": [
{
"Sid": "Stmt11111111,
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": "arn:aws:s3:::xxxxx/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "xx.xx.xxx.xxx/24"
}
}
}
]
}

Related

AWS S3 cross account policy

I would like to set a policy for a S3 bucket that is restricted to a VPC-ID(using a S3 endpoint). I have two accounts, A and B. I want a IAM user in A to access a bucket in B.
https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies-vpc-endpoint.html
{ "Version": "2012-10-17", "Id": "Policy1415115909153", "Statement": [
{
"Sid": "Access-to-specific-VPC-only",
"Principal": "*",
"Action": "s3:*",
"Effect": "Deny",
"Resource": ["arn:aws:s3:::awsexamplebucket1",
"arn:aws:s3:::awsexamplebucket1/*"],
"Condition": {
"StringNotEquals": {
"aws:SourceVpc": "vpc-111bbb22"
}
}
} ] }
Above won't work, but following will:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::bucket",
"arn:aws:s3:::bucket/*"
],
"Condition": {
"StringEquals": {
"aws:SourceVpc": "vpc-111111111111"
}
}
}
]
}
Feels like best practices is to use a deny policy. Anyone has an idea why and how solve it?
As pointed out you need to allow as-well. Combine both policies and it will work.

S3 access policy to allow download to a specific IP address

I'm trying to implement a batch virus scanner. I have a cron job set up to periodically scan unscanned files stored on S3. Whenever I try to wget the file, I get a 403.
I've set up this policy:
{
"Version": "2012-10-17",
"Id": "S3PolicyIPRestrict",
"Statement": [
{
"Sid": "IPAllow",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*",
"Condition": {
"IpAddress": {
"aws:SourceIp": "ip of my address/32"
}
}
}
]
}
Any idea what I'm doing wrong?
Use the below bucket policy if you want to allow specfic ip address to access the files on s3 bucket.
{
"Version": "2012-10-17",
"Id": "S3PolicyId1",
"Statement": [
{
"Sid": "IPAllow",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::<bucket>",
"arn:aws:s3:::<bucket>/*"
],
"Condition": {
"IpAddress": {
"aws:SourceIp": "<IP>/32"
}
}
}
]
}

Cannot modify any setting on S3 bucket from AWS console

I'm new to AWS.
When I added the bucket policy in the link. I lost all control from AWS console. What should I do change policy from console again.
Restricting Access to a Specific VPC
{
"Version": "2012-10-17",
"Id": "Policy1415115909153",
"Statement": [
{
"Sid": "Access-to-specific-VPC-only",
"Principal": "*",
"Action": "s3:*",
"Effect": "Deny",
"Resource": ["arn:aws:s3:::examplebucket",
"arn:aws:s3:::examplebucket/*"],
"Condition": {
"StringNotEquals": {
"aws:sourceVpc": "vpc-111bbb22"
}
}
}
]
}

How To Prevent Image Hotlinking With Bucket Policy AWS S3

I'm trying to prevent hotlinking of images in my S3 bucket. I thought bucket policy this would work but so far no luck:
{
"Version": "2008-10-17",
"Id": "Bucket policy for example.com",
"Statement": [
{
"Sid": "Allow GET requests referred by example.com",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example/*",
"Condition": {
"StringLike": {
"aws:Referer": [
"https://www.example.com/*",
"http://www.example.com/*",
"https://example.com/*",
"http://example.com/*"
]
}
}
},
{
"Sid": "Allow GET requests that don't specify a referrer",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example/*",
"Condition": {
"Null": {
"aws:Referer": true
}
}
}
]
}
How do I make it so this policy denies access to images embedded on other websites?

Accessing private files on s3

I have a private s3 bucket. I am trying to access it with the authorization. I am managing to do that only by generated a pre-signed url for each object in the bucket using AWS-SDK, which is not practical at all.
Also I have tried configuring the bucket policy and give premission only to a specific range of ips, but it won't work. How can I see what's wrong there?
What do you think is the best approach for accessing a private bucket? I am a bit confused.
policy:
{
"Version": "2008-10-17",
"Id": "S3PolicyId1",
"Statement": [
{
"Sid": "IPDeny",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": "public/private IP of ec2 instance/32"
}
}
},
{
"Sid": "IPDeny",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::bucket/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": "myIPAddress/32"
}
}
}
]
}
I think by default accounts are restricted from accessing S3 unless they have been given access via policy. However, S3 is designed by default to allow any IP address access. So to block IP's you would have to specify denies explicitly in the policy instead of allows.
You Should flip around the policy from allowing access from only my IP address to denying access from everywhere that is NOT my IP address.
So I Think You Might be Using this :
{
"Id": "S3PolicyId1",
"Statement": [
{
"Sid": "IPAllow",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3::: bucketname",
"Condition": {
"IpAddress": {
"aws:SourceIp": "CIDR Of Allowed IP"
}
}
}
]
You Should Try This :
{
"Id": "S3PolicyId1",
"Statement": [
{
"Sid": "IPDeny",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3::: bucket name*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": "CIDR Of Allowed IP"
}
}
}
]
I Hope This Helps