Skip to content

Commit 2362eb8

Browse files
committed
password hashing customized and also set already hashed by run.py
1 parent aeb9706 commit 2362eb8

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

backend/app.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ def APP_STATUS(): return "init" if db.get("password") is None else "run"
6565
def JWT_SECRET(): return db.get("secret")
6666

6767
def hash_psw(psw: str):
68-
salt = db.get("salt")
69-
if not salt:
70-
salt = secrets.token_hex(32)
71-
db.put("salt", salt)
72-
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()
68+
salt = secrets.token_hex(32)
69+
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()+"-"+salt
70+
71+
def verify_psw(psw: str, hashed: str) -> bool:
72+
psw_hash, salt = hashed.split("-")
73+
new_hashed = hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()
74+
return new_hashed == psw_hash
7375

7476
def set_psw(psw: str):
7577
db.put("password", hash_psw(psw))
@@ -142,7 +144,7 @@ async def login_api(form: OAuth2PasswordRequestForm = Depends()):
142144
if form.password == "":
143145
return {"status":"Cannot insert an empty password!"}
144146
await asyncio.sleep(0.3) # No bruteforce :)
145-
if db.get("password") == hash_psw(form.password):
147+
if verify_psw(form.password, db.get("password")):
146148
return {"access_token": create_access_token({"logged_in": True}), "token_type": "bearer"}
147149
raise HTTPException(406,"Wrong password!")
148150

@@ -185,8 +187,8 @@ async def get_ip_interfaces():
185187

186188
async def startup_main():
187189
db.init()
188-
if os.getenv("HEX_SET_PSW"):
189-
set_psw(bytes.fromhex(os.getenv("HEX_SET_PSW")).decode())
190+
if os.getenv("PSW_HASH_SET"):
191+
db.put("password", os.getenv("PSW_HASH_SET"))
190192
try:
191193
sysctl.set()
192194
except Exception as e:

run.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import subprocess
1010
import getpass
1111
import tarfile
12+
import hashlib
13+
import secrets
1214

1315
pref = "\033["
1416
reset = f"{pref}0m"
@@ -38,6 +40,10 @@ class colors:
3840
cyan = "36m"
3941
white = "37m"
4042

43+
def hash_psw(psw: str):
44+
salt = secrets.token_hex(32)
45+
return hashlib.pbkdf2_hmac("sha256", psw.encode(), salt.encode(), 500_000).hex()+"-"+salt
46+
4147
def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
4248
print(f'{pref}{1 if is_bold else 0};{color}' + text + reset, *args, **kwargs)
4349

@@ -260,7 +266,7 @@ def write_compose(skip_password = True):
260266
f"PORT={args.port}",
261267
f"HOST={args.host}",
262268
f"NTHREADS={args.threads}",
263-
*([f"HEX_SET_PSW={psw_set.encode().hex()}"] if psw_set else [])
269+
*([f"PSW_HASH_SET={hash_psw(psw_set)}"] if psw_set else [])
264270
],
265271
"volumes": [
266272
"firegex_data:/execute/db",
@@ -308,7 +314,7 @@ def write_compose(skip_password = True):
308314
"environment": [
309315
f"PORT={args.port}",
310316
f"NTHREADS={args.threads}",
311-
*([f"HEX_SET_PSW={psw_set.encode().hex()}"] if psw_set else [])
317+
*([f"PSW_HASH_SET={hash_psw(psw_set)}"] if psw_set else [])
312318
],
313319
"volumes": [
314320
"firegex_data:/execute/db"
@@ -760,7 +766,7 @@ def run_standalone():
760766
# Add password if set
761767
psw_set = get_password()
762768
if psw_set:
763-
env_vars.append(f"HEX_SET_PSW={psw_set.encode().hex()}")
769+
env_vars.append(f"PSW_HASH_SET={hash_psw(psw_set)}")
764770

765771
# Prepare environment string for chroot
766772
env_string = " ".join([f"{var}" for var in env_vars])

0 commit comments

Comments
 (0)