text
stringlengths
1
93.6k
# set a continuation header
my_headers=my_headers,
# no need for a body: we're initialising the server-side listener
req_body=b"",
is_last=True)
websock = socket_factory.build_socket()
log.debug("Sending \n---\n{}\n---".format(req.decode()))
websock.sendall(req)
# FUNDAMENTAL - do not close the websock here! It will be used
# by the select to listen for inbound data.
# instead, kill the generator
del inbound_data
# and reset it with the new websock so that subsequent calls
# to inbound_data.next() will retrieve the input from this connection
inbound_data = read_chunks(websock, is_async)
print("[*] Remote listener initialised")
# not sure is relevant any more. FIXME remove?
# print("[+] To establish a connection the remote socket needs to"
# " initiate the conversation by sending data. SSH or reverse"
# " shells are OK")
# use the object as storage of this variable
# meh. complicated.
counter_thread.first_message = True
socket_list = [consock, websock]
quit = False
while not quit:
# blocks until one is ready
read_sockets, write_sockets, error_sockets = select.select(
socket_list, [], [])
for sock in read_sockets:
# incoming message from client
if sock == consock:
log.debug("Client has data")
try:
# this is already bytes in python3
data = sock.recv(BUFFER_SIZE)
except socket.error as e:
print("[*] Error when receiving data: {}".format(e))
if len(data) == 0:
# socket closed
print('[*] Client closed connection')
quit = True
# send to server
if data:
log.debug("Sending {}".format(repr(data)))
try:
if is_async:
# In this scenario, the remote page can only
# read chunked-encoded inbound data when it
# is all sent. E.g. aspx, php (?).
# So the data is sent as a stand-alone
# HTTP POST request.
send_standalone_post(
socket_factory,
post_factory,
cookies,
data,
counter_thread,
mode)
else:
# JSP pages can read chunked-encoding content
# while it arrives. So it's all part of the
# same TCP stream.
req = to_chunk(data)
with websock_mutex:
# _chunked_data = to_chunk(data)
# websock.sendall(_chunked_data)
websock.sendall(req)
except socket.error as e:
# print, not raising BrokenSocketException
# so if there's any data in websock it gets sent at
# the next iteration of the for loop
print(("[*] Error when sending "
"data to server: {}").format(e))
quit = True
# then refresh the heartbeat timer
thread_hb.reset_timer()
# incoming message from remote server
elif sock == websock:
log.debug("Server has data")
# Read the next chunk of data (as in: content-encoded chunk).
# This is a hex value followed by '\r\n' followed by the raw
# data.
# If the server had sent more than one chunk, no problem -- the
# next select() iteration will detect more data to read, which
# will be pulled by the next() method below and sent to the
# 'consock' client.
# In other words, this polls one message at the time, and
# leaves the rest in the socket so the select() above will
# not block as long there's left-over stuff in the 'websock'.
data = next(inbound_data)