-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
socksproxyserver.plugin
232 lines (207 loc) · 9.28 KB
/
socksproxyserver.plugin
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
from __future__ import print_function
import _thread
import os
import queue
import socket
import ssl
import empire.server.common.helpers as helpers
from empire.server.common.plugins import Plugin
from empire.server.core.plugin_service import PluginService
class Plugin(Plugin):
def onLoad(self):
self.main_menu = None
self.info = {
'Name': 'socksproxyserver',
'Authors': [
{
"Name": "Anthony Rose",
"Handle": "@Cx01N",
"Link": "https://twitter.com/Cx01N_",
}, {
"Name": "",
"Handle": "@mjokic",
"Link": ""
}
],
'Description': ('Launches a Socks Proxy Server to run in the background of Empire.'),
'Software': '',
'Techniques': [''],
'Comments': []
}
self.options = {
'status': {
'Description': 'Start/stop the Socks Proxy server.',
'Required': True,
'Value': 'start',
'SuggestedValues': ['start', 'stop'],
'Strict': True
},
'handlerport': {
'Description': 'Port number.',
'Required': True,
'Value': '443'
},
'proxyport': {
'Description': 'Port number.',
'Required': True,
'Value': '1080'
},
'certificate': {
'Description': 'Certificate directory [Default: Empire self-signed cert].',
'Required': False,
'Value': ''
},
'privatekey': {
'Description': 'Private key directory [Default: Empire private key]',
'Required': False,
'Value': ''
},
}
# load default empire certs
self.cert_path = os.path.abspath("./empire/server/data/")
self.certificate = "%s/empire-chain.pem" % self.cert_path
self.private_key = "%s/empire-priv.key" % self.cert_path
self.running = False
def execute(self, command):
"""
Any modifications made to the main menu are done here
(meant to be overriden by child)
"""
try:
results = self.do_socksproxyserver(command)
return results
except Exception as e:
print(e)
return False
def register(self, mainMenu):
"""
any modifications to the main_menu go here - e.g.
registering functions to be run by user commands
"""
mainMenu.__class__.do_socksproxyserver = self.do_socksproxyserver
self.main_menu = mainMenu
self.plugin_service: PluginService = mainMenu.pluginsv2
def do_socksproxyserver(self, command):
"""
Launches a SocksProxy Server to run in the background of Empire
"""
self.status = command['status']
self.handler_port = command['handlerport']
self.proxy_port = command['proxyport']
if not command['certificate'] or command['privatekey']:
# load default empire certs
self.cert_path = os.path.abspath("./empire/server/data/")
self.certificate = "%s/empire-chain.pem" % self.cert_path
self.private_key = "%s/empire-priv.key" % self.cert_path
else:
self.certificate = command['certificate']
self.private_key = command['privatekey']
# Switch for starting and stopping server
if self.status == "start":
self.start_socks_server()
elif self.status == "stop":
self.shutdown()
else:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Usage: <start|stop>")
def start_socks_server(self):
if not self.running:
self.running = True
_thread.start_new_thread(self.server,
(self.handler_port, self.proxy_port, self.certificate, self.private_key))
else:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Socks Proxy Server Already Running!")
def shutdown(self):
"""
if the plugin spawns a process provide a shutdown method for when Empire exits else leave it as pass
"""
if self.running:
self.running = False
self.plugin_service.plugin_socketio_message(self.info["Name"], "[*] Stopping socks proxy server...")
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("127.0.0.1", int(self.handler_port)))
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect(("127.0.0.1", int(self.proxy_port)))
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Socks proxy server stopped")
else:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Server is not running!")
def handler_server(self, q, handler_port, certificate, private_key):
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.minimum_version = ssl.TLSVersion.TLSv1
context.set_ciphers('DEFAULT@SECLEVEL=0')
context.load_cert_chain(certificate, private_key)
try:
dock_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dock_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
dock_socket.bind(('', int(handler_port)))
dock_socket.listen(5)
self.plugin_service.plugin_socketio_message(self.info["Name"], "[+] Socks proxy server started")
while self.running:
try:
clear_socket, address = dock_socket.accept()
client_socket = context.wrap_socket(clear_socket, server_side=True)
try:
data = b""
while data.count(b'\n') < 3:
data_recv = client_socket.recv()
data += data_recv
client_socket.send(
b"HTTP/1.1 200 OK\nContent-Length: 999999\nContent-Type: text/plain\nConnection: Keep-Alive\nKeep-Alive: timeout=20, max=10000\n\n")
q.get(False)
except Exception as e:
pass
q.put(client_socket)
except Exception as e:
pass
except Exception as e:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] " + e.strerror)
finally:
dock_socket.close()
def get_active_connection(self, q):
try:
client_socket = q.get(block=True, timeout=10)
except:
return None
try:
client_socket.send(b"HELLO")
except:
return self.get_active_connection(q)
return client_socket
def server(self, handler_port, proxy_port, certificate, private_key):
q = queue.Queue()
_thread.start_new_thread(self.handler_server, (q, handler_port, certificate, private_key))
try:
dock_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
dock_socket2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
dock_socket2.bind(('127.0.0.1', int(proxy_port)))
dock_socket2.listen(5)
self.plugin_service.plugin_socketio_message(self.info["Name"], "[*] Socks server listening on: " + proxy_port)
while self.running:
try:
client_socket2, address = dock_socket2.accept()
client_socket = self.get_active_connection(q)
if client_socket == None:
client_socket2.close()
_thread.start_new_thread(self.forward, (client_socket, client_socket2))
_thread.start_new_thread(self.forward, (client_socket2, client_socket))
except Exception as e:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Exception: %s" % e)
except Exception as e:
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Exception: %s" % e)
finally:
dock_socket2.close()
self.plugin_service.plugin_socketio_message(self.info["Name"], "[!] Socks proxy server stopped")
def forward(self, source, destination):
try:
string = ' '
while string:
string = source.recv(1024)
if string:
destination.sendall(string)
else:
source.shutdown(socket.SHUT_RD)
destination.shutdown(socket.SHUT_WR)
except:
try:
source.shutdown(socket.SHUT_RD)
destination.shutdown(socket.SHUT_WR)
except:
pass
pass