Skip to content

Commit

Permalink
Release 4.0 update (#5)
Browse files Browse the repository at this point in the history
* Updated library import for new Empire 4.0 server directory structure (#4)

* Updated library import for new Empire 4.0 server directory structure

* Added check for new Empire directory structure to load appropriate modules for new Empire version with backward compat for older versions

* updates for socketio notifications

* release 4.0 updates

* minor update

Co-authored-by: Rey Bango <[email protected]>
  • Loading branch information
Cx01N and reybango authored Jul 7, 2021
1 parent 0e6ea6f commit 0580f1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@ The Socks Proxy Plugin runs a Socks Proxy Server for [Invoke-SocksProxy](https:/
that supports Socks 4 and 5 protocols. This plugin is entirely contained in [Empire](https://github.com/BC-SECURITY/Empire/)
and runs in the background.

`socksproxyserver <start|stop> [handler port] [proxy port] [certificate] [private key]`

![image](https://user-images.githubusercontent.com/20302208/95637897-d8221480-0a47-11eb-8a69-3f132fe5d079.png)

## Getting Started
* To run the plugin, you can download it fom the releases [Releases](https://github.com/BC-SECURITY/Invoke-SocksProxy/releases) page.

## Install
Prerequisites:
- Empire 3.5.0+
- Empire >= 4.0.0

1. Add SocksServer.py to the plugins folder of Empire.

![image](https://user-images.githubusercontent.com/20302208/95636534-49f85f00-0a44-11eb-87c1-754a2368febb.png)


2. Plugins are automatically loaded into Empire as of 3.4.0, otherwise run ```plugin SocksServer```
![image](https://user-images.githubusercontent.com/20302208/120246969-b4098280-c226-11eb-9345-4ff994ee5312.png)

![image](https://user-images.githubusercontent.com/20302208/95636737-b5dac780-0a44-11eb-9f82-34dcb66c24fe.png)
## Usage
### Client
![image](https://user-images.githubusercontent.com/20302208/120247213-8ffa7100-c227-11eb-8a7a-5f0de195f2e9.gif)

## Future Features
- Add multiple socks server support (similar to agents and listeners)
Expand Down
76 changes: 38 additions & 38 deletions plugins/socksproxyserver.py → socksproxyserver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import print_function

from lib.common.plugins import Plugin
import lib.common.helpers as helpers
from empire.server.common.plugins import Plugin
import empire.server.common.helpers as helpers

import socket
import _thread
Expand All @@ -14,8 +14,8 @@ class Plugin(Plugin):
description = "Launches a Socks Proxy Server to run in the background of Empire"

def onLoad(self):
""" any custom loading behavior - called by init, so any
behavior you'd normally put in __init__ goes here """
print(helpers.color("[*] Loading Empire Socks Proxy Server plugin"))
self.main_menu = None
self.info = {
'Name': 'socksproxyserver',

Expand All @@ -32,9 +32,11 @@ def onLoad(self):

self.options = {
'status': {
'Description': 'Start/stop the Chisel server. Specify a port or default to 8080.',
'Description': 'Start/stop the Socks Proxy server.',
'Required': True,
'Value': 'start'
'Value': 'start',
'SuggestedValues': ['start', 'stop'],
'Strict': True
},
'handlerport': {
'Description': 'Port number.',
Expand All @@ -47,7 +49,7 @@ def onLoad(self):
'Value': '1080'
},
'certificate': {
'Description': 'Certifcate directory [Default: Empire self-signed cert].',
'Description': 'Certificate directory [Default: Empire self-signed cert].',
'Required': False,
'Value': ''
},
Expand All @@ -58,9 +60,8 @@ def onLoad(self):
},
}


# load default empire certs
self.cert_path = os.path.abspath("./data/")
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

Expand All @@ -83,14 +84,18 @@ def execute(self, command):
def get_commands(self):
return self.commands

def register(self, main_menu):
""" any modifications to the mainMenu go here - e.g.
registering functions to be run by user commands """
main_menu.__class__.do_socksproxyserver = self.do_socksproxyserver
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

def do_socksproxyserver(self, args):
"Launches a SocksProxy Server to run in the background of Empire"

"""
Launches a SocksProxy Server to run in the background of Empire
"""
if not args:
# Load defaults for server
self.status = self.options['status']['Value']
Expand All @@ -99,16 +104,10 @@ def do_socksproxyserver(self, args):

if not self.options['certificate']['Value'] or self.options['privatekey']['Value']:
# load default empire certs
self.cert_path = os.path.abspath("./data/")
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

print(helpers.color(
"[!] Usage: socksserver <start|stop> [handler port] [proxy port] [certificate] [private key]"))
print(helpers.color(
"[+] Defaults: socksserver " + self.status + " " + self.handler_port + " " + self.proxy_port + " " +
self.certificate + " " + self.private_key))

else:
args = args.split(" ")

Expand All @@ -131,7 +130,7 @@ def do_socksproxyserver(self, args):
self.private_key = args[4]
else:
# load default empire certs
self.cert_path = os.path.abspath("./data/")
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

Expand All @@ -141,26 +140,28 @@ def do_socksproxyserver(self, args):
elif self.status == "stop":
self.shutdown()
else:
print(helpers.color("[!] Usage: socksserver <start|stop> [handler port] [proxy port] [certificate] ["
"private key]"))
self.main_menu.plugin_socketio_message(self.info[0]["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:
print(helpers.color("[!] Socks Proxy Server Already Running!"))
self.main_menu.plugin_socketio_message(self.info[0]["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 the plugin spawns a process provide a shutdown method for when Empire exits else leave it as pass
"""
if self.running:
self.running = False
print(helpers.color("[*] Stopping socks proxy server..."))
self.main_menu.plugin_socketio_message(self.info[0]["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.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] Socks proxy server stopped")
else:
print(helpers.color("[!] Server is not running!"))
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] Server is not running!")

def handler_server(self, q, handler_port, certificate, private_key):
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
Expand All @@ -170,10 +171,8 @@ def handler_server(self, q, handler_port, certificate, private_key):
dock_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
dock_socket.bind(('', int(handler_port)))
dock_socket.listen(5)
print(helpers.color("[*] Handler listening on: " + handler_port))
print(helpers.color("[*] Using certificate: " + certificate))
print(helpers.color("[*] Using private key: " + private_key))
print(helpers.color("[+] Socks proxy server started"))
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[+] Socks proxy server started")

while self.running:
try:
clear_socket, address = dock_socket.accept()
Expand All @@ -192,7 +191,7 @@ def handler_server(self, q, handler_port, certificate, private_key):
except Exception as e:
pass
except Exception as e:
pass
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] " + e.strerror)
finally:
dock_socket.close()

Expand All @@ -215,7 +214,8 @@ def server(self, handler_port, proxy_port, certificate, private_key):
dock_socket2.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
dock_socket2.bind(('127.0.0.1', int(proxy_port)))
dock_socket2.listen(5)
print(helpers.color("\n[*] Socks server listening on: " + proxy_port))
self.mainMenu.plugin_socketio_message(self.info[0]["Name"], "[*] Socks server listening on: " + proxy_port)

while self.running:
try:
client_socket2, address = dock_socket2.accept()
Expand All @@ -225,12 +225,12 @@ def server(self, handler_port, proxy_port, certificate, private_key):
_thread.start_new_thread(self.forward, (client_socket, client_socket2))
_thread.start_new_thread(self.forward, (client_socket2, client_socket))
except Exception as e:
print(helpers.color("[!] Exception: %s" % e))
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] Exception: %s" % e)
except Exception as e:
print(helpers.color("[!] Exception: %s" % e))
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] Exception: %s" % e)
finally:
dock_socket2.close()
print(helpers.color("\n[+] Socks proxy server stopped"))
self.main_menu.plugin_socketio_message(self.info[0]["Name"], "[!] Socks proxy server stopped")

def forward(self, source, destination):
try:
Expand Down

0 comments on commit 0580f1b

Please sign in to comment.