NetExec/tests/test_smb_database.py

264 lines
4.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
2023-03-22 21:16:10 +00:00
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
2023-09-14 21:41:23 +00:00
from nxc.nxcdb import delete_workspace, NXCDBMenu
from nxc.first_run import first_run_setup
from nxc.loaders.protocolloader import ProtocolLoader
from nxc.logger import NXCAdapter
from nxc.paths import WS_PATH
2023-03-22 21:16:10 +00:00
from sqlalchemy.dialects.sqlite import Insert
@pytest.fixture(scope="session")
def db_engine():
db_path = os.path.join(WS_PATH, "test/smb.db")
2023-05-08 18:39:36 +00:00
db_engine = create_engine(f"sqlite:///{db_path}", isolation_level="AUTOCOMMIT", future=True)
2023-03-22 21:16:10 +00:00
yield db_engine
db_engine.dispose()
@pytest.fixture(scope="session")
def db_setup(db_engine):
2023-03-22 21:16:10 +00:00
proto = "smb"
# setup_logger()
logger = NXCAdapter()
2023-03-22 21:16:10 +00:00
first_run_setup(logger)
p_loader = ProtocolLoader()
2023-03-22 21:16:10 +00:00
protocols = p_loader.get_protocols()
2023-09-14 21:41:23 +00:00
NXCDBMenu.create_workspace("test", p_loader, protocols)
2023-03-22 21:16:10 +00:00
protocol_db_path = p_loader.get_protocols()[proto]["dbpath"]
protocol_db_object = getattr(p_loader.load_protocol(protocol_db_path), "database")
2023-03-26 01:29:18 +00:00
database_obj = protocol_db_object(db_engine)
database_obj.reflect_tables()
2023-03-26 01:29:18 +00:00
yield database_obj
database_obj.shutdown_db()
2023-03-22 21:16:10 +00:00
delete_workspace("test")
@pytest.fixture(scope="function")
def db(db_setup):
yield db_setup
db_setup.clear_database()
@pytest.fixture(scope="session")
2023-03-22 21:16:10 +00:00
def sess(db_engine):
2023-05-02 15:17:59 +00:00
session_factory = sessionmaker(bind=db_engine, expire_on_commit=True)
Session = scoped_session(session_factory)
2023-03-22 21:16:10 +00:00
sess = Session()
yield sess
sess.close()
def test_add_host(db):
2023-03-26 01:29:18 +00:00
db.add_host(
2023-03-22 21:16:10 +00:00
"127.0.0.1",
"localhost",
"TEST.DEV",
"Windows Testing 2023",
False,
True,
True,
True,
False,
2023-05-02 15:17:59 +00:00
False,
2023-03-22 21:16:10 +00:00
)
inserted_host = db.get_hosts()
assert len(inserted_host) == 1
host = inserted_host[0]
assert host.id == 1
assert host.ip == "127.0.0.1"
assert host.hostname == "localhost"
assert host.os == "Windows Testing 2023"
assert host.smbv1 is False
assert host.signing is True
assert host.spooler is True
assert host.zerologon is True
assert host.petitpotam is False
assert host.dc is False
2023-03-22 21:16:10 +00:00
def test_update_host(db, sess):
host = {
"ip": "127.0.0.1",
"hostname": "localhost",
"domain": "TEST.DEV",
"os": "Windows Testing 2023",
"smbv1": True,
"signing": False,
2023-03-22 21:16:10 +00:00
"spooler": True,
"zerologon": False,
"petitpotam": False,
2023-05-02 15:17:59 +00:00
"dc": False,
2023-03-22 21:16:10 +00:00
}
iq = Insert(db.HostsTable)
sess.execute(iq, [host])
db.add_host(
"127.0.0.1",
"localhost",
"TEST.DEV",
"Windows Testing 2023 Updated",
False,
True,
False,
False,
False,
2023-05-02 15:17:59 +00:00
False,
)
inserted_host = db.get_hosts()
2023-03-26 01:29:18 +00:00
assert len(inserted_host) == 1
host = inserted_host[0]
assert host.id == 1
assert host.ip == "127.0.0.1"
assert host.hostname == "localhost"
assert host.os == "Windows Testing 2023 Updated"
assert host.smbv1 is False
assert host.signing is True
assert host.spooler is False
assert host.zerologon is False
assert host.petitpotam is False
assert host.dc is False
2023-03-22 21:16:10 +00:00
def test_add_credential():
pass
2023-03-22 21:16:10 +00:00
def test_update_credential():
pass
2023-03-22 21:16:10 +00:00
def test_remove_credential():
pass
2023-03-22 21:16:10 +00:00
def test_add_admin_user():
pass
2023-03-22 21:16:10 +00:00
def test_get_admin_relations():
pass
2023-03-22 21:16:10 +00:00
def test_remove_admin_relation():
pass
2023-03-22 21:16:10 +00:00
def test_is_credential_valid():
pass
2023-03-22 21:16:10 +00:00
def test_get_credentials():
pass
2023-03-22 21:16:10 +00:00
def test_get_credential():
pass
2023-03-22 21:16:10 +00:00
def test_is_credential_local():
pass
2023-03-22 21:16:10 +00:00
def test_is_host_valid():
pass
2023-03-22 21:16:10 +00:00
def test_get_hosts():
pass
2023-03-22 21:16:10 +00:00
def test_is_group_valid():
pass
2023-03-22 21:16:10 +00:00
def test_add_group():
pass
2023-03-22 21:16:10 +00:00
def test_get_groups():
pass
2023-03-22 21:16:10 +00:00
def test_get_group_relations():
pass
2023-03-22 21:16:10 +00:00
def test_remove_group_relations():
pass
2023-03-22 21:16:10 +00:00
def test_is_user_valid():
pass
2023-03-22 21:16:10 +00:00
def test_get_users():
pass
2023-03-22 21:16:10 +00:00
def test_get_user():
pass
2023-03-22 21:16:10 +00:00
def test_get_domain_controllers():
pass
2023-03-22 21:16:10 +00:00
def test_is_share_valid():
pass
2023-03-22 21:16:10 +00:00
def test_add_share():
pass
2023-03-22 21:16:10 +00:00
def test_get_shares():
pass
2023-03-22 21:16:10 +00:00
def test_get_shares_by_access():
pass
2023-03-22 21:16:10 +00:00
def test_get_users_with_share_access():
pass
2023-03-22 21:16:10 +00:00
def test_add_domain_backupkey():
pass
2023-03-22 21:16:10 +00:00
def test_get_domain_backupkey():
pass
2023-03-22 21:16:10 +00:00
def test_is_dpapi_secret_valid():
pass
2023-03-22 21:16:10 +00:00
def test_add_dpapi_secrets():
pass
2023-03-22 21:16:10 +00:00
def test_get_dpapi_secrets():
pass
2023-03-22 21:16:10 +00:00
def test_add_loggedin_relation():
pass
2023-03-22 21:16:10 +00:00
def test_get_loggedin_relations():
pass
2023-03-22 21:16:10 +00:00
def test_remove_loggedin_relations():
pass