class SpiderPipeline(object): def __init__(self): self.conn = None def process_item(self, item, spider): if isinstance(item, NewsItem): sql = "insert into bd_action.spider_news (site_id, news_url, news_title, news_img, news_content, news_pics, my_news_img, my_news_content, my_news_pics) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)" self.conn.execute(sql, item['site_id'], item['news_url'], item['news_title'], item['news_img'], item['news_content'], item['news_pics'], item['my_news_img'], item['my_news_content'], item['my_news_pics']) elif isinstance(item, WbItem): sql = "insert into bd_action.spider_wb (user_id, content, my_content, pics, my_pics, site_id,wb_id) values (%s,%s, %s, %s, %s, %s, %s);" self.conn.execute(sql, item['user_id'], item['content'], item['my_content'], item['pics'], item['my_pics'], item['site_id'], item['wb_id']) else: spider.logger.info(item) return item def open_spider(self, spider): self.conn = Connection(using="bd") def close_spider(self, spider): self.conn.close()
def get_daily_ranking(): conn = Connection() msg = '' ranked = sorted([(x[0], x[1], find_in_pi(x[2])) for x in conn.get_pi_members()], key=lambda tup: tup[2]) for rank in ranked: msg += str(ranked.index(rank) + 1) + "°: " + rank[ 1] + ", respectiva posição nos dígitos de π: " + str( rank[2]) + "\n" return msg
def setUp(self): super(TestUtils, self).setUp() setting = Setting() setting.initialize(os.path.join(current_dir, 'settings.cfg')) conn = Connection() conn.connect(setting.config) # initialize logger Logger.init(**setting.config.twisted.logging) # Redis 초기화 self.redis = conn.redis self.redis.delete('current_post_id')
def test_db_connection(): connection_success = Connection(dbhost=DBHOST, dbport=DBPORT, database=DATABASE, dbuser=DBUSER, dbpassword=DBPASSWORD, create_tables=False).get_connection() connection_wrong = Connection(dbhost='localhost', dbport=3306, database='xyz', dbuser='******', dbpassword='******', create_tables=False).get_connection() assert connection_success is not None, "The request was not successful. Check credentials, database existence, etc" assert connection_wrong is None, f"Connection with wrong credentials must be 'None'"
class Miamala: ctx = user.quick_login("admin", "admin") meseji = sms.get_sms(ctx, 1, 1, qty=10, unread_preferred=True) data = Connection() def save_miamala(self): """miamala ilio ingia kwenye account""" for ujumbe in self.meseji['response']['Messages']['Message']: if "HaloPesa" in ujumbe["Phone"]: muamala = ujumbe['Content'].split(' ') if "Transaction" and "received" in muamala: try: self.Transaction_id = muamala[2] amount = muamala[muamala.index("received") + 1] phone = muamala[muamala.index('number') + 1] sender = muamala[muamala.index('from') + 1] + " " + muamala[ muamala.index('from') + 2] time = muamala[muamala.index('time') + 2] date = muamala[muamala.index('time') + 1] query = f"""INSERT INTO watu.miamla (transaction_id,amout,phone,sender,time,date) values ({self.Transaction_id},{amount},{phone},{sender},{time},{date}) """ except Exception as e: logging.INFO("Error in muamala") pass finally: self.data.run_query(query) def watu_details(self): """get information of people""" query = "SELECT * FROM watu.people" watu = self.data.run_query(query) return jsonify(watu)
def append(s, clients): # Insert to table all clients in list for key in clients: # Get robot by anchor r = s.db.query(Robot)\ .join('container')\ .filter(Robot.anchor==key)\ .first() if not r: # robot doesn't exist - skip s.log.critical('Robot {0} does NOT exist'.format(key)) continue # Create connection row c = Connection(since=clients[key]['Connected Since'], vaddress=clients[key]['Virtual Address'], raddress=clients[key]['Real Address'], sent=int(clients[key]['Bytes Sent']), received=int(clients[key]['Bytes Received']), container=r.container.id, node=s.nodeid) # Add new item to session s.log.info('Append new connection: {0} -> {1}'.format( c.raddress, c.vaddress)) s.db.add(c) s.db_changed = True
def setUp(self): super(TestPostImpl, self).setUp() setting = Setting() setting.initialize(os.path.join(current_dir, 'settings.cfg')) conn = Connection() conn.connect(setting.config) # initialize logger Logger.init(**setting.config.twisted.logging) Post.drop_collection() # Redis 초기화 self.redis = conn.redis self.redis.delete('current_post_id') self.fake = Factory.create()
def __init__(self, fb_token=None, fb_id=None, tinder_token=None): if tinder_token: self._api = TinderApi(tinder_token=tinder_token) else: self._facebook_data = FacebookTools(access_token=fb_token, fb_id=fb_id) self._api = TinderApi(self._facebook_data.access_token, self._facebook_data.facebook_id) self._db = Connection(Config.DB_IP, Config.DB_PORT)
def save_search_query(conn: Connection, userid: int, query: str) -> bool: """ Method to save the search query in database Args: conn: Custom Connection object to interact with sqlite3 database userid: The id of the user who requested for this query query: The query string which the user requested Returns: bool: True if success; else False """ try: conn.execute( "INSERT INTO HARDCACHE (USERID, QUERY) VALUES ({}, '{}');".format( userid, query)) except Exception as e: print(e) print(ErrorMessage.INSERT_DB_EXCEPTION) return False return True
def get_member_ranking(username): conn = Connection() member = conn.query_sql("SELECT * FROM members where username = '******'") ranked = sorted([(x[0], x[1], find_in_pi(x[2])) for x in conn.get_pi_members()], key=lambda tup: tup[2]) if member: return str( ranked.index((member[0], member[1], find_in_pi(member[2]))) + 1) + "°: " + member[ 1] + ", respectiva posição nos dígitos de π: " + str( member[2]) + "\n" else: generate_member_ranking(username) member = conn.query_sql("SELECT * FROM members where username = '******'") ranked = sorted([(x[0], x[1], find_in_pi(x[2])) for x in conn.get_pi_members()], key=lambda tup: tup[2]) return str( ranked.index((member[0], member[1], find_in_pi(member[2]))) + 1) + "°: " + member[ 1] + ", respectiva posição nos dígitos de π: " + str( member[2]) + "\n"
def __init__(self): self.API_TOKEN = os.environ["SECRET"] self.contatinhosSheet = "http://bit.ly/contatosbcc021" self.githubRepo = "https://github.com/lineuzinho-icmc/lineuzinho" self.usefulLinks = "Estamos adicionando todo mundo aos poucos. Se puder ajudar a achar o pessoal, passa o link do grupo na descrição!\n\nInscrição na semana de recepção: calouros.icmc.usp.br/\n\nGuia do Bixo: https://bit.ly/3c9mcUG\n\nContatinho de geral: {0}\n\nEnquetes: https://t.me/joinchat/qrJ_MrnHDbE1ZmNh\n\n".format( self.contatinhosSheet) self.docsChannel = "https://t.me/docs21" self.greeter = Greeter() self.forwarder = Forwarder() self.beaner = Beaner() self.conn = Connection() self.last_pi_call = time.time()
def new_connection(uid, classId, webhookUrl, db): webhookId, webhookToken = clean_webhook_url(webhookUrl) regId, time = Classroom.from_uid(uid, db).register(classId) con = Connection( uid=uid, classId=classId, webhookId=webhookId, webhookToken=webhookToken, registration=regId, expire=parsetime(time), ) db.add(con)
def get_posts_from_db(self): try: from db import Connection except ImportError as error: logging.info(error) with Connection() as conn: try: dtypes = {} self.posts = pd.read_sql("SELECT * FROM posts", conn, parse_dates=['timestamp']) self.posts = self.posts.convert_dtypes() logging.info("Retrieved posts from database") except Exception as e: logging.error(e + "Please ensure container is up and running.")
def save(self, author, tipo, genre): try: connection = Connection().get_connection() cursor = connection.cursor() insert = """INSERT INTO music (name, author, genre) values ('{0}', '{1}', '{2}');""".format( author, tipo, genre) cursor.execute(insert) connection.commit() except (Exception, psycopg2.DatabaseError) as error: print("Error", error) finally: if connection: cursor.close() connection.close()
def generate_member_ranking(username): conn = Connection() #Verify if user is already in db if [i for i, v in enumerate(conn.get_members()) if v[1] == username]: num = str(randint(0, 1000)) while find_in_pi(num) == -1: num = str(randint(0, 1000)) conn.execute_sql("UPDATE members SET pi_rank = " + str(num) + " WHERE username = '******'") else: num = str(randint(0, 1000)) while find_in_pi(num) == -1: num = str(randint(0, 1000)) conn.execute_sql("INSERT INTO members (username, pi_rank) VALUES ('" + username + "', " + num + ");")
def find_search_history(conn: Connection, userid: int, query: str) -> list: """ Method to find from search history for the userid based on substring match Args: conn: Custom Connection object to interact with sqlite3 database userid: The id of the user who requested for this query query: The query string which the user requested Returns: list: The list of all the queries the user did containing the substring "query" """ try: cursor = conn.execute( "SELECT QUERY FROM HARDCACHE WHERE USERID = {} AND QUERY LIKE '%{}%'" .format(userid, query)) result = [] for row in cursor: result.append(row[0]) return result except Exception as e: print(e) print(ErrorMessage.SELECT_DB_EXCEPTION) return []
def __init__(self, **db_setting): Connection.__init__(self, **db_setting)
def put_session(self, session): ipinfo = None asn = None block = None country = None if self.do_ip_to_asn_resolution: ipinfo = get_ip_info(session["ip"]) if ipinfo: asn_obj = self.get_asn(ipinfo["asn"]) asn = ipinfo["asn"] block = ipinfo["ipblock"] country = ipinfo["country"] report = {} if self.vt != None: if self.web.get_ip_report(session["ip"]) == None: report = self.vt.query_ip_reports(session["ip"]) ipReport = IpReport(ip=session["ip"], report=json.dumps(report)) self.session.add(ipReport) # Calculate "hash" connhash = "" for event in session["stream"]: if event["in"]: line = event["data"] line = ''.join(char for char in line if ord(char) < 128 and ord(char) > 32) if line != "": linehash = abs(hash(line)) % 0xFFFF connhash += struct.pack("!H", linehash) connhash = connhash.encode("hex") backend_user = self.session.query(User).filter( User.username == session["backend_username"]).first() conn = Connection(ip=session["ip"], user=session["user"], date=session["date"], password=session["pass"], stream=json.dumps(session["stream"]), asn_id=asn, ipblock=block, country=country, connhash=connhash, backend_user_id=backend_user.id) self.session.add(conn) self.session.flush() req_urls = [] set_urls = set(session["urls"]) for url in set_urls: db_url = self.db.get_url(url).fetchone() url_id = 0 report = '' parsed_uri = urlparse(url) domain = '{uri.netloc}'.format(uri=parsed_uri) if self.vt != None: report = self.vt.query_domain_reports(domain) domainReport = DomainReport(domain=domain, report=json.dumps(report)) self.session.add(domainReport) if db_url == None: url_ip = None url_asn = None url_country = None if self.do_ip_to_asn_resolution: url_ip, url_info = get_url_info(url) if url_info: asn_obj_url = self.get_asn(url_info["asn"]) url_asn = url_info["asn"] url_country = url_info["country"] url_id = self.db.put_url(url, session["date"], url_ip, url_asn, url_country) req_urls.append(url) elif db_url["sample"] == None: req_urls.append(url) url_id = db_url["id"] else: # Sample exists already # TODO: Check url for oldness url_id = db_url["id"] self.db.link_conn_url(conn.id, url_id) # Find previous connections # A connection is associated when: # - same honeypot/user # - connection happened as long as 120s before # - same client ip OR same username/password combo assoc_timediff = 120 previous_conns = (self.session.query(Connection).filter( Connection.date > (conn.date - assoc_timediff), or_( and_(Connection.user == conn.user, Connection.password == conn.password), Connection.ip == conn.ip), Connection.backend_user_id == conn.backend_user_id, Connection.id != conn.id).all()) for prev in previous_conns: conn.conns_before.append(prev) # Check connection against all tags tags = self.session.query(Tag).all() conn = self.session.query(Connection).filter( Connection.id == conn.id).first() for tag in tags: json_obj = conn.json(depth=0) json_obj["text_combined"] = filter_ascii(json_obj["text_combined"]) if simple_eval(tag.code, names=json_obj) == True: self.db.link_conn_tag(conn.id, tag.id) return req_urls
import csv from bson.objectid import ObjectId from datetime import date from db import Connection def find(arr, key, value): for item in arr: if item[key] == value: return item lists = [] con = Connection() lists_cusror = con.find_lists({}) for list in lists_cusror: lists.append(list) current_date = date.today().strftime('%Y-%m-%d') to_read = find(lists, 'name', 'To Read')['name'] read = find(lists, 'name', 'Read')['name'] reading = find(lists, 'name', 'Currently Reading')['name'] books = [] print(to_read_id) print(read_id) print(reading_id) with open('/Users/abhishek/Downloads/goodreads_library_export.csv',
async def _get_crypto_token(self): async with Connection() as connect: return await connect.fetch( '''SELECT * FROM crypto_token WHERE email='$1' ''', self.email)
def open_spider(self, spider): self.conn = Connection(using="bd")
def generate_daily_ranking(): conn = Connection() for user in conn.get_usernames(): generate_member_ranking(user)
def __init__(self): Connection.__init__(self)
#!//usr/bin/python3.6 import hashlib import random import requests import json import threading from OpenSSL import SSL from flask import * from flask_cors import CORS from db import Connection from bs4 import BeautifulSoup import urllib.request as urllib2 import re from smmbget import get_level_info, check_valid_level, write_level_info, check_level_duplicate c = Connection() c.connect() members = c.fetch("members") if not members: c.create("members", {"username": None, "password": None, "salt": None, "admin": 0}) pepper = "efoijxewioufhaewprofwefz;dorfgjlakesdf;;yesIhitmyheadonmykeyboard" app = Flask(__name__, template_folder='templates', static_folder='templates/static') CORS(app) def set_admin(username): c.connect() members = c.fetch("members") members.add({"admin":1}, {"username":username}) return True
def main(): the_date = (datetime.datetime.now() + datetime.timedelta(days=-1)).strftime("%Y-%m-%d") if len(sys.argv) > 1: the_date = sys.argv[1] log_file = "./login_%s.txt" % (the_date) insertSql = "insert into bd_action.login_action(q_date,q_time,q_ip,q_uid,q_login_type,q_dev_type,q_result_code,q_channel,q_dev_imei,q_dev_model,q_net,q_app_ver,q_sys_version,q_mobile,q_login_uid,q_third_type,q_third_id,q_opt_duration) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);" delSql = "delete from bd_action.login_action where q_date='%s'" % ( the_date) conn = Connection(using="bd") conn.execute(delSql) ''' filePath = './big.txt' for chunk in read_in_chunks(filePath): print chunk ''' params = [] for line in open(log_file): log_str = line.strip() log = json.loads(line) param = log.get("q_req_param", "{}") req_data = None if isinstance(param.get("data", [{}])[0], dict) == False: req_data = json.loads(param.get("data", [{}])[0]) else: req_data = param.get("data", [{}])[0] result = None if isinstance(log.get("q_opt_result"), dict) == False: result = json.loads(log.get("q_opt_result")) else: result = log.get("q_opt_result") result_data = result.get("data", {}) if req_data.get("openid", "").find("Testyaliceshi") <> -1: continue if req_data.get("isTest", 0) == 1: continue q_time = log.get("q_opt_time", "0") if str(q_time).find(":") == -1: q_time = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(string.atof(log.get("q_opt_time", "0")) / 1000)) resultArr = [ the_date, q_time, log.get("q_ip", "").split(",", -1)[0], log.get("q_uid", ""), log.get("q_action", ""), log.get("q_dev_type", ""), result.get("code", ""), log.get("q_channel", ""), log.get("q_dev_imei", ""), log.get("q_dev_model", ""), log.get("q_net", ""), log.get("q_app_ver", ""), log.get("q_sys_version", ""), req_data.get("mobile", ""), result_data.get("userId", ""), req_data.get("thirdType", ""), req_data.get("openid", ""), log.get("q_opt_duration", "0") ] params.append(resultArr) #conn.execute(insertSql,*tuple(resultArr)) try: conn.executemany(insertSql, params) except Exception, e: print traceback.format_exc() print line
## Primary Author: Mayank Mohindra <github.com/mayankmtg> ## ## Description: Main file. Contains logic to handle all commands and start the bot ## import discord from discord.ext import commands from search import perform_search from config import Config from cache import save_search_query, find_search_history from db import Connection from error import ErrorMessage Config.load() client = discord.Client() conn = Connection(Config.DATABASE) # bot starts @client.event async def on_ready(): print("bot is running") # bot receives any messages @client.event async def on_message(command): # Ignore string from self if command.author == client.user: return
def search_prompt(): query = None choice = input('Search workout (name/type/duration/comments/year): ') if choice: query = {'$text': {'$search': '\"' + choice + '\"'}} return query clear() print('#' * 40) print('#' + ' ' * 15 + 'WORKOUTS' + ' ' * 15 + '#') print('#' * 40) con = Connection() choice = input( 'Choose one of the following options\n1. Enter new workout\n2. Search workout\n3. List last 5 workouts\n' ) clear() if choice == '1': workout = prompt({}) save_workout(workout) elif choice == '2': query = search_prompt() if query: search_workout(query) elif choice == '3':
async def _update_key(self): async with Connection() as connect: return await connect.execute( '''UPDATE crypto_token SET private_key='$1' WHERE email='$2' ''', self.key, self.email)
import time #package import chess #local import reflection import sys sys.path.append('../db') from db import Connection reflection.database.connect('www-data') Connection.register_orm(reflection.database.connection()) EcoName = reflection.VEcoName GameState = reflection.VGamestate OpeningVar3Agg = reflection.VOpeningVar3Agg KModePawn = reflection.VKmodePawn KModeAggPawn = reflection.VKmodeAggPawn KclassEcoNamePawn = reflection.VKclassEcoNamePawn KclassEcoVar1Pawn = reflection.VKclassEcoVar1Pawn KModeWmg = reflection.VKmodeWmg KModeAggWmg = reflection.VKmodeAggWmg KclassEcoNameWmg = reflection.VKclassEcoNameWmg KclassEcoVar1Wmg = reflection.VKclassEcoVar1Wmg KModeBmg = reflection.VKmodeBmg KModeAggBmg = reflection.VKmodeAggBmg
item = re.split('_', item) module_name, mthd = item[0], '_'.join(item[1:]) class_name = module_name.capitalize() + 'Controller' module = __import__('controllers.%s' % module_name) instance = getattr(getattr(module, module_name), class_name)() return getattr(instance, mthd) if __name__ == '__main__': setting = Setting() setting.initialize(os.path.join(current_dir, 'settings.cfg')) conn = Connection() conn.connect(setting.config) # initialize logger Logger.init(**setting.config.twisted.logging) # setup server handler = TodayInternalApiServiceHandler() processor = TodayInternalApiService.Processor(handler) pfactory = TBinaryProtocol.TBinaryProtocolFactory() server = reactor.listenTCP(setting.config.twisted.port, TTwisted.ThriftServerFactory(processor, pfactory), interface=setting.config.twisted.interface) reactor.run()
def handle(msg): global user_states chat_id = msg['chat']['id'] if 'text' in msg: message = msg['text'].lower() file = None elif 'document' in msg: file = msg['document']['file_id'] message = "- File (id: " + file + ") -" file = bot.getFile(file) else: bot.sendMessage(chat_id, Messages.unknowCMD, reply_markup = showOptions(Config.commands)) return print(" Got message : " + message) if message == "/start": bot.sendMessage(chat_id, Config.welcome, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None # print(bot.getChat(chat_id)) elif chat_id in user_states.keys(): if message == "/id": user_states[chat_id] = "id" bot.sendMessage(chat_id, Messages.typeID, reply_markup = ReplyKeyboardRemove()) elif message == "/name": user_states[chat_id] = "name" bot.sendMessage(chat_id, Messages.typeName, reply_markup = ReplyKeyboardRemove()) elif message == "/room": user_states[chat_id] = "room" bot.sendMessage(chat_id, Messages.typeRoom, reply_markup = showOptions(Connection.getRooms())) elif message == "/job": user_states[chat_id] = "job" bot.sendMessage(chat_id, Messages.typeJob, reply_markup = showOptions(Connection.getJobs())) elif message == "/update" and adminCheck(chat_id): user_states[chat_id] = "update" bot.sendMessage(chat_id, Messages.typeUpdate, reply_markup = ReplyKeyboardRemove()) elif user_states[chat_id] == "id": if message.isdigit(): response = Connection.getResidentById(int(message)) if response != None: user = response room = user['room'] face = Messages.face.get("default") if user['roomHead']: face = Messages.face.get("roomHead") room += " - " + Messages.roomHead elif user['areaHead']: face = Messages.face.get("areaHead") room += " - " + Messages.areaHead data = ( face + " " + user['name'] + " " + user['surname'] + "\n" + Messages.icons["mailbox"] + " " + user['email'] + "\n" + Messages.icons["telephone"] + " " + user['cell'] + "\n" + Messages.icons["books"] + " " + user['course'] + "\n" + Messages.icons["hotel"] + " " + room + "\n\n" ) bot.sendMessage(chat_id, data, reply_markup = showOptions(Config.commands)) else: bot.sendMessage(chat_id, Messages.noID, reply_markup = showOptions(Config.commands)) else: bot.sendMessage(chat_id, Messages.noID, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None elif user_states[chat_id] == "name": response = Connection.getResidentByName(message) if response != None and len(response) > 0: data = "" for user in response: room = user['room'] face = Messages.face.get("default") if user['roomHead']: face = Messages.face.get("roomHead") room += " - " + Messages.roomHead elif user['areaHead']: face = Messages.face.get("areaHead") room += " - " + Messages.areaHead data += ( face + " " + user['name'] + " " + user['surname'] + "\n" + Messages.icons["mailbox"] + " " + user['email'] + "\n" + Messages.icons["telephone"] + " " + user['cell'] + "\n" + Messages.icons["books"] + " " + user['course'] + "\n" + Messages.icons["hotel"] + " " + room + "\n" + Messages.icons["ticket"] + " " + str(user['id']) + "\n\n" ) bot.sendMessage(chat_id, data, reply_markup = showOptions(Config.commands)) else: bot.sendMessage(chat_id, Messages.noName, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None elif user_states[chat_id] == "room": response = Connection.getRoomResidents(message) if response != None and len(response) > 0: data = "" for user in response: extra = "" face = Messages.face.get("default") if user["roomHead"]: face = Messages.face.get("roomHead") + "*" extra = " - " + Messages.roomHead + "*" elif user["areaHead"]: face = Messages.face.get("areaHead") + "*" extra = " - " + Messages.areaHead + "*" data += (face + " " + user['name'] + " " + user['surname'] + extra + "\n") bot.sendMessage(chat_id, data, parse_mode = "Markdown", reply_markup = showOptions(Config.commands)) else: bot.sendMessage(chat_id, Messages.noRoom, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None elif user_states[chat_id] == "job": response = Connection.getEmployees(message) if response != None and len(response) > 0: data = "" for user in response: face = Messages.face.get("default") extra = "" if(user['manager']): face = Messages.face.get("manager") + "*" extra = " - " + Messages.jobManager + "*" data += (face + " " + user['name'] + " " + user['surname'] + extra + "\n") bot.sendMessage(chat_id, data, parse_mode = "Markdown", reply_markup = showOptions(Config.commands)) else: bot.sendMessage(chat_id, Messages.noJob, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None elif user_states[chat_id] == "update" and adminCheck(chat_id) and file != None: connection = Connection.connect() f = tempfile.TemporaryFile() try: bot.download_file(file['file_id'], f) f.flush() f.seek(0) sql = [x.strip() for x in f.readlines()] results = [] with connection.cursor() as cursor: print(sql) for query in sql: cursor.execute(query) result = cursor.fetchall() if(Config.debug): print(result) results.append(result) bot.sendMessage(chat_id, results) connection.commit() bot.sendMessage(chat_id, Messages.updated, reply_markup = showOptions(Config.commands)) except (RuntimeError, TypeError, NameError, pymysql.ProgrammingError, pymysql.Error) as err: bot.sendMessage(chat_id, "Error: {}".format(err), reply_markup = showOptions(Config.commands)) finally: connection.close() f.close() user_states[chat_id] = None else: bot.sendMessage(chat_id, Messages.unknowCMD, reply_markup = showOptions(Config.commands)) user_states[chat_id] = None else: bot.sendMessage(chat_id, Config.update, reply_markup = showOptions("/start"))
"channels": { "feed": 762952918848503859, "debug": 764641152682033172 } }] CACHE = {} LEVELER = None FROZEN_USERS = [] IGNORE_ROLES = ["@everyone", "Muted"] # load_dotenv() # TOKEN = os.getenv('DISCORD_TOKEN') TOKEN = os.environ["DISCORD_TOKEN"] conn = Connection() c = conn.cursor routes = Routes(conn, c) def is_moderator(ctx): mod_roles = CACHE[ctx.guild.id]["roles"]["Moderators"] admin_roles = CACHE[ctx.guild.id]["roles"]["Administrators"] valid_roles = mod_roles + admin_roles if any(r in ctx.author.roles for r in valid_roles): return True return False
def put_session(self, session): connhash = self.calc_connhash(session["stream"]).encode("hex") backend_user = self.session.query(User).filter( User.username == session["backend_username"]).first() conn = Connection(ip=session["ip"], user=session["user"], date=session["date"], password=session["pass"], stream=json.dumps(session["stream"]), connhash=connhash, backend_user_id=backend_user.id) conn.user = filter_ascii(conn.user) conn.password = filter_ascii(conn.password) if self.do_ip_to_asn_resolution: range = self.get_ip_range(conn.ip) if range: conn.country = range.country conn.city = range.city conn.lat = range.latitude conn.lon = range.longitude conn.asn = range.asn self.session.add(conn) self.session.flush() # to get id network_id = None samples = [] urls = [] for sample_json in session["samples"]: # Ignore junk - may clean up the db a bit if sample_json["length"] < 2000: continue sample, url = self.create_url_sample(sample_json) if sample: if network_id == None and sample.network_id != None: network_id = sample.network_id samples.append(sample) if url: if network_id == None and url.network_id != None: network_id = url.network_id conn.urls.append(url) urls.append(url) # Find previous connections # A connection is associated when: # - same honeypot/user # - connection happened as long as 120s before # - same client ip OR same username/password combo assoc_timediff = 120 assoc_timediff_sameip = 3600 previous_conns = (self.session.query(Connection).filter( or_( and_(Connection.date > (conn.date - assoc_timediff), Connection.user == conn.user, Connection.password == conn.password), and_(Connection.date > (conn.date - assoc_timediff_sameip), Connection.ip == conn.ip)), Connection.backend_user_id == conn.backend_user_id, Connection.id != conn.id).all()) for prev in previous_conns: if network_id == None and prev.network_id != None: network_id = prev.network_id conn.conns_before.append(prev) # Check connection against all tags tags = self.session.query(Tag).all() for tag in tags: json_obj = conn.json(depth=0) json_obj["text_combined"] = filter_ascii(json_obj["text_combined"]) if simple_eval(tag.code, names=json_obj) == True: self.db.link_conn_tag(conn.id, tag.id) # Only create new networks for connections with urls or associtaed conns, # to prevent the creation of thousands of networks # NOTE: only conns with network == NULL will get their network updated # later so whe should only create a network where we cannot easily # change it later haslogin = conn.user != None and conn.user != "" if (len(conn.urls) > 0 or len(previous_conns) > 0) and network_id == None and haslogin: print(" --- create network --- ") network_id = self.create_network().id # Update network on self conn.network_id = network_id # Update network on all added Urls for url in urls: if url.network_id == None: url.network_id = network_id # Update network on all added Samples for sample in samples: if sample.network_id == None: sample.network_id = network_id # Update network on all previous connections withut one if network_id != None: for prev in previous_conns: if prev.network_id == None: prev.network_id = network_id # Update number of first conns on network if len(prev.conns_before) == 0: conn.network.nb_firstconns += 1 self.session.flush() # Check for Malware type # only if our network exists AND has no malware associated if conn.network != None and conn.network.malware == None: # Find connections with similar connhash similar_conns = (self.session.query(Connection).filter( func.length(Connection.connhash) == len(connhash)).all()) min_sim = 2 min_conn = None for similar in similar_conns: if similar.network_id != None: c1 = connhash.decode("hex") c2 = similar.connhash.decode("hex") sim = self.calc_connhash_similiarity(c1, c2) if sim < min_sim and similar.network.malware != None: min_sim = sim min_conn = similar # 0.9: 90% or more words in session are equal # think this is probably the same kind of malware # doesn't need to be the same botnet though! if min_sim < 0.9: conn.network.malware = min_conn.network.malware else: conn.network.malware = Malware() conn.network.malware.name = random.choice(ANIMAL_NAMES) self.session.add(conn.network.malware) self.session.flush() # Update network number of first connections if len(previous_conns) == 0 and conn.network_id != None: conn.network.nb_firstconns += 1 return conn.json(depth=1)
async def _create_crypto_token(self): async with Connection() as connect: return await connect.execute( '''INSERT INTO crypto_token(email, private_key) VALUES('$1', '$2') ''', self.email, self.key)