Session Lifecycle
1. Session Creation
When an agent checks in for the first time, the server creates a new session:2. Active State
Active sessions:- Beacon to the server at regular intervals
- Check for pending tasks on each beacon
- Submit task results back to the server
- Update their
last_seentimestamp
3. Session Deactivation
Sessions can be deactivated by the operator using thekill command:
- Are marked as
active = Falsein the database - No longer receive new tasks
- Will not be restored on server restart
- Remain in the database for forensic analysis
Viewing Sessions
List all sessions with thelist command:
Session Attributes
| Attribute | Description | Example |
|---|---|---|
| session_id | Unique UUID identifier | a1b2c3d4-e5f6-7890-abcd-ef1234567890 |
| hostname | Computer name | VICTIM-PC |
| username | User running agent | jdoe |
| os | Operating system | Windows 10 22H2 |
| agent_ver | Agent version | 1.0.0 |
| jitter_pct | Beacon jitter (0-100%) | 20 |
| first_seen | Initial check-in timestamp | 1710172800.123 |
| last_seen | Most recent beacon | 1710173100.456 |
| active | Active status | True or False |
Session Identification
Always use the full session UUID when issuing commands:list output to avoid typing errors.
Session Sorting
Sessions are sorted bylast_seen in descending order (most recent first):
Session Persistence
Database Storage
All sessions are persisted to SQLite in thesessions table:
Server Restart
When the server restarts, active sessions are automatically restored:active = True are restored to memory. Inactive sessions remain in the database but are not loaded.
SessionManager Implementation
TheSessionManager class (in server/session_manager.py) provides:
Core Methods
create_session
Creates a new session and persists it:server/session_manager.py:34
get_session
Retrieves a session by ID:None if the session doesn’t exist.
Implementation: server/session_manager.py:67
update_last_seen
Updates the session’s last beacon timestamp:server/session_manager.py:72
list_sessions
Returns all sessions sorted by recency:server/session_manager.py:81
deactivate_session
Marks a session as inactive:kill command.
Implementation: server/session_manager.py:88
restore_from_db
Reloads active sessions on server startup:server/session_manager.py:98
In-Memory Session State
TheSessionManager maintains an in-memory dictionary of all active sessions:
Session Data Class
Sessions are represented by theSessionState dataclass:
server/session_manager.py:13
Concurrency Safety
All session operations are protected by an asyncio lock:Session Management Best Practices
1. Monitor Last Seen Times
Check thelast_seen timestamp to identify stale sessions:
2. Clean Up Inactive Sessions
Regularly deactivate sessions that are no longer needed:3. Track Session Metadata
Use hostname, username, and OS information to organize your sessions:4. Session ID Management
Maintain a separate list of important session IDs for quick reference:Error Handling
Session Not Found
list command.
Inactive Session
Logging
All session operations are logged with structured data:logs/ directory for session activity.