11import requests
22import os
3+ import re
34from bs4 import BeautifulSoup
45from Graphpython .utils .helpers import print_yellow , print_green , print_red , get_user_agent , get_access_token
56
@@ -98,20 +99,19 @@ def locate_permissionid(args):
9899 if not args .id :
99100 print_red ("[-] Error: --id argument is required for Locate-PermissionID command" )
100101 return
101-
102102 print_yellow ("[*] Locate-PermissionID" )
103103 print ("=" * 80 )
104104
105105 def parse_html (content ):
106106 soup = BeautifulSoup (content , 'html.parser' )
107107 permissions = {}
108-
108+
109109 for h3 in soup .find_all ('h3' ):
110110 title = h3 .text
111111 table = h3 .find_next ('table' )
112112 headers = [th .text for th in table .find ('thead' ).find_all ('th' )]
113113 rows = table .find ('tbody' ).find_all ('tr' )
114-
114+
115115 permission_data = {}
116116 for row in rows :
117117 cells = row .find_all ('td' )
@@ -123,28 +123,28 @@ def parse_html(content):
123123 headers [2 ]: delegated
124124 }
125125 permissions [title ] = permission_data
126-
126+
127127 return permissions
128128
129129 def highlight (text , should_highlight ):
130130 if should_highlight :
131131 return f"\033 [92m{ text } \033 [0m"
132132 return text
133-
134- def print_permission (permission , data , app_ids , delegated_ids ):
133+
134+ def print_permission (permission , data , identifiers ):
135135 print_green (f"{ permission } " )
136136 for category , values in data .items ():
137137 print (f" { category } :" )
138- app_highlight = data ['Identifier' ]['Application' ] in app_ids
139- delegated_highlight = data ['Identifier' ]['Delegated' ] in delegated_ids
138+ app_highlight = data ['Identifier' ]['Application' ] in identifiers or permission in identifiers
139+ delegated_highlight = data ['Identifier' ]['Delegated' ] in identifiers or permission in identifiers
140140 print (f" Application: { highlight (values ['Application' ], app_highlight )} " )
141141 print (f" Delegated: { highlight (values ['Delegated' ], delegated_highlight )} " )
142142 print ()
143143
144144 identifiers = args .id .split (',' )
145145 script_dir = os .path .dirname (os .path .abspath (__file__ ))
146146 file_path = os .path .join (script_dir , 'graphpermissions.txt' )
147-
147+
148148 try :
149149 with open (file_path , 'r' ) as file :
150150 content = file .read ()
@@ -156,25 +156,80 @@ def print_permission(permission, data, app_ids, delegated_ids):
156156 print_red (f"[-] An error occurred: { e } " )
157157 print ("=" * 80 )
158158 return
159-
159+
160160 permissions = parse_html (content )
161- app_ids = []
162- delegated_ids = []
163-
164- for permission , data in permissions .items ():
165- if data ['Identifier' ]['Application' ] in identifiers :
166- app_ids .append (data ['Identifier' ]['Application' ])
167- if data ['Identifier' ]['Delegated' ] in identifiers :
168- delegated_ids .append (data ['Identifier' ]['Delegated' ])
169-
170161 found_permissions = False
171-
162+
172163 for permission , data in permissions .items ():
173- if data ['Identifier' ]['Application' ] in app_ids or data ['Identifier' ]['Delegated' ] in delegated_ids :
174- print_permission (permission , data , app_ids , delegated_ids )
164+ if (data ['Identifier' ]['Application' ] in identifiers or
165+ data ['Identifier' ]['Delegated' ] in identifiers or
166+ permission in identifiers ):
167+ print_permission (permission , data , identifiers )
175168 found_permissions = True
176-
169+
177170 if not found_permissions :
178- print_red ("[-] Permission ID not found" )
171+ print_red ("[-] Permission ID or name not found" )
172+
173+ print ("=" * 80 )
174+
175+ def locate_directoryrole (args ):
176+ if not args .id :
177+ print_red ("[-] Error: --id argument is required for Locate-DirectoryRole command" )
178+ return
179+ print_yellow ("[*] Locate-DirectoryRole" )
180+ print ("=" * 80 )
181+
182+ def parse_html (content ):
183+ soup = BeautifulSoup (content , 'html.parser' )
184+ roles = []
185+ for row in soup .find_all ('tr' )[1 :]: # skip header row
186+ cells = row .find_all ('td' )
187+ if len (cells ) == 3 :
188+ role_name = cells [0 ].text .strip ()
189+ description = cells [1 ].text .strip ()
190+ template_id = cells [2 ].text .strip ()
191+ privileged = 'privileged-roles-permissions' in str (cells [1 ])
192+ roles .append ({
193+ 'name' : role_name ,
194+ 'description' : description ,
195+ 'template_id' : template_id ,
196+ 'privileged' : privileged
197+ })
198+ return roles
199+
200+ def print_role (role ):
201+ print (f"Role: \033 [92m{ role ['name' ]} \033 [0m" )
202+ print (f"Description: \033 [92m{ role ['description' ]} \033 [0m" )
203+ print (f"Template ID: \033 [92m{ role ['template_id' ]} \033 [0m" )
204+ print (f"Privileged: \033 [92m{ 'Yes' if role ['privileged' ] else 'No' } \033 [0m" )
205+ print ()
206+
207+ identifier = args .id .lower ()
208+
209+ script_dir = os .path .dirname (os .path .abspath (__file__ ))
210+ file_path = os .path .join (script_dir , 'directoryroles.txt' )
179211
212+ try :
213+ with open (file_path , 'r' , encoding = 'utf-8' ) as file :
214+ content = file .read ()
215+ except FileNotFoundError :
216+ print_red (f"[-] The file { file_path } does not exist." )
217+ print ("=" * 80 )
218+ return
219+ except Exception as e :
220+ print_red (f"[-] An error occurred while reading the file: { e } " )
221+ print ("=" * 80 )
222+ return
223+
224+ roles = parse_html (content )
225+ found_role = False
226+
227+ for role in roles :
228+ if identifier in role ['name' ].lower () or identifier == role ['template_id' ].lower ():
229+ print_role (role )
230+ found_role = True
231+
232+ if not found_role :
233+ print_red ("[-] Directory role ID or name not found" )
234+
180235 print ("=" * 80 )
0 commit comments