Skip to content

Instantly share code, notes, and snippets.

@xr09
Created October 27, 2020 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xr09/222fd2a374d1b6319f33ffdae3ebdefe to your computer and use it in GitHub Desktop.
Save xr09/222fd2a374d1b6319f33ffdae3ebdefe to your computer and use it in GitHub Desktop.
Python script to Create API User
import logging
import sys
import json
import random
import string
import argparse
import os
# Set framework path
sys.path.append("/var/ossec/framework")
try:
from wazuh.security import (
create_user,
get_users,
get_roles,
set_user_role,
update_user,
)
except Exception as e:
logging.error("No module 'wazuh' found.")
sys.exit(1)
def db_users():
users_result = get_users()
return {user["username"]: user["id"] for user in users_result.affected_items}
def db_roles():
roles_result = get_roles()
return {role["name"]: role["id"] for role in roles_result.affected_items}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='add_user script')
parser.add_argument('--username', action="store", dest="username")
parser.add_argument('--password', action="store", dest="password")
results = parser.parse_args()
username = results.username
password = results.password
initial_users = db_users()
if username not in initial_users:
# create a new user
create_user(username=username, password=password)
users = db_users()
uid = users[username]
roles = db_roles()
rid = roles["administrator"]
set_user_role(
user_id=[
str(uid),
],
role_ids=[
str(rid),
],
)
else:
# modify an existing user ("wazuh" or "wazuh-wui")
uid = initial_users[username]
update_user(
user_id=[
str(uid),
],
password=password,
)
# set a random password for all other users
for name, id in initial_users.items():
if name != username:
random_pass = "".join(
random.choices(
string.ascii_uppercase
+ string.ascii_lowercase
+ string.digits
+ "@$!%*?&-_",
k=16,
)
)
update_user(
user_id=[
str(id),
],
password=random_pass,
)
@xr09
Copy link
Author

xr09 commented Oct 27, 2020

Download to /var/ossec/framework/scripts/create_user.py:

wget https://gist.github.com/xr09/222fd2a374d1b6319f33ffdae3ebdefe/raw/ae61649efb68aee7981f7042d613f2b8249d1499/create_user.py -O /var/ossec/framework/scripts/create_user.py

Execute with the python interpreter bundled with Wazuh:

/var/ossec/framework/python/bin/python3 /var/ossec/framework/scripts/create_user.py --username bilbo --password ".S3cur3Pa55w0rd*-"

The passsword must comply with requirements (8+ length, uppercase, lowercase, specials chars).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment