强制用户开启MFA

功能:强制所有IAM User开启MFA,不然访问不了AWS任何服务。

原理:通过IAM Policy实现,只要用户没有开启MFA,就把所有权限都禁掉

创建IAM Policy

进入AWS控制台,创建新的Policy:

image-20220413122955317

选择JSON格式编辑:

image-20220413123020997

Policy内容如下:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AllowViewAccountInfo",
            "Effect": "Allow",
            "Action": [
                "iam:GetAccountPasswordPolicy",
                "iam:ListVirtualMFADevices"
            ],
            "Resource": "*"
        },
        {
            "Sid": "AllowManageOwnPasswords",
            "Effect": "Allow",
            "Action": [
                "iam:ChangePassword",
                "iam:GetUser"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnAccessKeys",
            "Effect": "Allow",
            "Action": [
                "iam:CreateAccessKey",
                "iam:DeleteAccessKey",
                "iam:ListAccessKeys",
                "iam:UpdateAccessKey"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnSigningCertificates",
            "Effect": "Allow",
            "Action": [
                "iam:DeleteSigningCertificate",
                "iam:ListSigningCertificates",
                "iam:UpdateSigningCertificate",
                "iam:UploadSigningCertificate"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnSSHPublicKeys",
            "Effect": "Allow",
            "Action": [
                "iam:DeleteSSHPublicKey",
                "iam:GetSSHPublicKey",
                "iam:ListSSHPublicKeys",
                "iam:UpdateSSHPublicKey",
                "iam:UploadSSHPublicKey"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnGitCredentials",
            "Effect": "Allow",
            "Action": [
                "iam:CreateServiceSpecificCredential",
                "iam:DeleteServiceSpecificCredential",
                "iam:ListServiceSpecificCredentials",
                "iam:ResetServiceSpecificCredential",
                "iam:UpdateServiceSpecificCredential"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnVirtualMFADevice",
            "Effect": "Allow",
            "Action": [
                "iam:CreateVirtualMFADevice",
                "iam:DeleteVirtualMFADevice"
            ],
            "Resource": "arn:aws:iam::*:mfa/${aws:username}"
        },
        {
            "Sid": "AllowManageOwnUserMFA",
            "Effect": "Allow",
            "Action": [
                "iam:DeactivateMFADevice",
                "iam:EnableMFADevice",
                "iam:ListMFADevices",
                "iam:ResyncMFADevice"
            ],
            "Resource": "arn:aws:iam::*:user/${aws:username}"
        },
        {
            "Sid": "DenyAllExceptListedIfNoMFA",
            "Effect": "Deny",
            "NotAction": [
                "iam:CreateVirtualMFADevice",
                "iam:EnableMFADevice",
                "iam:GetUser",
                "iam:ListMFADevices",
                "iam:ListVirtualMFADevices",
                "iam:ResyncMFADevice",
                "sts:GetSessionToken"
            ],
            "Resource": "*",
            "Condition": {
                "BoolIfExists": {
                    "aws:MultiFactorAuthPresent": "false"
                }
            }
        }
    ]
}

进入下一步,policy命名为force-mfa:

image-20220413123114667

点击创建。

创建用户进行测试

在控制台添加新用户:

image-20220413123157387

输入用户名和密码:

image-20220413123306352

进入下一步,添加上一步创建的force-mfa policy:

image-20220413123335494

再添加一条S3FullAccess权限,用于测试该用户能否访问S3(如果没开启MFA则访问不了):

image-20220413123535898

点击创建。

用户访问S3权限测试

使用刚才创建用户的帐号和密码登录到AWS控制台,进入S3页面,由于用户没有设置MFA,所以访问S3没有任何权限:

image-20220413123625387

进入IAM控制面板,点击添加MFA

image-20220413123904053

分配MFA设备:

image-20220413123928264

选择虚拟MFA设备

image-20220413123941394

在手机上安装Google Authenticator,扫描二维码,输入数字:

image-20220413124128648

添加完成:

image-20220413124140739

此时用户已经配置了MFA登录。退出AWS控制台,重新登录(要输入MFA代码)。进入S3页面,能够成功的访问S3:

image-20220413124329511


如果想批量给用户设置强制MFA,使用用户组来操作是比较简单的方式:

image-20220413133446027