Created
February 15, 2017 23:24
-
-
Save onyxraven/e870cc7c28f1d3653b331d092db04e71 to your computer and use it in GitHub Desktop.
Find IAM principals with any of the given actions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
users = JSON.parse(`aws iam list-users`)['Users'].map { |u| u['Arn'] } | |
groups = JSON.parse(`aws iam list-groups`)['Groups'].map { |u| u['Arn'] } | |
roles = JSON.parse(`aws iam list-roles`)['Roles'].map { |u| u['Arn'] } | |
all = users + groups + roles | |
actions = ARGV | |
puts "Searching #{all.size} principals for #{actions} *" | |
all.each do |arn| | |
out = JSON.parse(`aws iam simulate-principal-policy --policy-source-arn #{arn} --action-names #{actions.join(' ')}`) | |
allowed = out['EvaluationResults'].select { |res| res["EvalDecision"] == "allowed" } | |
unless allowed.empty? | |
policies = allowed.map { |r| r['MatchedStatements'].map { |s| s['SourcePolicyId'] } }.flatten.uniq | |
puts "* ALLOW: #{arn} by #{policies}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment