Skip to content

Commit

Permalink
ntlmrelayx.py: Make SOCKS5 address and port configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpt-erikgeiser committed Oct 24, 2023
1 parent 419e6f2 commit 7cffe05
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
13 changes: 12 additions & 1 deletion examples/ntlmrelayx.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def stop_servers(threads):
'SMB Server (16 hex bytes long. eg: 1122334455667788)')
parser.add_argument('-socks', action='store_true', default=False,
help='Launch a SOCKS proxy for the connection relayed')
parser.add_argument('-socks-address', default='127.0.0.1:1080', help='SOCKS5 server address, port or address:port, the address is also used for the HTTP API')
parser.add_argument('-wh','--wpad-host', action='store',help='Enable serving a WPAD file for Proxy Authentication attack, '
'setting the proxy host to the one supplied.')
parser.add_argument('-wa','--wpad-auth-num', action='store', type=int, default=1, help='Prompt for authentication N times for clients without MS16-077 installed '
Expand Down Expand Up @@ -471,8 +472,18 @@ def stop_servers(threads):
threads = set()
socksServer = None
if options.socks is True:
socks_address_parts = options.socks_address.split(":")
if len(socks_address_parts) == 1 and socks_address_parts[0].isdigit():
socks_address = ("127.0.0.1", int(socks_address_parts[0]))
elif len(socks_address_parts) == 1 and not socks_address_parts[0].isdigit():
socks_address = (socks_address_parts[0], 1080)
elif len(socks_address_parts) == 2 and socks_address_parts[1].isdigit():
socks_address = (socks_address_parts[0], int(socks_address_parts[1]))
else:
raise ValueError(f"malformed SOCKS5 server address: {options.socks_address}")

# Start a SOCKS proxy in the background
socksServer = SOCKS()
socksServer = SOCKS(server_address=socks_address)
socksServer.daemon_threads = True
socks_thread = Thread(target=socksServer.serve_forever)
socks_thread.daemon = True
Expand Down
58 changes: 31 additions & 27 deletions impacket/examples/ntlmrelayx/servers/socksserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,36 +243,40 @@ def activeConnectionsWatcher(server):
LOG.info('Relay connection for %s at %s(%d) already exists. Discarding' % (userName, target, port))
client.killConnection()

def webService(server):
from flask import Flask, jsonify

app = Flask(__name__)
def webService(addr):
def _webService(server):
from flask import Flask, jsonify

log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__)

@app.route('/')
def index():
print(server.activeRelays)
return "Relays available: %s!" % (len(server.activeRelays))
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)

@app.route('/ntlmrelayx/api/v1.0/relays', methods=['GET'])
def get_relays():
relays = []
for target in server.activeRelays:
for port in server.activeRelays[target]:
for user in server.activeRelays[target][port]:
if user != 'data' and user != 'scheme':
protocol = server.activeRelays[target][port]['scheme']
isAdmin = server.activeRelays[target][port][user]['isAdmin']
relays.append([protocol, target, user, isAdmin, str(port)])
return jsonify(relays)
@app.route('/')
def index():
print(server.activeRelays)
return "Relays available: %s!" % (len(server.activeRelays))

@app.route('/ntlmrelayx/api/v1.0/relays', methods=['GET'])
def get_info(relay):
pass
@app.route('/ntlmrelayx/api/v1.0/relays', methods=['GET'])
def get_relays():
relays = []
for target in server.activeRelays:
for port in server.activeRelays[target]:
for user in server.activeRelays[target][port]:
if user != 'data' and user != 'scheme':
protocol = server.activeRelays[target][port]['scheme']
isAdmin = server.activeRelays[target][port][user]['isAdmin']
relays.append([protocol, target, user, isAdmin, str(port)])
return jsonify(relays)

app.run(host='0.0.0.0', port=9090)
@app.route('/ntlmrelayx/api/v1.0/relays', methods=['GET'])
def get_info(relay):
pass

app.run(host=addr, port=9090)

return _webService

class SocksRequestHandler(socketserver.BaseRequestHandler):
def __init__(self, request, client_address, server):
Expand Down Expand Up @@ -453,8 +457,8 @@ def handle(self):


class SOCKS(socketserver.ThreadingMixIn, socketserver.TCPServer):
def __init__(self, server_address=('0.0.0.0', 1080), handler_class=SocksRequestHandler):
LOG.info('SOCKS proxy started. Listening at port %d', server_address[1] )
def __init__(self, server_address=('127.0.0.1', 1080), handler_class=SocksRequestHandler):
LOG.info('SOCKS proxy started. Listening on %s:%d', server_address[0], server_address[1])

self.activeRelays = {}
self.socksPlugins = {}
Expand All @@ -476,7 +480,7 @@ def __init__(self, server_address=('0.0.0.0', 1080), handler_class=SocksRequestH
self.__timer = RepeatedTimer(KEEP_ALIVE_TIMER, keepAliveTimer, self)

# Let's start our RESTful API
self.restAPI = Thread(target=webService, args=(self, ))
self.restAPI = Thread(target=webService(server_address[0]), args=(self, ))
self.restAPI.daemon = True
self.restAPI.start()

Expand Down

0 comments on commit 7cffe05

Please sign in to comment.