Skip to content

How do I restrict senders for SES SMTP by source IP address?

0

I've found external guidance for setting up a filter to do this in the SMTP IAM user permissions (https://www.matraex.com/wp-content/uploads/2023/03/AWS-IAM-console.png), but the filter "Ipaddress" is not available, nor are AMAZON examples available here on Amazon.

It seems I can only restrict by email address, which is easily masqueraded.

2 Answers
0

Hi,

Would this example be what you're looking for ?

{
    “Version”: “2012-10-17”
    “Statement”: [
        {
            “Effect”: “Allow”,
            “Action”: “ses:SendRawEmail”,
            “Resource”: “*”,
            “Condition”: {
                “IpAddress”: {
                    “aws:SourceIp”: [
                        “xx.yy.zz.0/24”,
                        “xx.zz.yy.0/22”
                    ]
                }
            }
        }
    ]
}  

For details, on the Condition with SourceIp, see https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_aws_deny-ip.html

Best,

Didier

EXPERT

answered 2 years ago

0

The policy example behind the link is quite correct, if the SMTP connections are arriving to SES's public endpoints on the public internet. If you're accessing SES from inside a VPC with a VPC endpoint for SES's SMTP service, the aws:SourceIp condition key will not be present, and you should use aws:SourceVpc instead to restrict access by the ID of the VPC where the VPC endpoint resides.

IpAddress in the example is not the condition key; it's a comparison operator, similar to StringEquals that would compare string values instead of IP CIDRs. If you're using the graphical wizard to construct the policy statement, the condition key to inspect is aws:SourceIp, with IpAddress as the operator. It's probably easier just to copy the policy example in the raw JSON policy editor than trying to find a way to do that with the wizard.

In general, for IAM users that are meant only to access services from specific IP addresses or VPCs, I'd suggest using an explicit "Deny" statement to block access to all services, unless the request is coming from the one of the permitted sources. The example below does that, and allows both a specific VPC ID, the listed IP ranges, and internal calls between AWS services, such as S3 calling KMS for encryption services.

Using the explicit Deny prevents inadvertent mistakes, such as would typically happen if a resource-based policy in the account, such as an S3 bucket policy, granted access broadly to all users/roles in the account without enforcing any IP or other restrictions. In that case, while the IAM user's identity-based policy would only allow the SMTP send permission, the S3 bucket policy would additionally grant access to the S3 bucket policy, without it being in any way visible in the IAM user's policies. When the "Deny" statement below is applied, access to every service, including S3, will be blocked unless the request is coming from the accepted source networks, regardless of what is configured in any resource-based policy.

Using this type of Deny statement also makes the Allow statements often easier to read, because the IP and VPC restrictions don't need to be included in them. You can remove the StringNotEqualsIfExists comparison clause for the aws:SourceVpc criterion if the mails are sent over the internet and not from inside a VPC.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyAllAccessExceptFromPermittedNetworkOrigins"
      "Effect": "Deny",
      "Action": "*",
      "Resource": "*",
      "Condition": {
        "StringNotEqualsIfExists": {
          "aws:SourceVpc": ["vpc-00000000000000000"]
        },
        "NotIpAddressIfExists": {
          "aws:SourceIp": ["192.0.2.0/24","192.0.3.0/24"]
        },
        "BoolIfExists": {
          "aws:ViaAWSService": "false"
        }
      }
    },
    {
      "Sid": "AllowSmtpSend"
      "Effect": "Allow",
      "Action": "ses:SendRawEmail",
      "Resource": "*"
    }
  ]
}
EXPERT

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.