跳转到主要内容

议会审核您的AWS IAM策略

项目描述

议会是一个AWS IAM代码审查库。它审查策略以查找问题,例如

  • 格式不正确的JSON
  • 缺少必需元素
  • 错误的前缀和操作名称
  • 错误的资源或条件
  • 类型不匹配
  • 不良策略模式

这个库在浏览器中审查IAM策略时复制了(并添加了!)网页控制台页面的大部分功能。我们希望将该功能作为库。

演示

安装

pip install parliament

使用方法

cat > test.json << EOF
{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action":["s3:GetObject"],
        "Resource": ["arn:aws:s3:::bucket1"]
    }
}
EOF

parliament --file test.json

这将输出

MEDIUM - No resources match for the given action -  - [{'action': 's3:GetObject', 'required_format': 'arn:*:s3:::*/*'}] - {'actions': ['s3:GetObject'], 'filepath': 'test.json'}

此示例显示操作s3:GetObject需要一个与对象路径匹配的资源(即它必须包含"/")。

允许的不同输入类型包括

  • --file: 文件名
  • --directory: 目录路径,例如: --directory . --include_policy_extension json --exclude_pattern ".*venv.*"
  • --aws-managed-policies: 用于特定于repo https://github.com/z0ph/aws_managed_policies
  • --auth-details-file: 用于与"aws iam get-account-authorization-details"返回的文件
  • --字符串:提供如下字符串:“{“Version”: “2012-10-17”,“Statement”: {“Effect”: “Allow”,“Action”: [“s3:GetObject”, “s3:PutBucketPolicy”],“Resource”: [“arn:aws:s3:::bucket1”, “arn:aws:s3:::bucket2/*”]}}”

将Parliament用作库

Parliament原本是打算在其它项目中作为库使用的。以下是一个基本示例。

from parliament import analyze_policy_string

analyzed_policy = analyze_policy_string(policy_doc)
for f in analyzed_policy.findings:
  print(f)

自定义配置文件

您可能决定您想要更改发现的严重性、与之关联的文本,或者您想要忽略某些类型的发现。为了支持这一点,您可以提供一个覆盖配置文件。首先,创建一个test.json文件

{
    "Version": "2012-10-17",
    "Id": "123",
    "Statement": [
      {
        "Effect": "Allow",
        "Action": "s3:abc",
        "Resource": "*"
      },
      {
        "Effect": "Allow",
        "Action": ["s3:*", "ec2:*"],
        "Resource": "arn:aws:s3:::test/*"
      }
    ]
 }

这将包含两个发现

  • 低 - 未知操作 - - 未知操作 s3:abc
  • 中 - 给定操作没有匹配的资源

第二个发现将会非常长,因为每个s3和ec2操作都会展开,并且大多数对于提供的S3对象路径资源都是不正确的。

现在创建一个文件config_override.yaml,内容如下

UNKNOWN_ACTION:
  severity: MEDIUM
  ignore_locations:
  - filepath:
    - testa.json
    - .*.py

RESOURCE_MISMATCH:
  ignore_locations:
  - actions: ".*s3.*"

现在运行:parliament --file test.json --config config_override.yaml 您将只有一个输出:中 - 未知操作 - - 未知操作 s3:abc

请注意,该发现的严重性已从更改为。另外,注意另一个发现已消失,因为之前的RESOURCE_MISMATCH发现包含一个actions元素为["s3:*", "ec2:*"]。忽略逻辑将您提供的值和发现值转换为小写,然后使用您的字符串作为正则表达式。这意味着我们正在检查s3是否在str(["s3:*", "ec2:*"])

现在将test.json重命名为testa.json并重新运行命令。您将不再有任何输出,因为我们正在根据UNKNOWN_ACTION的文件路径进行过滤,并过滤包含testa.json.py的任何文件路径。

您还可以使用echo $?检查退出状态,并看到在没有发现时退出状态为0。当有发现时,退出状态将不为0。

您可以在ignore_locations中有多个元素。例如,

- filepath: "test.json"
  action: "s3:GetObject"
  resource: 
  - "a"
  - "b"
- resource: "c.*"

假设发现具有以下类型的值在location元素中,这将忽略任何匹配“test.json”文件路径、操作“s3:GetObject”和资源“a”或“b”的发现。它还会忽略匹配“c.*”的资源。

自定义审计员

私有审计员

本节将展示如何创建自己的私有审计员来查找授予对敏感桶secretbucketothersecretbucket访问权限的任何策略。

创建一个包含以下内容的文件test.json

{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::secretbucket/*"
    }
}

这是我们想要警告的策略的示例。该策略通常不会生成任何发现。

创建包含以下内容的文件config_override.yaml

SENSITIVE_BUCKET_ACCESS:
  title: Sensitive bucket access
  description: Allows read access to an important S3 bucket
  severity: MEDIUM
  group: CUSTOM

parliament目录(包含iam_definition.json)中,创建目录private_auditors和文件parliament/private_auditors/sensitive_bucket_access.py

from parliament import is_arn_match, expand_action

def audit(policy):
    action_resources = {}
    for action in expand_action("s3:*"):
        # Iterates through a list of containing elements such as
        # {'service': 's3', 'action': 'GetObject'}
        action_name = "{}:{}".format(action["service"], action["action"])
        action_resources[action_name] = policy.get_allowed_resources(action["service"], action["action"])
    
    for action_name in action_resources:
        resources = action_resources[action_name]
        for r in resources:
            if is_arn_match("object", "arn:aws:s3:::secretbucket*", r) or is_arn_match("object", "arn:aws:s3:::othersecretbucket*", r):
                policy.add_finding("SENSITIVE_BUCKET_ACCESS", location={"action": action_name, "resource": r})

这将查找对感兴趣的桶的任何s3访问,包括不仅包括对象访问,如s3:GetObject访问,还包括像s3:PutBucketAcl这样的操作。

针对我们的测试文件运行,我们将得到以下输出

./bin/parliament --file test.json --config config_override.yaml --json

{"issue": "SENSITIVE_BUCKET_ACCESS", "title": "Sensitive bucket access", "severity": "MEDIUM", "description": "Allows read access to an important S3 bucket", "detail": "", "location": {"action": "s3:GetObject", "resource": "arn:aws:s3:::secretbucket/*", "filepath": "test.json"}}

您现在可以决定这个特定情况是否适合您,并选择通过修改config_override.yaml来忽略它,包括

ignore_locations:
  - filepath: "test.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"

请注意,我必须对转义星号进行双重转义。如果您创建了另一个策略,比如test2.json,您想要忽略,您只需将这些值追加到列表中

ignore_locations:
  - filepath: "test.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"
  - filepath: "test2.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"

或者您也可以这样做

ignore_locations:
  - filepath:
    - "test.json"
    - "test2.json"
    action: "s3:GetObject"
    resource: "arn:aws:s3:::secretbucket/\\*"

私有审计员的单元测试

为了为我们的新私有审计员创建单元测试,创建目录./parliament/private_auditors/tests/并在其中创建文件test_sensitive_bucket_access.py,内容如下

from parliament import analyze_policy_string

class TestCustom():
    """Test class for custom auditor"""

    def test_my_auditor(self):
        policy = analyze_policy_string(
            """{
    "Version": "2012-10-17",
    "Statement": {
        "Effect": "Allow",
        "Action": "s3:GetObject",
        "Resource": "arn:aws:s3:::secretbucket/*"}}""",
        )
        assert_equal(len(policy.findings), 1)

该测试确保对于给定的策略(授予对我们的秘密桶的读取访问权限)将创建一个发现。

现在当您运行./tests/scripts/unit_tests.sh时,应该有一个额外的测试运行。

社区审计员

  • 社区审计员的过程与私有审计员相同,除了
  • 社区审计员位于 parliament/community_auditors 文件夹中,而不是 parliament/private_auditors 文件夹。
  • 社区审计员与软件包捆绑在一起,用户可以通过指定 --include-community-auditors 标志将其包含在测试中。

开发

设置测试环境

python3 -m venv ./venv && source venv/bin/activate
pip3 install -r requirements.txt

使用以下方式运行单元测试:

make test

本地运行

bin/parliament

更新权限信息

IAM 数据是通过爬取文档 此处 并使用 beautifulsoup 解析这些信息,通过 ./utils/update_iam_data.py 获取的。

使用以下脚本生成新的 iam_definition.json

python3 -m venv ./venv
source ./venv/bin/activate
pip install requests beautifulsoup4
wget "https://raw.githubusercontent.com/duo-labs/parliament/main/utils/update_iam_data.py"
python ./update_iam_data.py > iam_definition.json

找到您安装 Parliament 的 Python 环境,并覆盖旧的 iam_definition.json

使用 Parliament 的项目

  • CloudMapper:具有审计 AWS 环境的功能,并将审计 IAM 策略作为其中的一部分。
  • tf-parliament:针对 terraform 文件运行 Parliament
  • iam-lint:用于 lint AWS IAM 策略文档的 Github 动作
  • Paco:云编排工具,作为库将 Parliament 集成以验证项目的 IAM 策略,并警告发现的问题。
  • ConsoleMe:使管理和使用多个 AWS 账户更简单的网络服务,使用 Parliament 进行 IAM 策略 linting
  • iamlive:通过客户端监控观察 AWS 调用来生成 IAM 策略

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

parliament-1.6.3.tar.gz (533.4 kB 查看哈希值)

上传时间 源代码

构建分发

parliament-1.6.3-py3-none-any.whl (554.1 kB 查看哈希值)

上传时间 Python 3

支持者