1
1
import requests
2
2
import os
3
+ import re
3
4
from bs4 import BeautifulSoup
4
5
from Graphpython .utils .helpers import print_yellow , print_green , print_red , get_user_agent , get_access_token
5
6
@@ -98,20 +99,19 @@ def locate_permissionid(args):
98
99
if not args .id :
99
100
print_red ("[-] Error: --id argument is required for Locate-PermissionID command" )
100
101
return
101
-
102
102
print_yellow ("[*] Locate-PermissionID" )
103
103
print ("=" * 80 )
104
104
105
105
def parse_html (content ):
106
106
soup = BeautifulSoup (content , 'html.parser' )
107
107
permissions = {}
108
-
108
+
109
109
for h3 in soup .find_all ('h3' ):
110
110
title = h3 .text
111
111
table = h3 .find_next ('table' )
112
112
headers = [th .text for th in table .find ('thead' ).find_all ('th' )]
113
113
rows = table .find ('tbody' ).find_all ('tr' )
114
-
114
+
115
115
permission_data = {}
116
116
for row in rows :
117
117
cells = row .find_all ('td' )
@@ -123,28 +123,28 @@ def parse_html(content):
123
123
headers [2 ]: delegated
124
124
}
125
125
permissions [title ] = permission_data
126
-
126
+
127
127
return permissions
128
128
129
129
def highlight (text , should_highlight ):
130
130
if should_highlight :
131
131
return f"\033 [92m{ text } \033 [0m"
132
132
return text
133
-
134
- def print_permission (permission , data , app_ids , delegated_ids ):
133
+
134
+ def print_permission (permission , data , identifiers ):
135
135
print_green (f"{ permission } " )
136
136
for category , values in data .items ():
137
137
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
140
140
print (f" Application: { highlight (values ['Application' ], app_highlight )} " )
141
141
print (f" Delegated: { highlight (values ['Delegated' ], delegated_highlight )} " )
142
142
print ()
143
143
144
144
identifiers = args .id .split (',' )
145
145
script_dir = os .path .dirname (os .path .abspath (__file__ ))
146
146
file_path = os .path .join (script_dir , 'graphpermissions.txt' )
147
-
147
+
148
148
try :
149
149
with open (file_path , 'r' ) as file :
150
150
content = file .read ()
@@ -156,25 +156,80 @@ def print_permission(permission, data, app_ids, delegated_ids):
156
156
print_red (f"[-] An error occurred: { e } " )
157
157
print ("=" * 80 )
158
158
return
159
-
159
+
160
160
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
-
170
161
found_permissions = False
171
-
162
+
172
163
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 )
175
168
found_permissions = True
176
-
169
+
177
170
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' )
179
211
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
+
180
235
print ("=" * 80 )
0 commit comments