Skip to content
Snippets Groups Projects
Commit d5e3fc20 authored by Pierre LALET's avatar Pierre LALET
Browse files

Implement bridge_and_sniff() using sniff()

This removes duplicated code and should make bridge_and_sniff()
function work under Windows.
parent a80424e9
No related branches found
No related tags found
No related merge requests found
...@@ -734,76 +734,48 @@ Examples: ...@@ -734,76 +734,48 @@ Examples:
@conf.commands.register @conf.commands.register
def bridge_and_sniff(if1, if2, count=0, store=1, prn=None, lfilter=None, def bridge_and_sniff(if1, if2, prn=None, L2socket=None, *args, **kargs):
L2socket=None, timeout=None, stop_filter=None, *args, """Forward traffic between interfaces if1 and if2, sniff and return the
**kargs): exchanged packets.
"""Forward traffic between two interfaces and sniff packets exchanged
bridge_and_sniff([count=0,] [prn=None,] [store=1,] [offline=None,] Arguments:
[lfilter=None,] + L2Socket args) -> list of packets
if1, if2: the interfaces to use
count: number of packets to capture. 0 means infinity
store: whether to store sniffed packets or discard them The other arguments are the same than for the function sniff(),
prn: function to apply to each packet. If something is returned, except for opened_socket, offline and iface that are ignored.
it is displayed. Ex: See help(sniff) for more.
ex: prn = lambda x: x.summary()
lfilter: python function applied to each packet to determine
if further action may be done
ex: lfilter = lambda x: x.haslayer(Padding)
timeout: stop sniffing after a given time (default: None)
L2socket: use the provided L2socket
stop_filter: python function applied to each packet to determine
if we have to stop the capture after this packet
ex: stop_filter = lambda x: x.haslayer(TCP)
""" """
c = 0 for arg in ['opened_socket', 'offline', 'iface']:
if arg in kargs:
log_runtime.warning("Argument %s cannot be used in "
"bridge_and_sniff() -- ignoring it.", arg)
del kargs[arg]
if L2socket is None: if L2socket is None:
L2socket = conf.L2socket L2socket = conf.L2socket
s1 = L2socket(iface=if1) s1 = L2socket(iface=if1)
s2 = L2socket(iface=if2) s2 = L2socket(iface=if2)
peerof={s1:s2,s2:s1} peers = {if1: s2, if2: s1}
label={s1:if1, s2:if2} def prn_send(pkt):
try:
lst = [] sendsock = peers[pkt.sniffed_on]
if timeout is not None: except KeyError:
stoptime = time.time()+timeout return
remain = None try:
try: sendsock.send(pkt.original)
stop_event = False except:
while not stop_event: log_runtime.warning('Cannot forward packet [%s] received from %s',
if timeout is not None: pkt.summary(), pkt.sniffed_on, exc_info=True)
remain = stoptime-time.time() if prn is None:
if remain <= 0: prn = prn_send
break else:
if conf.use_bpf: prn_orig = prn
from scapy.arch.bpf.supersocket import bpf_select def prn(pkt):
ins = bpf_select([s1, s2], remain) prn_send(pkt)
else: return prn_orig(pkt)
ins, _, _ = select([s1, s2], [], [], remain)
for s in ins: return sniff(opened_socket={s1: if1, s2: if2}, prn=prn, *args, **kargs)
p = s.recv()
if p is not None:
peerof[s].send(p.original)
if lfilter and not lfilter(p):
continue
if store:
p.sniffed_on = label[s]
lst.append(p)
c += 1
if prn:
r = prn(p)
if r is not None:
print(r)
if stop_filter and stop_filter(p):
stop_event = True
break
if 0 < count <= c:
stop_event = True
break
except KeyboardInterrupt:
pass
finally:
return plist.PacketList(lst,"Sniffed")
@conf.commands.register @conf.commands.register
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment