password hashing customized and also set already hashed by run.py
This commit is contained in:
@@ -65,11 +65,13 @@ def APP_STATUS(): return "init" if db.get("password") is None else "run"
|
|||||||
def JWT_SECRET(): return db.get("secret")
|
def JWT_SECRET(): return db.get("secret")
|
||||||
|
|
||||||
def hash_psw(psw: str):
|
def hash_psw(psw: str):
|
||||||
salt = db.get("salt")
|
|
||||||
if not salt:
|
|
||||||
salt = secrets.token_hex(32)
|
salt = secrets.token_hex(32)
|
||||||
db.put("salt", salt)
|
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()+"-"+salt
|
||||||
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()
|
|
||||||
|
def verify_psw(psw: str, hashed: str) -> bool:
|
||||||
|
psw_hash, salt = hashed.split("-")
|
||||||
|
new_hashed = hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()
|
||||||
|
return new_hashed == psw_hash
|
||||||
|
|
||||||
def set_psw(psw: str):
|
def set_psw(psw: str):
|
||||||
db.put("password", hash_psw(psw))
|
db.put("password", hash_psw(psw))
|
||||||
@@ -142,7 +144,7 @@ async def login_api(form: OAuth2PasswordRequestForm = Depends()):
|
|||||||
if form.password == "":
|
if form.password == "":
|
||||||
return {"status":"Cannot insert an empty password!"}
|
return {"status":"Cannot insert an empty password!"}
|
||||||
await asyncio.sleep(0.3) # No bruteforce :)
|
await asyncio.sleep(0.3) # No bruteforce :)
|
||||||
if db.get("password") == hash_psw(form.password):
|
if verify_psw(form.password, db.get("password")):
|
||||||
return {"access_token": create_access_token({"logged_in": True}), "token_type": "bearer"}
|
return {"access_token": create_access_token({"logged_in": True}), "token_type": "bearer"}
|
||||||
raise HTTPException(406,"Wrong password!")
|
raise HTTPException(406,"Wrong password!")
|
||||||
|
|
||||||
@@ -185,8 +187,8 @@ reset, startup, shutdown = load_routers(api)
|
|||||||
|
|
||||||
async def startup_main():
|
async def startup_main():
|
||||||
db.init()
|
db.init()
|
||||||
if os.getenv("HEX_SET_PSW"):
|
if os.getenv("PSW_HASH_SET"):
|
||||||
set_psw(bytes.fromhex(os.getenv("HEX_SET_PSW")).decode())
|
db.put("password", os.getenv("PSW_HASH_SET"))
|
||||||
try:
|
try:
|
||||||
sysctl.set()
|
sysctl.set()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
12
run.py
12
run.py
@@ -9,6 +9,8 @@ import multiprocessing
|
|||||||
import subprocess
|
import subprocess
|
||||||
import getpass
|
import getpass
|
||||||
import tarfile
|
import tarfile
|
||||||
|
import hashlib
|
||||||
|
import secrets
|
||||||
|
|
||||||
pref = "\033["
|
pref = "\033["
|
||||||
reset = f"{pref}0m"
|
reset = f"{pref}0m"
|
||||||
@@ -38,6 +40,10 @@ class colors:
|
|||||||
cyan = "36m"
|
cyan = "36m"
|
||||||
white = "37m"
|
white = "37m"
|
||||||
|
|
||||||
|
def hash_psw(psw: str):
|
||||||
|
salt = secrets.token_hex(32)
|
||||||
|
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()+"-"+salt
|
||||||
|
|
||||||
def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
|
def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
|
||||||
print(f'{pref}{1 if is_bold else 0};{color}' + text + reset, *args, **kwargs)
|
print(f'{pref}{1 if is_bold else 0};{color}' + text + reset, *args, **kwargs)
|
||||||
|
|
||||||
@@ -260,7 +266,7 @@ def write_compose(skip_password = True):
|
|||||||
f"PORT={args.port}",
|
f"PORT={args.port}",
|
||||||
f"HOST={args.host}",
|
f"HOST={args.host}",
|
||||||
f"NTHREADS={args.threads}",
|
f"NTHREADS={args.threads}",
|
||||||
*([f"HEX_SET_PSW={psw_set.encode().hex()}"] if psw_set else [])
|
*([f"PSW_HASH_SET={hash_psw(psw_set)}"] if psw_set else [])
|
||||||
],
|
],
|
||||||
"volumes": [
|
"volumes": [
|
||||||
"firegex_data:/execute/db",
|
"firegex_data:/execute/db",
|
||||||
@@ -308,7 +314,7 @@ def write_compose(skip_password = True):
|
|||||||
"environment": [
|
"environment": [
|
||||||
f"PORT={args.port}",
|
f"PORT={args.port}",
|
||||||
f"NTHREADS={args.threads}",
|
f"NTHREADS={args.threads}",
|
||||||
*([f"HEX_SET_PSW={psw_set.encode().hex()}"] if psw_set else [])
|
*([f"PSW_HASH_SET={hash_psw(psw_set)}"] if psw_set else [])
|
||||||
],
|
],
|
||||||
"volumes": [
|
"volumes": [
|
||||||
"firegex_data:/execute/db"
|
"firegex_data:/execute/db"
|
||||||
@@ -760,7 +766,7 @@ def run_standalone():
|
|||||||
# Add password if set
|
# Add password if set
|
||||||
psw_set = get_password()
|
psw_set = get_password()
|
||||||
if psw_set:
|
if psw_set:
|
||||||
env_vars.append(f"HEX_SET_PSW={psw_set.encode().hex()}")
|
env_vars.append(f"PSW_HASH_SET={hash_psw(psw_set)}")
|
||||||
|
|
||||||
# Prepare environment string for chroot
|
# Prepare environment string for chroot
|
||||||
env_string = " ".join([f"{var}" for var in env_vars])
|
env_string = " ".join([f"{var}" for var in env_vars])
|
||||||
|
|||||||
Reference in New Issue
Block a user