def __get_results_json(): """ get host's logs through the API in JSON type Returns: an array with JSON events """ session = create_connection(__language()) __api_key_check(app, flask_request, __language()) try: _id = int(__get_value(flask_request, "id")) scan_id_temp = session.query(Report).filter(Report.id == _id).all() except Exception as _: _id = "" if(scan_id_temp): result_id = session.query(Report).join(HostsLog, Report.scan_id == HostsLog.scan_id).filter(Report.scan_id == scan_id_temp[0].scan_id).all() else: result_id = [] json_object = {} if(result_id): scan_id = result_id[0].scan_id data = __logs_by_scan_id(scan_id, __language()) json_object = json.dumps(data) date_from_db = scan_id_temp[0].date date_format = datetime.strptime(date_from_db, "%Y-%m-%d %H:%M:%S") date_format = str(date_format).replace("-", "_").replace(":", "_").replace(" ", "_") filename = "report-" + date_format +"".join(random.choice(string.ascii_lowercase) for x in range(10)) return Response(json_object, mimetype='application/json', headers={'Content-Disposition':'attachment;filename='+filename+'.json'})
def __get_results_csv(): """ get host's logs through the API in JSON type Returns: an array with JSON events """ session = create_connection(__language()) __api_key_check(app, flask_request, __language()) try: _id = int(__get_value(flask_request, "id")) scan_id_temp = session.query(Report).filter(Report.id == _id).all() except Exception as _: _id = "" if (scan_id_temp): result_id = session.query(Report).join( HostsLog, Report.scan_id == HostsLog.scan_id).filter( Report.scan_id == scan_id_temp[0].scan_id).all() else: result_id = [] date_from_db = scan_id_temp[0].date date_format = datetime.strptime(date_from_db, "%Y-%m-%d %H:%M:%S") date_format = str(date_format).replace("-", "_").replace(":", "_").replace(" ", "_") filename = "report-" + date_format + "".join( random.choice(string.ascii_lowercase) for x in range(10)) _reader = '' if (result_id): scan_id = result_id[0].scan_id data = __logs_by_scan_id(scan_id, __language()) keys = data[0].keys() with open(filename, "w") as output_file: dict_writer = csv.DictWriter(output_file, fieldnames=keys, quoting=csv.QUOTE_ALL) dict_writer.writeheader() for i in data: dictdata = { key: value for key, value in i.items() if key in keys } dict_writer.writerow(dictdata) print_data = [] with open(filename, 'r') as output_file: _reader = output_file.read() return Response(_reader, mimetype='text/csv', headers={ 'Content-Disposition': 'attachment;filename=' + filename + '.csv' })
def sort_logs(log_in_file, language, graph_flag, scan_id, scan_cmd, verbose_level, api_flag, profile, scan_method, ports): """ sort all events, create log file in HTML/TEXT/JSON and remove old logs Args: log_in_file: output filename language: language graph_flag: graph name scan_id: scan hash id scan_cmd: scan cmd verbose_level: verbose level number api_flag: API flag profile: profiles scan_method: module names ports: ports Returns: True if success otherwise None """ _HOST = messages(language, "HOST") _USERNAME = messages(language, "USERNAME") _PASSWORD = messages(language, "PASSWORD") _PORT = messages(language, "PORT") _TYPE = messages(language, "TYPE") _DESCRIPTION = messages(language, "DESCRIPTION") _TIME = messages(language, "TIME") events_num = 0 report_type = "" JSON_FROM_DB = __logs_by_scan_id(scan_id, language) JSON_Data = sorted(JSON_FROM_DB, key=sorted) if compatible.version() is 2: import sys reload(sys) sys.setdefaultencoding('utf8') if (len(log_in_file) >= 5 and log_in_file[-5:] == '.html') or ( len(log_in_file) >= 4 and log_in_file[-4:] == '.htm'): report_type = "HTML" data = sorted(JSON_FROM_DB, key=lambda x: sorted(x.keys())) # if user want a graph _graph = '' if graph_flag is not None: _graph = build_graph(graph_flag, language, data, 'HOST', 'USERNAME', 'PASSWORD', 'PORT', 'TYPE', 'DESCRIPTION') from lib.html_log import _log_data _css = _log_data.css_1 _table = _log_data.table_title.format(_graph, _css, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE, _DESCRIPTION, _TIME) for value in data: _table += _log_data.table_items.format(value['HOST'], value['USERNAME'], value['PASSWORD'], value['PORT'], value['TYPE'], value['DESCRIPTION'], value['TIME']) events_num += 1 _table += _log_data.table_end + '<p class="footer">' + messages(language, "nettacker_version_details") \ .format(compatible.__version__, compatible.__code_name__, now()) + '</p>' __log_into_file(log_in_file, 'w' if type(_table) == str else 'wb', _table, language, final=True) elif len(log_in_file) >= 5 and log_in_file[-5:] == '.json': graph_flag = "" report_type = "JSON" data = json.dumps(JSON_Data) events_num = len(JSON_Data) __log_into_file(log_in_file, 'w', data, language, final=True) else: graph_flag = "" report_type = "TEXT" data, events_num = __build_texttable(JSON_FROM_DB, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE, _DESCRIPTION, _TIME, language) __log_into_file(log_in_file, 'wb', data, language, final=True) data = data if report_type == "TEXT" else __build_texttable(JSON_FROM_DB, _HOST, _USERNAME, _PASSWORD, _PORT, _TYPE, _DESCRIPTION, _TIME, language)[0] info(messages(language, "updating_database")) category = [] for sm in scan_method: if sm.rsplit("_")[-1] not in category: category.append(sm.rsplit("_")[-1]) category = ",".join(list(set(category))) scan_method = ",".join(scan_method) if ports is None: ports = "default" submit_report_to_db(now(), scan_id, log_in_file, events_num, 0 if verbose_level is 0 else 1, api_flag, report_type, graph_flag, category, profile, scan_method, language, scan_cmd, ports) info(messages(language, "removing_logs_db")) hosts = [] for log in JSON_Data: if log["HOST"] not in hosts: hosts.append(log["HOST"]) for host in hosts: for sm in scan_method.rsplit(','): remove_old_logs(host, sm, scan_id, language) # info(messages(language,"inserting_logs_db")) # for log in JSON_Data: # submit_logs_to_db(language, log) if events_num: info(messages(language, "summary_report")) write(data) else: info(messages(language, "no_event_found")) info(messages(language, "file_saved").format(log_in_file)) return True