Sunday, March 10, 2019

nevermind the shit page configuration!!!!! LOKIBOT library code

Skip to content
Tree: 1e8a7a98bb 
Find fileCopy path
1 contributor
223 lines (181 sloc)  7.83 KB
# Date: 07/02/2018
# Author: Pure-L0G1C
# Description: DBMS
import bcrypt
import sqlite3
from . import const
from time import time
from os import urandom
from hashlib import sha256
from base64 import b64encode
from datetime import datetime
class Database(object):
def __init__(self):
self.db_path = const.DATABASE
self.create_tables()
self.create_default_account()
def create_tables(self):
self.db_create('''
CREATE TABLE IF NOT EXISTS
Account(
user_id TEXT PRIMARY KEY,
username TEXT,
password TEXT
);
''')
self.db_create('''
CREATE TABLE IF NOT EXISTS
Status(
last_online INTEGER,
date_created INTEGER,
stat_id TEXT NOT NULL,
FOREIGN KEY(stat_id) REFERENCES Account(user_id)
);
''')
self.db_create('''
CREATE TABLE IF NOT EXISTS
Attempt(
last_attempt INTEGER,
attempts_made INTEGER,
ampt_id TEXT NOT NULL,
FOREIGN KEY(ampt_id) REFERENCES Account(user_id)
);
''')
self.db_create('''
CREATE TABLE IF NOT EXISTS
Lock(
time_locked INTEGER DEFAULT 0,
lock_id TEXT NOT NULL,
FOREIGN KEY(lock_id) REFERENCES Account(user_id)
);
''')
def add_account(self, username, password):
username = username.lower()
user_id = self.gen_user_id(username, password)
hashed_password = self.hash_password(password)
self.db_update('''
INSERT INTO Account(user_id, username, password)
VALUES(?, ?, ?);
''', [user_id, username, hashed_password])
self.db_update('''
INSERT INTO Status(last_online, date_created, stat_id)
VALUES(?, ?, ?);
''', [time(), time(), user_id])
self.db_update('''
INSERT INTO Attempt(last_attempt, attempts_made, ampt_id)
VALUES(?, ?, ?);
''', [time(), 0, user_id])
self.db_update('''
INSERT INTO Lock(lock_id)
VALUES(?);
''', [user_id])
def account_exists(self, username):
data = self.db_query('SELECT * FROM Account WHERE username=?;', [username], False)
return True if len(data) else False
def compare_passwords(self, user_id, password):
hashed_password = self.db_query('SELECT password FROM Account WHERE user_id=?;', [user_id])
return True if bcrypt.hashpw(password.encode('utf-8'), hashed_password) == hashed_password else False
def check_password(self, username, password):
hashed_password = self.db_query('SELECT password FROM Account WHERE username=?;', [username])
return True if bcrypt.hashpw(password.encode('utf-8'), hashed_password) == hashed_password else False
def authenticate(self, username, password):
username = username.lower()
if self.account_exists(username):
user_id = self.get_user_id(username)
if not self.is_locked(user_id):
if self.check_password(username, password):
return self.get_user_id(username)
else:
self.failed_attempt(user_id)
return None
def is_empty(self):
data = self.db_query('SELECT * FROM Account;', [], False)
return False if len(data) else True
def create_default_account(self):
if self.is_empty():
self.add_account('loki', 'ikol')
# -------- Attempts -------- #
def lock_account(self, user_id):
self.db_update('UPDATE Lock SET time_locked=? WHERE lock_id=?;', [time(), user_id])
def failed_attempt(self, user_id):
current_value = self.failed_attempts_counts(user_id)
if current_value >= const.MAX_FAILED_ATTEMPTS-1:
if not self.is_locked(user_id):
self.lock_account(user_id)
else:
self.db_update('UPDATE Attempt SET attempts_made=? WHERE ampt_id=?;', [current_value + 1, user_id])
def failed_attempts_counts(self, user_id):
return self.db_query('SELECT attempts_made FROM Attempt WHERE ampt_id=?;', [user_id])
def is_locked(self, user_id):
time_locked = self.locked(user_id)
if time_locked:
if (time() - time_locked) >= const.LOCK_TIME:
self.remove_locked_account(user_id)
return False
else:
return True
else:
return False
def locked(self, user_id):
return self.db_query('''
SELECT time_locked
FROM Lock
INNER JOIN Account ON account.user_id = Lock.lock_id
WHERE Lock.lock_id=?;
''', [user_id])
def remove_locked_account(self, user_id):
self.db_update('UPDATE Attempt SET attempts_made=? WHERE ampt_id=?;', [0, user_id])
# -------- Database Wrappers -------- #
def db_query(self, cmd, args, fetchone=True):
database = sqlite3.connect(self.db_path)
sql = database.cursor().execute(cmd, args)
data = sql.fetchone()[0] if fetchone else sql.fetchall()
database.close()
return data
def db_update(self, cmd, args):
database = sqlite3.connect(self.db_path)
database.cursor().execute(cmd, args)
database.commit()
database.close()
def db_create(self, cmd):
database = sqlite3.connect(self.db_path)
database.cursor().execute(cmd)
database.commit()
database.close()
# -------- Update -------- #
def update_password(self, user_id, password):
hashed_password = self.hash_password(password)
self.db_update('UPDATE Account SET password=? WHERE user_id=?;', [hashed_password, user_id])
def update_username(self, user_id, username):
self.db_update('UPDATE Account SET username=? WHERE user_id=?;', [username.lower(), user_id])
# -------- Misc -------- #
def hash_password(self, password):
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
def gen_user_id(self, username, password):
_username = username.encode('utf-8') + urandom(64 * 1024)
_password = password.encode('utf-8') + urandom(64 * 1024)
_username_password = b64encode(_username + _password + urandom(64 * 64))
secure_hash = sha256(_username_password).digest().hex()
return secure_hash
def get_date_created(self, user_id):
return self.db_query('SELECT date_created FROM Status WHERE stat_id=?;', [user_id])
def get_user_id(self, username):
return self.db_query('SELECT user_id FROM Account WHERE username=?;', [username])
def get_last_active(self, user_id):
epoch_time = self.db_query('SELECT last_online FROM Status WHERE stat_id=?;', [user_id])
self.db_update('UPDATE Status SET last_online=? WHERE stat_id=?;', [time(), user_id])
return datetime.fromtimestamp(epoch_time).strftime('%b %d, %Y at %I:%M %p')
def get_account_status(self, user_id, username):
default_username = 'loki'
default_password = 'ikol'
username = username.lower()
is_same_password = self.compare_passwords(user_id, default_password)
if all([username == default_username, is_same_password]):
status = '** Please consider changing your username and password **'
elif username == default_username:
status = '** Please consider changing your username **'
elif is_same_password:
status = '** Please consider changing your passsword **'
else:
status = None
return status

