This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
manage-users.py
282 lines (220 loc) · 8.71 KB
/
manage-users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
from time import time
import boto3
import time
import re
import yaml
import sys
import click
ssm_client = boto3.client('ssm')
ec2_client = boto3.client('ec2')
def get_instance_id():
"""
Returns the EC2 instance ID of the Ghidra server.
"""
response = ec2_client.describe_instances(Filters=[
{
'Name': 'tag:Name',
'Values': ['ghidra-*']
},
{
'Name': 'instance-state-name',
'Values': ['running']
}
])
return response["Reservations"][0]["Instances"][0]["InstanceId"]
def run_ssm_command(cmd):
"""
Runs an arbitrary command on the Ghidra server using SSM.
Keyword arguments:
cmd - The command to run on the Ghidra server.
"""
instance_id = get_instance_id()
run_commands = ["source /etc/profile", cmd]
response = ssm_client.send_command(
InstanceIds=[instance_id],
DocumentName="AWS-RunShellScript",
Parameters={'commands': run_commands}
)
command_id = response['Command']['CommandId']
time.sleep(5)
output = ssm_client.get_command_invocation(
CommandId=command_id,
InstanceId=instance_id,
)
return output["StandardOutputContent"]
def list_users():
"""
Lists all users on the Ghidra server.
"""
svr_admin_list_cmd = f'/opt/ghidra/server/svrAdmin -list --users'
return run_ssm_command(svr_admin_list_cmd)
def add_user(user):
"""
Adds a user to the Ghidra server.
Keyword arguments:
user - The user to add to the Ghidra server.
"""
svr_admin_add_cmd = f'/opt/ghidra/server/svrAdmin -add {user}'
run_ssm_command(svr_admin_add_cmd)
def set_permission(user, perm):
"""
Sets a permission for a user on the Ghidra server.
Keyword arguments:
user - The user to set permissions for.
perm - The permission to set for the user. Should be one of: read-only, write, admin.
"""
svr_admin_grant_cmd = f'/opt/ghidra/server/svrAdmin -grant {user} {perm} tp'
run_ssm_command(svr_admin_grant_cmd)
def remove_user(user):
"""
Removes a user from the Ghidra server.
Keyword arguments:
user - The user to remove from the Ghidra server.
"""
svr_admin_remove_cmd = f'/opt/ghidra/server/svrAdmin -remove {user}'
run_ssm_command(svr_admin_remove_cmd)
def check_and_set_permission(user, perm):
"""
Checks if the supplied permission is valid and sets it for the user.
Keyword arguments:
user - The user to check and set permissions for.
perm - The permission to set for the user. Should be one of: read-only, write, admin.
"""
match perm:
case "admin":
set_permission(user, "+a")
case "write":
set_permission(user, "+w")
case "read-only":
set_permission(user, "+r")
case _:
print("User", user, "has invalid permissions. Should be one of: read-only, write, admin", file=sys.stderr)
raise SystemExit(1)
def get_ghidra_users():
"""
Returns a dictionary of all users on the Ghidra server and their permissions.
"""
std_output = list_users()
regex = r"tp"
regex_search_result = re.search(regex, std_output)
regex_search_result_stripped = std_output[regex_search_result.end(
):].strip()
raw_user_list = regex_search_result_stripped.replace(
'\n', '').replace(' ', ',').split(",")
filtered_user_list = list(filter(None, (raw_user_list)))
current_users = {}
for i in range(1, len(filtered_user_list), 2):
curr_user = {filtered_user_list[i-1]: filtered_user_list[i].replace("(","").replace(")","")}
current_users.update(curr_user)
return current_users
def remove_ghidra_users(current_users,yaml_users,dry_run):
"""
Removes any users that are no longer in the source control list.
Keyword arguments:
current_users - The current users on the Ghidra server.
yaml_users - The users in the source control list.
dry_run - If set, the script will run in dry-run mode and no action will be taken.
Returns:
ret_users - The number of users that will be removed.
"""
ret_users = 0
users_to_remove = [ghidra_user for ghidra_user in current_users.keys() if all(ghidra_user != user.get('ghidraName') for user in yaml_users)]
# Remove any users that are no longer in the source control list
for ghidra_user in users_to_remove:
print(ghidra_user, "isn't in the source control list anymore. Removing...",end='')
if not dry_run:
# Commenting this out for now
# remove_user(ghidra_user)
print("Done.")
else:
print("Dry run mode. Skipping.")
ret_users += 1
return ret_users
def update_ghidra_users(current_users,yaml_users,dry_run):
"""
Updates any users that have had their permissions changed in the source control list.
Keyword arguments:
current_users - Dict of the current users and their permission on the Ghidra server.
yaml_users - Dict of the users and their permissions in the source control list.
dry_run - If set, the script will run in dry-run mode and no action will be taken.
Returns:
ret_users - The number of users that will be updated.
"""
ret_users = 0
for i in range(len(yaml_users)):
curr_yaml_name = yaml_users[i]["ghidraName"]
curr_yaml_perm = (yaml_users[i]["permissions"]).lower()
if curr_yaml_name in current_users.keys() and current_users[curr_yaml_name] != curr_yaml_perm:
print("User", curr_yaml_name, "permissions don't match. Updating...", end='')
if not dry_run:
check_and_set_permission(curr_yaml_name, curr_yaml_perm)
print("Done.")
else:
print("Dry run mode. Skipping.")
ret_users += 1
else:
print("User", curr_yaml_name, "is up-to-date.")
return ret_users
def add_ghidra_users(current_users,yaml_users,dry_run):
"""
Adds any users that are in the source control list but not on the Ghidra server.
Keyword arguments:
current_users - Dict of the current users and their permission on the Ghidra server.
yaml_users - Dict of the users and their permissions in the source control list.
dry_run - If set, the script will run in dry-run mode and no action will be taken.
Returns:
ret_users - The number of users and their permissions that will be added.
"""
# dict to track num users to add and their permissions
ret_users = {
"num_users": 0,
"permissions": []
}
# list comprehension to grab the user:permission pairs that appear in yaml_users but not in current_users
users_to_add = [user for user in yaml_users if user.get('ghidraName') not in current_users.keys()]
for ghidra_user in users_to_add:
print("User", ghidra_user["ghidraName"],
"doesn't exist. Creating...", end='')
if not dry_run:
add_user(ghidra_user["ghidraName"])
print("Done.")
else:
print("Dry run mode. Skipping.")
ret_users["num_users"] += 1
print("Setting permissions for", ghidra_user["ghidraName"] + "...", end='')
if not dry_run:
check_and_set_permission(ghidra_user["ghidraName"], ghidra_user["permissions"])
print("Done.")
else:
print("Dry run mode. Skipping.")
ret_users["permissions"].append(ghidra_user["permissions"])
return ret_users
@click.command()
@click.option('--dry-run', is_flag=True, help='Dry run mode.')
def manage_users(dry_run=False):
"""
Manages users on the Ghidra server.
Keyword arguments:
dry_run - If set, the script will run in dry-run mode and no action will be taken.
"""
ghidra_server_users = get_ghidra_users()
with open("users.yaml", "r") as stream:
try:
source_control_users = yaml.safe_load(stream)
rm_users = remove_ghidra_users(ghidra_server_users,source_control_users,dry_run)
update_users = update_ghidra_users(ghidra_server_users,source_control_users,dry_run)
add_users = add_ghidra_users(ghidra_server_users,source_control_users,dry_run)
if dry_run:
if rm_users > 0 or update_users > 0:
print("Users are being updated or deleted. Failing automerge check!")
sys.exit(1)
elif add_users["num_users"] > 0:
for permission in add_users["permissions"]:
if permission in ["write", "admin"]:
sys.exit(1)
sys.exit(0)
except yaml.YAMLError as err:
print(err)
if __name__ == "__main__":
manage_users()