From c6c494c8090d3e76eb1a41a35c42810a15764553 Mon Sep 17 00:00:00 2001 From: Aidaho12 Date: Fri, 1 Jun 2018 18:27:58 +0600 Subject: [PATCH] v2.5.6 Need more security!! bugs --- app/apachelogs.py | 4 +- app/config.py | 4 +- app/configshow.py | 4 +- app/configver.py | 4 +- app/create_db.py | 73 +++++++++++++++++++++++++---- app/delver.py | 4 +- app/diff.py | 4 +- app/edit.py | 4 +- app/funct.py | 5 ++ app/ha.py | 4 +- app/haproxy-webintarface.config | 2 + app/haproxy-wi.db.sql | 4 ++ app/ihap.py | 4 +- app/keepalivedconfig.py | 4 +- app/login.py | 7 ++- app/logs.py | 4 +- app/map.py | 4 +- app/options.py | 9 +++- app/overview.py | 6 ++- app/sql.py | 81 ++++++++++++++++++++++++++++++++- app/templates/base.html | 3 +- app/viewlogs.py | 4 +- app/viewsttats.py | 4 +- inc/script.js | 60 ++++++++++++++++-------- inc/users.js | 11 +++-- 25 files changed, 262 insertions(+), 55 deletions(-) diff --git a/app/apachelogs.py b/app/apachelogs.py index d942f99b..c9128135 100644 --- a/app/apachelogs.py +++ b/app/apachelogs.py @@ -28,6 +28,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = [('haproxy-wi.error.log','error.log'), ('haproxy-wi.access.log','access.log')] + token = sql.get_token(user_id.value) except: pass @@ -41,7 +42,8 @@ output_from_parsed_template = template.render(h2 = 1, selects = servers, serv = form.getvalue('serv'), rows = rows, - grep = grep) + grep = grep, + token = token) print(output_from_parsed_template) diff --git a/app/config.py b/app/config.py index f41ea14c..b9fca4fe 100644 --- a/app/config.py +++ b/app/config.py @@ -26,6 +26,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -92,5 +93,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Edit Runnig HAPro selects = servers, stderr = stderr, error = error, - note = 1) + note = 1, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/configshow.py b/app/configshow.py index aa34122c..c81c65e4 100644 --- a/app/configshow.py +++ b/app/configshow.py @@ -16,6 +16,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -26,5 +27,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Show Runnig confi select_id = "serv", serv = serv, selects = servers, - note = 0) + note = 0, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/configver.py b/app/configver.py index 685f639d..9cecf7c9 100644 --- a/app/configver.py +++ b/app/configver.py @@ -26,6 +26,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -75,5 +76,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Old Versions HAPr open = form.getvalue('open'), onclick = "showUploadConfig()", error = error, - note = 1) + note = 1, + token = token) print(output_from_parsed_template) diff --git a/app/create_db.py b/app/create_db.py index 47ed2dbf..1f7433fd 100644 --- a/app/create_db.py +++ b/app/create_db.py @@ -127,6 +127,11 @@ def create_table(): `username` VARCHAR ( 64 ) NOT NULL, `password` VARCHAR ( 64 ) NOT NULL ); + CREATE TABLE IF NOT EXISTS `token` ( + `user_id` INTEGER, + `token` varchar(64), + `exp` DATETIME default '0000-00-00 00:00:00' + ); insert into cred('enable','username','password') values ('1', 'root','password'); """ try: @@ -248,7 +253,7 @@ def update_db_v_2_5_3(**kwargs): print(kwargs.get('silent')) if kwargs.get('silent') != 1: if e.args[0] == 'duplicate column name: enable': - print('Already updated. No run more. Thx =^.^=') + print('Updating... go to version 2.5.6') else: print("An error occurred:", e) return False @@ -263,6 +268,58 @@ def update_db_v_2_5_3(**kwargs): return True cur.close() con.close() + +def update_db_v_2_5_6(**kwargs): + con, cur = get_cur() + if mysql_enable == '1': + sql = """ + ALTER TABLE `uuid` ADD COLUMN `exp` timestamp default '0000-00-00 00:00:00'; + """ + else: + sql = """ + ALTER TABLE `uuid` ADD COLUMN `exp` DATETIME default '0000-00-00 00:00:00'; + """ + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + if kwargs.get('silent') != 1: + if e.args[0] == 'duplicate column name: exp' or e == "1060 (42S21): Duplicate column name 'exp' ": + print('Updating... go to version 2.5.6.1') + else: + print("An error occurred:", e) + return False + else: + print("DB was update to 2.5.6.1
") + return True + cur.close() + con.close() + +def update_db_v_2_5_6_1(**kwargs): + con, cur = get_cur() + if mysql_enable == '1': + sql = """ + CREATE TABLE IF NOT EXISTS `token` (`user_id` INTEGER, `token` varchar(64), `exp` timestamp default '0000-00-00 00:00:00'); + """ + else: + sql = """ + CREATE TABLE IF NOT EXISTS `token` (`user_id` INTEGER, `token` varchar(64), `exp` DATETIME default '0000-00-00 00:00:00'); + """ + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + if kwargs.get('silent') != 1: + if e.args[0] == 'duplicate column name: token' or e == "1060 (42S21): Duplicate column name 'token' ": + print('Already updated. No run more. Thx =^.^=') + else: + print("An error occurred:", e) + return False + else: + print("DB was update to 2.5.6.1
") + return True + cur.close() + con.close() def update_all(): update_db_v_2_0_1() @@ -270,6 +327,8 @@ def update_all(): update_db_v_2_0_5() update_db_v_2_4() update_db_v_2_5_3() + update_db_v_2_5_6() + update_db_v_2_5_6_1() def update_all_silent(): update_db_v_2_0_1(silent=1) @@ -277,12 +336,6 @@ def update_all_silent(): update_db_v_2_0_5(silent=1) update_db_v_2_4(silent=1) update_db_v_2_5_3(silent=1) - -#if check_db(): -# create_table() -#else: -# print('DB already exists, try update') -#update_all() -#if update_db_v_2_0_1(): -# print('DB was property update to version 2.0.1.') -#update_db_v_2_0_1_1() \ No newline at end of file + update_db_v_2_5_6(silent=1) + update_db_v_2_5_6_1(silent=1) + \ No newline at end of file diff --git a/app/delver.py b/app/delver.py index 38f0107e..f77afeb3 100644 --- a/app/delver.py +++ b/app/delver.py @@ -23,6 +23,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -69,5 +70,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Delete old versio stderr = stderr, open = form.getvalue('open'), Select = form.getvalue('del'), - file = file) + file = file, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/diff.py b/app/diff.py index 263d0a8c..c49a09c1 100644 --- a/app/diff.py +++ b/app/diff.py @@ -16,6 +16,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -24,5 +25,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Compare configs", user = user, onclick = "showCompareConfigs()", select_id = "serv", - selects = servers) + selects = servers, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/edit.py b/app/edit.py index 90dcaad2..31d1a452 100644 --- a/app/edit.py +++ b/app/edit.py @@ -16,6 +16,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit(virt=1) + token = sql.get_token(user_id.value) except: pass @@ -25,5 +26,6 @@ output_from_parsed_template = template.render(h2 = 1, user = user, onclick = "showRuntime()", select_id = "serv", - selects = servers) + selects = servers, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/funct.py b/app/funct.py index ac5332ff..59202028 100644 --- a/app/funct.py +++ b/app/funct.py @@ -17,12 +17,14 @@ def get_config_var(sec, var): config = ConfigParser(interpolation=ExtendedInterpolation()) config.read(path_config) except: + print('Content-type: text/html\n') print('
Check the config file, whether it exists and the path. Must be: app/haproxy-webintarface.config
') try: var = config.get(sec, var) return var except: + print('Content-type: text/html\n') print('
Check the config file. Presence section %s and parameter %s
' % (sec, var)) def get_data(type): @@ -72,7 +74,10 @@ def check_login(**kwargs): user_uuid = cookie.get('uuid') ref = os.environ.get("SCRIPT_NAME") + sql.delete_old_uuid() + if user_uuid is not None: + sql.update_last_act_user(user_uuid.value) if sql.get_user_name_by_uuid(user_uuid.value) is None: print('' % ref) else: diff --git a/app/ha.py b/app/ha.py index 2f839eb9..052b059e 100644 --- a/app/ha.py +++ b/app/ha.py @@ -19,6 +19,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -26,5 +27,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Configure HA", role = sql.get_user_role_by_uuid(user_id.value), user = user, serv = serv, - selects = servers) + selects = servers, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/haproxy-webintarface.config b/app/haproxy-webintarface.config index f1726a42..e5ab6cd4 100644 --- a/app/haproxy-webintarface.config +++ b/app/haproxy-webintarface.config @@ -8,6 +8,8 @@ time_zone = UTC proxy = #Time to live users sessions. In days session_ttl = 5 +#Time to live users tokens. In days +token_ttl = 5 [configs] #Server for save configs from HAproxy servers diff --git a/app/haproxy-wi.db.sql b/app/haproxy-wi.db.sql index 2a1598e6..fdf74ba8 100644 --- a/app/haproxy-wi.db.sql +++ b/app/haproxy-wi.db.sql @@ -10,3 +10,7 @@ INSERT INTO `role` (name, description) VALUES ('guest','Read only access'); CREATE TABLE IF NOT EXISTS `groups` (`id` INTEGER NOT NULL AUTO_INCREMENT,`name` VARCHAR ( 80 ) UNIQUE,`description` VARCHAR ( 255 ),PRIMARY KEY(`id`)); INSERT INTO `groups` (name, description) VALUES ('All','All servers enter in this group'); CREATE TABLE IF NOT EXISTS `servers` (`id` INTEGER NOT NULL AUTO_INCREMENT,`hostname` VARCHAR ( 64 ) UNIQUE,`ip` VARCHAR ( 64 ) UNIQUE,`groups` VARCHAR ( 64 ), PRIMARY KEY(`id`)); +CREATE TABLE IF NOT EXISTS `uuid` (`user_id` INTEGER NOT NULL, `uuid` varchar ( 64 ) ); +CREATE TABLE IF NOT EXISTS `cred` (`enable` INTEGER NOT NULL DEFAULT 1, `username` VARCHAR ( 64 ) NOT NULL, `password` VARCHAR ( 64 ) NOT NULL ); +insert into cred('enable','username','password') values ('1', 'root','password'); +CREATE TABLE IF NOT EXISTS `token` (`user_id` INTEGER, `token` varchar(64), `exp` timestamp default '0000-00-00 00:00:00'); \ No newline at end of file diff --git a/app/ihap.py b/app/ihap.py index 52a55064..39f3c7b1 100644 --- a/app/ihap.py +++ b/app/ihap.py @@ -16,6 +16,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -23,6 +24,7 @@ output_from_parsed_template = template.render(h2 = 1, title = "Installation HAPr role = sql.get_user_role_by_uuid(user_id.value), user = user, select_id = "haproxyaddserv", - selects = servers) + selects = servers, + token = token) print(output_from_parsed_template) diff --git a/app/keepalivedconfig.py b/app/keepalivedconfig.py index 81ab7c92..5669761f 100644 --- a/app/keepalivedconfig.py +++ b/app/keepalivedconfig.py @@ -28,6 +28,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.is_master("123", master_slave=1) + token = sql.get_token(user_id.value) except: pass @@ -85,5 +86,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Edit Runnig Keepa selects = servers, stderr = stderr, error = error, - keepalived = 1) + keepalived = 1, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/login.py b/app/login.py index 8e73aae6..75010481 100644 --- a/app/login.py +++ b/app/login.py @@ -64,6 +64,7 @@ if login is not None and password is not None: session_ttl = config.getint('main', 'session_ttl') expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) user_uuid = str(uuid.uuid4()) + user_token = str(uuid.uuid4()) for users in USERS: if login in users[1] and password == users[3]: @@ -73,7 +74,7 @@ if login is not None and password is not None: c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") print(c) sql.write_user_uuid(login, user_uuid) - + sql.write_user_token(login, user_token) print("Content-type: text/html\n") print('ok') sys.exit() @@ -88,7 +89,9 @@ if login is None: if create_db.create_table(): create_db.update_all() db_create = '
DB was created

Now you can login, default: admin/admin
' - + +create_db.update_all_silent() + output_from_parsed_template = template.render(h2 = 1, title = "Login page. Enter please", role = role, user = user, diff --git a/app/logs.py b/app/logs.py index 9c5481e2..8b0cad56 100644 --- a/app/logs.py +++ b/app/logs.py @@ -27,6 +27,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -40,7 +41,8 @@ output_from_parsed_template = template.render(h2 = 1, selects = servers, serv = form.getvalue('serv'), rows = rows, - grep = grep) + grep = grep, + token = token) print(output_from_parsed_template) diff --git a/app/map.py b/app/map.py index 290808cc..edcc8d28 100644 --- a/app/map.py +++ b/app/map.py @@ -16,6 +16,7 @@ try: user_id = cookie.get('uuid') user = sql.get_user_name_by_uuid(user_id.value) servers = sql.get_dick_permit() + token = sql.get_token(user_id.value) except: pass @@ -24,5 +25,6 @@ output_from_parsed_template = template.render(h2 = 1, title = "Show Map", user = user, onclick = "showMap()", select_id = "serv", - selects = servers) + selects = servers, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/options.py b/app/options.py index 3765c081..9916eb90 100644 --- a/app/options.py +++ b/app/options.py @@ -14,8 +14,13 @@ req = form.getvalue('req') serv = form.getvalue('serv') act = form.getvalue('act') backend = form.getvalue('backend') + print('Content-type: text/html\n') - + +if form.getvalue('token') is None: + print("What the fuck?! U r hacker Oo?!") + sys.exit() + if form.getvalue('getcert') is not None and serv is not None: commands = [ "ls -1t /etc/ssl/certs/ |grep pem" ] try: @@ -34,7 +39,7 @@ if form.getvalue('ssh_cert'): else: print('
Ssh key was save into: %s
' % ssh_keys) try: - funct.logging("local", "users.py#ssh upload new ssl cert %s" % ssh_keys) + funct.logging("local", "users.py#ssh upload new ssh cert %s" % ssh_keys) except: pass diff --git a/app/overview.py b/app/overview.py index 658283f5..321a8130 100644 --- a/app/overview.py +++ b/app/overview.py @@ -7,8 +7,8 @@ env = Environment(loader=FileSystemLoader('templates/')) template = env.get_template('ovw.html') print('Content-type: text/html\n') -funct.check_login() create_db.update_all_silent() +funct.check_login() try: cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) @@ -16,6 +16,7 @@ try: user = sql.get_user_name_by_uuid(user_id.value) users = sql.select_users() groups = sql.select_groups() + token = sql.get_token(user_id.value) except: pass @@ -25,5 +26,6 @@ output_from_parsed_template = template.render(h2 = 1, role = sql.get_user_role_by_uuid(user_id.value), user = user, users = users, - groups = groups) + groups = groups, + token = token) print(output_from_parsed_template) \ No newline at end of file diff --git a/app/sql.py b/app/sql.py index 143099f1..082295b7 100644 --- a/app/sql.py +++ b/app/sql.py @@ -271,22 +271,64 @@ def get_enable_checkbox(id, **kwargs): def write_user_uuid(login, user_uuid): con, cur = create_db.get_cur() + session_ttl = funct.get_config_var('main', 'session_ttl') + session_ttl = int(session_ttl) sql = """ select id from user where username = '%s' """ % login try: cur.execute(sql) except sqltool.Error as e: print('An error occurred: ' + e.args[0] + ' X') for id in cur.fetchall(): - sql = """ insert into uuid (user_id, uuid) values('%s', '%s') """ % (id[0], user_uuid) + if mysql_enable == '1': + sql = """ insert into uuid (user_id, uuid, exp) values('%s', '%s', now()+ INTERVAL %s day) """ % (id[0], user_uuid, session_ttl) + else: + sql = """ insert into uuid (user_id, uuid, exp) values('%s', '%s', datetime('now', '+%s days')) """ % (id[0], user_uuid, session_ttl) try: cur.execute(sql) con.commit() except sqltool.Error as e: - print('An error occurred: ' + e + ' X') + print('An error occurred: ' + e.args[0] + ' X') con.rollback() cur.close() con.close() +def write_user_token(login, user_token): + con, cur = create_db.get_cur() + token_ttl = funct.get_config_var('main', 'token_ttl') + sql = """ select id from user where username = '%s' """ % login + try: + cur.execute(sql) + except sqltool.Error as e: + print('An error occurred: ' + e.args[0] + ' X') + for id in cur.fetchall(): + if mysql_enable == '1': + sql = """ insert into token (user_id, token, exp) values('%s', '%s', now()+ INTERVAL %s day) """ % (id[0], user_token, token_ttl) + else: + sql = """ insert into token (user_id, token, exp) values('%s', '%s', datetime('now', '+%s days')) """ % (id[0], user_token, token_ttl) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + print('An error occurred: ' + e.args[0] + ' X') + con.rollback() + cur.close() + con.close() + +def get_token(uuid): + con, cur = create_db.get_cur() + + sql = """ select token.token from token left join uuid as uuid on uuid.user_id = token.user_id where uuid.uuid = '%s' """ % uuid + try: + cur.execute(sql) + except sqltool.Error as e: + print('An error occurred: ' + e.args[0] + ' X') + else: + for token in cur.fetchall(): + return token[0] + + cur.close() + con.close() + def delete_uuid(uuid): con, cur = create_db.get_cur() sql = """ delete from uuid where uuid = '%s' """ % uuid @@ -298,6 +340,41 @@ def delete_uuid(uuid): cur.close() con.close() +def delete_old_uuid(): + con, cur = create_db.get_cur() + if mysql_enable == '1': + sql = """ delete from uuid where exp < now() or exp is NULL """ + sql1 = """ delete from token where exp < now() or exp is NULL """ + else: + sql = """ delete from uuid where exp < datetime('now') or exp is NULL""" + sql1 = """ delete from token where exp < datetime('now') or exp is NULL""" + try: + cur.execute(sql) + cur.execute(sql1) + con.commit() + except sqltool.Error as e: + print('An error occurred: ' + e.args[0] + ' X') + con.rollback() + cur.close() + con.close() + +def update_last_act_user(uuid): + con, cur = create_db.get_cur() + session_ttl = funct.get_config_var('main', 'session_ttl') + + if mysql_enable == '1': + sql = """ update uuid set exp = now()+ INTERVAL %s day where uuid = '%s' """ % (session_ttl, uuid) + else: + sql = """ update uuid set exp = datetime('now', '+%s days') where uuid = '%s' """ % (session_ttl, uuid) + try: + cur.execute(sql) + con.commit() + except sqltool.Error as e: + print('
An error occurred: ' + e.args[0] + ' X
') + con.rollback() + cur.close() + con.close() + def get_user_name_by_uuid(uuid): con, cur = create_db.get_cur() sql = """ select user.username from user left join uuid as uuid on user.id = uuid.user_id where uuid.uuid = '%s' """ % uuid diff --git a/app/templates/base.html b/app/templates/base.html index 61357321..c214349f 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -22,6 +22,7 @@ +