Skip to content

Commit f6f1330

Browse files
committedNov 29, 2024··
Initial poll command
1 parent ba481ca commit f6f1330

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
 

‎poll.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import requests
2+
import time
3+
4+
# Configuration
5+
BASE_URL = "http://localhost:5000"
6+
USERNAME = "poshc2"
7+
PASSWORD = "change_on_install"
8+
FILTER_USERNAME = "bt" # The username to filter tasks by
9+
TASKS_ENDPOINT = f"{BASE_URL}/tasks"
10+
TASK_ENDPOINT = f"{BASE_URL}/task"
11+
POLL_INTERVAL = 5 # Poll every 5 seconds when no task is found
12+
13+
14+
def get_latest_task_id():
15+
"""Get the latest task ID from the tasks API."""
16+
response = requests.get(f"{TASKS_ENDPOINT}/1", auth=(USERNAME, PASSWORD))
17+
if response.status_code == 200:
18+
tasks = response.json()
19+
if isinstance(tasks, list) and tasks:
20+
latest_task = tasks[0]
21+
task_id = latest_task.get("id")
22+
if task_id is not None:
23+
return int(task_id)
24+
return 0
25+
26+
27+
def get_task_by_id(task_id):
28+
"""Get task details by task ID from the task API."""
29+
response = requests.get(f"{TASK_ENDPOINT}/{task_id}", auth=(USERNAME, PASSWORD))
30+
if response.status_code == 200:
31+
try:
32+
task = response.json()
33+
if task and task.get("completed_time") is not None:
34+
return task
35+
except ValueError:
36+
return None
37+
return None
38+
39+
40+
def format_task(task, status):
41+
"""Format the task details for printing."""
42+
task_id = f"Task {int(task.get('id', 0)):05d}" # Format task ID as 5-digit
43+
operator = task.get("user", "Unknown")
44+
implant = task.get("implant_numeric_id", "Unknown")
45+
output = task.get("output")
46+
context = task.get("output", "Unknown").splitlines()[0] if task.get("output") else "Unknown Context"
47+
timestamp = task.get("sent_time") if status == "sent" else task.get("completed_time", "Unknown Time")
48+
command = task.get("command", "Unknown Command")
49+
if status == "sent":
50+
return f"{task_id} sent | Operator: {operator} | Implant: {implant} | Context: {context} | {timestamp}\n{command}\n"
51+
elif status == "returned":
52+
return f"{task_id} returned | Operator: {operator} | Implant: {implant} | Context: {context} | {timestamp}\n\n{output}\n"
53+
return ""
54+
55+
56+
def poll_new_tasks(latest_task_id):
57+
"""Continuously poll for new tasks."""
58+
while True:
59+
latest_task_id += 1
60+
task = get_task_by_id(latest_task_id)
61+
if task:
62+
# Check if the task belongs to the specified username
63+
task_user = task.get("user")
64+
if task_user == FILTER_USERNAME:
65+
print(format_task(task, "sent"))
66+
if task.get("output"): # If task is completed and has output
67+
print(format_task(task, "returned"))
68+
else:
69+
# No task found, start the timer before retrying
70+
latest_task_id -= 1 # Reset ID if no task found
71+
time.sleep(POLL_INTERVAL)
72+
73+
74+
if __name__ == "__main__":
75+
print(f"Fetching latest task ID for user '{FILTER_USERNAME}'...")
76+
latest_task_id = get_latest_task_id()
77+
print(f"Starting polling from task ID: {latest_task_id + 1}")
78+
poll_new_tasks(latest_task_id)

0 commit comments

Comments
 (0)
Please sign in to comment.