Skip to content

Instantly share code, notes, and snippets.

@soravux
Created September 17, 2017 17:38
Show Gist options
  • Save soravux/87c88f4d533b25d84dbe34965028132b to your computer and use it in GitHub Desktop.
Save soravux/87c88f4d533b25d84dbe34965028132b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
VERSION = "v0.2.0"
"""
Copyright (c) 2013 devunt
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
import sys
if sys.version_info < (3, 4):
print('Error: You need python 3.4.0 or above.')
exit(1)
from argparse import ArgumentParser
from socket import TCP_NODELAY
from time import time
from traceback import print_exc
from io import BytesIO
import socket
import asyncio
import logging
import random
import functools
import re
from stegano import lsb
REGEX_HOST = re.compile(r'(.+?):([0-9]{1,5})')
REGEX_CONTENT_LENGTH = re.compile(r'\r\nContent-Length: ([0-9]+)\r\n', re.IGNORECASE)
REGEX_CONNECTION = re.compile(r'\r\nConnection: (.+)\r\n', re.IGNORECASE)
clients = {}
logging.basicConfig(level=logging.INFO, format='[%(asctime)s] {%(levelname)s} %(message)s')
logging.getLogger('asyncio').setLevel(logging.CRITICAL)
logger = logging.getLogger('warp')
verbose = 0
def accept_client(client_reader, client_writer, *, loop=None):
ident = hex(id(client_reader))[-6:]
task = asyncio.async(process_warp(client_reader, client_writer, loop=loop), loop=loop)
clients[task] = (client_reader, client_writer)
started_time = time()
def client_done(task):
del clients[task]
client_writer.close()
logger.debug('[%s] Connection closed (took %.5f seconds)' % (ident, time() - started_time))
logger.debug('[%s] Connection started' % ident)
task.add_done_callback(client_done)
@asyncio.coroutine
def outbound_sink(reader, writer, tosteg=False):
try:
while True:
line = yield from reader.read(512)
if len(line) == 0:
break
if tosteg:
faux_file = BytesIO()
secret = lsb.hide("img.png", line.decode('ascii'))
secret.save(faux_file, format="png")
#print("original: ", line)
line = faux_file.getvalue()
#import pdb; pdb.set_trace()
writer.write(bytes(str(len(line)), "ascii") + b"\n")
print("size sent: ", str(len(line)))
#faux_file.seek(0)
#print("reveal: ", lsb.reveal(faux_file))
else:
print("pas tosteg: ", line)
writer.write(line)
logger.info('Relay ended')
except:
print_exc()
@asyncio.coroutine
def process_warp(client_reader, client_writer, *, loop=None):
# Send to Relay
logger.info('Connecting to relay <%s %s>' % (relayhost, relayport))
req_reader, req_writer = yield from asyncio.open_connection(relayhost, relayport, ssl=False, loop=loop)
tasks = [
asyncio.async(outbound_sink(client_reader, req_writer, tosteg=True)),
asyncio.async(outbound_sink(req_reader, client_writer, tosteg=False)),
]
yield from asyncio.wait(tasks, loop=loop)
szim = {}
desteg_raw = {}
desteg = {}
@asyncio.coroutine
def read_desteg(reader, sz=None, until=None):
if reader not in desteg or len(desteg[reader]) == 0:
print("reading new size...")
szim[reader] = yield from reader.readline()
print(szim[reader])
try:
szim[reader] = int(szim[reader])
except ValueError:
print("invalid img size: {}".format(szim[reader]))
return b''
desteg[reader] = b""
desteg_raw[reader] = b""
while True:
print(szim[reader] - len(desteg_raw[reader]))
if szim[reader] - len(desteg_raw[reader]) > 0:
#print("Reading: ", min(1024, szim[reader] - len(desteg_raw[reader])))
newdata = yield from reader.read(min(1024, szim[reader] - len(desteg_raw[reader])))
#print("Read!", len(newdata))
if len(newdata) == 0:
print("Closed Comm")
break
desteg_raw[reader] += newdata
if szim[reader] > 0 and (szim[reader] == len(desteg_raw[reader])):
faux_file = BytesIO()
faux_file.write(desteg_raw[reader])
faux_file.seek(0)
desteg[reader] += lsb.reveal(faux_file).encode('utf-8')
szim[reader] = 0
if sz is not None and len(desteg[reader]) >= sz:
toyield = desteg[reader][:sz]
desteg[reader] = desteg[reader][sz:]
print("here2!", sz, toyield, desteg[reader])
return toyield
print(until, desteg[reader])
if until in desteg[reader]:
pos = desteg[reader].find(until)
toyield, desteg[reader] = desteg[reader][:pos+len(until)], desteg[reader][pos+len(until):]
print("------------")
print(toyield)
print(desteg[reader])
return toyield
if szim[reader] == 0:
desteg_raw[reader] = b""
print("Reading line")
szim[reader] = yield from reader.readline()
szim[reader] = int(szim[reader])
print(szim[reader])
@asyncio.coroutine
def relay(client_reader, client_writer, *, loop=None):
ident = str(hex(id(client_reader)))[-6:]
header = ''
payload = b''
try:
RECV_MAX_RETRY = 3
recvRetry = 0
while True:
print(">>>>> 1")
line = yield from read_desteg(client_reader, until=b"\r\n")#client_reader.readline()
print(">>>>> 2")
if not line:
if len(header) == 0 and recvRetry < RECV_MAX_RETRY:
# handle the case when the client make connection but sending data is delayed for some reasons
recvRetry += 1
yield from asyncio.sleep(0.2, loop=loop)
continue
else:
break
if line == b'\r\n':
break
if line != b'':
header += line.decode()
m = REGEX_CONTENT_LENGTH.search(header)
if m:
cl = int(m.group(1))
while (len(payload) < cl):
payload += yield from read_desteg(client_reader, sz=1024)#client_reader.read(1024)
print(">>>>> 2.1")
except:
print_exc()
print(">>>>> 3")
if len(header) == 0:
logger.debug('[%s] !!! Task reject (empty request)' % ident)
return
req = header.split('\r\n')[:-1]
if len(req) < 4:
logger.debug('[%s] !!! Task reject (invalid request)' % ident)
return
head = req[0].split(' ')
if head[0] == 'CONNECT': # https proxy
try:
logger.info('%sBYPASSING <%s %s> (SSL connection)' %
('[%s] ' % ident if verbose >= 1 else '', head[0], head[1]))
m = REGEX_HOST.search(head[1])
host = m.group(1)
port = int(m.group(2))
req_reader, req_writer = yield from asyncio.open_connection(host, port, ssl=False, loop=loop)
client_writer.write(b'HTTP/1.1 200 Connection established\r\n\r\n')
@asyncio.coroutine
def relay_stream(reader, writer, desteg):
try:
while True:
if desteg:
line = yield from read_desteg(client_reader, 1024)
else:
line = yield from reader.read(1024)
if len(line) == 0:
break
writer.write(line)
except:
print_exc()
tasks = [
asyncio.async(relay_stream(client_reader, req_writer, desteg=True), loop=loop),
asyncio.async(relay_stream(req_reader, client_writer, desteg=False), loop=loop),
]
yield from asyncio.wait(tasks, loop=loop)
except:
print_exc()
finally:
return
phost = False
sreq = []
sreqHeaderEndIndex = 0
for line in req[1:]:
headerNameAndValue = line.split(': ', 1)
if len(headerNameAndValue) == 2:
headerName, headerValue = headerNameAndValue
else:
headerName, headerValue = headerNameAndValue[0], None
if headerName.lower() == "host":
phost = headerValue
elif headerName.lower() == "connection":
if headerValue.lower() in ('keep-alive', 'persist'):
# current version of this program does not support the HTTP keep-alive feature
sreq.append("Connection: close")
else:
sreq.append(line)
elif headerName.lower() != 'proxy-connection':
sreq.append(line)
if len(line) == 0 and sreqHeaderEndIndex == 0:
sreqHeaderEndIndex = len(sreq) - 1
if sreqHeaderEndIndex == 0:
sreqHeaderEndIndex = len(sreq)
print("here!!!")
m = REGEX_CONNECTION.search(header)
if not m:
sreq.insert(sreqHeaderEndIndex, "Connection: close")
if not phost:
phost = '127.0.0.1'
path = head[1][len(phost)+7:]
logger.info('%sWARPING <%s %s>' % ('[%s] ' % ident if verbose >= 1 else '', head[0], head[1]))
new_head = ' '.join([head[0], path, head[2]])
m = REGEX_HOST.search(phost)
if m:
host = m.group(1)
port = int(m.group(2))
else:
host = phost
port = 80
try:
req_reader, req_writer = yield from asyncio.open_connection(host, port, flags=TCP_NODELAY, loop=loop)
req_writer.write(('%s\r\n' % new_head).encode())
yield from req_writer.drain()
yield from asyncio.sleep(0.2, loop=loop)
def generate_dummyheaders():
def generate_rndstrs(strings, length):
return ''.join(random.choice(strings) for _ in range(length))
import string
return ['X-%s: %s\r\n' % (generate_rndstrs(string.ascii_uppercase, 16),
generate_rndstrs(string.ascii_letters + string.digits, 128)) for _ in range(32)]
req_writer.writelines(list(map(lambda x: x.encode(), generate_dummyheaders())))
yield from req_writer.drain()
req_writer.write(b'Host: ')
yield from req_writer.drain()
def feed_phost(phost):
i = 1
while phost:
yield random.randrange(2, 4), phost[:i]
phost = phost[i:]
i = random.randrange(2, 5)
for delay, c in feed_phost(phost):
yield from asyncio.sleep(delay / 10.0, loop=loop)
req_writer.write(c.encode())
yield from req_writer.drain()
req_writer.write(b'\r\n')
req_writer.writelines(list(map(lambda x: (x + '\r\n').encode(), sreq)))
req_writer.write(b'\r\n')
if payload != b'':
req_writer.write(payload)
req_writer.write(b'\r\n')
yield from req_writer.drain()
try:
while True:
buf = yield from req_reader.read(1024)
if len(buf) == 0:
break
client_writer.write(buf)
except:
print_exc()
except:
print_exc()
client_writer.close()
def accept_relay(client_reader, client_writer, *, loop=None):
ident = hex(id(client_reader))[-6:]
task = asyncio.async(relay(client_reader, client_writer, loop=loop), loop=loop)
clients[task] = (client_reader, client_writer)
started_time = time()
def client_done(task):
del clients[task]
client_writer.close()
logger.debug('[%s] Connection closed (took %.5f seconds)' % (ident, time() - started_time))
logger.debug('[%s] Connection started' % ident)
task.add_done_callback(client_done)
@asyncio.coroutine
def start_warp_server(host, port, fct, *, loop=None):
try:
accept = functools.partial(fct, loop=loop)
server = yield from asyncio.start_server(accept, host=host, port=port, loop=loop)
except OSError as ex:
logger.critical('!!! Failed to bind server at [%s:%d]: %s' % (host, port, ex.args[1]))
raise
else:
logger.info('Server bound at [%s:%d].' % (host, port))
return server
def main():
"""CLI frontend function. It takes command line options e.g. host,
port and provides `--help` message.
"""
parser = ArgumentParser(description='Simple HTTP transparent proxy')
parser.add_argument('-r', '--relay', action='store_true', dest="relay", default=False)
parser.add_argument('--relayhost', default="127.0.0.1")
parser.add_argument('--relayport', default=8928)
parser.add_argument('-H', '--host', default='127.0.0.1',
help='Host to listen [default: %(default)s]')
parser.add_argument('-p', '--port', type=int, default=8000,
help='Port to listen [default: %(default)d]')
parser.add_argument('-v', '--verbose', action='count', default=0,
help='Print verbose')
#parser.set_defaults(relay=False)
args = parser.parse_args()
if not (1 <= args.port <= 65535):
parser.error('port must be 1-65535')
if args.verbose >= 3:
parser.error('verbose level must be 1-2')
if args.verbose >= 1:
logger.setLevel(logging.DEBUG)
if args.verbose >= 2:
logging.getLogger('warp').setLevel(logging.DEBUG)
logging.getLogger('asyncio').setLevel(logging.DEBUG)
global verbose
verbose = args.verbose
loop = asyncio.get_event_loop()
global relayhost, relayport
relayhost = args.relayhost
relayport = args.relayport
if args.relay:
try:
loop.run_until_complete(start_warp_server(args.host, args.port, accept_relay))
loop.run_forever()
except OSError:
pass
except KeyboardInterrupt:
print('bye')
finally:
loop.close()
else:
try:
loop.run_until_complete(start_warp_server(args.host, args.port, accept_client))
loop.run_forever()
except OSError:
pass
except KeyboardInterrupt:
print('bye')
finally:
loop.close()
if __name__ == '__main__':
exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment