| 69 | |
| 70 | |
| 71 | class Agent(FirstClassObjectInterface, BaseObject): |
| 72 | |
| 73 | schema = AgentSchema() |
| 74 | load_schema = AgentSchema(partial=['paw', 'origin_link_id']) |
| 75 | |
| 76 | RESERVED = dict(server='#{server}', group='#{group}', agent_paw='#{paw}', location='#{location}', |
| 77 | exe_name='#{exe_name}', upstream_dest='#{upstream_dest}', |
| 78 | payload=re.compile('#{payload:(.*?)}', flags=re.DOTALL)) |
| 79 | |
| 80 | @property |
| 81 | def unique(self): |
| 82 | return self.hash(self.paw) |
| 83 | |
| 84 | @property |
| 85 | def display_name(self): |
| 86 | return '{}${}'.format(self.host, self.username) |
| 87 | |
| 88 | @classmethod |
| 89 | def is_global_variable(cls, variable): |
| 90 | if variable.startswith('payload:'): |
| 91 | return True |
| 92 | if variable == 'payload': |
| 93 | return False |
| 94 | if variable in cls.RESERVED: |
| 95 | return True |
| 96 | return False |
| 97 | |
| 98 | def __init__(self, sleep_min=30, sleep_max=60, watchdog=0, platform='unknown', server='unknown', host='unknown', |
| 99 | username='unknown', architecture='unknown', group='red', location='unknown', pid=0, ppid=0, |
| 100 | trusted=True, executors=(), privilege='User', exe_name='unknown', contact='unknown', paw=None, |
| 101 | proxy_receivers=None, proxy_chain=None, origin_link_id='', deadman_enabled=False, |
| 102 | available_contacts=None, host_ip_addrs=None, upstream_dest=None, pending_contact=None): |
| 103 | super().__init__() |
| 104 | self.paw = paw if paw else self.generate_name(size=6) |
| 105 | self.host = host |
| 106 | self.username = username |
| 107 | self.group = group |
| 108 | self.architecture = architecture |
| 109 | self.platform = platform.lower() |
| 110 | url = urlparse(server) |
| 111 | self.server = '%s://%s:%s' % (url.scheme, url.hostname, url.port) |
| 112 | self.location = location |
| 113 | self.pid = pid |
| 114 | self.ppid = ppid |
| 115 | self.trusted = trusted |
| 116 | self.created = datetime.now(timezone.utc) |
| 117 | self.last_seen = self.created |
| 118 | self.last_trusted_seen = self.created |
| 119 | self.executors = executors |
| 120 | self.privilege = privilege |
| 121 | self.exe_name = exe_name |
| 122 | self.sleep_min = int(sleep_min) |
| 123 | self.sleep_max = int(sleep_max) |
| 124 | self.watchdog = int(watchdog) |
| 125 | self.contact = contact |
| 126 | self.links = [] |
| 127 | self.access = self.Access.BLUE if group == 'blue' else self.Access.RED |
| 128 | self.proxy_receivers = proxy_receivers if proxy_receivers else dict() |