Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add csv as an additional output to the check_privesc function #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -501,4 +501,5 @@ paket-files/

PrivEscScanner/all_org_folder_proj_sa_permissions.json
PrivEscScanner/privesc_methods.txt
PrivEscScanner/privesc_methods.csv
PrivEscScanner/setIamPolicy_methods.txt
23 changes: 17 additions & 6 deletions PrivEscScanner/check_for_privesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import json
import csv

# You can correlate these to the described methods here: https://rhinosecuritylabs.com/gcp/privilege-escalation-google-cloud-platform-part-1/ and here: https://rhinosecuritylabs.com/cloud-security/privilege-escalation-google-cloud-platform-part-2/
methods_and_permissions = {
Expand Down Expand Up @@ -261,19 +262,25 @@
}


def check_privesc(permissions, resource_type, resource_id, member, f):
def check_privesc(permissions, resource_type, resource_id, member, f, csvwriter):
first_method = True
third_column = ""
for privesc_method in methods_and_permissions:
if set(methods_and_permissions[privesc_method]['Permissions']).issubset(set(permissions)) and resource_type[:-1] in methods_and_permissions[privesc_method]['Scope']:
if first_method: # Only print out a user if there is a method associated with it
print(f'{member} on {resource_type[:-1]} {resource_id}:')
f.write(f'{member} on {resource_type[:-1]} {resource_id}:\n')
first_column = f'%s' % (member)
second_column = f'%s %s' % (resource_type[:-1], resource_id)
first_method = False
print(f' {privesc_method}')
f.write(f' {privesc_method}\n')
third_column = third_column + f'%s\n' % (privesc_method)

if first_method is False:
f.write('\n')
row = [first_column, second_column, third_column]
csvwriter.writerow(row)


try:
Expand All @@ -286,10 +293,14 @@ def check_privesc(permissions, resource_type, resource_id, member, f):

print('All Privilege Escalation Methods\n')
with open('privesc_methods.txt', 'w+') as f:
for resource_type in permissions: # Org, Folder, Proj, SA
for resource in permissions[resource_type]: # IDs of Orgs, Folders, Projs, SAs
for member in permissions[resource_type][resource]: # Members with permissions on the current resource
check_privesc(permissions[resource_type][resource][member], resource_type, resource, member, f)
with open('privesc_methods.csv', 'w', newline='') as csv_f:
csvwriter = csv.writer(csv_f)
csv_headers = ['Principal', 'Resource', 'Privesc Methods']
csvwriter.writerow(csv_headers)
for resource_type in permissions: # Org, Folder, Proj, SA
for resource in permissions[resource_type]: # IDs of Orgs, Folders, Projs, SAs
for member in permissions[resource_type][resource]: # Members with permissions on the current resource
check_privesc(permissions[resource_type][resource][member], resource_type, resource, member, f, csvwriter)

print('Misc. setIamPolicy Permissions\n')
with open('setIamPolicy_methods.txt', 'w+') as f:
Expand All @@ -309,4 +320,4 @@ def check_privesc(permissions, resource_type, resource_id, member, f):
f.write('\n')

print('\nDone!')
print('Results output to ./privesc_methods.txt and ./setIamPolicy_methods.txt...')
print('Results output to ./privesc_methods.txt, ./privesc_methods.csv and ./setIamPolicy_methods.txt...')