Friday, March 8, 2019

...back to war, another weekend, firday, 08.36 am! ...about sha256 reverse, if we go deeper..we go to kernel build-in...and bottom point...force the kernel to disable, and get 0 as parameter..like "The following build options are enabled by default. # Use either --without in your rpmbuild command or force values # to 0 in here to disable them." here's how debugging

All should default to 1 (enabled) and be flipped to 0 (disabled)
# by later arch-specific checks.

# The following build options are enabled by default.
# Use either --without  in your rpmbuild command or force values
# to 0 in here to disable them.
#
# standard kernel
%define with_up        %{?_without_up:        0} %{?!_without_up:        1}
# kernel-smp (only valid for ppc 32-bit)
%define with_smp       %{?_without_smp:       0} %{?!_without_smp:       1}
# kernel-kdump
%define with_kdump     %{?_without_kdump:     0} %{?!_without_kdump:     1}
# kernel-debug
%define with_debug     %{?_without_debug:     0} %{?!_without_debug:     1}
# kernel-doc
%define with_doc       %{?_without_doc:       0} %{?!_without_doc:       1}
# kernel-headers
%define with_headers   %{?_without_headers:   0} %{?!_without_headers:   1}
# kernel-firmware
%define with_firmware  %{?_with_firmware:     1} %{?!_with_firmware:     0}
# perf noarch subpkg
%define with_perf      %{?_without_perf:      0} %{?!_without_perf:      1}
# kernel-debuginfo
%define with_debuginfo %{?_without_debuginfo: 0} %{?!_without_debuginfo: 1}
# kernel-bootwrapper (for creating zImages from kernel + initrd)
%define with_bootwrapper %{?_without_bootwrapper: 0} %{?!_without_bootwrapper: 1}
# Want to build a the vsdo directories installed
%define with_vdso_install %{?_without_vdso_install: 0} %{?!_without_vdso_install: 1}
# Use dracut instead of mkinitrd for initrd image generation
%define with_dracut       %{?_without_dracut:       0} %{?!_without_dracut:       1}
# kernel-abi-whitelists
%define with_kernel_abi_whitelists %{?_with_kernel_abi_whitelists: 0} %{?!_with_kernel_abi_whitelists: 1}

