Skip to content

Commit

Permalink
Support (BPF) filter in sniff() with offline parameter set
Browse files Browse the repository at this point in the history
Fixes secdev#393
Also, fixes secdev#355
  • Loading branch information
p-l- committed Dec 19, 2016
1 parent f912538 commit e243418
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
31 changes: 28 additions & 3 deletions scapy/arch/windows/compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
Instanciate part of the customizations needed to support Microsoft Windows.
"""

import itertools
import os
import re
import socket
import subprocess
import sys
import time

from scapy.arch.consts import LOOPBACK_NAME
from scapy.config import conf,ConfClass
from scapy.base_classes import Gen, SetGen
import scapy.plist as plist
from scapy.utils import PcapReader
from scapy.data import MTU, ETH_P_ARP
import os,re,sys,socket,time, itertools

WINDOWS = True

Expand Down Expand Up @@ -171,6 +178,7 @@ def sniff(count=0, store=1, offline=None, prn = None, lfilter=None, L2socket=Non
prn: function to apply to each packet. If something is returned,
it is displayed. Ex:
ex: prn = lambda x: x.summary()
filter: provide a BPF filter
lfilter: python function applied to each packet to determine
if further action may be done
ex: lfilter = lambda x: x.haslayer(Padding)
Expand All @@ -186,8 +194,25 @@ def sniff(count=0, store=1, offline=None, prn = None, lfilter=None, L2socket=Non
L2socket = conf.L2listen
s = L2socket(type=ETH_P_ALL, *arg, **karg)
else:
s = PcapReader(offline)

flt = karg.get('filter')
if flt is not None:
if isinstance(offline, basestring):
s = PcapReader(
subprocess.Popen(
[conf.prog.tcpdump, "-r", offline, "-w", "-", flt],
stdout=subprocess.PIPE
).stdout
)
else:
s = PcapReader(
subprocess.Popen(
[conf.prog.tcpdump, "-r", "-", "-w", "-", flt],
stdin=offline,
stdout=subprocess.PIPE
).stdout
)
else:
s = PcapReader(offline)
lst = []
if timeout is not None:
stoptime = time.time()+timeout
Expand Down
27 changes: 25 additions & 2 deletions scapy/sendrecv.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ def sniff(count=0, store=1, offline=None, prn=None, lfilter=None,
prn: function to apply to each packet. If something is returned,
it is displayed. Ex:
ex: prn = lambda x: x.summary()
filter: provide a BPF filter
lfilter: python function applied to each packet to determine
if further action may be done
ex: lfilter = lambda x: x.haslayer(Padding)
Expand Down Expand Up @@ -603,8 +604,30 @@ def sniff(count=0, store=1, offline=None, prn=None, lfilter=None,
sniff_sockets = [L2socket(type=ETH_P_ALL, iface=iface, *arg,
**karg)]
else:
sniff_sockets = [PcapReader(offline)]

flt = karg.get('filter')
if flt is not None:
if isinstance(offline, basestring):
sniff_sockets = [
PcapReader(
subprocess.Popen(
[conf.prog.tcpdump, "-r", offline, "-w", "-",
flt],
stdout=subprocess.PIPE
).stdout
)
]
else:
sniff_sockets = [
PcapReader(
subprocess.Popen(
[conf.prog.tcpdump, "-r", "-", "-w", "-", flt],
stdin=offline,
stdout=subprocess.PIPE
).stdout
)
]
else:
sniff_sockets = [PcapReader(offline)]
lst = []
if timeout is not None:
stoptime = time.time()+timeout
Expand Down

0 comments on commit e243418

Please sign in to comment.