Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 45 additions & 12 deletions scapy/fwdmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import enum
import functools
import os
import select
import socket
import ssl
import sys
Expand All @@ -26,7 +25,7 @@
from scapy.themes import DefaultTheme
from scapy.utils import get_temp_file

from scapy.layers.tls.all import (
from scapy.layers.tls.cert import (
Cert,
PrivKey,
PrivKeyECDSA,
Expand Down Expand Up @@ -203,7 +202,10 @@ def run(self):
dest = (self.remote_address, self.remote_port or self.port)
elif dest[0] in self.local_ips and self.remote_address:
dest = (self.remote_address,) + dest[1:]
print(self.ct.green("%s -> %s connected !" % (repr(addr), repr(dest))))
if dest[0] is None:
print(self.ct.green("%s connected !" % repr(addr)))
else:
print(self.ct.green("%s -> %s connected !" % (repr(addr), repr(dest))))
try:
threading.Thread(
target=self.handler,
Expand Down Expand Up @@ -418,8 +420,10 @@ def get_key_and_alike_chain(self, cas, dest, server_name):

# Remove policies, revocation URLs and CT data from the original chain.
c.tbsCertificate.extensions = [
x for x in c.tbsCertificate.extensions
if x.extnID not in [
x
for x in c.tbsCertificate.extensions
if x.extnID
not in [
"2.5.29.32", # certificatePolicies
"2.5.29.31", # cRLDistributionPoints
"1.3.6.1.5.5.7.1.1", # authorityInfoAccess
Expand All @@ -439,7 +443,17 @@ def get_key_and_alike_chain(self, cas, dest, server_name):
self.cache[ident] = privkey, result
return privkey, result

def handler(self, sock, addr, dest):
def handler(
self,
sock,
addr,
dest,
tls=None,
cls=None,
on_connect=None,
remote_af=None,
initial_packet=None,
):
"""
Handler of a client socket
"""
Expand All @@ -448,15 +462,17 @@ def handler(self, sock, addr, dest):
self,
addr=addr,
dest=dest,
remote_af=self.remote_af,
remote_af=self.remote_af if remote_af is None else remote_af,
proto=self.proto,
tls=self.tls,
cls=self.cls,
tls=self.tls if tls is None else tls,
cls=self.cls if cls is None else cls,
)
self.newconn(ctx)

# Initialize peer socket
ss = self._getpeersock(dest, ctx)
if on_connect is not None:
on_connect()

# Wrap both server and peer sockets in SSL
if ctx.tls:
Expand Down Expand Up @@ -534,16 +550,28 @@ def cb_sni(sock, server_name, _):
except Exception as ex:
self.vprint(self.ERROR, ctx, 1, "TLS failure: %s" % ex, None)
sock.close()
ss.close()
self.delconn(ctx)
return
ss = _clisock[0]

# The connection timeout also bounds TLS setup, not response reads.
ss.settimeout(None)

# Wrap the sockets
sock = ctx.sockcls(sock, ctx.cls)
ss = ctx.sockcls(ss, ctx.cls)
sock.streamsession = ss.streamsession
ctx.client_socket = sock
ctx.server_socket = ss
try:
while True:
# Listen on both ends of the connection
for thissock in select.select([ss, sock], [], [], 0)[0]:
# Process a previously parsed request before waiting for more data.
if initial_packet is not None:
ready = [sock]
else:
ready = StreamSocketPeekless.select([ss, sock], remain=0)
for thissock in ready:
if thissock is ss:
cs = 0
func = self.xfrmsc
Expand All @@ -554,7 +582,10 @@ def cb_sni(sock, server_name, _):
othersock = ss
# get data
try:
data = thissock.recv(self.MTU)
if initial_packet is not None and thissock is sock:
data, initial_packet = initial_packet, None
else:
data = thissock.recv(self.MTU)
except EOFError as ex:
othersock.ins.shutdown(socket.SHUT_RDWR)
raise RuntimeError(str(ex))
Expand Down Expand Up @@ -582,7 +613,9 @@ def cb_sni(sock, server_name, _):
ctx,
server_hostname=ex.server_hostname,
)
ss.settimeout(None)
ss = ctx.sockcls(ss, ctx.cls)
ctx.server_socket = ss
self.vprint(self.REDIRECT_TO, ctx, 1, ctx.dest, ex.dest)
ctx.dest = ex.dest # update context
# Shut the old one.
Expand Down
Loading
Loading