# Build the kernel-doc package, but don't fail the build if it botches.
# Here "true" means "continue" and "false" means "fail the build".
%if 0%{?released_kernel}
%define doc_build_fail false
%else
%define doc_build_fail true
%endif

# Control whether we perform a compat. check against published ABI.
%define with_kabichk   %{?_with_kabichk:      1} %{?!_with_kabichk:      0}
# Control whether we perform a compat. check against published ABI.
%define with_fips      %{?_without_fips:      0} %{?!_without_fips:      1}

# Additional options for user-friendly one-off kernel building:
#
# Only build the base kernel (--with baseonly):
%define with_baseonly  %{?_with_baseonly:     1} %{?!_with_baseonly:     0}
# Only build the smp kernel (--with smponly):
%define with_smponly   %{?_with_smponly:      1} %{?!_with_smponly:      0}
# Only build the debug kernel (--with dbgonly):
%define with_dbgonly   %{?_with_dbgonly:      1} %{?!_with_dbgonly:      0}

# should we do C=1 builds with sparse
%define with_sparse    %{?_with_sparse:       1} %{?!_with_sparse:       0}

# Cross compile requested?
%define with_cross    %{?_with_cross:         1} %{?!_with_cross:        0}

# Set debugbuildsenabled to 1 for production (build separate debug kernels)
#  and 0 for rawhide (all kernels are debug kernels).
# See also 'make debug' and 'make release'.
%define debugbuildsenabled 1

# pkg_release is what we'll fill in for the rpm Release: field
%if 0%{?released_kernel}

%if 0%{?stable_rc}
%define stable_rctag .rc%{stable_rc}
%endif
%define pkg_release %{distro_build}%{?buildid}

%else

# non-released_kernel
%if 0%{?rcrev}
%define rctag .rc%rcrev
%else
%define rctag .rc0
%endif
%if 0%{?gitrev}
%define gittag .git%gitrev
%else
%define gittag .git0
%endif
%define pkg_release 0.%{distro_build}%{?rctag}%{?gittag}%{?dist}%{?buildid}

%endif

# The kernel tarball/base version
%define kversion 2.6.32-573.8.1.el6 

%define make_target bzImage

%define hdrarch %_target_cpu
%define asmarch %_target_cpu

%if 0%{!?nopatches:1}
%define nopatches 0
%endif

%if %{nopatches}
%define with_bootwrapper 0
%define variant -vanilla
%else
%define variant_fedora -fedora
%endif

%define using_upstream_branch 0
%if 0%{?upstream_branch:1}
%define stable_update 0
%define using_upstream_branch 1
%define variant -%{upstream_branch}%{?variant_fedora}
%define pkg_release 0.%{distro_build}%{upstream_branch_tag}%{?dist}%{?buildid}
%endif

%define pkg_release %{distro_build}%{?buildid}
%define rhel_release %{rhel_build}%{?dist}
%define KVERREL %{rpmversion}-%{pkg_release}

%if !%{debugbuildsenabled}
%define with_debug 1
%endif

%if !%{with_debuginfo}
%define _enable_debug_packages 0
%endif
%define debuginfodir /usr/lib/debug

%define with_pae 0

# if requested, only build base kernel
%if %{with_baseonly}
%define with_smp 0
%define with_kdump 0
%define with_debug 0
%endif

# if requested, only build smp kernel
%if %{with_smponly}
%define with_up 0
%define with_kdump 0
%define with_debug 0
%endif

# if requested, only build debug kernel
%if %{with_dbgonly}
%if %{debugbuildsenabled}
%define with_up 0
%endif
%define with_smp 0
%define with_pae 0
%define with_xen 0
%define with_kdump 0
%define with_perf 0
%endif

%define all_x86 i386 i686

%if %{with_vdso_install}
# These arches install vdso/ directories.
%define vdso_arches %{all_x86} x86_64 ppc ppc64 s390 s390x
%endif

# Overrides for generic default options

# only ppc and alphav56 need separate smp kernels
%ifnarch ppc alphaev56
%define with_smp 0
%endif

%ifarch s390x
%define with_kdump 1
%else
%define with_kdump 0
%endif

# don't do debug builds on anything but i686 and x86_64
%ifnarch i686 x86_64 s390x ppc64
%define with_debug 0
%endif

# only package docs noarch
%ifnarch noarch
%define with_doc 0
%define with_kernel_abi_whitelists 0
%endif

See it at : https://download.openvz.org/kernel/branches/rhel6-2.6.32/042stab113.17/kernel.spec