def save(update, context): session = Session() new_name = context.user_data["new_name"] project = context.user_data["current_project"] session.query(Project).filter(Project.id == project.id).update( {"name": new_name}) session.commit() project.name = new_name context.user_data["current_project"] = project return edit(update, context)
def new_message(): def format_text(project_name, message): cases = { "error": "\u26D4New error from %s:\n" "%s", "warning": "⚠️New warning from %s:\n" "%s", "log": "\U0001F4ACNew log from %s:\n" "%s", } return cases[message["type"]] % (project_name, log["message"]["content"]) log = request.json session = Session() project = session.query(Project).filter_by(token=log["token"]).first() if not project: return { "ok": False, "error": "Project not found! Please check your token" } text = format_text(project.name, log["message"]) bot.sendMessage(chat_id=project.user_id, text=text) return jsonify({"ok": True})
def is_password_correct(password: str) -> bool: """Returns whether a user email exists in the database. Args: email: An email that may belong to a user. Returns: Returns true if the email exists, false otherwise. """ session = Session() return session.query(User).filter_by(password=password).first() != None
def get_role_groups(role_id: int) -> List[Group]: """Returns a list of groups with the given role. Args: role_id: An role's role_id. Returns: Returns a list of groups if an role has the role_id, otherwise returns None. """ session = Session() return session.query(Group).join(RoleToGroup).filter( RoleToGroup.role_id == role_id).all()
def get_employee_roles(email: str) -> List[EmployeeToRole]: """Returns a list of roles with the given email. Args: email: An employee's email. Returns: Returns a list of roles if an employee has the email, otherwise returns None. """ session = Session() return session.query(EmployeeToRole).filter( EmployeeToRole.email == email).all()
def get_slack_groups() -> List[Group]: """Returns a list of groups that belong to Slack. Returns: Returns a list containing all the groups from Slack. """ groups = None session = Session() try: groups = session.query(Group).filter(Group.app_id == 1).all() except Exception as e: print(e) return groups
def get_employees_with_ids(ids: List[int]) -> List[Employee]: """Returns a list of employees that match the specified ids. Args: ids: The ids being used to query employees. Returns: Returns a list of employess. """ session = Session() employees = [] try: employees = session.query(Employee).filter(Employee.id.in_(ids)).all() except Exception as e: print(e) return employees
def get_roles_with_ids(ids: List[int]) -> List[Role]: """Returns a list of roles that match the specified ids. Args: ids: The ids being used to query roles. Returns: Returns a list of roles. """ session = Session() roles = [] try: roles = session.query(Role).filter(Role.id.in_(ids)).all() except Exception as e: print(e) return roles
def get_employees_by_role(role: Role) -> List[Employee]: """Returns a list of employees with the given role. Args: role: An instance of Role. Returns: Returns a list of employees. """ employees = [] session = Session() try: employees = session.query(Employee).join(EmployeeToRole).filter( EmployeeToRole.role_id == role.id).all() except Exception as e: print(e) return employees
def show_projects(update, context): chat_id = update.callback_query.message.chat.id try: session = Session() projects = session.query(Project).filter_by(user_id=chat_id) text = "Your projects" buttons = [] count = projects.count() context.user_data["projects"] = projects for i in range(0, count, 2): row = [ InlineKeyboardButton(text=projects[i].name, callback_data=str(EDITING) + str(projects[i].name)), ] if i + 1 < count: row.append( InlineKeyboardButton(text=projects[i + 1].name, callback_data=str(EDITING) + str(projects[i + 1].name))) buttons.append(row) buttons.append([ InlineKeyboardButton(text='Return to main', callback_data=str(MAIN)) ]) keyboard = InlineKeyboardMarkup(buttons) update.callback_query.answer() update.callback_query.edit_message_text(text=text, reply_markup=keyboard) return SELECTING_ACTION except Exception as e: print(str(e))
class SiteCrawler: # threat actor associated with the leak site actor: str = "" # url for the leak site url: str = "" # list of victims on the leak site from current scrape current_victims: List[Victim] = [] # new victims on the leak site from current scrape new_victims: List[Victim] = [] # is the site up? set by is_site_up() is_up: bool = False # db session, set in __init__() session: SessionType # site object from db, set in __init__() site: Site # is this the first ingest of the site? set in __init__() # if the first run, don't notify on new victims (b/c they are all "new") first_run: bool = False # headers to be used for requests headers: Dict[str, str] def __init__(self, url: str): """ make sure site is in db check if site is up if it is, update last_up in db """ # this should be statically defined in child implementations of the class assert self.actor != "" self.current_victims = [] self.new_victims = [] self.url = url self.session = Session() q = self.session.query(Site).filter_by(actor=self.actor) self.headers: Dict[str, str] = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0" } if q.count() == 0: # site is new, init obj self.site = Site(actor=self.actor, url=self.url) self.session.add(self.site) self.session.commit() self.first_run = True else: # site exists, set obj self.site = q.first() # if we haven't successfully scraped the site before, consider this the first run if self.site.last_scraped is None: self.first_run = True # check if site is up self.is_up = self.is_site_up() def is_site_up(self) -> bool: """ check if the site is up this might have specific criteria for some sites """ with Proxy() as p: try: r = p.get(self.url, headers=self.headers, timeout=Config["timeout"]) if r.status_code >= 400: return False except Exception as e: return False self.site.last_up = datetime.utcnow() return True def scrape_victims(self): """ pull each listing on the site check if its already in the db if it is, update the last seen if it isn't, add it to the db store each org name in a list (self.current_victims) this also sets self.new_victims, which has new victims to notify with """ raise Exception("Function implementation not found") def identify_removed_victims(self) -> List[Victim]: """ check org name list against db if something is in the db, not already removed, and not in the list, alert """ # get the current victims from the last scrape victims = self.session.query(Victim).filter_by(site=self.site, removed=False).all() # remove anything from the last scrape that was also in this scrape # the remaining set is things that were present last time, but not this time for v in self.current_victims: try: victims.remove(v) except ValueError: # i think there is an edge case here that can be caused by # running the scrape while a victim is removed # it _should_ get picked up on the next run though, so we # can safely ignore this pass # mark the victims as removed, since they are no longer on the leak site for v in victims: v.removed = True self.session.commit() return victims