13
from app.agents.schema.agents import SyncedWazuhAgent
14
from app.agents.velociraptor.schema.agents import VelociraptorAgent
15
from app.agents.velociraptor.schema.agents import VelociraptorClients
16
+from app.agents.velociraptor.schema.agents import VelociraptorOrganizations
17
from app.agents.wazuh.schema.agents import WazuhAgent
18
from app.agents.wazuh.schema.agents import WazuhAgentsList
19
from app.connectors.models import Connectors
41
)
42
43
43
-async def fetch_velociraptor_clients() -> VelociraptorClients:
44
+async def fetch_velociraptor_clients(org_id: str) -> VelociraptorClients:
45
"""
46
Fetches clients from Velociraptor service.
47
51
Returns:
52
VelociraptorClientsList: The fetched clients.
53
"""
53
- collected_velociraptor_agents = await velociraptor_services.collect_velociraptor_clients()
54
+ collected_velociraptor_agents = await velociraptor_services.collect_velociraptor_clients(org_id=org_id)
55
return VelociraptorClients(
56
clients=collected_velociraptor_agents,
57
)
58
59
60
+async def fetch_velociraptor_organizations() -> VelociraptorOrganizations:
61
+ """
62
+ Fetches organizations from Velociraptor service.
63
+
64
+ Args:
65
+ None
66
+
67
+ Returns:
68
+ VelociraptorOrgsList: The fetched orgs.
69
+ """
70
+ collected_velociraptor_orgs = await velociraptor_services.collect_velociraptor_organizations()
71
+ logger.info(f"Collected Velociraptor Orgs: {collected_velociraptor_orgs}")
72
+ return VelociraptorOrganizations(
73
+ organizations=collected_velociraptor_orgs,
74
+ )
75
+
76
+
77
async def fetch_velociraptor_agent(agent_name: str) -> VelociraptorAgent:
78
"""
79
Fetches agent details from Velociraptor service.
316
:rtype: SyncedAgentsResponse
317
"""
318
agents_added_list: List[VelociraptorAgent] = []
301
-
302
- velociraptor_clients = await fetch_velociraptor_clients()
303
- velociraptor_clients = velociraptor_clients.clients if hasattr(velociraptor_clients, "clients") else []
304
-
305
- async with get_db_session() as session: # Create a new session here
306
- existing_agents_query = select(Agents)
307
- result = await session.execute(existing_agents_query)
308
- existing_agents = result.scalars().all()
309
-
310
- for agent in existing_agents:
311
- logger.info(f"Collecting Velociraptor Agent for {agent.hostname}")
312
-
313
- try:
314
- # Build the velociraptor_agent where the hostname or `client_id` is that equal to the `agents`
315
- velociraptor_agent = next(
316
- (
317
- client
318
- for client in velociraptor_clients
319
- if client.os_info.hostname == agent.hostname or client.client_id == agent.velociraptor_id
320
- ),
321
- None,
322
- )
323
- # Convert Unix epoch timestamp to datetime
324
- last_seen_at = datetime.fromtimestamp(
325
- int(velociraptor_agent.last_seen_at) / 1e6,
326
- ) # Divide by 1e6 to convert from microseconds to seconds
327
- # Convert datetime to ISO 8601 format without fractional seconds
328
- last_seen_at_iso = last_seen_at.replace(tzinfo=timezone.utc).isoformat(timespec="seconds")
329
- velociraptor_agent = VelociraptorAgent(
330
- velociraptor_id=velociraptor_agent.client_id,
331
- velociraptor_last_seen=last_seen_at_iso,
332
- velociraptor_agent_version=velociraptor_agent.agent_information.version,
333
- )
334
-
335
- except Exception as e:
336
- logger.error(
337
- f"Failed to collect Velociraptor Agent for {agent.hostname}: {e}",
338
- )
339
- continue
340
-
341
- if velociraptor_agent:
342
- # Update the agent with the Velociraptor client's details
343
- await update_agent_with_velociraptor_in_db(session, agent, velociraptor_agent)
344
- agents_added_list.append(velociraptor_agent)
345
-
346
- # Close the session
347
- await session.close()
319
+ velo_orgs = await fetch_velociraptor_organizations()
320
+ logger.info(f"Collected Velociraptor Orgs: {velo_orgs}")
321
+ for org in velo_orgs.organizations:
322
+ velociraptor_clients = await fetch_velociraptor_clients(org_id=org.OrgId)
323
+ logger.info(f"Collected Velociraptor Clients: {velociraptor_clients}")
324
+ velociraptor_clients = velociraptor_clients.clients if hasattr(velociraptor_clients, "clients") else []
325
+
326
+ async with get_db_session() as session: # Create a new session here
327
+ existing_agents_query = select(Agents)
328
+ result = await session.execute(existing_agents_query)
329
+ existing_agents = result.scalars().all()
330
+
331
+ for agent in existing_agents:
332
+ logger.info(f"Collecting Velociraptor Agent for {agent.hostname}")
333
+
334
+ try:
335
+ # Build the velociraptor_agent where the hostname or `client_id` is that equal to the `agents`
336
+ velociraptor_agent = next(
337
+ (
338
+ client
339
+ for client in velociraptor_clients
340
+ if client.os_info.hostname == agent.hostname or client.client_id == agent.velociraptor_id
341
+ ),
342
+ None,
343
+ )
344
+ # Convert Unix epoch timestamp to datetime
345
+ last_seen_at = datetime.fromtimestamp(
346
+ int(velociraptor_agent.last_seen_at) / 1e6,
347
+ ) # Divide by 1e6 to convert from microseconds to seconds
348
+ # Convert datetime to ISO 8601 format without fractional seconds
349
+ last_seen_at_iso = last_seen_at.replace(tzinfo=timezone.utc).isoformat(timespec="seconds")
350
+ velociraptor_agent = VelociraptorAgent(
351
+ velociraptor_id=velociraptor_agent.client_id,
352
+ velociraptor_last_seen=last_seen_at_iso,
353
+ velociraptor_agent_version=velociraptor_agent.agent_information.version,
354
+ velociraptor_org=org.OrgId,
355
+ )
356
+
357
+ except Exception as e:
358
+ logger.error(
359
+ f"Failed to collect Velociraptor Agent for {agent.hostname}: {e}",
360
+ )
361
+ continue
362
+
363
+ if velociraptor_agent:
364
+ # Update the agent with the Velociraptor client's details
365
+ await update_agent_with_velociraptor_in_db(session, agent, velociraptor_agent)
366
+ agents_added_list.append(velociraptor_agent)
367
+
368
+ # Close the session
369
+ await session.close()
370
371
logger.info(f"Agents Added List: {agents_added_list}")
372
return SyncedAgentsResponse(